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
Superb! Excellent website!
good site
awesome website…
This site is very good for beginner.
xcellent explanation
Very very good
Thank you 🙂
the best ever
thankks…
OMG,i checked out this website bfor my exams,it was awesome,truly magnificient!!!!!!!!!!!!!!!!!!!!!!
Thanks a lot for your valuable comments.
why or when do you use /n in printf
\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.
Good Job.Thankx a lot 🙂
what does conio do?
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
Really very nice site to learn very quickly
good
Can u explain me why do u use scanf and printf instead of cin>> and cout<
explaination is good
awesome site…
woohoo atlast a nice site
its really easy to understand …….thanks for assisting us …..
I never understand c but after joining this it look like a piece of cake
Realy superb job,plz help best website to learn embedded programming…..
how to start prorgraming of c where we write all the scripts????
Thank you for putting this site up. Your efforts are greatly appreciated!
hi,
can u please explain what does ‘%d’ do in printf AND scanf statements
exceelent
This website is really awesome….superb stuff…keep rocking!!!!!
excellent website.you guys are really doing good job.
really awesome website for learning c language
@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.
excellent
“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”
can u tell me how to give different inputs when we use scanf ?
perfect
very good
and interesting
awe4some
awqewsomwe
awesome
Jinhe programming bilkul bhi nhi aati unhe shuruaat is website s karni chahie…. it impressed me actually.
excellent bro
I don’t understand any programing lang… Can any1 help me out.. I feel difficult
What issue are you facing?
Very nice Website and innovative way with programming
please all of those codes their aren’t working in my compiler