Professional Documents
Culture Documents
Characteristics of C
The following are some characteristics of C:
Small size
Loose typing
Structured language
Operators
A operator specifies an operation to be performed between two or more operands. An
operand is a data item on which an operator acts. C includes a number of operators as
follows1.
2.
3.
4.
5.
6.
7.
8.
9.
Arithmetic Operators
Assignment Operators
Increment and Decrement Operators
Relational Operators
Logical Operators
Conditional Operators
Bitwise Operators
Comma Operators
Sizeof Operators
1. Arithmetic Operators
Arithmetic operators are used for numeric calculations. They are two types:
(1) Unary arithmetic operators
(2) Binary arithmetic operators
(1) Unary arithmetic operator required only one operand. For example: +a, -b
(2) Binary arithmetic operator required two operands. There are five arithmetic
operators
Purpose
Operators
+
Addition
Subtraction
Multiplication
Division
2. Assignment operators
A value can be stored in a variable with the use of assignment operators.
The value of right hand operand is assigned to the left hand operand.
For example:
x=10
y=x
s=x+ y
a=a+2
(i.e. a+=2)
b=b-4
(i.e. b-=4)
c=c/5
(i.e. c/=5)
d=d*6
(i.e. d*=6)
e=e%7
(i.e. e%=7)
Less than
>
Greater than
<=
>=
==
Equal to
Not equal to
!=
AND
||
OR
NOT
Logical NOT is a unary operator while the other two are binary operators.
AND Operators
Expression1
Expression2
Result
False
False
False
True
False
False
False
True
False
True
True
True
OR Operators
Expression1
Expression2
Result
False
False
False
True
False
True
False
True
True
True
True
True
NOT operator
Expression
Result
False
True
True
False
6. Conditional Operators
Conditional operator is a ternary operator (? And :) which requires three
expressions as operands. This is written asTestExpression ? expression1 : expression2
Firstly the TestExpression is evaluated.
(1) If TestExpression is true, then expression1 is evaluated and it becomes the
value of the overall conditional expression.
(2) If TestExpression is False, then expression2 is evaluated and it becomes the
value of the overall conditional expression.
For example: Let a=5 and b=8
X= if(a<b) ? a : b
Here, condition a<b is true then x=a i.e. x=5
7. Bitwise Operators
C has the ability to support the manipulation of data at the bit level. Bitwise
operators are used for operations on individual bits. Bitwise operators operate
on integers only. They are as followsOperators
Meaning
Bitwise AND
P
&
Bitwise AND
Bitwise OR
Bitwise NOT
<<
Left shift
>>
Right sift
Bitwise XOR
Q
Result
0
1
For example:
A
0010 1001 0011 1011 (0x293B)
B
0001 1010 0010 1111 (0x1A2F)
A&B 0000 1000 0010 1011 (0x082B)
Bitwise OR
P
Result
For example:
A
0010 1001 0011 1011 (0x293B)
B
0001 1010 0010 1111 (0x1A2F)
A|B 0011 1011 0011 1111 (0x3B3F)
Bitwise NOT
P
Result
Bitwise XOR
P
Result
For example:
A
0010 1001 0011 1011 (0x293B)
B
0001 1010 0010 1111 (0x1A2F)
A^B 0011 0011 0001 0100 (0x3314)
Bitwise Left Shift
This operator is used for shifting the bit left. On shifting the bits left, an equal
number of bit positions on the right are vacated. These positions are filled in with 0 bits.
For example:
A= 0x1346 i.e.
Datatype
C supports different types of data. There are four fundamental datatypes in C
which are int, char, float and double. char is used to store any single character,
int is used to store integer value, float is used for storing single precision
floating point number and double is used for storing double precision floating
point number. C support type qualifiers with these basic types to get some more
types. There are two types of type qualifiers1. Size qualifiers
short, long
2. Sign qualifiers
signed, unsigned
When the qualifier unsigned is used the number is always positive and when
signed is used number may be positive or negative. If the sign qualifier is not
mentioned, then by default signed qualifier assumed. The range of values for
signed data types is less than that of unsigned type. This is because in signed
type, the leftmost bit is representing the sign.
Basic Datatype
Char
Int
Float
Double
Size (bytes)
1
1
2
2
1
4
float
double
long double
4
8
10
Range
-128 to 127
0 to 255
-32768 to 32767
0 to 65535
-128 to 127
-2147483648 to
2147483647
3.4E-38 to 3.4E+38
1.7E-308 to 1.7E+308
3.4E-4932 to 1.1E+4932
Control Statements
Control Structure as its shown by its name- That controls the structure.
Meaning- Program is flowing like a river & control structures are like a dam, which control
the flow of river. In other words we can say that, Control Structures are those which can
control or mould the flow of program.
If we connect control structure with our life, so the concept of control structure is:Our daily schedule is the biggest example. In our daily life we repeat some work, we take
some decision & we apply some conditions for that, if we want to make a program, we have
two options:Suppose a program is for monthly schedule:
A) One option is to repeat the some steps again & again.
B) Just instruct computer at once & it shows our monthly schedule.
So which one is better than which obviously, the second one isnt assume it is just for 30 to
31 times if uses need is greater than this, than think.
Control Structures are based on the concept of the second option.
Need of Control Structures: - Now the question arises, why control structures are
needed or what are those requirements, which are fulfilled by control structures.
As we explained before & the details are as follows:
A) Make C popular for all the time, as well as it was in its nascent stage.
B) Decrease complexity of program.
C) Make C software more compatible & more resistible in compare to the other software
of languages.
D) To control the memory consumption.
E) The most important quality of C is its compactness to maintain that control structures
are made.
F) Through some very unique function of C, in which Control structure are also be
counted, C can be said as The mother of many language
G) Control the wastage of time.
H) Control Structures are those which can mould the program according to uses need.
Uses of Control Structures:- There are many uses of control structure. Basically its uses
are to make C language, a friendly language for users. Now take a look on the uses of Control
Structure:
A) It is very useful & helpful to make decision.
B) It offers some following statement like If, if else if- helps in choosing option in the form of Boolean expression.
Switch- helps to select any option from many options.
For- iterative loop & required certain limit.
While & do while- iterative loop & required condition.
goto, break, return, continue- use to jump the statements.
Advantages:- From some uses & need of control structure is very much clear that it is a
very nice feature of C with many more advantages within it.
1) It makes program very easy to write as well as to understand.
2) It provides features like if, switch, goto, for etc.
3) Nesting of loops & statements.
4) It provides some jump statements which are explain later.
5) Decrease users effort.
6) Provide better results.
7) Increase efficiency.
8) Increase impact of program.
9) User friendly feature.
10) It makes program integrated.
11) The biggest advantages of control structure are that there is no barrier or condition
required for using loops & statement; mean not a particular structure is designed for
using control structure.
Example- Suppose firstly user while loop & than use for then if & than go to as inner
loops or statements. So the point is that, there is no limit or condition is applied for
using CS.
Repeative or Itrative
Jump
1.If
2.If else
3.Ifelseif
4.Switchcase
while
do while
for
goto
continue
break
return
Selective:- Those statements which are help to select any option from many choices(option)
is known as Selective Statements.
Suppose, you need a bag. So, what will you do? Obviously first of all, you will go to the
market & select a bag according to you or your moms choice from many options. This is
called Selection.
The selective statements are:If statement- If is a selective statement, it is use to check the condition & if condition is true
it print the result.
Syntax: if (condition)
{
Statements;
}
Eg: /* WAP to test & print even numbers */
# include< stdio-h>
# include< conio-h>
void main ( )
{
int a ;
clrser ( );
printf (Insert the number : );
scanf (% d, &a);
if (a%2= =0)
{
printf (%d, a);
}
getch ( );
}
If else & if else if statement: - By these options, many options can be written together
.Suppose the given condition is not true than go to the other conditions.
Syntax: - if (condition)
{
Statements;
}
else if (condition)
{
Statements;
}
else
{
Statements;
}
Eg: /* WAP to find out largest among 3 numbers */.
# include <stdio.h>
# include <conio.h>
void main( )
{
int a, b, c;
printf( insert three numbers:);
scanf(%d %d 5d%, & a, &b, &c);
if (a>b && a>c)
{
printf (a is largest);
}
else if (b>a && b>c)
{
printf (b is largest);
}
else
{
printf (c is largest);
}
getch( );
}
Switch case: - It is use to write many conditions within a pair of curly brasses.
These conditions are written with a keyboard & jump statement break. Break helps user to
come out of loop if condition is true.
Syntax: - switch (condition)
{
case 1:
Statement;
Break;
,
,
,
default:
Statement;
}
Eg: /* WAP to find out the day with the help of sequence number.*/
# include <stdio.h>
# include <conio.h>
void main ( )
{
int seq;
printf( insert the sequence);
scanf (%d, & seq);
switch (seq)
{
case 1:
printf (Sunday);
break;
case 2:
printf(Monday);
break;
,
,
,
default:
Print f (no day);
}
getch( );
}
Explanation:- In this example ,if we enter 3, then case 1 & 2 are false, than condition is
check for 3 which is right, so the result is Tuesday & than program is come out from the loop
. That is the work of switch case & break.
Difference between Nested if & Switch case
S.No
Nested if
1.
Many condition can be written
together
2.
3.
Switch case
many condition can be written
together within only a pair of
curly brasses.
Only one condition can be true.
It is a simple program.
Repetitive or Itrative: - These loops are those loops from which user can get results
according to his desire, but without repeating steps.
These are the two divisions of Iterative loops
1.For loopfor (initialization; condition; increment/decrement)
{
Statements;
}
Example:
/* Program to generate fibonacci series*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
long x, y, z;
clrscr();
x=0;
y=1;
printf("Enter the number of terms : ");
scanf("%d", &n);
printf("%ld", y);
for (i=1; i<n; i++)
{
z = x + y;
printf("%ld", z);
x = y;
y = z;
}
printf("\n");
getch();
}
Nested for:/* Program to print the following pattern:
*
***
*****
*******
*********
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n;
printf("Enter n : ");
scanf("%d", n);
for(i=1; i<=n; i++)
{
for(j=1; j<=n-i; j++)
{
printf(" ");
}
for(j=1; j<i; j++)
{
printf("*");
}
for(j=1; j<i; j++)
{
printf("*");
}
printf("\n");
}
getch();
}
2.While loop :while (condition)
{
Statements;
Increment/decrement;
}
Example:
/* Program to print the sum of digits of any number.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, sum = 0, r;
clrscr();
printf("Enter the number : ");
scanf("%d", &n);
while(n>0)
{
r = n % 10;
sum = sum + r;
n = n / 10;
}
printf("%d", sum);
getch();
}
3. do....while :do
{
Statements;
} while (condition);
Example:
/* Program to print the sum of numbers entered.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, sum = 0;
clrscr();
do
{
printf("Enter the number (0 to stop): ");
scanf("%d", &n);
sum = sum + n;
} while(n!=0);
getch();
}
Difference between while and do...while
1. In while loop, first the condition is checked and then the statements are
executed whereas in a do while loop, first the statements are executed and
then the condition is checked.
2. If initially the condition is false the while loop will not executed at all,
whereas the do while loop will always executed at least once.
3. In do while loop, a semicolon is placed after the condition, whereas in while
loop no semicolon is placed after the condition.
Nested Loop:
/* Program to print the armstrong numbers from 100 to 999.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int m, n, sum, c, d;
clrscr();
printf("Armstrong numbers are : \n ");
for(m=100; m<=999; m++)
{
n = m;
sum = 0;
while(n>0)
{
d = n % 10;
n = n / 10;
c = d * d * d;
sum = sum + c;
}
if(m == sum)
{
printf(" %d\n", m);
}
}
getch();
}
Break statement:
When break statement is encountered, loop is terminated and the control is
transferred to the statement immediately after the loop.
/* Program to find the number is prime or not.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, flag=1;
clrscr();
printf("Enter the number : ");
scanf("%d", &n);
for(i=2; i<= sqrt(n); i++)
{
if(n % i == 0)
{
printf("Not Prime.");
flag=0;
break;
}
}
if(flag == 1)
{
printf("Prime Number.");
}
getch();
}
Continue statement:
The continue statement is used when we want to go to the next iteration of the
loop after skipping some statements of the loop.
Example:
/* Program to find the sum and average of 10 positive integers.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i=1, sum=0;
float avg;
clrscr();
printf("Enter 10 positive numbers : ");
while (i<=10)
{
scanf("%d", &n);
if(n<0)
{
printf("Enter only positive numbers \n");
continue;
}
sum = sum + n;
i++;
}
avg=sum/10.0;
printf("Sum = %d and Average = %d", sum, avg);
getch();
}
Goto statement:
This is an unconditional control statement that transfers the flow of control to
another part of the program.
Syntax:
goto label:
...............;
.................;
label:
....................;
......................;
Example:
/* Program to print whether the number is even or odd.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter the number : ");
scanf("%d", &n);
if(n%2==0)
goto even;
else
goto odd;
even :
printf("Number is EVEN.");
goto end;
odd :
printf("Number is ODD.");
goto end;
end :
printf("\n");
getch();
}