Writing and Compiling C Program in Linux GCC Part 1

  • GCC compiler is usually installed together with your Linux. If not, use the command $sudo apt-get install gcc (for Ubuntu user) and #yum install gcc (for Fedora, Redhat user). For Fedora user, you need to be in su mode before installation.
  • Below is a simple C program called sick.c
#include <stdio.h>
int main(void)
printf ("Sick of printing hello!\n");
return 0;
  • To compile the program #gcc -Wall sick.c -o verysick
  • The option -Wall display all possible Warning and –o indicate to the compiler that user want the output to be name after the -o option.
  • To run the program indicate the complete path or $./verysick

Writing and Compiling C Program using Math Library

  • Below is a more complex C program called tvm.c
#include <stdio.h>
#include <math.h>
double tvm (double rate, int terms, double principal);
int main (void)
double myrate;
int myterm;
double myprincipal;
double myreturn;
printf ("Please enter the following:\n");
printf ("Rate of return:(In decimal 50%% is 0.5) ");
scanf ("%lf", &myrate);
printf ("\n");
printf ("Number of terms:(No decimals): ");
scanf ("%d", &myterm);
printf ("\n");
printf ("The principal amount: ");
scanf ("%lf", &myprincipal);
printf ("\n");
myreturn = tvm (myrate, myterm, myprincipal);
printf ("Your return after %d term is $%.2lf.\n", myterm, myreturn);
return 0;
double tvm (double rate, int terms, double principal)
double dn;
double pp;
dn = terms;
pp = pow (1+rate, dn);
return principal * pp;
  • To compile the program #gcc -Wall tvm.c -lm -o tvm
  • The additional option -lm is required so that the compiler will search for the math library in reference to including math.h.

Comments are closed.

%d bloggers like this: