Академический Документы
Профессиональный Документы
Культура Документы
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.
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
float 4 byte
double 8 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.
An operator is simply a symbol that is used to perform operations. There can be many types
of operations like arithmetic, logical, bitwise etc.
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
Constant
1. const keyword
2. #define preprocessor
#include<stdio.h>
int main(){
PI=4.5;
return 0;
#include <stdio.h>
#define PI 3.14
main() {
printf("%f",PI);
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);
return 0;
#include<stdio.h>
int main(){
int x=0,y=0,result=0;
printf("enter first number:");
scanf("%d",&x);
scanf("%d",&y);
result=x+y;
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){
return 0;
}
If-else statement
if(expression){
}else{
}
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number%2==0){
else{
return 0;
If-else-if statement
if(condition1){
}else if(condition2){
else if(condition3){
...
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){
}
else if(number==50){
else if(number==100){
else{
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.
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
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:
break;
case 100:
break;
default:
return 0;
Loops
The loops in C language are used to execute a block of code or a part of the program
several times.
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.
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(){
for(i=1;i<=10;i++){
printf("%d \n",i);
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.
#include<stdio.h>
int main(){
for(i=1;i<=10;i++){
continue;
printf("%d \n",i);
return 0;
Goto
#include <stdio.h>
int main() {
int age;
ineligible:
scanf("%d", &age);
if(age<18)
goto ineligible;
else
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
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:
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.
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...);
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. }
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.
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