C Programming:
-----------------
C is a general purpose programming language founded by Dennis Ritche in 1972 at Bell labs.
Language:
-------
Langue is a vocabulary and set of gamer rules to community human in the world.
1)Learn alphabets(a-z)
2)Form words(i,we,he she,they ,the ete)
3)Form sentence by set of gramer rules.
4)Paragraph.
ex:
I go to office everyday at 10 am.
Programming language:
Programming language is a vocabulary and set of gramer rules to instruct the computer to perform various task.
1)learn alphabets,numbers,special symbols(a-z,0-9,@,$,^)
2)int a,b,c;
3)c=a+b;
4)program
c can be defines as follows:
1.mother language.
Most of the compilers,JVM's and kernels are written in c.
Most of the languages follow c syntax.
2.Procedure oriented language.
A procedure is know as function,methods,routine or sub routine.A procedure language specifies a series of steps for the program to perform tasks.
3.Structured language.
Structured means breaks the program into parts/functions,so that programmer is easy to understand.
4.Mid level language.
It supports the features of both low level and high level languages.
5.System programming language.
C is used to create system software.
features of C:
-------------
1.simple:
it is very easy to understand and it is English like language.the synatx is very easy.
2.structured programming.
Program is divided into procedures/functions,so that programmers can easy understand.
3.portable or machine independent.
unlike ALP ,c can be executed on any machines.
4.Rich library:
c provides lots of in build functions that make the development easy.
5.Memory management.
c supports dynamic memory allocation.so that we can free the memory using free() function at any time.
6.Fast speed.
The compilation and execution time of c is very fast,since there are very less functions.
7.Pointers.
C provides the pointers,so we can directly interact with memory using pointers.
8.Recursion.
we can the function with in the function,so that it provides code re usability for every function.
Flow of C program:
----------------------------------
1)Predecessor:
source file sample.c is sent Pre processor and preprocessor is converts preprocessor directives to respective values.
This is generates expanded source code ie. sample.i
2)compiler:
Compiler is responsible to compile the source code and generate the assembly code ie. sample.s
3)Assembler:
Assembler is covert the code into object code. ie. sample.obj
4)Linker:
Linker is links the header files and generate the object files ie. sample.exe file.
5)Loader:
Loader is load the exe file and loaded into run time memory and then executed.after that output is sent to console.
Printf() and scanf():
--------------------
Printf() function is used to print the output in console.
syntax:
printf("format string",argument list);
format String:
%d ->numbers
%f ->Float
%c->character
%s->Strings
Scanf():
scanf() function is used to read the data from input device.
syntax:
scanf("format string",arguement list);
Variables in c:
--------------
Variable is a name of the memory location.it ie used to story data.
sy:
data_type variable;
ex:
int a;
float a;
int a=10;
Rules to declare variables:
1)the variables can have alphabets,numbers and _ .
2)the variable name should start with an alphabet or _,it should not start with numbers.
3)No white spaces are allowed to declare a variable.
4)The variables must not contains the reserved words like int ,float,char etc.
Types of variables in c:
------------------------
1)Local variables.
2)Global variables.
3)static variables.
4)extern variables.
1)Local variables:
Local variables are declared inside functions or block.
ex:
void f1()
{
int a=10;
}
-we have to initialize the local variables before use.
2)Global variables:
A variable is declared outside the functions are called Global variable.The Global variables are accessible to any functions,we can change the global variable to any
function.
ex:
int pi=3.14;
void f1()
{
printf("%d",pi);
}
void f2()
{
printf("%d",pi);
}
3)Static variable:
A variable is declared inside the function with static keyword called static variable.
The value of static variable is retain for multiple calls for any function.
ex:
void f1()
{
int a=10;
static int b=20;
a++;
b++;
printf("%d,%d",a,b);
}
output
------
1)first call of f1();
11 21
2)second call of f1()
11 22
3)Third time call of f1()
11 23
4)extern variable:
we can share the variables in multiple files in c programming by using external variables.
ex:
myfile.h
-----------
extern int a=10;
sample.c
--------
#include "file.h"
#include<stdio.h>
void main()
{
printf("%d",a);
}
Data types:
-------------
Data types specifies type of data that variable can store.
There 4 data types in c:
1.Basic data type:
char,short,int,float,long etc.
2)Derived data type:
arrays,Pointer,struct,union.
3)Enumeration data type:
enum
4)Void data type:
void
Basic data types:
1)char: to store character data.
size:1 bype(8 bits)
range:-128 to 127.
ex:
char ch='a';
2)short:to store short integer value.
size:2 bytes
Range:-32768 to 32767
ex:
short i=12;
3)int:to store integer value.
size:2 bytes.
range:-32768 to 32767.
ex:
int a=5000;
4)long:to store long integer value.
size:4 bytes.
range:-2,147,483,648 to 2,147,483,647
ex:
long n=1233545;
5)Float:to store real numbers with decimal places.
size:4 bytes.
Range:
ex:
float price=50.50;
6)double:to store real numbers with big decimal places.
size:8 bytes
range:
ex:
double price=6789.565656565;
Keywords in c: 32 keywords.
----------------------------------
1)auto 2)static 3)extern 4)char 5)short
6)int 7)long 8)float 9)double 10)signed
11)unsigned 12)if 13) else 14)switch 15)case
16)break 17)default 18)while 19)do 20)for
21)continue 22)goto 23)sizeOf 24)struct 25)union
26)file 27)void 28)typeDef 29)volatile 30)const 31)return 32)enum
Operators in c:
--------------------
Operators are used to operate one or more operends to perform specific tasks.
Operators types in c:
1)Arithmatic operators.
2)Assignment operators.
3)Increment/Decrement operators.
4)Relational operators.
5)Logical operators.
6)Ternary operators.
7)Bitwise operators.
8)SizeOf() operator.
1)Arithmatic operators:
Arithmatic operators ar eused to perform arithmatic operations.
Ex:int a=10,b=5,c;
1)adition
c=a+b; // c=10+5=15
2)subtraction
c=a-b; // c=10-5=5
3)Multiplication
c=a*b // c=10*5=50
4)Division
c=a/b; // c=10/5 =2
5)Remainder
c=a%b; // c=10%5=0
2)Assignment operators:
Assignment operator is only one operator is used to assign some value to variable.
ex: int a=10;
1)Increment and assign
a+=20;->a=a+20=10+20=30;
2)Decrement and assign
int b=20;
b-=5;->b=b-5=20-5=15;
3)Increment /Decrement operators:
Increment /Decrement operators are used to Increment /Decrement a value by one.
ex:int a=10,b=20,c=30,d=40;
1)Pre increment
++a; // 11
2)Pre decrement
--b; // 19
3)Post increment
c++; // 31
4)Post decrement
d--; // 39
4)Relational operators.
Relational operators are used to check if the relation b/n operends,if the the relation is true then it result will be 1 otherwise 0.
ex:
int a=10,b=15;
1)Equal to (==)
a==b;->10==15;-->false=0
2)Not Equal to (!=)
a!=b;->10!=15;-->true=1
3)Less than (<)
a<b; ->10<15;-->true=1
4)Less than or equal to (<=)
a<=b; ->10<=15; -->true=1
5)Greater than (>)
a>b; ->10>15; -->false=0
6)Greater than or equal to (>=)
a>=b; ->10>=15; -->false =0
5)Logical operators:
Logical operators are used to check the relation b/n the expressions.if the releation b/n the expr is true then the result will be 1 otherwise 0.
ex:
int a=5, b=10,c=15;
1)Logical AND(&&)
(a<b) && (b<c)=(5<10) && (10<15)->1 && 1 =1
2)Logical OR(||)
(a>b) || (b<c) =(5>10) || (10<15)= 0 || 1 =1
3)Logical Not(!)
!(a>b)=!(5>10)=!0=1
6)Ternary operators:
syntax: (expression)? statement1:statement2;
Ternary operators is used to check if expression is true,if the expression is true then the first statement will be executed otherwise the statement2 will be executed.
ex:
int a=5,b=10;
(a>b)?pritf("Hello"):printf("Bye");
(5>10)?printf("Hello"):printf("Bye");
0??printf("Hello"):printf("Bye");
output:
Bye
7)Bit wise operators.
Bit wise operators are used to perform the bit wise operations.
a b a&b a|b a^b ~a
---------------------------
1 1 1 1 0 0
1 0 0 1 1 0
0 1 0 1 1 1
0 0 0 0 0 1
ex:
1) Bitwise AND (&)
a= 1001
b= 0001
--------
a&b=0001
--------
2) Bitwise OR (|)
a= 1001
b= 0001
--------
a|b=1001
--------
3) Bitwise XOR(^)
a= 1001
b= 0001
--------
a^b=1000
--------
4) Bitwise complement(~)
a= 1001
--------
~a= 0110
--------
5) Bitwise Left shift(<<)
a= 1001
--------
a<<2=0100
--------
6) Bitwise Right shift(>>)
a= 1001
--------
a>>2=0001
--------
8)SizeOf() operator:
The sizeOf operator is used to find the size of operand.
ex:
int a=10; //2
char ch='a'; // 1
float p=100.40; // 4
double price=500.50; //8
Control statements in C:
Control statements are used to control the flow of execution.
#include<stdio.h>
void main()
{
int a=10;
int b=5;
int c=a+b;
printf("%d'c);
}
Note:In the program execution will be is sequential order. if we want to control the flow of execution
we should go for control statements.
1)if
2)if else
3)if else ladder.
4)Nested if
5)Switch
6)while loop.
7)Do while loop.
8)for loop.
9)break
10)continue
1)if
-The operations specified in if block are executed if and only if the given condition is true.
sy:
if(condition)
{
// code to be executed
}
Flowchart:

Ex:
int age=18;
if(age>=18) //checks the condition 18>=18
{
printf("Major");
}
-----
output:
Major
2)if else statement:
syntax:
if(expression){
//code to be executed if condition is true
}else{
//code to be executed if condition is false
}
Flow chart:

3)if else ladder:
syntax:
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
}
Flowchart:

-----------------
C is a general purpose programming language founded by Dennis Ritche in 1972 at Bell labs.
Language:
-------
Langue is a vocabulary and set of gamer rules to community human in the world.
1)Learn alphabets(a-z)
2)Form words(i,we,he she,they ,the ete)
3)Form sentence by set of gramer rules.
4)Paragraph.
ex:
I go to office everyday at 10 am.
Programming language:
Programming language is a vocabulary and set of gramer rules to instruct the computer to perform various task.
1)learn alphabets,numbers,special symbols(a-z,0-9,@,$,^)
2)int a,b,c;
3)c=a+b;
4)program
c can be defines as follows:
1.mother language.
Most of the compilers,JVM's and kernels are written in c.
Most of the languages follow c syntax.
2.Procedure oriented language.
A procedure is know as function,methods,routine or sub routine.A procedure language specifies a series of steps for the program to perform tasks.
3.Structured language.
Structured means breaks the program into parts/functions,so that programmer is easy to understand.
4.Mid level language.
It supports the features of both low level and high level languages.
5.System programming language.
C is used to create system software.
features of C:
-------------
1.simple:
it is very easy to understand and it is English like language.the synatx is very easy.
2.structured programming.
Program is divided into procedures/functions,so that programmers can easy understand.
3.portable or machine independent.
unlike ALP ,c can be executed on any machines.
4.Rich library:
c provides lots of in build functions that make the development easy.
5.Memory management.
c supports dynamic memory allocation.so that we can free the memory using free() function at any time.
6.Fast speed.
The compilation and execution time of c is very fast,since there are very less functions.
7.Pointers.
C provides the pointers,so we can directly interact with memory using pointers.
8.Recursion.
we can the function with in the function,so that it provides code re usability for every function.
Flow of C program:
----------------------------------
1)Predecessor:
source file sample.c is sent Pre processor and preprocessor is converts preprocessor directives to respective values.
This is generates expanded source code ie. sample.i
2)compiler:
Compiler is responsible to compile the source code and generate the assembly code ie. sample.s
3)Assembler:
Assembler is covert the code into object code. ie. sample.obj
4)Linker:
Linker is links the header files and generate the object files ie. sample.exe file.
5)Loader:
Loader is load the exe file and loaded into run time memory and then executed.after that output is sent to console.
Printf() and scanf():
--------------------
Printf() function is used to print the output in console.
syntax:
printf("format string",argument list);
format String:
%d ->numbers
%f ->Float
%c->character
%s->Strings
Scanf():
scanf() function is used to read the data from input device.
syntax:
scanf("format string",arguement list);
Variables in c:
--------------
Variable is a name of the memory location.it ie used to story data.
sy:
data_type variable;
ex:
int a;
float a;
int a=10;
Rules to declare variables:
1)the variables can have alphabets,numbers and _ .
2)the variable name should start with an alphabet or _,it should not start with numbers.
3)No white spaces are allowed to declare a variable.
4)The variables must not contains the reserved words like int ,float,char etc.
Types of variables in c:
------------------------
1)Local variables.
2)Global variables.
3)static variables.
4)extern variables.
1)Local variables:
Local variables are declared inside functions or block.
ex:
void f1()
{
int a=10;
}
-we have to initialize the local variables before use.
2)Global variables:
A variable is declared outside the functions are called Global variable.The Global variables are accessible to any functions,we can change the global variable to any
function.
ex:
int pi=3.14;
void f1()
{
printf("%d",pi);
}
void f2()
{
printf("%d",pi);
}
3)Static variable:
A variable is declared inside the function with static keyword called static variable.
The value of static variable is retain for multiple calls for any function.
ex:
void f1()
{
int a=10;
static int b=20;
a++;
b++;
printf("%d,%d",a,b);
}
output
------
1)first call of f1();
11 21
2)second call of f1()
11 22
3)Third time call of f1()
11 23
4)extern variable:
we can share the variables in multiple files in c programming by using external variables.
ex:
myfile.h
-----------
extern int a=10;
sample.c
--------
#include "file.h"
#include<stdio.h>
void main()
{
printf("%d",a);
}
Data types:
-------------
Data types specifies type of data that variable can store.
There 4 data types in c:
1.Basic data type:
char,short,int,float,long etc.
2)Derived data type:
arrays,Pointer,struct,union.
3)Enumeration data type:
enum
4)Void data type:
void
Basic data types:
1)char: to store character data.
size:1 bype(8 bits)
range:-128 to 127.
ex:
char ch='a';
2)short:to store short integer value.
size:2 bytes
Range:-32768 to 32767
ex:
short i=12;
3)int:to store integer value.
size:2 bytes.
range:-32768 to 32767.
ex:
int a=5000;
4)long:to store long integer value.
size:4 bytes.
range:-2,147,483,648 to 2,147,483,647
ex:
long n=1233545;
5)Float:to store real numbers with decimal places.
size:4 bytes.
Range:
ex:
float price=50.50;
6)double:to store real numbers with big decimal places.
size:8 bytes
range:
ex:
double price=6789.565656565;
Keywords in c: 32 keywords.
----------------------------------
1)auto 2)static 3)extern 4)char 5)short
6)int 7)long 8)float 9)double 10)signed
11)unsigned 12)if 13) else 14)switch 15)case
16)break 17)default 18)while 19)do 20)for
21)continue 22)goto 23)sizeOf 24)struct 25)union
26)file 27)void 28)typeDef 29)volatile 30)const 31)return 32)enum
Operators in c:
--------------------
Operators are used to operate one or more operends to perform specific tasks.
Operators types in c:
1)Arithmatic operators.
2)Assignment operators.
3)Increment/Decrement operators.
4)Relational operators.
5)Logical operators.
6)Ternary operators.
7)Bitwise operators.
8)SizeOf() operator.
1)Arithmatic operators:
Arithmatic operators ar eused to perform arithmatic operations.
Ex:int a=10,b=5,c;
1)adition
c=a+b; // c=10+5=15
2)subtraction
c=a-b; // c=10-5=5
3)Multiplication
c=a*b // c=10*5=50
4)Division
c=a/b; // c=10/5 =2
5)Remainder
c=a%b; // c=10%5=0
2)Assignment operators:
Assignment operator is only one operator is used to assign some value to variable.
ex: int a=10;
1)Increment and assign
a+=20;->a=a+20=10+20=30;
2)Decrement and assign
int b=20;
b-=5;->b=b-5=20-5=15;
3)Increment /Decrement operators:
Increment /Decrement operators are used to Increment /Decrement a value by one.
ex:int a=10,b=20,c=30,d=40;
1)Pre increment
++a; // 11
2)Pre decrement
--b; // 19
3)Post increment
c++; // 31
4)Post decrement
d--; // 39
4)Relational operators.
Relational operators are used to check if the relation b/n operends,if the the relation is true then it result will be 1 otherwise 0.
ex:
int a=10,b=15;
1)Equal to (==)
a==b;->10==15;-->false=0
2)Not Equal to (!=)
a!=b;->10!=15;-->true=1
3)Less than (<)
a<b; ->10<15;-->true=1
4)Less than or equal to (<=)
a<=b; ->10<=15; -->true=1
5)Greater than (>)
a>b; ->10>15; -->false=0
6)Greater than or equal to (>=)
a>=b; ->10>=15; -->false =0
5)Logical operators:
Logical operators are used to check the relation b/n the expressions.if the releation b/n the expr is true then the result will be 1 otherwise 0.
ex:
int a=5, b=10,c=15;
1)Logical AND(&&)
(a<b) && (b<c)=(5<10) && (10<15)->1 && 1 =1
2)Logical OR(||)
(a>b) || (b<c) =(5>10) || (10<15)= 0 || 1 =1
3)Logical Not(!)
!(a>b)=!(5>10)=!0=1
6)Ternary operators:
syntax: (expression)? statement1:statement2;
Ternary operators is used to check if expression is true,if the expression is true then the first statement will be executed otherwise the statement2 will be executed.
ex:
int a=5,b=10;
(a>b)?pritf("Hello"):printf("Bye");
(5>10)?printf("Hello"):printf("Bye");
0??printf("Hello"):printf("Bye");
output:
Bye
7)Bit wise operators.
Bit wise operators are used to perform the bit wise operations.
a b a&b a|b a^b ~a
---------------------------
1 1 1 1 0 0
1 0 0 1 1 0
0 1 0 1 1 1
0 0 0 0 0 1
ex:
1) Bitwise AND (&)
a= 1001
b= 0001
--------
a&b=0001
--------
2) Bitwise OR (|)
a= 1001
b= 0001
--------
a|b=1001
--------
3) Bitwise XOR(^)
a= 1001
b= 0001
--------
a^b=1000
--------
4) Bitwise complement(~)
a= 1001
--------
~a= 0110
--------
5) Bitwise Left shift(<<)
a= 1001
--------
a<<2=0100
--------
6) Bitwise Right shift(>>)
a= 1001
--------
a>>2=0001
--------
8)SizeOf() operator:
The sizeOf operator is used to find the size of operand.
ex:
int a=10; //2
char ch='a'; // 1
float p=100.40; // 4
double price=500.50; //8
Control statements in C:
Control statements are used to control the flow of execution.
#include<stdio.h>
void main()
{
int a=10;
int b=5;
int c=a+b;
printf("%d'c);
}
Note:In the program execution will be is sequential order. if we want to control the flow of execution
we should go for control statements.
1)if
2)if else
3)if else ladder.
4)Nested if
5)Switch
6)while loop.
7)Do while loop.
8)for loop.
9)break
10)continue
1)if
-The operations specified in if block are executed if and only if the given condition is true.
sy:
if(condition)
{
// code to be executed
}
Flowchart:

Let's see a simple example
Ex:
int age=18;
if(age>=18) //checks the condition 18>=18
{
printf("Major");
}
-----
output:
Major
2)if else statement:
- The if-else statement is used to perform two operations for a single condition.
- we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition.
syntax:
if(expression){
//code to be executed if condition is true
}else{
//code to be executed if condition is false
}
Flow chart:

Let's see the simple example to check whether a number is even or odd using if-else statement in C language.
#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;
}
Output
enter a number:4
4 is even number
Program to check whether a person is eligible to vote or not.
#include <stdio.h>
int main()
{
int age;
printf("Enter your age?");
scanf("%d",&age);
if(age>=18)
{
printf("You are eligible to vote...");
}
else
{
printf("Sorry ... you can't vote");
}
}
Output
Enter your age?18
You are eligible to vote...
Enter your age?13
Sorry ... you can't vote
3)if else ladder:
- The if-else-if ladder statement is an extension to the if-else statement.
- It is used in the scenario where there are multiple cases to be performed for different conditions.
syntax:
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
}
Flowchart:

The example of an if-else-if statement in C language is given below.
Program to calculate the grade of the student according to the specified marks.
#include <stdio.h>
int main()
{
int marks;
printf("Enter your marks?");
scanf("%d",&marks);
if(marks > 85 && marks <= 100)
{
printf("Congrats ! you scored grade A ...");
}
else if (marks > 60 && marks <= 85)
{
printf("You scored grade B + ...");
}
else if (marks > 40 && marks <= 60)
{
printf("You scored grade B ...");
}
else if (marks > 30 && marks <= 40)
{
printf("You scored grade C ...");
}
else
{
printf("Sorry you are fail ...");
}
}
Output:
Enter your marks?10
Sorry you are fail ...
Enter your marks?40
You scored grade C ...
Enter your marks?90
Congrats ! you scored grade A ...
4)Nested if statement:
Syntax:
if(condition)
{
//outer block
if(condition)
{
/inner block
}
}
Ex:
int age=18;
int weight=60;
if(age>=18) //18>=18=true
{
if(weight>50) //60>50=true
{
printf("You are eligible to donate blood");
}
}
5)Swtich statement:
syntax:
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;
}
Rules for switch statement in C language
1) The switch expression must be of an integer or character type.
2) The case value must be an integer or character constant.
3) The case value can be used only inside the switch statement.
4) The break statement in switch case is not must. It is optional.
If there is no break statement found in the case, all the cases will be executed present after the matched case.
It is known as fall through the state of C switch statement.
Flowchart:

- The Nested if statement includes the if statement with in the another if block or else block.
- Inner block will executes only when the outer if condition is true.
Syntax:
if(condition)
{
//outer block
if(condition)
{
/inner block
}
}
Ex:
int age=18;
int weight=60;
if(age>=18) //18>=18=true
{
if(weight>50) //60>50=true
{
printf("You are eligible to donate blood");
}
}
5)Swtich statement:
- The switch statement in C is an alternate to if-else-if ladder statement.
- Switch allows us to execute multiple operations for the different possibles values of a single variable called switch variable.
syntax:
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;
}
Rules for switch statement in C language
1) The switch expression must be of an integer or character type.
2) The case value must be an integer or character constant.
3) The case value can be used only inside the switch statement.
4) The break statement in switch case is not must. It is optional.
If there is no break statement found in the case, all the cases will be executed present after the matched case.
It is known as fall through the state of C switch statement.
Flowchart:

#include<stdio.h>
void main()
{
int day;
printf("Enter a day:");
scanf("%d",&day);
switch(day)
{
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thirsday");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saterday");
break;
default:
System.out.println("Invalid case");
break;
}
}
output:
enter a day:2
Monday
Comments
Post a Comment