Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

PRIMARY DATA TYPES

Every C compiler supports five primary data types:

void As the name suggests it holds no value and is generally used for specifying the type
of function or what it returns. If the function has a void type, it means that the
function will not return any value.

int Used to denote an integer type.

Example: int a //declaration of a

int a = 4000; // declaration and initialization of a

format string %d

long int Example: long int a; //declaration of a

long int a=123454666; // declaration and initialization of a

format string %ld

char Used to denote a character type.

format string %c

Example: char a; //declaration of a

char a = 'Z'; //declaration and initialization of a

float, double Used to denote a floating point type.

format string %f

Example: float a; //declaration of a

float a = 5.2324; //declaration and initialization of a

double i = 4.1234567890;

float j = -3.55;

Primary data types

These are fundamental data types in C namely integer(int), floating(float), charater(char) and void.

Derived data types


Derived data types are like arrays, functions, stuctures and pointers.

Integer type

Integers are used to store whole numbers.

Size and range of Integer type on 16-bit machine

Type Size(bytes) Range

int or signed int 2 -32,768 to 32767

unsigned int 2 0 to 65535

short int or signed short int 1 -128 to 127

long int or signed long int 4 -2,147,483,648 to 2,147,483,647

unsigned long int 4 0 to 4,294,967,295


Floating type

Floating types are used to store real numbers.

Size and range of Integer type on 16-bit machine

Type Size(bytes) Range

Float 4 3.4E-38 to 3.4E+38

double 8 1.7E-308 to 1.7E+308

long double 10 3.4E-4932 to 1.1E+4932

Character type

Character types are used to store characters value.

Size and range of Integer type on 16-bit machine

Type Size(bytes) Range

char or signed char 1 -128 to 127

unsigned char 1 0 to 255

void type void type means no value. This is usually used to specify the type of functions.

C OPERATOR PRECEDENCE TABLE


C operators in order of precedence (highest to lowest). Their associativity indicates in what order operators of
equal precedence in an expression are applied.
Operator Description Associativity
() Parentheses (function call) (see Note 1) left-to-right
[] Brackets (array subscript)
. Member selection via object name
-> Member selection via pointer
++ -- Postfix increment/decrement (see Note 2)
++ -- Prefix increment/decrement right-to-left
+- Unary plus/minus
!~ Logical negation/bitwise complement
(type) Cast (convert value to temporary value of type)
* Dereference
& Address (of operand)
sizeof Determine size in bytes on this implementation
* / % Multiplication/division/modulus left-to-right
+ - Addition/subtraction left-to-right
<< >> Bitwise shift left, Bitwise shift right left-to-right
< <= Relational less than/less than or equal to left-to-right
> >= Relational greater than/greater than or equal to
== != Relational is equal to/is not equal to left-to-right
& Bitwise AND left-to-right
^ Bitwise exclusive OR left-to-right
| Bitwise inclusive OR left-to-right
&& Logical AND left-to-right
|| Logical OR left-to-right
?: Ternary conditional right-to-left
= Assignment right-to-left
+= -= Addition/subtraction assignment
*= /= Multiplication/division assignment
%= &= Modulus/bitwise AND assignment
^= |= Bitwise exclusive/inclusive OR assignment
<<= >>= Bitwise shift left/right assignment
, Comma (separate expressions) left-to-right

Note 1:
Parentheses are also used to group sub-expressions to force a different precedence;
such parenthetical expressions can be nested and are evaluated from inner to outer.
Note 2:
Postfix increment/decrement have high precedence, but the actual increment or
decrement of the operand is delayed (to be accomplished sometime before the
statement completes execution). So in the statement y = x * z++; the current value
of z is used to evaluate the expression (i.e., z++ evaluates to z) and z only
incremented after all else is done. See postinc.cfor another example.
TERNARY OPERATOR (?:)

A conditional operator is a ternary operator, that is, it works on 3 operands.

Conditional Operator Syntax

conditionalExpression ?expression1 : expression2

The conditional operator works as follows:

 The first expression conditionalExpression is evaluated at first. This expression evaluates to 1 if it's and
evaluates to 0 if it's false.
 If conditionalExpression is true, expression1 is evaluated.
 If conditionalExpression is false, expression2 is evaluated.

void main()

int big,a=5,b=7;

clrscr();

(a>b)?printf("True") :printf("False");

// printf("%d",big);

getch();

DECISION MAKING STATEMENTS

C conditional statements allow you to make a decision, based upon the result of a condition. These statements
are called as Decision Making Statements or Conditional Statements.

If statement
If statements in C is used to control the program flow based on some condition, it's used to execute some
statement code block if the expression is evaluated to true, otherwise, it will get skipped. This is the simplest
way to modify the control flow of the program.

The if statement in C can be used in various forms depending on the situation and complexity.

Syntax:
if(test_expression)
{
statement 1;
statement 2;
...
}

Flowchart of if Statement:

If else statements

If else statements in C is also used to control the program flow based on some condition, only the difference is:
it's used to execute some statement code block if the expression is evaluated to true, otherwise executes
else statement code block.

Syntax:
if(test_expression)
{
//execute your code
}
else
{
//execute your code
}

Flowchart of if-else Statement:

C program code for Leap Year, C program is used to check whether the given year of input is a leap year or
not.
Program for PALINDROME

If a number, which when read in both forward and backward way is same, then such a number is called a
palindrome number. example: 121, 23532, 12321 etc.
EXPLANATION

Consider a numbern=121, reverse=0 and remainder;


number=121

now the while loop is executed /* the condition (n>0) is satisfied */

/* calculate remainder */
remainder of 121 divided by 10=(121%10)=1;
now reverse=(reverse*10)+remainder
=(0*10)+1 /* we have initialized reverse=0 */
=1
number=number/10
=121/10
=12

now the number is 12, greater than 0. The above process is repeated for number=12.
remainder=12%10=2;
reverse=(1*10)+2=12;
number=12/10=1;
now the number is 1, greater than 0. The above process is repeated for number=1.
remainder=1%10=1;
reverse=(12*10)+1=121;
number=1/10 /* the condition n>0 is not satisfied,control leaves the while loop */

Program stops here. The given number=121 equals the reverse of the number. Thus the given number is a
palindrome number.

Nested if else statements


Nested if else statements play an important role in C programming, it means you can use conditional statements
inside another conditional statement.

Syntax:
if(test_expression one)
{
if(test_expression two)
{
//Statement block Executes when the boolean test expression two is true.
}
}
else
{
//else statement block
}
main()
{
int x=20,y=30;
if(x==20)
{
if(y==30)
{
printf("value of x is 20, and value of y is 30.");
}
}
}

Execution of the above code produces the following result.


Output:
value of x is 20, and value of y is 30.

else-if statements
else-if statements in C is like another if condition, it's used in a program when if statement having multiple
decisions.

Syntax:
if(test_expression)
{
//execute your code
}
else if(test_expression n)
{
//execute your code
}
else
{
//execute your code
}
OUTPUT

Enter a,b,c: 3 5 8

c is Greater than a and b

EXPLANATION

Consider three numbers a=5,b=4,c=8

if(a>b && a>c) then a is greater than b and c

now check this condition for the three numbers 5,4,8 i.e.

if(5>4 && 5>8) /* 5>4 is true but 5>8 fails */

so the control shifts to else if condition

else if(b>a && b>c) then b is greater than a and c

now checking this condition for 5,4,8 i.e.

else if(4>5 && 4>8) /* both the conditions fail */


now the control shifts to the next else if condition

else if(c>a && c>b) then c is greater than a and b

now checking this condition for 5,4,8 i.e.

else if(8>5 && 8>4) /* both conditions are satisfied */

Thus c is greater than a and b.

Switch Statement
C switch statement is used when you have multiple possibilities for the if statement.

Syntax:
switch(variable)
{
case 1:
//execute your code
break;
case n:
//execute your code
break;
default:
//execute your code
break;
}

After the end of each block it is necessary to insert a break statement because if the programmers do not use the
break statement, all consecutive blocks of codes will get executed from each and every case onwards after
matching the case block.

Read examples from your class notes

goto Statement
C supports a special form of a statement that is the goto Statement which is used to branch unconditionally
within a program from one point to another. Although it is not a good habit to use goto statement in C, there
may be some situations where the use of goto statement might be desirable.
goto statement is used by programmers to change the sequence of execution of a C program by shifting the
control to a different part of the same program.

Syntax:
goto label;

A label is an identifier required for goto statement in order to place where the branch is to be made. A label is a
valid variable name which is followed by a colon and is put immediately before the statement where the control
needs to be jumped/transferred unconditionally.
Syntax:
goto label;
- - -- - -
--------
label:
statement - X;
/* This the forward jump of goto statement */

or
label:
- - -- - -
--------
goto label;
/*This is the backward jump of goto statement */

An Example of a C Program to Demonstrate goto Statement


Example:
void main()
{
int age;
g: //label name
printf("you are Eligible\n");
s: //label name
printf("you are not Eligible");
printf("Enter you age:");
scanf("%d", &age);
if(age>=18)
goto g; //goto label g
else
goto s; //goto label s
getch(); }

LOOPING STATEMENTS

Sometimes it is necessary for the program to execute the statement several times, and C loops execute a block
of commands a specified number of times until a condition is met. In this chapter, you will learn about all the
looping statements of C programming along with their use.

WHAT IS LOOP

A computer is the most suitable machine to perform repetitive tasks and can tirelessly do a task tens of
thousands of times. Every programming language has the feature to instruct to do such repetitive tasks with the
help of certain form of statements. The process of repeatedly executing a collection of statement is
called looping. The statements get executed many numbers of times based on the condition. But if the
condition is given in such a logic that the repetition continues any number of times with no fixed condition to
stop looping those statements, then this type of looping is called infinite looping.

Loop control statements


Loop control statements are used to change the normal sequence of execution of the loop.

while loop
C while loops statement allows to repeatedly run the same block of code until a condition is met.

while loop is a most basic loop in C programming. while loop has one control condition, and executes as long
the condition is true. The condition of the loop is tested before the body of the loop is executed, hence it is
called an entry-controlled loop.
Syntax:
While (condition)
{
statement(s);
Incrementation;
}

Flowchart of while loop:

Example of a C Program to Demonstrate while loop


Example:
int main ()
{
int n = 1,times=5; /* local variable Initialization */
while( n <= times ) /* while loops execution */
{
printf("C while loops: %d\n", n);
n++;
}
return 0;
}

Program Output:
do while loops
C do while loops are very similar to the while loops, but it always executes the code block at least once and
furthermore as long as the condition remains true. This is an exit-controlled loop.
Syntax:
do
{
statement(s);
}while( condition );

Flowchart of do while loop:

Example of a C Program to Demonstrate do while loop


Example:
int main ()
{
int n = 1,times=5; /* local variable Initialization */
do /* do loops execution */
{
printf("C do while loops: %d\n", n);
n = n + 1;
}while( n <= times );
return 0;
}

Program Output:

for loops
C for loops is very similar to a while loops in that it continues to process a block of code until a statement
becomes false, and everything is defined in a single line. The for loop is also entry-controlled loop.
Syntax:
for ( initalization; condition; increment or decrement )
{
statement(s);
}

Flowchart of for loop:


Example of a C Program to Demonstrate for loop
Example:
#include<stdio.h>
int main ()
{
int n,times=5; /* local variable Initialization */
for( n = 1; n <= times; n = n + 1 ) /* for loops execution */
{
printf("C for loops: %d\n", n);
}
return 0;
}

Program Output:
Break, Goto and Continue Statements

Statement Syntax Description

break break; Is used to terminate loop or switch statements.


statement

continue continue; Is used to suspend the execution of current


statement loop iteration and transfer control to the loop
for the next iteration.

goto goto labelName; It transfers current program execution


statement sequence to some other part of the program.
labelName: statement;

You might also like