The smallest possible program in C
main() {
}
Every C program must have the “main” function somewhere.
C is case sensitive.
Little more than smallest
Test0.c
int main()
{
return(123);
}
Compile and link
$gcc –o test0 test0.c
$./test0; echo $?
123
Hello World!
#include <stdio.h>
int main(void) {
printf(“Hello world!\n”);
}
Hello World, again…
#include <stdio.h>
void print_my_string(char str[]);
char hello[] = “Hello world!”;
int main(void) {
print_my_string(hello);
return(0);
}
void print_my_string(char str[]) {
printf(“%s\n”, str);
}
C Program Structure
preprocessor commands
type definitions
function prototypes
global variables
functions
Basics on C compiling
Compile and run program file.c
> gcc file.c –o file –Wall –g
./file
Compile multiple sources
> gcc file1.c –c –Wall –g
> gcc file2.c –c –Wall -g
> gcc file1.o file2.o –o file
> ./file
Use Makefile to compile
Makefile
All:file
file1.o:file1.c
gcc file1.c -c
file2.o:file2.c
gcc file2.c -c
file:file1.o file2.o
gcc file1.o file2.o -o file
Clean:
rm -f *.o; rm -f file
Make, make clean
Use GDB/Insight to debug
Makefile
All:file
file1.o:file1.c
gcc file1.c –c -g
file2.o:file2.c
gcc file2.c –c -g
file:file1.o file2.o
gcc file1.o file2.o -o file
Clean:
rm -f *.o; rm -f file; rm -f core
Gdb file
Insight file
Basics on C compiling
> gcc file1.c –c –Wall –g
> gcc file2.c –c –Wall -g
> gcc file1.o file2.o –o file
> ./file
Makefile
All:file
file1.o:file1.c
file2.o:file2.c
file:file1.o file2.o
Clean:
rm -f *.o; rm -f file