Week 4 Lecture PART 1
Week 4 Lecture PART 1
Malware Forensics
C Language
Basic C Program Structure
//Library imports
#include <stdio.h>
// Global variables and function defs.
//main function, where the program starts and runs from
int main() {
// Add functionality e.g., printf
printf( “Hello World\n” );
return 0 ;
}
// function definitions go below main()
● Install MingW32
● vim main.c (enter program)
● gcc -m32 -g main.c (compile w/ debug)
● gdb -q .\a.exe (debug)
● disassemble /m main (disassemble)
● q (to quit gdb)
int main() {
int mark = 52 ;
printf( "Value: %d\n", mark ) ;
printf( "Address: %p\n", &mark ) ;
return 0 ;
}
Basic C Output
● printf(...) print formatted string
● Simple template language, which allows
variables to copied into the output stream
Basic C Input
● scanf(...) - scan formatted string
● Simple template language, which allows
variables to copied from the output stream
● Use & in front of variables
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
printf("%d\n", n);
return 0;
}
Basic C Input
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n) ;
printf("%d\n", n) ;
char str[50] ;
scanf("%s", &str) ;
printf("%s\n", str) ;
return 0;
}
Conditionals in C
> greater than: 5 > 4 is TRUE
int sum = 1;
for(int i=0; i<5; i=i+1) {
sum = 1 + sum * i ;
}
printf(“sum = %d”, sum);
for in C
#include <stdio.h>
int main()
{
int sum = 1 ;
for( int i = 0 ; i < 5 ; i = i+1 ) {
sum = 1 + sum * i ;
}
printf( "sum = %d" , sum ) ;
return 0;
}
Arrays in C
dataType arrayName[arraySize];
int mark[5];
int mark[5] = {19, 10, 8, 17, 9};
int mark[] = {19, 10, 8, 17, 9};
Arrays I/O in C
#include <stdio.h>
int main() {
int values[5];
// taking input and storing it in an array
for(int i = 0; i < 5; i++) {
scanf("%d", &values[i]);
}
printf("\n");
// printing elements of an array
for(int i = 0; i < 5; i++) {
printf("%d\n", values[i]);
}
return 0 ;
}