SlideShare a Scribd company logo
1 of 81
Data Types can also be classified as shown in the image below –
Primitive, Derived and User Defined.
1.Primitive Data Types Fundamental data types are
further categorized into 3 types. Let us discuss each of this type briefly.
Int (Integer)
Integer data type is used to store numeric values without any decimal point
e.g. 7, -101, 107, etc.
A variable declared as 'int' must contain whole numbers e.g. age is
always in number.
Syntax:
int variable name;
E.g. int roll, marks, age;
Float
Float data type is used to store numeric values with decimal point. In other
words, float data type is used to store real values, e.g. 3.14, 7.67 etc. A
variable declared as float must contain decimal values e.g. percentage,
price, pi, area etc. may contain real values.
Syntax:
Float variable name;
E.g. float per, area;
Char (Character)
Char (Character) data type is used to store single character, within single
quotes e.g. 'a', 'z','e' etc. A variable declared as 'char' can store only
single character e.g. Yes or No Choice requires only 'y' or 'n' as an
answer.
Syntax:
Char variable name;
E.g. Char chi='a', cha;
Void
Void data type is used to represent an empty value. It is used as a return
type if a function does not return any value.
Type modifier
Type modifier is used to change the meaning of basic or fundamental
data types so that they can be used in different situations. Various
type modifiers are- signed, unsigned, short and long.
C language offers 3 different 'int' type modifiers - short, int, and long
that represents three different integer sizes. C language also offers 3
different floating point type modifiers - float, double, and long
double. Two special 'char' type modifiers are- signed, unsigned.
C language offers 3 different 'int' type modifiers - short, int, and long
that represents three different integer sizes.
C language also offers 3 different floating point type modifiers - float,
double, and long double. Two special 'char' type modifiers are-
signed, unsigned.
Type Size (in bytes) Range
Int (signed/ short) 2 -32768 to 32767
Int (unsigned) 2 0 to 65535
Long (signed) 4 -2,147,483,648 to 2,147,483,648
Long (unsigned) 4 0 to 4,294,467,295
Float 4 3.4*10-34 to 3.4*10-34
Double (long float) 8 1.7*10-308 to 1.7*10-308 -1
Long double 10 3.4*10-4932 to 3.4*10-4932 -1
Char 1 -128 to 127
Signed char 1 0 to 255
Unsigned char 1 -128 to 127
2. Derived Data Types
Data types that are derived from fundamental data types are called derived
data types. Derived data types don't create a new data type; instead, they
add some functionality to the basic data types. Two derived data type
are - Array & Pointer.
Array
An array is a collection of variables of same type i.e. collection of
homogeneous data referred by a common name. In memory, array
elements are stored in a continuous location.
Syntax:
[];
E.g. int a[10];
Char chi [20];
According to the rules of C language, 1st element of array is stored at
location a[0] , 2nd at a[1] & so on.
Pointer
A pointer is a special variable that holds a memory address (location in
memory) of another variable.
Syntax:
*
Data type is the type whose address we want to store in the pointer variable.
* is a pointer variable.
'var name' is the name where the variable is to be stored.
E.g. if we want to store the address of an 'int' data type variable into a
pointer variable, it is done in the following manner:
int a,*b;
b=&a;
In the above case, variable 'b' stores the address of variable 'a'.
3. User Defined Data Type
User defined data type is used to create new data types. The new data
types formed are fundamental data types. Different user defined data
types are: struct, union, enum, typedef.
Struct
A struct is a user defined data type that stores multiple values of same
or different data types under a single name. In memory, the entire
structure variable is stored in sequence.
Syntax:
Struct < structure name>
{
Var1;
Var2;
-----
-----
};
Structure name is the name of structure that we have to give e.g. if we want
to store details of a student as- name, roll, marks then in structure it will
be declared in the following manner:
Struct student
{
Char name [20];
Int roll,
Float marks;
};
Now, its variable is declared as: struct
E.g. Struct student s1;
And its variable is accessed using dot (.) operator.
S1.roll, s1.name, s1.marks
Union
A union is a user defined data type that stores multiple values of same or
different data types under a single name. In memory, union variables are
stored in a common memory location.
Syntax:
Union < tag name>
{
Var1;
Var2;
-----
----
};
Tag name is the name of union, e.g. if we want to store details of a student as-
name, roll, marks then it will be done in the following manner
:
Union student
{
Char name [20];
Int roll,
Float marks;
};
Now, the variable is declared as follows:
struct
e.g. Union student s1;
And its variable is accessed by using dot (.) operator.
S1.roll, s1.name, s1.marks
The only difference between Union and Struct is allocation of memory.
'Union' is assigned a memory size equal to the biggest data type used
in union declaration whereas 'struct' will occupy the size equal to the
sum of all variables sizes.
Enum:
An enumeration is a data type similar to a struct or a union. Its members are
constants that are written as variables, though they have signed integer
values. These constant represent values that can be assigned to
corresponding enumeration variables.
Syntax:
Enum {value1, value2, _ _ _ _ _ _, value n};
Enum is the required keyword;
Tag is the name that identifies enumerations having this composition;
Value1, value2, - - - - - - , value n represents the individual identifiers
that may be assigned to a variable of this type.
E.g. : Enum colors {black, blue, cyan, green, red, yellow};
Color foreground, background;
First line defines enum and second one defines enum variables. Thus,
variables foreground and background can be assigned any one of
constant black, blue, - - - - ,yellow.
In declaration black is assigned 0 value, and blue 1, - - - -, and yellow
5. We can also assign the value accordingly as we want to assign,
like:
Enum colors {black, blue, cyan=4, green, red, yellow}
Here black=0, blue=1, cyan=4, green=5, red=6, yellow=7
Typedef:
The 'typedef' allows the user to define new data-types that are
equivalent to existing data types. Once a user defined data type has
been established, then new variables, array, structures, etc. can be
declared in terms of this new data type.
A new data type is defined as:
Typedef type new-type;
Type refers to an existing data type,
New-type refers to the new user-defined data type.
E.g. : typedef int number
Now we can declare integer variables as:
number roll, age, marks;
It is equivalent to:
int roll, age, marks;
A variable can be defined in many ways. At the basic level, a variable
can be defined as a memory location declared to store any kind of
data (which may change many times during program execution). It
can also be defined as any quantity/entity which may vary during
program execution. To identify a variable (the declared memory
location), it can be assigned a name – known as variable name. The
name given to a variable is known as an identifier. An identifier
can be a combination of alphabets , digits and underscore. There are
certain set of rules which must be observed while naming a variable.
Rules for naming a variable:-
 A variable name (identifier) can be any combination of alphabets,
digits and underscore.
 First character should be a letter (alphabet).
 Length of variable name can range from 1 to 8. (Note: Different
compilers may allow different ranges, say upto 31. But it is a good
practice to keep the variable name short.)
 A space in between is not allowed. Ex: A variable name can not be
declared as var name
 Underscore can be used to concatenate name combinations. Ex:
var_name , var_123 are valid identifiers.
 No commas or other special characters (other than underscore _ ) are
allowed in a variable name.
 C is a case sensitive language – which means a variable name declared
as flag is not same as FLAG. They both will be treated as different
variables.
There are certain reserved words in C language, known as keywords.
Words similar to a keyword can not be used as a variable name. Ex:- In
the previous article on data types, we saw int, char, float etc. These
are actually keywords. So you can’t declare a variable with names
int, char or float.
Variable Declaration
A variable must be declared first before we can use it in a
program for manipulations. A variable is declared with its
storage class, data type and identifier. The format is shown
below:-
Storage-class Data-Type Variable-name;
Storage class is some thing we will learn in coming chapters. Let me
brief it. There are 4 storage classes namely Automatic, Static,
External and Register storage classes. Each of them has its own
meaning and usage. Storage class is used to attribute certain features
to a variable like its scope (local or global), where to store the
variable (in memory or in register), the life time of a variable etc. It
is not necessary to specify a storage class while declaring a variable.
By default all variable declarations (without any storage class
specified) will be assigned to “automatic storage class”. We will
discuss more about storage class in another chapter.
So our variable declaration would be like:-
Data-Type Variable-name;
Examples:-
int a;
int count;
char name;
Two or more variables of the same data type can be declared in a single line,
separating each variable names with a comma and ending the line with a
semicolon.
Examples:-
int n,count,flag,i,j;
char name,address,nick_nm;
Initial values can be assigned to variables while declaring it.
Examples:-
int a,num,count=0,flag=10,mark=100;
char name=Robert,sur_nam=Nero,mid_nam=de;
Let us examine what happens when we declare a variable.
int j=10;
Here we have declared a variable of data type integer with name as j
and initial value as 10. This declaration tells the C compiler to:-
Reserve space in memory to hold the integer value.
Assign the name ‘j’ to that reserved memory space.
Store the value 10 in this memory location.
The symbols which are used to perform logical and mathematical
operations are called C operators.
These C operators join individual constants and variables to form
expressions. Operators, functions, constants and variables are
combined together to form expressions.
Example : A + B * 5
where,
+, * - operators
A, B - variables
5 – constant
A + B * 5 - expression
Types of C operators:
C language offers many types of operators. They are,
Arithmetic operators
Assignment operators
Relational operators
Logical operators
Bit wise operators
Conditional operators (ternary operators)
Increment/decrement operators
Special operators
1. Arithmetic Operators:
These operators are used to perform mathematical calculations
like addition, subtraction, multiplication, division and modulus.
S.no Operator Operation
1 + Addition
2 - Subtraction
3 * Multiplication
4 / Division
5 % Modulus
Example program for C arithmetic operators:
# include <stdio.h>
int main()
{
int a=40,b=20, add,sub,mul,div,mod;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is : %dn", add);
printf("Subtraction of a, b is : %dn", sub);
printf("Multiplication of a, b is : %dn", mul);
printf("Division of a, b is : %dn", div);
printf("Modulus of a, b is : %dn", mod);
}
Output:
Addition of a, b is : 60
Subtraction of a, b is : 20
Multiplication of a, b is : 800
Division of a, b is : 2
Modulus of a, b is : 0
2. Assignment operators:
The values for the variables are assigned using assignment
operators. For example, if the value 10 is to be assigned for the
variable sum, it can be done like below.
sum = 10;
Below are some of other assignment operators:
Operator Example Explanation
Simple operator = sum = 10
Value 10 is assigned to
variable sum
Compound assignment
operator
+= sum+=10
This is same as sum =
sum+10
-= sum – = 10
This is same as sum =
sum – 10
*= sum*=10
This is same as sum =
sum*10
/+ sum/=10
This is same as sum =
sum/10
%= sum%=10
This is same as sum =
sum%10
&= sum&=10
This is same as sum =
sum&10
^= sum^=10
This is same as sum =
sum^10
Example program for assignment operators:
# include <stdio.h>
int main()
{
int Total=0,i;
for(i=0;i<10;i++)
{
Total+=i; // This is same as Total = Toatal+i
}
printf("Total = %d", Total);
}
Output:
Total = 45
3. Relational operators:
These operators are used to find the relation between two
variables. That is, used to compare the value of two variables.
Operator Example
> x > y
< x < y
>= x >= y
<= x <= y
== x == y
!= x != y
Example program for relational operators:
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m == n) {
printf("m and n are equal");
}
else {
printf("m and n are not equal");
}
}
Output:
m and n are not equal
4. Logical operators:
These operators are used to perform logical operations on the
given two variables.
Operator Example
&& x && y
|| x || y
! x (x || y), !(x && y)
Example program for logical operators:
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m>n && m !=0) {
printf("m is greater than n and not equal to 0");
}
}
Output:
m is greater than n and not equal to 0
5. Bit wise operators:
These operators are used to perform bit operations on given two
variables.
Truth table for bitwise operation:
x y x/y x & y x ^ y
0 0 0 0 0
0 1 1 0 1
1 0 1 0 1
1 1 1 1 0
Bitwise operators:
Operator Explanation
& Bitwise AND
| Bitwise OR
~ Bitwise NOT
^ XOR
<< Left Shift
>> Right Shift
Example program for bitwise operators:
#include <stdio.h>
int main()
{
int m=40,n=20,AND_opr,OR_opr,XOR_opr ;
AND_opr = (m&n);
OR_opr = (m|n);
XOR_opr = (m^n);
printf("AND_opr value = %dn",AND_opr );
printf("XOR_opr value = %dn",XOR_opr );
printf("OR_opr value = %dn",OR_opr );
}
Output:
AND_opr value = 0
XOR_opr value = 60
OR_opr value = 60
6. Conditional operators:
Conditional operators return one value if condition is true and
returns other value is condition is false.
This operator is also called as ternary operator.
Syntax : (Condition? true_value: false_value);
Example : (A > 100 ? 0 : 1);
Here, if A is greater than 100, 0 is returned else 1 is
returned. This is equal to if else conditional statements.
Example program for conditional/ternary operators:
#include <stdio.h>
int main()
{
int x=1, y ;
y = ( x ==1 ? 2 : 0 ) ;
printf("x value is %dn", x);
printf("y value is %d", y);
}
Output:
x value is 1
y value is 2
7. Increment and decrement operators:
These operators are used to either increase or decrease the value
of the variable by one.
Syntax : ++var_name; (or)
var_name++; (or)
– - var_name; (or)
var_name- -;
Example : ++i, i++, - -i, i- -
}
Example program for increment operators:
#include <stdio.h>
int main()
{
int i=1;
while(i<10)
{
printf("%dn",i);
i++;
}
}
Output:
1
2
3
4
5
6
7
8
9
Example program for decrement operators:
#include <stdio.h>
int main()
{
int i=20;
while(i>10)
{
printf("%dn",i);
i--;
}
}
Output:
20
19
18
17
16
15
14
13
12
11
8. Special Operators:
Below are some of special operators that the C language offers.
S.no Operators Description
1 &
This is used to get the address of the variable.
Example: &a will give address of a.
2 *
This is used as pointer to a variable.
Example: * a where, * is pointer to the variable a.
3 Size of ()
This gives the size of the variable.
Example: size of (char) will give us 1.
4 ternary This is ternary operator and act exactly as if … else statement.
5 Type cast
Type cast is the concept of modifying variable from one data type to
other.
Conditional statements are used to execute a statement or a group of
statement based on certain conditions.
We will look into following conditional statements.
if
if else
else if
switch
Goto
1) if conditional statement in C :
Syntax for if statement in C :
if(condition)
{
Valid C Statements;
}
If the condition is true the statements inside the parenthesis { }, will be
executed, else the control will be transferred to the next statement
after if.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
a=10;
b=5;
if(a>b)
{
printf("a is greater");
}
getch();
}
Program Algorithm / Explanation :
 #include<stdio.h> header file is included because, the C in-built
statement printf we used in this program comes under stdio.h header
files.
 #include<conio.h> is used because the C in-built function getch()
comes under conio.h header files.
 main () function is the place where C program execution begins.
 Two variables a&b of type int is declared.
 Variable a is assigned a value of 10 and variable b with 5.
 A if condition is used to check whether a is greater than b. if(a>b)
 As a is greater than b the printf inside the if { } is executed with a
message “a is greater”.
Output :
2) if else in C :
Syntax for if :
if(condition)
{
Valid C Statements;
}
else
{
Valid C Statements;
}
In if else if the condition is true the statements between if and else is
executed. If it is false the statement after else is executed.
Sample Program :
if_else.c
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Enter a value for a:");
scanf("%d",&a);
printf("nEnter a value for b:");
scanf("%d",&b);
if(a>b)
{
printf("n a got greater value");
}
else
{
printf("n b got greater value");
}
printf("n Press any key to close the Application");
getch();
}
Program Algorithm / Explanation :
 #include<stdio.h> header file is included because, the C in-built
statement printf we used in this program comes under stdio.h
header files.
 #include<conio.h> is used because the C in-built function getch()
comes under conio.h header files.
 main () function is the place where C program execution begins.
 Two integer variable a and b are declared.
 User Input values are received for both a and b using scanf.
 An if else conditional statement is used to check whether a is greater
than b.
 If a is greater than b, the message “a got greater value” is displayed
using printf.
 If b is greater the message “b got greater value” is displayed.
Output :
else if in C :
Syntax :
if(condition)
{
Valid C Statements;
}
else if(condition 1)
{
Valid C Statements;
}
-
-
else if(condition n)
{
Valid C Statements;
}
else
{
Valid C Statements;
}
In else if, if the condition is true the statements between if and else if is
executed. If it is false the condition in else if is checked and if it is true it
will be executed. If none of the condition is true the statement under else
is executed.
else_if.c
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Enter a value for a:");
scanf("%d",&a);
printf("nEnter a value for b:");
scanf("%d",&b);
if(a>b)
{
printf("n a is greater than b");
}
else if(b>a)
{
printf("n b is greater than a");
}
else
{
printf("n Both a and b are equal");
}
printf("n Press any key to close the application");
getch();
}
Program Algorithm / Explanation :
 #include<stdio.h> header file is included because, the C in-built
statement printf we used in this program comes under stdio.h
header files.
 #include<conio.h> is used because the C in-built function getch
comes under conio.h header files.
 main () function is the place where C program execution begins.
 Two variables a & b of type int are declared.
 Values for a & b are received from user through scanf.
 if condition is used to check whether a is greater than b. if(a>b)
 If the condition is true the statement between if and else if is
executed.
 If the condition is not satisfied else if() condition is checked. Else
if(b>a)
 If b is greater than a the statement between else if and else is
executed.
 If both the conditions are false the statement after else is executed.
Output :
4) Switch statement in C :
Syntax :
switch(variable)
{
case 1:
Valid C Statements;
break;
-
-
case n:
Valid C Statements;
break;
default:
Valid C Statements;
break;
}
Switch statements can also be called matching case statements. If
matches the value in variable (switch(variable)) with any of the case
inside, the statements under the case that matches will be executed.
If none of the case is matched the statement under default will be
executed.
switch.c
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf("Enter a no between 1 and 5 : ");
scanf("%d",&a);
switch(a)
{
case 1:
printf("You choosed One");
break;
case 2:
printf("You choosed Two");
break;
case 3:
printf("You choosed Three");
break;
case 4:
printf("You choosed Four");
break;
case 5:
printf("You choosed Five");
break;
default :
printf("Wrong Choice. Enter a no between 1 and 5");
break;
}
getch();
}
Program Algorithm / Explanation :
 #include<stdio.h> header file is included because, the C in-built
statement printf we used in this program comes under stdio.h
header files.
 #include<conio.h> is used because the C in-built function getch()
comes under conio.h header files.
 main () function is the place where C program execution begins.
 Variable a of type int is declared.
 scanf is used to get the value from user for variable a.
 variable a is included for switch case. switch(a).
 If the user Input was 1 the statement inside case 1: will be executed.
Likewise for the rest of the case’s until 5.
 But if the user Input is other than 1 to 5 the statement under default
: will be executed.
Output :
5) goto statement in C :
goto is a unconditional jump statement.
Syntax :
goto label;
so we have to use the goto carefully inside a conditional statement.
goto.c
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Enter 2 nos A and B one by one : ");
scanf("%d%d",&a,&b);
if(a>b)
{
goto first;
}
else
{
goto second;
}
first:
printf("n A is greater..");
goto g;
second:
printf("n B is greater..");
g:
getch();
}
Program Algorithm / Explanation :
 #include<stdio.h> header file is included because, the C in-built
statement printf we used in this program comes under stdio.h header
files.
 #include<conio.h> is used because the C in-built function getch()
comes under conio.h header files.
 main () function is the place where C program execution begins.
 Two variables a & b of type int are declared.
 User Inputs are received for a & b using scanf.
 if condition is used to check whether a is greater than b. if(a>b).
 if it is true goto statement is used to jump to the label first : and the
statement under first : is executed and then again jump is performed
to get to the end of the program. goto g:
 if the condition is false a statement is used to jump to label second.
goto second : and the statement under second : is executed.
Output :
Looping statements are used to execute a statement or a group of
statements repeatedly until the condition is true.
Here, we will look into the following looping statements.
for loop
while loop
do while
1) for Loop in C :
Syntax :
for(variable_initialisation;condition;increment/decrement)
{
Valid C Statements;
}
In for loop first the variable is initialised and checks for the condition.
If the condition is true. The statement inside for loop will be
executed.
On the next cycle the initialised variable will be either incremented or
decremented and again checks for the condition. If the condition is
true, again the statement inside will be executed. This process will
continue until the condition is true. Once the condition fails the
control jumps out of for loop.
for.c
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a=0;
for(i=1;i<=10;i++)
{
printf("n %d",i);
}
getch();
}
Program Algorithm / Explanation :
 #include<stdio.h> header file is included because, the C in-built
statement printf we used in this program comes under stdio.h header
files.
 #include<conio.h> is used because the C in-built function getch()
comes under conio.h header files.
 main() function is the place where C program execution begins.
 A Variable of int i is declared.
 A for loop is started and i is initialised as 1 and condition is if i less
than or equal to 10. And i will be incremented.
 On the first cycle, i=1 which means 1 is less than 10. so the
condition is satisfied.
 So the value of i is displayed using printf inside the for loop.
 On the next cycle i is incremented. So now i becomes 2.
 It again checks for the condition i<=10 means 2<=10. the condition
is still satisfied, so i will displayed as 2.
 The cycle continues until i is 10. When i becomes 11 the condition
i<=10 fails. Eg : 11<=10 is false. So the control jumps out of for
loop.
Output :
2) while Loop in C :
Syntax :
while(condition)
{
Valid C Statements;
}
While loop executes statements based on certain conditions. But unlike
for loop, while loop don’t have variable initialisation or
increment/decrement.
while.c
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1;
while(a<11)
{
printf("n %d",a);
a++;
}
getch();
}
Program Algorithm / Explanation :
 #include<stdio.h> header file is included because, the C in-built
statement printf we used in this program comes under stdio.h header
files.
 #include<conio.h> is used because the C in-built function getch()
comes under conio.h header files.
 main() function is the place where C program execution begins.
 Variable a of type int is declared and initialised with value of 1. int
a = 1;
 A while loop is started with condition a<11. a is initialised as 1 so
1<11 is true and hence the value of a is printed as 1 using printf.
 Then a is incremented inside while loop a++; so a becomes 2.
 while loop again checks for condition a<11, a is 2 so 2<11 is true
and once again the value of a is printed.
 The cycle continues until a is 10. When a becomes 11 condition
fails. a<11 means 11<11 is false.
 Hence control jumps out of while loop.
Output :
3) do while Loop in C :
Syntax :
do
{
Valid C Statements;
}while(condition);
do while performs exactly like while except one condition. On the first
cycle, condition is not checked as the condition is placed below.
dowhile.c
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1;
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1;
do
{
printf("n %d",a);
a++;
}while(a<5);
getch();
}
Program Algorithm / Explanation :
 #include<stdio.h> header file is included because, the C in-built
statement printf we used in this program comes under stdio.h header
files.
 #include<conio.h> is used because the C in-built function getch()
comes under conio.h header files.
 main() function is the place where C program execution begins.
 Variable a of type int is declared and initialised as 1.
 Now do while loop is used to print a.
 On the 1st cycle no condition is checked and the value of a is
printed.
 The loop continues until a is 4. when a becomes 5 the condition
(a<5) (i.e) (5<5) is false so the control jumps out of do while loop.
Output :
dowhile1.c
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1;
do
{
printf("n %d",a);
a++;
}while(a<1);
getch();
}
Program Algorithm / Explanation :
 #include<stdio.h> header file is included because, the C in-built
statement printf we used in this program comes under stdio.h
header files.
 #include<conio.h> is used because the C in-built function getch()
comes under conio.h header files.
 main() function is the place where C program execution begins.
 Variable a of type int is declared and initialised as 1.
 Now do while loop is used to print a.
On the 1st cycle no condition is checked and the value of a is printed.
Then it checks for condition a<1. a is already 1 means 1<1 is false and
so the control jumps out of do while loop.
Output :

More Related Content

What's hot

Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Jayanshu Gundaniya
 
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.Niraj Bharambe
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in CMuthuganesh S
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programmingHarshita Yadav
 
Programming Fundamentals lecture 2
Programming Fundamentals lecture 2Programming Fundamentals lecture 2
Programming Fundamentals lecture 2REHAN IJAZ
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & LanguagesGaditek
 
Union in c language
Union  in c languageUnion  in c language
Union in c languagetanmaymodi4
 
Evolution of Programming Languages
Evolution of Programming LanguagesEvolution of Programming Languages
Evolution of Programming LanguagesSayanee Basu
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsTech
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CSowmya Jyothi
 

What's hot (20)

Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
C tokens
C tokensC tokens
C tokens
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Structure in C
Structure in CStructure in C
Structure in C
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in C
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
 
Programming Fundamentals lecture 2
Programming Fundamentals lecture 2Programming Fundamentals lecture 2
Programming Fundamentals lecture 2
 
C Token’s
C Token’sC Token’s
C Token’s
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 
Evolution of Programming Languages
Evolution of Programming LanguagesEvolution of Programming Languages
Evolution of Programming Languages
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Applets in java
Applets in javaApplets in java
Applets in java
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming Concepts
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 

Similar to C PROGRAMMING LANGUAGE

Similar to C PROGRAMMING LANGUAGE (20)

Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
 
variablesfinal-170820055428 data type results
variablesfinal-170820055428 data type resultsvariablesfinal-170820055428 data type results
variablesfinal-170820055428 data type results
 
Concept Of C++ Data Types
Concept Of C++ Data TypesConcept Of C++ Data Types
Concept Of C++ Data Types
 
Concept of c data types
Concept of c data typesConcept of c data types
Concept of c data types
 
cassignmentii-170424105623.pdf
cassignmentii-170424105623.pdfcassignmentii-170424105623.pdf
cassignmentii-170424105623.pdf
 
C++ data types
C++ data typesC++ data types
C++ data types
 
Data Handling
Data HandlingData Handling
Data Handling
 
Chap 2(const var-datatype)
Chap 2(const var-datatype)Chap 2(const var-datatype)
Chap 2(const var-datatype)
 
Datatypes in c
Datatypes in cDatatypes in c
Datatypes in c
 
Data types
Data typesData types
Data types
 
Python - variable types
Python - variable typesPython - variable types
Python - variable types
 
Data types in C
Data types in CData types in C
Data types in C
 
C tokens
C tokensC tokens
C tokens
 
Data Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingData Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# Programming
 
Chapter 2.datatypes and operators
Chapter 2.datatypes and operatorsChapter 2.datatypes and operators
Chapter 2.datatypes and operators
 
Data types
Data typesData types
Data types
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 
Data types
Data typesData types
Data types
 
Variable and constants in Vb.NET
Variable and constants in Vb.NETVariable and constants in Vb.NET
Variable and constants in Vb.NET
 
Data Types | CS8251- Programming in c | Learn Hub
Data Types | CS8251- Programming in c | Learn HubData Types | CS8251- Programming in c | Learn Hub
Data Types | CS8251- Programming in c | Learn Hub
 

C PROGRAMMING LANGUAGE

  • 1.
  • 2.
  • 3. Data Types can also be classified as shown in the image below – Primitive, Derived and User Defined.
  • 4. 1.Primitive Data Types Fundamental data types are further categorized into 3 types. Let us discuss each of this type briefly. Int (Integer) Integer data type is used to store numeric values without any decimal point e.g. 7, -101, 107, etc. A variable declared as 'int' must contain whole numbers e.g. age is always in number. Syntax: int variable name; E.g. int roll, marks, age; Float Float data type is used to store numeric values with decimal point. In other words, float data type is used to store real values, e.g. 3.14, 7.67 etc. A variable declared as float must contain decimal values e.g. percentage, price, pi, area etc. may contain real values.
  • 5. Syntax: Float variable name; E.g. float per, area; Char (Character) Char (Character) data type is used to store single character, within single quotes e.g. 'a', 'z','e' etc. A variable declared as 'char' can store only single character e.g. Yes or No Choice requires only 'y' or 'n' as an answer. Syntax: Char variable name; E.g. Char chi='a', cha;
  • 6. Void Void data type is used to represent an empty value. It is used as a return type if a function does not return any value. Type modifier Type modifier is used to change the meaning of basic or fundamental data types so that they can be used in different situations. Various type modifiers are- signed, unsigned, short and long. C language offers 3 different 'int' type modifiers - short, int, and long that represents three different integer sizes. C language also offers 3 different floating point type modifiers - float, double, and long double. Two special 'char' type modifiers are- signed, unsigned. C language offers 3 different 'int' type modifiers - short, int, and long that represents three different integer sizes.
  • 7. C language also offers 3 different floating point type modifiers - float, double, and long double. Two special 'char' type modifiers are- signed, unsigned. Type Size (in bytes) Range Int (signed/ short) 2 -32768 to 32767 Int (unsigned) 2 0 to 65535 Long (signed) 4 -2,147,483,648 to 2,147,483,648 Long (unsigned) 4 0 to 4,294,467,295 Float 4 3.4*10-34 to 3.4*10-34 Double (long float) 8 1.7*10-308 to 1.7*10-308 -1 Long double 10 3.4*10-4932 to 3.4*10-4932 -1 Char 1 -128 to 127 Signed char 1 0 to 255 Unsigned char 1 -128 to 127
  • 8. 2. Derived Data Types Data types that are derived from fundamental data types are called derived data types. Derived data types don't create a new data type; instead, they add some functionality to the basic data types. Two derived data type are - Array & Pointer. Array An array is a collection of variables of same type i.e. collection of homogeneous data referred by a common name. In memory, array elements are stored in a continuous location. Syntax: []; E.g. int a[10]; Char chi [20];
  • 9. According to the rules of C language, 1st element of array is stored at location a[0] , 2nd at a[1] & so on. Pointer A pointer is a special variable that holds a memory address (location in memory) of another variable. Syntax: * Data type is the type whose address we want to store in the pointer variable. * is a pointer variable. 'var name' is the name where the variable is to be stored. E.g. if we want to store the address of an 'int' data type variable into a pointer variable, it is done in the following manner:
  • 10. int a,*b; b=&a; In the above case, variable 'b' stores the address of variable 'a'. 3. User Defined Data Type User defined data type is used to create new data types. The new data types formed are fundamental data types. Different user defined data types are: struct, union, enum, typedef. Struct A struct is a user defined data type that stores multiple values of same or different data types under a single name. In memory, the entire structure variable is stored in sequence.
  • 11. Syntax: Struct < structure name> { Var1; Var2; ----- ----- }; Structure name is the name of structure that we have to give e.g. if we want to store details of a student as- name, roll, marks then in structure it will be declared in the following manner:
  • 12. Struct student { Char name [20]; Int roll, Float marks; }; Now, its variable is declared as: struct E.g. Struct student s1; And its variable is accessed using dot (.) operator. S1.roll, s1.name, s1.marks Union A union is a user defined data type that stores multiple values of same or different data types under a single name. In memory, union variables are stored in a common memory location.
  • 13. Syntax: Union < tag name> { Var1; Var2; ----- ---- }; Tag name is the name of union, e.g. if we want to store details of a student as- name, roll, marks then it will be done in the following manner :
  • 14. Union student { Char name [20]; Int roll, Float marks; }; Now, the variable is declared as follows: struct e.g. Union student s1; And its variable is accessed by using dot (.) operator. S1.roll, s1.name, s1.marks The only difference between Union and Struct is allocation of memory. 'Union' is assigned a memory size equal to the biggest data type used in union declaration whereas 'struct' will occupy the size equal to the sum of all variables sizes.
  • 15. Enum: An enumeration is a data type similar to a struct or a union. Its members are constants that are written as variables, though they have signed integer values. These constant represent values that can be assigned to corresponding enumeration variables. Syntax: Enum {value1, value2, _ _ _ _ _ _, value n}; Enum is the required keyword; Tag is the name that identifies enumerations having this composition; Value1, value2, - - - - - - , value n represents the individual identifiers that may be assigned to a variable of this type. E.g. : Enum colors {black, blue, cyan, green, red, yellow}; Color foreground, background;
  • 16. First line defines enum and second one defines enum variables. Thus, variables foreground and background can be assigned any one of constant black, blue, - - - - ,yellow. In declaration black is assigned 0 value, and blue 1, - - - -, and yellow 5. We can also assign the value accordingly as we want to assign, like: Enum colors {black, blue, cyan=4, green, red, yellow} Here black=0, blue=1, cyan=4, green=5, red=6, yellow=7 Typedef: The 'typedef' allows the user to define new data-types that are equivalent to existing data types. Once a user defined data type has been established, then new variables, array, structures, etc. can be declared in terms of this new data type.
  • 17. A new data type is defined as: Typedef type new-type; Type refers to an existing data type, New-type refers to the new user-defined data type. E.g. : typedef int number Now we can declare integer variables as: number roll, age, marks; It is equivalent to: int roll, age, marks;
  • 18. A variable can be defined in many ways. At the basic level, a variable can be defined as a memory location declared to store any kind of data (which may change many times during program execution). It can also be defined as any quantity/entity which may vary during program execution. To identify a variable (the declared memory location), it can be assigned a name – known as variable name. The name given to a variable is known as an identifier. An identifier can be a combination of alphabets , digits and underscore. There are certain set of rules which must be observed while naming a variable. Rules for naming a variable:-  A variable name (identifier) can be any combination of alphabets, digits and underscore.
  • 19.  First character should be a letter (alphabet).  Length of variable name can range from 1 to 8. (Note: Different compilers may allow different ranges, say upto 31. But it is a good practice to keep the variable name short.)  A space in between is not allowed. Ex: A variable name can not be declared as var name  Underscore can be used to concatenate name combinations. Ex: var_name , var_123 are valid identifiers.  No commas or other special characters (other than underscore _ ) are allowed in a variable name.  C is a case sensitive language – which means a variable name declared as flag is not same as FLAG. They both will be treated as different variables. There are certain reserved words in C language, known as keywords.
  • 20. Words similar to a keyword can not be used as a variable name. Ex:- In the previous article on data types, we saw int, char, float etc. These are actually keywords. So you can’t declare a variable with names int, char or float. Variable Declaration A variable must be declared first before we can use it in a program for manipulations. A variable is declared with its storage class, data type and identifier. The format is shown below:- Storage-class Data-Type Variable-name;
  • 21. Storage class is some thing we will learn in coming chapters. Let me brief it. There are 4 storage classes namely Automatic, Static, External and Register storage classes. Each of them has its own meaning and usage. Storage class is used to attribute certain features to a variable like its scope (local or global), where to store the variable (in memory or in register), the life time of a variable etc. It is not necessary to specify a storage class while declaring a variable. By default all variable declarations (without any storage class specified) will be assigned to “automatic storage class”. We will discuss more about storage class in another chapter. So our variable declaration would be like:- Data-Type Variable-name;
  • 22. Examples:- int a; int count; char name; Two or more variables of the same data type can be declared in a single line, separating each variable names with a comma and ending the line with a semicolon. Examples:- int n,count,flag,i,j; char name,address,nick_nm; Initial values can be assigned to variables while declaring it. Examples:- int a,num,count=0,flag=10,mark=100;
  • 23. char name=Robert,sur_nam=Nero,mid_nam=de; Let us examine what happens when we declare a variable. int j=10; Here we have declared a variable of data type integer with name as j and initial value as 10. This declaration tells the C compiler to:- Reserve space in memory to hold the integer value. Assign the name ‘j’ to that reserved memory space. Store the value 10 in this memory location.
  • 24. The symbols which are used to perform logical and mathematical operations are called C operators. These C operators join individual constants and variables to form expressions. Operators, functions, constants and variables are combined together to form expressions. Example : A + B * 5 where, +, * - operators A, B - variables 5 – constant A + B * 5 - expression
  • 25. Types of C operators: C language offers many types of operators. They are, Arithmetic operators Assignment operators Relational operators Logical operators Bit wise operators Conditional operators (ternary operators) Increment/decrement operators Special operators
  • 26. 1. Arithmetic Operators: These operators are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus. S.no Operator Operation 1 + Addition 2 - Subtraction 3 * Multiplication 4 / Division 5 % Modulus
  • 27. Example program for C arithmetic operators: # include <stdio.h> int main() { int a=40,b=20, add,sub,mul,div,mod; add = a+b; sub = a-b; mul = a*b; div = a/b; mod = a%b; printf("Addition of a, b is : %dn", add); printf("Subtraction of a, b is : %dn", sub); printf("Multiplication of a, b is : %dn", mul); printf("Division of a, b is : %dn", div);
  • 28. printf("Modulus of a, b is : %dn", mod); } Output: Addition of a, b is : 60 Subtraction of a, b is : 20 Multiplication of a, b is : 800 Division of a, b is : 2 Modulus of a, b is : 0 2. Assignment operators: The values for the variables are assigned using assignment operators. For example, if the value 10 is to be assigned for the variable sum, it can be done like below. sum = 10;
  • 29. Below are some of other assignment operators: Operator Example Explanation Simple operator = sum = 10 Value 10 is assigned to variable sum Compound assignment operator += sum+=10 This is same as sum = sum+10 -= sum – = 10 This is same as sum = sum – 10 *= sum*=10 This is same as sum = sum*10 /+ sum/=10 This is same as sum = sum/10 %= sum%=10 This is same as sum = sum%10 &= sum&=10 This is same as sum = sum&10 ^= sum^=10 This is same as sum = sum^10
  • 30. Example program for assignment operators: # include <stdio.h> int main() { int Total=0,i; for(i=0;i<10;i++) { Total+=i; // This is same as Total = Toatal+i } printf("Total = %d", Total); } Output: Total = 45
  • 31. 3. Relational operators: These operators are used to find the relation between two variables. That is, used to compare the value of two variables. Operator Example > x > y < x < y >= x >= y <= x <= y == x == y != x != y
  • 32. Example program for relational operators: #include <stdio.h> int main() { int m=40,n=20; if (m == n) { printf("m and n are equal"); } else { printf("m and n are not equal"); } }
  • 33. Output: m and n are not equal 4. Logical operators: These operators are used to perform logical operations on the given two variables. Operator Example && x && y || x || y ! x (x || y), !(x && y)
  • 34. Example program for logical operators: #include <stdio.h> int main() { int m=40,n=20; if (m>n && m !=0) { printf("m is greater than n and not equal to 0"); } } Output: m is greater than n and not equal to 0
  • 35. 5. Bit wise operators: These operators are used to perform bit operations on given two variables. Truth table for bitwise operation: x y x/y x & y x ^ y 0 0 0 0 0 0 1 1 0 1 1 0 1 0 1 1 1 1 1 0
  • 36. Bitwise operators: Operator Explanation & Bitwise AND | Bitwise OR ~ Bitwise NOT ^ XOR << Left Shift >> Right Shift
  • 37. Example program for bitwise operators: #include <stdio.h> int main() { int m=40,n=20,AND_opr,OR_opr,XOR_opr ; AND_opr = (m&n); OR_opr = (m|n); XOR_opr = (m^n); printf("AND_opr value = %dn",AND_opr ); printf("XOR_opr value = %dn",XOR_opr ); printf("OR_opr value = %dn",OR_opr ); }
  • 38. Output: AND_opr value = 0 XOR_opr value = 60 OR_opr value = 60 6. Conditional operators: Conditional operators return one value if condition is true and returns other value is condition is false. This operator is also called as ternary operator. Syntax : (Condition? true_value: false_value); Example : (A > 100 ? 0 : 1); Here, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if else conditional statements.
  • 39. Example program for conditional/ternary operators: #include <stdio.h> int main() { int x=1, y ; y = ( x ==1 ? 2 : 0 ) ; printf("x value is %dn", x); printf("y value is %d", y); } Output: x value is 1 y value is 2
  • 40. 7. Increment and decrement operators: These operators are used to either increase or decrease the value of the variable by one. Syntax : ++var_name; (or) var_name++; (or) – - var_name; (or) var_name- -; Example : ++i, i++, - -i, i- - }
  • 41. Example program for increment operators: #include <stdio.h> int main() { int i=1; while(i<10) { printf("%dn",i); i++; } }
  • 43. #include <stdio.h> int main() { int i=20; while(i>10) { printf("%dn",i); i--; } }
  • 44. Output: 20 19 18 17 16 15 14 13 12 11 8. Special Operators: Below are some of special operators that the C language offers.
  • 45. S.no Operators Description 1 & This is used to get the address of the variable. Example: &a will give address of a. 2 * This is used as pointer to a variable. Example: * a where, * is pointer to the variable a. 3 Size of () This gives the size of the variable. Example: size of (char) will give us 1. 4 ternary This is ternary operator and act exactly as if … else statement. 5 Type cast Type cast is the concept of modifying variable from one data type to other.
  • 46. Conditional statements are used to execute a statement or a group of statement based on certain conditions. We will look into following conditional statements. if if else else if switch Goto 1) if conditional statement in C : Syntax for if statement in C : if(condition) { Valid C Statements; }
  • 47. If the condition is true the statements inside the parenthesis { }, will be executed, else the control will be transferred to the next statement after if. #include<stdio.h> #include<conio.h> void main() { int a,b; a=10; b=5; if(a>b) { printf("a is greater"); } getch(); }
  • 48. Program Algorithm / Explanation :  #include<stdio.h> header file is included because, the C in-built statement printf we used in this program comes under stdio.h header files.  #include<conio.h> is used because the C in-built function getch() comes under conio.h header files.  main () function is the place where C program execution begins.  Two variables a&b of type int is declared.  Variable a is assigned a value of 10 and variable b with 5.  A if condition is used to check whether a is greater than b. if(a>b)  As a is greater than b the printf inside the if { } is executed with a message “a is greater”. Output :
  • 49. 2) if else in C : Syntax for if : if(condition) { Valid C Statements; } else { Valid C Statements; } In if else if the condition is true the statements between if and else is executed. If it is false the statement after else is executed.
  • 50. Sample Program : if_else.c #include<stdio.h> #include<conio.h> void main() { int a,b; printf("Enter a value for a:"); scanf("%d",&a); printf("nEnter a value for b:"); scanf("%d",&b); if(a>b) { printf("n a got greater value"); }
  • 51. else { printf("n b got greater value"); } printf("n Press any key to close the Application"); getch(); } Program Algorithm / Explanation :  #include<stdio.h> header file is included because, the C in-built statement printf we used in this program comes under stdio.h header files.  #include<conio.h> is used because the C in-built function getch() comes under conio.h header files.  main () function is the place where C program execution begins.  Two integer variable a and b are declared.
  • 52.  User Input values are received for both a and b using scanf.  An if else conditional statement is used to check whether a is greater than b.  If a is greater than b, the message “a got greater value” is displayed using printf.  If b is greater the message “b got greater value” is displayed. Output :
  • 53. else if in C : Syntax : if(condition) { Valid C Statements; } else if(condition 1) { Valid C Statements; } - -
  • 54. else if(condition n) { Valid C Statements; } else { Valid C Statements; } In else if, if the condition is true the statements between if and else if is executed. If it is false the condition in else if is checked and if it is true it will be executed. If none of the condition is true the statement under else is executed. else_if.c #include<stdio.h> #include<conio.h>
  • 55. void main() { int a,b; printf("Enter a value for a:"); scanf("%d",&a); printf("nEnter a value for b:"); scanf("%d",&b); if(a>b) { printf("n a is greater than b"); } else if(b>a) { printf("n b is greater than a"); }
  • 56. else { printf("n Both a and b are equal"); } printf("n Press any key to close the application"); getch(); } Program Algorithm / Explanation :  #include<stdio.h> header file is included because, the C in-built statement printf we used in this program comes under stdio.h header files.  #include<conio.h> is used because the C in-built function getch comes under conio.h header files.  main () function is the place where C program execution begins.
  • 57.  Two variables a & b of type int are declared.  Values for a & b are received from user through scanf.  if condition is used to check whether a is greater than b. if(a>b)  If the condition is true the statement between if and else if is executed.  If the condition is not satisfied else if() condition is checked. Else if(b>a)  If b is greater than a the statement between else if and else is executed.  If both the conditions are false the statement after else is executed.
  • 59. 4) Switch statement in C : Syntax : switch(variable) { case 1: Valid C Statements; break; - - case n: Valid C Statements; break; default: Valid C Statements; break; }
  • 60. Switch statements can also be called matching case statements. If matches the value in variable (switch(variable)) with any of the case inside, the statements under the case that matches will be executed. If none of the case is matched the statement under default will be executed. switch.c #include<stdio.h> #include<conio.h> void main() { int a; printf("Enter a no between 1 and 5 : "); scanf("%d",&a);
  • 61. switch(a) { case 1: printf("You choosed One"); break; case 2: printf("You choosed Two"); break; case 3: printf("You choosed Three"); break; case 4: printf("You choosed Four"); break;
  • 62. case 5: printf("You choosed Five"); break; default : printf("Wrong Choice. Enter a no between 1 and 5"); break; } getch(); } Program Algorithm / Explanation :  #include<stdio.h> header file is included because, the C in-built statement printf we used in this program comes under stdio.h header files.
  • 63.  #include<conio.h> is used because the C in-built function getch() comes under conio.h header files.  main () function is the place where C program execution begins.  Variable a of type int is declared.  scanf is used to get the value from user for variable a.  variable a is included for switch case. switch(a).  If the user Input was 1 the statement inside case 1: will be executed. Likewise for the rest of the case’s until 5.  But if the user Input is other than 1 to 5 the statement under default : will be executed. Output :
  • 64. 5) goto statement in C : goto is a unconditional jump statement. Syntax : goto label; so we have to use the goto carefully inside a conditional statement. goto.c #include<stdio.h> #include<conio.h> void main() { int a,b; printf("Enter 2 nos A and B one by one : "); scanf("%d%d",&a,&b);
  • 65. if(a>b) { goto first; } else { goto second; } first: printf("n A is greater.."); goto g; second: printf("n B is greater.."); g: getch(); }
  • 66. Program Algorithm / Explanation :  #include<stdio.h> header file is included because, the C in-built statement printf we used in this program comes under stdio.h header files.  #include<conio.h> is used because the C in-built function getch() comes under conio.h header files.  main () function is the place where C program execution begins.  Two variables a & b of type int are declared.  User Inputs are received for a & b using scanf.  if condition is used to check whether a is greater than b. if(a>b).  if it is true goto statement is used to jump to the label first : and the statement under first : is executed and then again jump is performed to get to the end of the program. goto g:  if the condition is false a statement is used to jump to label second. goto second : and the statement under second : is executed.
  • 68. Looping statements are used to execute a statement or a group of statements repeatedly until the condition is true. Here, we will look into the following looping statements. for loop while loop do while 1) for Loop in C : Syntax : for(variable_initialisation;condition;increment/decrement) { Valid C Statements; } In for loop first the variable is initialised and checks for the condition. If the condition is true. The statement inside for loop will be executed.
  • 69. On the next cycle the initialised variable will be either incremented or decremented and again checks for the condition. If the condition is true, again the statement inside will be executed. This process will continue until the condition is true. Once the condition fails the control jumps out of for loop. for.c #include<stdio.h> #include<conio.h> void main() { int i,a=0; for(i=1;i<=10;i++) { printf("n %d",i); } getch(); }
  • 70. Program Algorithm / Explanation :  #include<stdio.h> header file is included because, the C in-built statement printf we used in this program comes under stdio.h header files.  #include<conio.h> is used because the C in-built function getch() comes under conio.h header files.  main() function is the place where C program execution begins.  A Variable of int i is declared.  A for loop is started and i is initialised as 1 and condition is if i less than or equal to 10. And i will be incremented.  On the first cycle, i=1 which means 1 is less than 10. so the condition is satisfied.  So the value of i is displayed using printf inside the for loop.  On the next cycle i is incremented. So now i becomes 2.
  • 71.  It again checks for the condition i<=10 means 2<=10. the condition is still satisfied, so i will displayed as 2.  The cycle continues until i is 10. When i becomes 11 the condition i<=10 fails. Eg : 11<=10 is false. So the control jumps out of for loop. Output :
  • 72. 2) while Loop in C : Syntax : while(condition) { Valid C Statements; } While loop executes statements based on certain conditions. But unlike for loop, while loop don’t have variable initialisation or increment/decrement. while.c #include<stdio.h> #include<conio.h>
  • 73. void main() { int a=1; while(a<11) { printf("n %d",a); a++; } getch(); } Program Algorithm / Explanation :  #include<stdio.h> header file is included because, the C in-built statement printf we used in this program comes under stdio.h header files.
  • 74.  #include<conio.h> is used because the C in-built function getch() comes under conio.h header files.  main() function is the place where C program execution begins.  Variable a of type int is declared and initialised with value of 1. int a = 1;  A while loop is started with condition a<11. a is initialised as 1 so 1<11 is true and hence the value of a is printed as 1 using printf.  Then a is incremented inside while loop a++; so a becomes 2.  while loop again checks for condition a<11, a is 2 so 2<11 is true and once again the value of a is printed.  The cycle continues until a is 10. When a becomes 11 condition fails. a<11 means 11<11 is false.  Hence control jumps out of while loop.
  • 76. 3) do while Loop in C : Syntax : do { Valid C Statements; }while(condition); do while performs exactly like while except one condition. On the first cycle, condition is not checked as the condition is placed below. dowhile.c #include<stdio.h> #include<conio.h> void main() { int a=1;
  • 77. #include<stdio.h> #include<conio.h> void main() { int a=1; do { printf("n %d",a); a++; }while(a<5); getch(); } Program Algorithm / Explanation :  #include<stdio.h> header file is included because, the C in-built statement printf we used in this program comes under stdio.h header files.
  • 78.  #include<conio.h> is used because the C in-built function getch() comes under conio.h header files.  main() function is the place where C program execution begins.  Variable a of type int is declared and initialised as 1.  Now do while loop is used to print a.  On the 1st cycle no condition is checked and the value of a is printed.  The loop continues until a is 4. when a becomes 5 the condition (a<5) (i.e) (5<5) is false so the control jumps out of do while loop. Output :
  • 80. Program Algorithm / Explanation :  #include<stdio.h> header file is included because, the C in-built statement printf we used in this program comes under stdio.h header files.  #include<conio.h> is used because the C in-built function getch() comes under conio.h header files.  main() function is the place where C program execution begins.  Variable a of type int is declared and initialised as 1.  Now do while loop is used to print a. On the 1st cycle no condition is checked and the value of a is printed. Then it checks for condition a<1. a is already 1 means 1<1 is false and so the control jumps out of do while loop.