5/25/09

My First Program in C Programming Language

Programing in C is not that difficult if you know the basic rules.

I'll show you how to create a simple program in C.

First of all open a text editor(kate,kwrite,vi,whatever you want). To compile the file I will show you, you will need to have gcc installed in your pc. If you are on Linux you open the software management,select gcc and install it. If you are on Windows I suggest you use a C developing program like Dev-Cpp witch is free.

If you installed Dev-Cpp on Windows or gcc on linux just do the following.

Open Dev-Cpp or a text editor and create a new empty file and save it as myfirstcprogram.c

Now first you have to include the stdio (standard library for input/output) so write in the first line

#include <stdio.h>

The next step is to write the header of the main function,open brackets {} and write your code.

This is your first program and I'm going to explain it.


#include <stdio.h>
int main(int argc, char *argv[]) {
int a,b;
printf("give number a : ");
scanf("%d",&a);
printf("give number b : ");
scanf("%d",&b);
printf("%d %d = %d\n",a,b,a b);
exit(0);
}

Now, What does the above code do?

Line 1: include the stdio library(explained above)

Line 2: main function from where your program will start execution

Line 3: we say that we have two integers named a and b

Line 4: print at the desktop the message give number a

Line 5: read the number entered. the function scanf waits until enter is pressed

Line 5 and 6 : do the same as 4 and 5 to read the number b

Line 7: prints the message number a + number b = a+b

Line 8: the program ended successfully

Now that you wrote the above code you have to compile it.

In Linux in the directory you saved the file type in the Konsole

gcc myfirstcprogram.c -o myfirstprogram

in Windows from the Dev-C++ menu select exectute - compile.

In windows open command prompt windows (start - run -cmd - ok)

use cd command to go to the directory you saved it.

In windows type myfirstprogram and press enter

and in Linux type ./myfirstprogram

Now you see the message " give number a : " type a number and press enter and you will see the same message for the number b so do the same again.

After that you will see the result : a + b = sum

Example of output:

give number a : 3

give number b : 4

3 + 4 = 7

That's it. You wrote and compiled your first program in C!!!
Del.icio.us

3 comments:

Kevin Rodrigues said...

That is a nice explanation of understanding what a basic C program consists of.

james said...

Thanks a lot for a bunch of good tips. I look forward to reading more on the topic in the future. Keep up the good work! This blog is going to be great resource. Love reading it.

academic term papers-Essay Writing Help

kris of Software Development Company said...

I remember when I was still studying in college and my first experience on coding with C language. It is fun but confusing in a good way because you are thinking about the logic behind the syntax.

Related Posts Plugin for WordPress, Blogger...