Creating your first program

To be able to code your program, you would need a text editor. Any command-line editors and GUI editors will do. Simply follow the basic form of a C program,

#include<stdio.h> // call header files
main() //main function
{ //start of function
printf(”Hello World!”); //body of the program
} //end of function

Save the file with your desired filename with the extension .c.

program.c

Open your terminal console for us to compile our code.

To compile, simply type

gcc -o <program_name> <source_code.c>

This command simply compiles the code an executable file which is the program we will execute.

To execute the program, simply type

./<program_name>

Published in: on October 18, 2007 at 7:03 pm Comments (0)

Sorting Program

#include<stdio.h>
#include<stdlib.h>
main()
{
int num[5];
int x,y,loader;

system("clear");

for(x=0;x<5;x++)
{
printf(”Enter a number: “);scanf(”%d”,&num[x]);
}

for(x=0;x<5;x++)
{
for(y=0;y<5;y++)
{
if(num[y]<num[x])
{
loader=num[x];
num[x]=num[y];
num[y]=loader;
}
}
}
y=4;
printf(”Descending Order\t”);printf(”Ascending Order\n”);
for(x=0;x<5;x++)
{
printf(”%d\t\t\t”,num[x]);printf(”%d\n”,num[y]);

y--;
}

}

Published in: on at 7:02 pm Comments (0)