Chapter 2 Primitive Data Types




C Language is rich in its data types. Storage representations and machine instructions to handle constants differ from machine to machine.
Size and Range of Data types on a 16-bit machine.

Type
Size (bits)
Range
char
8
-128 to 127
unsigned char
8
0 to 255
int
16
-32,768 to 32767
unsigned int
16
0 to 65535
short int
8
-128 to 127
unsigned short int
8
0 to 255
long int
32
-2,147,483,648 to 2,147,483, 647
unsigned long int
32
0 to 4,294,967,295
float
32
3.4E-38 to 3.4E+38
double
64
1.7E-308 to 1.7E+308
long double
80
3.4E-4932 to 1.1E+4932
Variables :
A variable is a data name that may be used to store a data value. Variable name can be chosen by the programmer in a meaningful way so as to reflect its function or nature in the program.
Example :
Average
Total


Variable names may consist of letters, digits and the underscore character. Subject to the following condition :
They must begin with a letter. Some systems permit underscore as the first character.
ANSI standard recognizes a length should not be normally more than eight character, Since only the first eight characters are treated significant by many compilers.
Uppercase and Lowercase are significant.
The variable name should not be a keyword.
White space is not allowed.
Arithmetic Operators :
Operator
Meaning
+
Addition or Unary Plus
-
Subtraction or Unary Minus
*
Multiplication
/
Division
%
Modular division (mod)
Increment and Decrement Operators :
Operator
Meaning
++
Add 1 to the operand
--
Subtracts 1 to the operand.


Relational Operators :
Operator
Meaning
<
is less than
<=
is less than or equal to
>
is greater than
>=
is greater than or equal to
==
is equal to
!=
is not equal to
Logical Operators :
Operator
Meaning
&&
Logical AND
||
Logical OR
!
Logical NOT
Assignment Operators :
=
Assignment
Bit wise Operators :
Operator
Meaning
&
Bit wise AND
|
Bit wise OR
^
Bit wise exclusive OR
<<
Shift left
>>
Shift right
~
One's complement
If Statement :
The if statement is a powerful decision making statement and is used to control the flow of execution of statement. It is basically a two-way decision statement and is used in conjunction with an expression.
It allows the computer to evaluate the expression first and then, depending on whether the value of the expression is 'true' or 'false', then  transfer the control to a particular statement.
Syntax :
if (test expression)
{
statement-block;
//true
}
else
{
statement-block;
//false
}
Flow chart:

Example :

#include <stdio.h>
main()
{
int m,n;
if (m>n)
     printf("M is big");
else
     printf("N is big");
}
The ? : Operator :
The C language has an unusual operator, useful for making two - way decisions. This operator is a combination of ? and :, and takes three operands. This operator is popularly known as the conditional operator.
Flow chart :

Syntax :
conditional expression ? expression1 : expression2
The conditional expression is evaluated first. If the result is nonzero, expression1 is evaluated and is returned as the value of the conditional expression. Otherwise expression2 is evaluated and its value is returned.
Example :
#include<stdio.h>
main()
{
int a,b,big;

a=77;
b=99;
big=((a>b)?a:b);
printf("Big is %d",big);
}
The goto Statement :
c supports the goto statement to branch unconditionally from one point to another in the program. The goto requires a label in order to identify the place where the branch is to be made. A label is any valid variable name, and must be followed by a colon. The label is placed immediately before the statement where the control is to be transferred. The general forms of goto and label statements are shown below:
The label: can be anywhere in the program either before or after the goto label; statement.
During running of a program when a statement like
goto begin;
is met, the flow of control will jump to the statement immediately following the label begin:. This happens unconditionally.

Note that a goto breaks the normal sequential execution of the program. If the label: is before the statement goto label; a loop will be formed and some statements will be executed repeatedly. Such a jump is known as backward jump. On the other hand, if the label: is placed after the goto label; some statements will be skipped and the jump is known as a forward jump.
Example :
#include<stdio.h>
main()
{
int no,eng,maths,sci,total;

printf("\n Enter no : ");
scanf("%d",&no);

Eng:
printf("\n Enter English marks : ");
scanf("%d",&eng);
if((eng<0)||(eng>100))
{       printf("\nInvalid Marks ! Try again");
goto Eng;
}

Sci:
printf("\n Enter Science marks : ");
scanf("%d",&sci);
if((sci<0)||(sci>100))
{       printf("\nInvalid Marks ! Try again");
goto Sci;
}

Maths:
printf("\n Enter Maths marks : ");
scanf("%d",&maths);
if((maths<0)||(maths>100))
{       printf("\nInvalid Marks ! Try again");
goto Maths;
}

total=eng+sci+maths;
printf("\n Total is : %d",total);
}

Switch Statement :
Switch is a Multi-Way decision statement. The Switch statement tests the value of a given variable (or expression) against a list of case values and when a match is found, a block of statement associated with case is executed.
Syntax :
switch(expression)
{
case value-1 :

block-1;
break;
case value-2 :
block-2;
break;
.....
.....
default :
default-block;
break;
}
statement-X;
Flow chart :
The expression is an integer or character. Value-1, Value-2 ... are constant or constant expression. Default is an optional case.
Example :
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int month;



printf("Enter month:");
scanf("%d",&month);



switch(month)
{
case 1: printf("January");
break;
case 2: printf("February");
break;
case 3: printf("March");
break;
case 4: printf("April");
break;
case 5: printf("May");
break;
case 6: printf("June");
break;
case 7: printf("July");
break;
case 8: printf("August");
break;
case 9: printf("September");
break;
case 10:printf("October");
break;
case 11:printf("November");
break;
case 12:printf("December");
break;
default:printf("Error");
break;
}
getch();
}


while Statement :
While is an entry-controlled loop statement. The basic structure of while is shown below.
Syntax :
while(test condition)
{
body of the loop
}
Flow chart :

The test-condition is evaluated and if the condition is true, then the body of loop is executed. After execution of the body, the test-condition is once again evaluated and if it is true, The body is executed once again. This process of repeated execution of the body continues until the test-condition finally becomes false and the control is transferred out of the loop.
Example :
#include <stdio.h>
#include <conio.h>
main()
{
int i;
i=1;
while(i<=10)
{
printf("\n CDAC");
i++;
}
getch();
}


Do While Statement :
The do-while loop is exit-controlled loop. On some occasions it might be necessary to execute the body of the loop before the test is performed.
Syntax :
do
{
body of the loop
} while (test-condition);
Flow chart :

On reaching the do statement, the program proceeds to evaluate the body of the loop first the end of the loop, the test-condition in the while statement is evaluated. If the condition is true, the program continues to evaluates the body of the loop once again. This process continues as long as the condition is true.
Example :
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i;
i=1;
do
{
printf("\n CDAC");
i++;
} while(i<=10);
getch();
}
For Statement :
The for loop is another entry-controlled loop that provides a more concise loop control structure. The general form of for loop is :
for(initialization;test-condition; increment)
{
body of the loop
}
Flow chart:


Initialization of the control variable is done first, Ex. i=1 or i=0. The value of control variable is testing using the test-condition. Ex. i<10. When the body of the loop is executed, the control is transferred back to the for statement after evaluating the last statement in the loop ex. i++,j++.
Example :
#include <stdio.h>
#include <conio.h>
main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf("\n CDAC");
}
getch();
}