How the Compilation Process Works for C Programs

How the Compilation Process Works for C Programs

#include <stdio.h>int main(void)
{
printf("Hello, World!\n");
return (0);
}
vagrant@vagrant-ubuntu-trusty-64:~$ gcc main.c
vagrant@vagrant-ubuntu-trusty-64:~$ ls
a.out main.c
vagrant@vagrant-ubuntu-trusty-64:~$ ./a.out
Hello, World!
vagrant@vagrant-ubuntu-trusty-64:~$
gcc -o <desired_output_filename> <source filename>
vagrant@vagrant-ubuntu-trusty-64:~$ gcc -o main main.c
vagrant@vagrant-ubuntu-trusty-64:~$ ls
main main.c
vagrant@vagrant-ubuntu-trusty-64:~$
vagrant@vagrant-ubuntu-trusty-64:~$ ./main
Hello, World!
vagrant@vagrant-ubuntu-trusty-64:~$

In some instances, the C source file will not successfully compile.

#include <stdio.h>int main(void)
{
printf("Hello, World!\n")
return (0);
}
vagrant@vagrant-ubuntu-trusty-64:~$ gcc -o main main.c
main.c: In function 'main':
main.c:5:7: error: expected ';' before 'return'
return (0);
^
vagrant@vagrant-ubuntu-trusty-64:~$
#include <stdio.h>int main(void)
{
printf("Hello, World!\n");
return (0);
}
vagrant@vagrant-ubuntu-trusty-64:~$ gcc -o main main.c
vagrant@vagrant-ubuntu-trusty-64:~$ ./main
Hello, World!
vagrant@vagrant-ubuntu-trusty-64:~$

Even if your source file compiles, check if your output is correct.

#include <stdio.h>int main(void)
{
printf("The sum of 9+2 is: %i\n", 10);
return (0);
}
vagrant@vagrant-ubuntu-trusty-64:~$ ./main2
The sum of 9+2 is: 10
vagrant@vagrant-ubuntu-trusty-64:~$
printf("The sum of 9+2 is: %i\n", 10);
#include <stdio.h>int main(void)
{
printf("The sum of 9+2 is: %i\n", 11);
return (0);
}