Learn C with help of an example

In this article, we will learn C by going through the below program and understanding each line of C code in detail.

1) /*
2) Program:
3) Addition of two numbers
4) */
5) //Here it starts
6) #include<stdio.h>
7) #include<stdlib.h>
8) void main()
9) {
10) int a, b, c;
11) /*Assign the values to the variable a and b */
12) a = 5;
13) b = 4;
14) /*Perform addition of a and b */
15) c = a + b;
16) printf(“\nAddition of a and b is %d”, c);
17) }

Let us learn C by going through the above code:

Line 1 thru 4:
This is the comments in the program. It is called multi-line comment. The text written within /*…*/ are ignored by the compiler.

Line 5:
This is another way to comment a text or statement. It is called single line comment. The entire line following the tag // is ignored by the compiler.

Line 6 and 7:
#include is known as pre- processor directives. It tell the compiler to include text from another file, stuffing it right into your source code.
is a file name enclosed within angle brackets. The whole statement #include tells the compiler to take text from the file stdio.h and stick it into your source code before the source code is compiled.

Line 8:
This is the start of the main function. The program execution starts from this function. main() is the name given to a set of statements. This name has to be main(). The statement that belongs to main() are enclosed within a pair of braces {}.

Line 10:
Any variable that we need to use must be declared before using it.

Line 12 thru 15:
Here the values are getting assigned to the variables.

Line 16:
Once the value of a+b is assigned to c, it needs to be displayed on the screen. We can use readymade library function to display the value on the screen. One such function is printf().

The general form of the printf() function is:
printf("", );

can contain,
%d for printing integer values
%f for printing real values
%c for printing character values
\n is a newline character. It takes the cursor to the new line. It is one of the several escape sequences available in C.

Line 9 and 17:
The function body should be enclosed in opening and closing braces.

Note:
Though comments are not necessary, it is a good programming practice to include comments in the C program. Adding comments in the program increases the readability of the program. We can include as many comments as possible. A comment can be split over more than one line as shown in the above program (Line 1 thru 4). Any C statement always ends with a ;

Receiving Input
In the above C program, we have assigned the value 5 and 4 to a and b respectively. But, every time we cant do so. A situation might arise wherein we need to take input from the user. Here we will learn how to receive input from the user.

scanf() – It is a standard library function in C. This function will accept the input from the user and stores it in a particular user defined variable.

The syntax of this function is:
scanf( “”, &);

can contain,
%d for printing integer values
%f for printing real values
%c for printing character values

‘&’ before the variable name is a must. ‘&’ is the ‘address of’ operator. It gives the address used by the variable in memory.

Let us consider an example:
scanf(“%d”,&a);

When we execute this statement, it will ask for input. Suppose the input given is 5. 5 will be stored as integer at the address of variable a. we can print the value stored at that address using simple printf() statement as shown below.
printf(“%d”, a);

Now that we have understood and learned basic C program, go through the below C code and try to understand it by yourself. If you have any questions understanding the below C program, feel free to comment below.

Example:

1) /*
2) Program in C to accept three numbers and prints the sum of these numbers
3) */
4) #include<stdio.h>
5) #include<stdlib.h>
6) void main()
7) {
8) int a, b, c;
9) /*Assign the values to the variable a and b */
10) scanf(“%d %d”, &a, &b);
11) /*Perform addition of a and b */
12) c = a + b;
13) printf(“\nAddition of a and b is %d”, c);
14) }

If you want to go through more programs in order to learn C, visit – C programming examples

You may also like...

47 Responses

  1. Anonymous says:

    Superb! Excellent website!

  2. Anonymous says:

    good site

  3. Anonymous says:

    This site is very good for beginner.

  4. Anonymous says:

    xcellent explanation

  5. Anonymous says:

    Very very good

  6. Anonymous says:

    the best ever

  7. Anonymous says:

    thankks…

  8. rocketraja says:

    OMG,i checked out this website bfor my exams,it was awesome,truly magnificient!!!!!!!!!!!!!!!!!!!!!!

  9. LearnCOnline says:

    Thanks a lot for your valuable comments.

  10. Anonymous says:

    why or when do you use /n in printf

  11. LearnCOnline says:

    \n is a newline character. It takes the cursor to the new line. It is one of the several escape sequences available in C.

    \n is used if we want to print a character or a string in a new line.

  12. Anonymous says:

    Good Job.Thankx a lot 🙂

  13. Anonymous says:

    what does conio do?

  14. LearnCOnline says:

    conio.h is a C header file used in old MS-DOS compilers to create text user interfaces.

    Refer the below link for information in detail:
    http://en.wikipedia.org/wiki/Conio.h

  15. Anonymous says:

    Really very nice site to learn very quickly

  16. Anonymous says:

    good

  17. Can u explain me why do u use scanf and printf instead of cin>> and cout<

  18. Anonymous says:

    explaination is good

  19. Anonymous says:

    awesome site…

  20. Anonymous says:

    woohoo atlast a nice site

  21. Anonymous says:

    its really easy to understand …….thanks for assisting us …..

  22. Anonymous says:

    I never understand c but after joining this it look like a piece of cake

  23. Realy superb job,plz help best website to learn embedded programming…..

  24. how to start prorgraming of c where we write all the scripts????

  25. Anonymous says:

    Thank you for putting this site up. Your efforts are greatly appreciated!

  26. Mithun Reddy says:

    hi,
    can u please explain what does ‘%d’ do in printf AND scanf statements

  27. Anonymous says:

    exceelent

  28. Anonymous says:

    This website is really awesome….superb stuff…keep rocking!!!!!

  29. manasa says:

    excellent website.you guys are really doing good job.

  30. brijnandan says:

    really awesome website for learning c language

  31. Anonymous says:

    @mithun reddy
    “%d” prints the value of the given value supplied to the int (integer) variable.
    If we were to be dealing with floating point variables instead of whole numbers above, “%f” would be issued within the scanf() function to print the floats value.
    Now, if anyone can mind explaining how the single character d somehow correlates with a whole number I’d appreciate it greatly seeing as %f = float, %c = character makes perfect legitimate sense.

  32. Anonymous says:

    excellent

  33. Anonymous says:

    “Now, if anyone can mind explaining how the single character d somehow correlates with a whole number I’d appreciate it greatly seeing as %f = float, %c = character makes perfect legitimate sense.”

    I have always taken it to reference “digit”

  34. Anonymous says:

    can u tell me how to give different inputs when we use scanf ?

  35. very good
    and interesting

  36. Anonymous says:

    awe4some

  37. Anonymous says:

    awqewsomwe

  38. prashanth namani says:

    awesome

  39. AnKit says:

    Jinhe programming bilkul bhi nhi aati unhe shuruaat is website s karni chahie…. it impressed me actually.

  40. stalin wesley says:

    excellent bro

  41. kalai says:

    I don’t understand any programing lang… Can any1 help me out.. I feel difficult

  42. Shoumya Anmita says:

    Very nice Website and innovative way with programming

  43. Panacea says:

    please all of those codes their aren’t working in my compiler

Leave a Reply

Your email address will not be published. Required fields are marked *

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet