Basic form of C Programming
#include<stdio.h> int main(){ int a , b , c; a = 15; b = 10; c=a+b; printf("Sum of a and b :%d",c); return 0; }
If you look at the above program and previous example , you will notice that there are certain rules and pattern followed in C language . The rules are :
- There is common , #include<stdio.h> , that is called header file , which contain the set of predefined standard library function . In C program , necessarily contain header file , to use input and output functionality in the program . In C program , for input we use scanf() library function and for output we use printf() library function .
- All the statement written in C programming language place inside the curly braces {} .
- Every C statement must end with a semicolon ; , that act as statement terminator and all statement should be in lower case letters .
- Blank spaces may be inserted between two words to improve the readability of the statement .
How to Write comments in C programming Language
Comments are necessary for any programming language for understanding the source code . It is a good practice to begin a program with comment , which indicate the purpose of the program i.e., date , title of the program . Comments are also necessary but not important ,to understand the source code in later for modification or updation . Also implementation of comment beside the source code statement , help us to understand the logic .
Comment in C program :
There are basically two ways to write comments in C programming language .
- Comment should enclose inside /* */ .
- Write comment start with // .
/* Printing Integer , decimal and character */ #include<stdio.h> int main() {int a=20; float b=25.5; char c_val='g'; printf("In this program the integer varible is : %i\n",a); printf("In this program the integer varible is : %f\n",b); printf("In this program the integer varible is : %c",c_val); return 0; }
If you look at the first line of above program , you will notice comment . C compiler just ignore the first line .
Multi-line comment in C program:
- Multi-line comment should be written also within inside the /* */ .
/* Printing Integer , decimal and character integer variable must be declare with int , decimal variable must also be declare with float */ #include<stdio.h> int main() {int a=20; float b=25.5; char c_val='g'; printf("In this program the integer varible is : %i\n",a); printf("In this program the integer varible is : %f\n",b); printf("In this program the integer varible is : %c",c_val); return 0; }
Single line comment in C program:
Single line comment should be written within // .
/* Printing Integer , decimal and character integer variable must be declare with int , decimal variable must also be declare with float */ #include<stdio.h> int main() {int a=20; // this is integer variable float b=25.5; // this is float variable char c_val='g'; printf("In this program the integer varible is : %i\n",a); printf("In this program the integer varible is : %f\n",b); printf("In this program the integer varible is : %c",c_val); return 0; }Note :- If you write comment with /* */ , comment should be enclosed .
/* Printing Integer , decimal and character
integer variable must be declare with int ,
decimal variable must also be declare with float
#include<stdio.h>
int main()
{int a=20; // this is integer variable
float b=25.5; // this is float variable
char c_val='g';
printf("In this program the integer varible is : %i\n",a);
printf("In this program the integer varible is : %f\n",b);
printf("In this program the integer varible is : %c",c_val);
return 0;
}
If you look at above program from first line to third line , you will notice that , we remove */ , I mean to say , we wrote comments , but did not close it. As a result , compiler simply return an error - C Files\variable.c [Error] unterminated comment
main() Function in C language:
main() is very important for writing code in C language . There are multiple function in C language , main() is one of them . A function is container for set of statements . All statement belongs to the main() are enclosed within a pair of curly braces{} .
Function in C language also return a value . main() function always return an integer value , thats why there is int before main() .
Syntax:
int main()
{
statement 1;
statement 2;
return 0;
printf() function in C language:
Suppose you just write a simple code to multiply 50 with 87 . Lets write the code ,
#include<stdio.h> int main() { int a =50; int b =87; int c; c=a*b; // multiplication of 50 and 87 return 0; }
If you run the above code , C compiler will simply run the code and as a output it will give something like below .
-------------------------------- Process exited after 0.06084 seconds with return value 0 Press any key to continue . . . |
To able to use printf() function , it is necessary to use #include<stdio.h> at the beginning of the program . #include is a pre-processor directive.
Syntax of use of printf() function :
printf("format string", variable);
format string -> Which type of output want to print .
- %i or %d for printing integer type(5) .
- %f for printing real value (5.5) .
- %c for printing character value(a) .
#include<stdio.h> int main() { int a =50; int b =87; int c; c=a*b; // multiplication of 50 and 87 printf("%i",c); /*printing the multiplication value*/ return 0; }
Output will be something like this ,
4350 -------------------------------- Process exited after 0.1151 seconds with return value 0 Press any key to continue . . . |
#include<stdio.h> int main() { int a =50; int b =87; int c; c=a*b; // multiplication of 50 and 87 printf("Multiplication of 50 and 87: %i",c); /*printing the multiplication value*/ return 0; }
Output of the above code something like this ,
Multiplication of 50 and 87: 4350 -------------------------------- Process exited after 0.0691 seconds with return value 0 Press any key to continue . . . |
Please update next topic .
ReplyDelete