25/8/2018
Online C compiler
www.onlinegdb.com
First C program
#include <stdio.h>
int main(){
printf("Hello C Language");
return 0;
#include <stdio.h> includes the standard input output library functions. The printf()
function is defined in stdio.h .
int main() The main() function is the entry point of every program in c language.
printf() The printf() function is used to print data on the console.
return 0 The return 0 statement, returns execution status to the OS. The 0 value is used
for successful execution and 1 for unsuccessful execution.
Data Types
Types Data Types
Basic Data Type int, char, float, double
Derived Data Type array, pointer, structure, union
Enumeration Data Type enum
Void Data Type void
Data Types Memory Size Range
char 1 byte −128 to 127
signed char 1 byte −128 to 127
unsigned char 1 byte 0 to 255
short 2 byte −32,768 to 32,767
signed short 2 byte −32,768 to 32,767
unsigned short 2 byte 0 to 65,535
int 2 byte −32,768 to 32,767
signed int 2 byte −32,768 to 32,767
unsigned int 2 byte 0 to 65,535
short int 2 byte −32,768 to 32,767
signed short int 2 byte −32,768 to 32,767
unsigned short int 2 byte 0 to 65,535
long int 4 byte -2,147,483,648 to 2,147,483,647
signed long int 4 byte -2,147,483,648 to 2,147,483,647
unsigned long int 4 byte 0 to 4,294,967,295
float 4 byte
double 8 byte
long double 10 byte
A keyword is a reserved word. You cannot use it as a variable name, constant name etc.
There are only 32 reserved words (keywords) in C language.
A list of 32 keywords in c language is given below:
auto brea case char const continu defaul do
k e t
doubl else enum exter float for goto if
e n
int long regist retur short signed sizeof stati
er n c
struct switc typed union unsigne void volatil whil
h ef d e e
Operators
An operator is simply a symbol that is used to perform operations. There can be many types
of operations like arithmetic, logical, bitwise etc.
There are following types of operators to perform different types of operations in C
language.
o Arithmetic Operators
o Relational Operators
o Shift Operators
o Logical Operators
o Bitwise Operators
o Ternary or Conditional Operators
o Assignment Operator
o Misc Operator
ategory Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right
Constant
There are two ways to define constant in C programming.
1. const keyword
2. #define preprocessor
The const keyword is used to define constant in C programming.
const float PI=3.14;
#include<stdio.h>
int main(){
const float PI=3.14;
PI=4.5;
printf("The value of PI is: %f",PI);
return 0;
The #define preprocessor is also used to define constant.
#define token value
#include <stdio.h>
#define PI 3.14
main() {
printf("%f",PI);
Printf and Scanf statement
The printf() and scanf() functions are used for input and output in C language. Both
functions are inbuilt library functions, defined in stdio.h (header file).
printf("format string",argument_list);
scanf("format string",argument_list);
#include<stdio.h>
int main(){
int number;
printf("enter a number:");
scanf("%d",&number);
printf("cube of number is:%d ",number*number*number);
return 0;
#include<stdio.h>
int main(){
int x=0,y=0,result=0;
printf("enter first number:");
scanf("%d",&x);
printf("enter second number:");
scanf("%d",&y);
result=x+y;
printf("sum of 2 numbers:%d ",result);
return 0;
Decision Making
If Statement
The single if statement in C language is used to execute the code if condition is true. The
syntax of if statement is given below:
if(expression){
//code to be executed
}
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
return 0;
}
If-else statement
if(expression){
//code to be executed if condition is true
}else{
//code to be executed if condition is false
}
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
else{
printf("%d is odd number",number);
return 0;
If-else-if statement
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
else if(condition3){
//code to be executed if condition3 is true
...
else{
//code to be executed if all the conditions are false
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number==10){
printf("number is equals to 10");
}
else if(number==50){
printf("number is equal to 50");
else if(number==100){
printf("number is equal to 100");
else{
printf("number is not equal to 10, 50 or 100");
return 0;
Switch statement
The switch statement in C language is used to execute the code from multiple conditions. It
is like if else-if ladder statement.
The syntax of switch statement in c language is given below:
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
Valid Switch Invalid Switch Valid Case Invalid Case
switch(x) switch(f) case 3; case 2.5;
switch(x>y) switch(x+2.5) case 'a'; case x;
switch(a+b-2) case 1+2; case x+2;
switch(func(x,y)) case 'x'>'y'; case 1,2,3;
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
switch(number){
case 10:
printf("number is equals to 10");
break;
case 50:
printf("number is equal to 50");
break;
case 100:
printf("number is equal to 100");
break;
default:
printf("number is not equal to 10, 50 or 100");
return 0;
Loops
The loops in C language are used to execute a block of code or a part of the program
several times.
In other words, it iterates a code or group of code many times.
There are three types of loops in C language that is given below:
1. do while
2. while
3. for
The for loop in C language is also used to iterate the statement or a part of the program
several times, like while and do-while loop.
But, we can initialize and increment or decrement the variable also at the time of checking
the condition in for loop.
Unlike do while loop, the condition or expression in for loop is given before the statement,
so it may execute the statement 0 or more times.
1. for(initialization;condition;incr/decr){
2. //code to be executed
3. }
#include<stdio.h>
int main(){
int i=0;
for(i=1;i<=10;i++){
printf("%d \n",i);
return 0;
Do while
To execute a part of program or code several times, we can use do-while loop of C
language. The code given between the do and while block will be executed until condition is
true.
In do while loop, statement is given before the condition, so statement or code will be
executed at lease one time. In other words, we can say it is executed 1 or more times.
It is better if you have to execute the code at least once.
1. do{
2. //code to be executed
3. }while(condition);
#include<stdio.h>
int main(){
int i=1;
do{
printf("%d \n",i);
i++;
}while(i<=10);
return 0;
While
The while loop in C language is used to iterate the part of program or statements many
times.
In while loop, condition is given before the statement. So it is different from the do while
loop. It can execute the statements 0 or more times.
1. while(condition){
2. //code to be executed
3. }
#include<stdio.h>
int main(){
int i=1;
while(i<=10){
printf("%d \n",i);
i++;
return 0;
Break
The break statement in C language is used to break the execution of loop (while, do while
and for) and switch case.
#include<stdio.h>
int main(){
int i=1;//initializing a local variable
//starting a loop from 1 to 10
for(i=1;i<=10;i++){
printf("%d \n",i);
if(i==5){//if value of i is equal to 5, it will break the loop
break;
return 0;
}
Continue
The continue statement in C language is used to continue the execution of loop (while, do
while and for). It is used with if condition within the loop.
In case of inner loops, it continues the control of inner loop only.
#include<stdio.h>
int main(){
int i=1;//initializing a local variable
//starting a loop from 1 to 10
for(i=1;i<=10;i++){
if(i==5){//if value of i is equal to 5, it will continue the loop
continue;
printf("%d \n",i);
}//end of for loop
return 0;
Goto
The goto statement is known as jump statement in C language. It is used to unconditionally
jump to other label. It transfers control to other parts of the program.
#include <stdio.h>
int main() {
int age;
ineligible:
printf("You are not eligible to vote!\n");
printf("Enter you age:\n");
scanf("%d", &age);
if(age<18)
goto ineligible;
else
printf("You are eligible to vote!\n");
return 0;
Type Casting
#include<stdio.h>
int main(){
float f= (float)9/4;
printf("f : %f\n", f );
return 0;
functions in C
The function in C language is also known as procedure or subroutine in other
programming languages.
To perform any task, we can create function. A function can be called many times. It
provides modularity and code reusability.
Advantage of functions in C
There are many advantages of functions.
1) Code Reusability
By creating functions in C, you can call it many times. So we don't need to write the
same code again and again.
2) Code optimization
It makes the code optimized, we don't need to write much code.
Suppose, you have to check 3 numbers (781, 883 and 531) whether it is prime number
or not. Without using function, you need to write the prime number logic 3 times. So,
there is repetition of code.
But if you use functions, you need to write the logic only once and you can reuse it
several times.
Types of Functions
There are two types of functions in C programming:
1. Library Functions: are the functions which are declared in the C header files
such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.
2. User-defined functions: are the functions which are created by the C
programmer, so that he/she can use it many times. It reduces complexity of a big
program and optimizes the code.
Declaration of a function
The syntax of creating function in c language is given below:
1. return_type function_name(data_type parameter...){
2. //code to be executed
3. }
Return Value
A C function may or may not return a value from the function. If you don't have to return
any value from the function, use void for the return type.
Let's see a simple example of C function that doesn't return any value from the function.
Example without return value:
1. void hello(){
2. printf("hello c");
3. }
Parameters in C Function
A c function may have 0 or more parameters. You can have any type of parameter in C
program such as int, float, char etc. The parameters are also known as formal arguments.
1. int add(int a, int b){
2. return a+b;
3. }
Calling a function in C
If a function returns any value, you need to call function to get the value returned from the
function. The syntax of calling a function in c programming is given below:
1. variable=function_name(arguments...);
1. hello();//calls function that doesn't return a value
2. int value=get();//calls function that returns value
3. int value2=add(10,20);//calls parameterized function by passing 2 values
Call by value and call by reference in C
There are two ways to pass value or data to function in C language: call by value and call by
reference. Original value is not modified in call by value but it is modified in call by
reference.
Call by value in C
In call by value, original value is not modified.
In call by value, value being passed to the function is locally stored by the function
parameter in stack memory location. If you change the value of function parameter, it is
changed for the current function only. It will not change the value of variable inside the
caller method such as main().
Let's try to understand the concept of call by value in c language by the example given
below:
1. #include<stdio.h>
2. void change(int num) {
3. printf("Before adding value inside function num=%d \n",num);
4. num=num+100;
5. printf("After adding value inside function num=%d \n", num);
6. }
7. int main() {
8. int x=100;
9. printf("Before function call x=%d \n", x);
10. change(x);//passing value in function
11. printf("After function call x=%d \n", x);
12. return 0;
13. }
Call by reference in C
In call by reference, original value is modified because we pass reference (address).
Here, address of the value is passed in the function, so actual and formal arguments shares
the same address space. Hence, value changed inside the function, is reflected inside as
well as outside the function.
1. #include<stdio.h>
2. void change(int *num) {
3. printf("Before adding value inside function num=%d \n",*num);
4. (*num) += 100;
5. printf("After adding value inside function num=%d \n", *num);
6. }
7. int main() {
8. int x=100;
9. printf("Before function call x=%d \n", x);
10. change(&x);//passing reference in function
11. printf("After function call x=%d \n", x);
12. return 0;
13. }
Difference between call by value and call by
reference in c
No. Call by value Call by reference
1 A copy of value is passed to the An address of value is passed to the
function function
2 Changes made inside the function is Changes made inside the function is
not reflected on other functions reflected outside the function also
3 Actual and formal arguments will be Actual and formal arguments will be
created in different memory location created in same memory location
Recursion in C
When function is called within the same function, it is known as recursion in C. The
function which calls the same function, is known as recursive function.
A function that calls itself, and doesn't perform any task after function call, is know as tail
recursion. In tail recursion, we generally call the same function with return statement. An
example of tail recursion is given below.
Let's see a simple example of recursion.
1. recursionfunction(){
2. recursionfunction();//calling self function
3. }
1. #include<stdio.h>
2. int factorial (int n)
3. {
4. if ( n < 0)
5. return -1; /*Wrong value*/
6. if (n == 0)
7. return 1; /*Terminating condition*/
8. return (n * factorial (n -1));
9. }
10. int main(){
11. int fact=0;
12. fact=factorial(5);
13. printf("\n factorial of 5 is %d",fact);
14. return 0;
15. }
End of 25/8/2018