SlideShare a Scribd company logo
1 of 79
C Language
Topics
 1.Overview of C
 2.Constants
 3.Variable and Data Types
 4.Operators and Expression
 5.Managing Input and Output Operators
 6.Decision Making and Branching
 7.IF statement
 8.IF-ELSE statement
 9.GO TO statement
 10.Decision Making and Looping
 11. WHILE statement
 12. DO statement and FOR statement
 13. Jumps in Loop (Break and Continue)
Overview of C
 Low level Language
 Translator (Compiler, Interpreter,…)
 History of C
 Characteristics of C
Low Level Languages
 The languges in this category are the Machine level
language and Assembly language.
 1. Machine level Language
 2. Assembly Language
 3. High-Level Language
Machine Level Language
 Computers can understand only digital signals, which are in
binary digits i.e.(0 or 1)
 So the instructions given to the computer can be only in
binary codes.
 The machine language consists of instructions that are in
binary 0 or 1. so computers can understand only machine
level language.
 Writing a program in machine level language is a dififcult
task because to write instructions in binary code is not easy.
 It is error-prone and maintenance is very difficult.
 Not Portable.
 Every computer has its own machine instructions,so the
programs written for one computer are not valid for other
computers
Assembly Level Language
 The difficulties faced in machine level language were
reduced to some extent by using a modified form of
machine level language called Assembly language.
 Modified form in English words like MOV,ADD,SUB.
 So easy to write and understand assembly programs.
 Computer can understand machine language so assembly
language program must be translated into machine
language.The translator is called “Assembler”.
 In this, data is stored in computer registers and each
computer has different set of registers.Thus it’s not
portable.
 The low level languages are related with the hardware,
the execution of a low-level program is faster.
High Level Language
 Portable and Machine Independent.
 These are English like language, so easy to write and
understand program of high level language.
 For translating a high-level language program into
machine language, compiler or interpreter is used.
 Every language has its own compiler or interpreter.
 Some languages in this category are : FORTRAN,
COBOL, BASIC,Pascal etc.
Translators
 Assembler
 Compiler
 Interpreter
 Assembler is used for converting the code of low-level
language(assembly language) into machine level
language.
 Compiler and interpreter are used to convert the code of
high-level language into machine language. The high
level program is known as source program and the
corresponding machine language program is known as
object program. Although both compilers and
interpreters perform the same task, there is a difference
in their working.-----
Translator
 Question: difference between compiler and interpreter.
 A Compiler searches all the errors of program and lists
them.If the program is error free then it converts the
code of program into machine code and then the
program can be executed by separate commands.
 An Interpreter chacks the errors of program statement by
statement. After checking one statement, it converts that
statement into machine code and then executes that
statement.This process continues until the last statement
of program or an erroneous statement occurs.
History of C
 C Language was developed by Dennis Ritchie in 1970’s at
AT&T Bell laboratories.
 Intially it was designed for programming in the OS called
UNIX.After advent of C, the whole UNIX OS was rewritten
using it.
 The C language is derived from the B languge, which was
written by Ken Thompson at AT&T Bell laboratories
 The B language was adopted from a language called
BCPL(Basic Combined Programming Language),which was
developed by Martin Richards at Cambirdge University .
 C programming is considered as the base for other
programming languages, that is why it is known as mother
language.
 It can be defined by following ways:
• Mother language
• System programming language
• Procedure-oriented programming language
• Structured programming language
• Mid level programming language
Characteristics of C
 C is middle level language.
 It has the simplicity of a high level language as well as the
power of a low level language.
 This aspect of C makes it suitable for writing both
Application program and System program
 It is an excellent,efficient and general-purpose language for
most of the applications such as mathematical,scientific,
business and system software applications
 C is small language,consisting 32 keywords.
 The power of C is augmented by the library functions provided
with it.the language is extendible since it allows the users to
add their own library functions to the library.
 C is structured programming language.
 C is the widely used language. It provides a lot
of features that are given below.
• Simple
• Machine Independent or Portable
• Mid-level programming language
• structured programming language
• Rich Library
• Memory Management
• Fast Speed
• Pointers
• Recursion
• Extensible
Constants
 Constants is a value that cannot be changed during
execution of the program.
CONSTANT
NUMERIC CONSTANT CHARACTER CONSTANT STRING CONSTANT
INTERGER CONSTANT REAL CONSTANT
DECIMAL OCTAL HEXADECIMAL
Numeric Constant
 Numeric constants consist of numeric digits, they
may or may not have decimal point(.).These are the
rules for defining numeric constants:
 1. Numeric constant should have at least one digit.
 2. No comma or space is allowed within the numeric
constant.
 3. Numeric constants can either be positive or
negative but default sign is always positive.
Integer Constants
 There are three type of constant based on different number
systems(decimal,octal,hexadecimal)
 Decimal constants: 0 to 9 (base 10)
 Octal constants: 0 to 7 (base 8)
 Hexadecimal: 0 to 9 or A,B,C,D,E,F (base 16)
 Some valid decimal Integer constants are
 0 123 3705 232759
 Some invalid decimal Integer constants are
 2.5 (illegal character (.)) 3#5(# illegal character) 98 5 (No blank space) 092 (first digit
can not be zero) 8,25 (comma is not allowed)
 Octal constants: 0 06 077 032 {first digit muse be O}
 Hexadeciamal constants:
 0x23 0x251 0xac 0xFF {first two characters should be 0x or
OX }
Real(floating ) Constant
 Floating point constants are numeric constants that contain
decimal point.some are :
 0.5 5.3
 400.0 0.002
 5478.513
 For expressing very large or very small real constant,
exponential (scientific) form is used.Here the number is
written in the mantissa and exponent form, which are
separated by ‘e’ or ‘E’.
 The mantissa can be an integer or a real number.
 While the exponent can be only an integer (+ or -)
 for example: 1800000 can be written as 1.8e6.
 Here 1.8 is mantissa and 6 is the exponent.
 2500000000 -> 2.5*109
exponent form 2.5e9
 -670000 -> -6.7*105
exponent form -6.7E5
Character Constant
 A character constant is a single character that is enclosed within
single quotes. Some are
 ‘8’ ‘D’ ‘$’ ‘ ’ ‘#’
 Some invalid are
 ‘four ’ There should be only one character within quotes
 “d” Double quotes are not allowed
 ‘’ No character between single quotes
 Y Sinlge quotes missing
 Every character constant has a unique integer value associated with
it.This integer is the value of the character in the machine’s
character code.
 A-Z ASCII value (65-90)
 a-z ASCII value (97-122)
 0-9 ASCII value (48-57)
 ; ASCII value (59)
String Constant
 A string constant is enclosed within double quotes (“”).
At the end of string, the null character ‘0’ is
automatically placed by the compiler. Some are
 “kumar”
 “594”
 “8”
 “ ”
 “A”
 Note that “A” and ‘A’ are different, the first one is a string
constant which consist of character A and ‘0’ while the
second one is a character constant which represents
integer value 65(in ASCII)
Identifiers (Variables)
 All words in C programs will be either keywords or
identifiers.
 Identifiers are user defined words and are used to
give names to entities like variable, arrays,
functions,structures etc.
 The rules for naming variables are same as that for
naming identifiers.
 Variable is a name that can be used to store values, it
can take different values but one at a time. These
values can be changed during execution of program.
Declaration of Variables
 It is must to declare a variable before it is used in the
program.
 Declaration of a variable specifies its name and datatype.
 The type and range of values that a variable can store
depends upon its datatype.
 Syntax: datatype variable name;
 int x;
 float salary;
 char grade;
 long y;
 short z;
 int,float,char,long,short are datatype
Initialization of Variable
 When a variable is declared it contains undefined value
commonly known as garbage value.
 If we want we can assign some initial value to the
variable during the declaration itself, this is called
initialization of the variable.
 int a=5;
 float x=8.9,y=10.5;
 char ch= ‘Y’;
 double num=0.15197e-7;
 int l,m,n,total=0;
 In the last declaration, only the variable total is
initialized.
Rules for naming identifiers
 1.The name should consist of only uppercase and
lowercase latters,digits and underscore sign( _ ).
 2. First character should be alphabet or underscore.
 3. The name should not be a keyword.
 4. Since C is case sensitive, the uppercase and
lowercase letters are considered different. For
example code, Code and CODE are three different
identifier.
Data Type
 A data type is associated with each variable and its
decides what values the variable can take.
 A data type defines a domain of allowed values and the
operation that can be performed on those values.
 Storage representation of different data type is different
in memory.
 There are four fundamental data type in C, which are
int,char,float and char.
 We can use type qualifiers with these basic types to get
some more types.
 There are two types of type qualifiers
 1. Size qualifiers - short, long
 2.Sign qualifiers - signed, unsigned
Data Type
 The qualifier signed and unsigned can be applied to char and
integer types.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 in integers, then by default
signed qualifier is assumed.
 If the sign qualifier is not mentioned for char type, then whether the
char type is signed or unsigned is machine dependent.
 The range of values for signed data types is less than that or
unsinged type. This is because in signed type.
 the leftmost bit is used to represent the sign. While in unsigned
type this bit is also used to represent the value.
 The qualifier short and long can be applied to int type to get types
short int and long int. The qualifier long can be applied to double to
get type long double which stores extended precision floating point
number.
Int int
short
long
Primary
Float float
double
Char char
void
Secondary
Data Type (Range) {-2
n-1
to 2
n-1
-1}
Baic
Data
Data type with type Size
(bytes)
Formula Range
char signed char 1 -2
8-1
to 28-1
-1 -128 to 127
unsigned char 1 0 to 255
int int or signed int 2 -32768 to 32767
unsigned int 2 0 to 65535
short int or signed short int 1 -128 to 127
unsigned short int 1 0 to 255
long int or signed long int 4 -2147483648 to
2147483647
unsigned long int 4 0 to 4294967295
float float 4 3.4E-38 to 3.4E38
double double 8 1.7E-308 to 1.7E308
long
double
long double 10 3.4E-4932 to
3.4E4932
Operator and Expression
 An operator specifies an operation to be performed that yields a value.
 The variables, constants can be joined by various operators to form an
expression.
 An operand is a data item on which an operator acts.
 Expression => operand + operator
 Several different categories of operators:
 1. Arithmetic operators (+ - * / %)
 2. Assignment operators (= += -= *= /= %=)
 3.Increment and Decrement operators (++ --)
 4. Relational operators (> >= < <= == !=)
 5. Logical operators (&& || !)
 6. Conditional operator (text expressing ? exp1 : exp2)
 7. Comma operator (,)
 8.Sizeof operator sizeof()
 9. Bitwise operators (& | ~ << >> ^ )
 10.Other operators
Operator (Arithmetic + - * / %)
 Arithmetic operators are used for numeric calculations. They are
two types
 1. Unary arithmetic operator
 2. Binary arithmetic operator
 Unary operators require only one operand (+x -y).
 Binary operators require two operand. (operand )
 % (modulus operator ) can not be applied with floating point
operands.
 Unary plus and unary minus operators are different from the
addition and subtraction operators.
 When both operands are intergers, the result value always an
integer.
 When one operand is interger type and the other is of float type, the
resulting value is floating type.
 The modulus operator % can’t be used with floating point numbers.
/* Integer arithmetic operations */
#include<stdio.h>
int main()
{
int a=17, b=4;
printf(“Sum=%dn”, a+b );
printf(“Difference=%dn”, a-b );
printf(“Product=%dn”, a*b );
printf(“Quotient=%dn”, a/b );
printf(“Remainder=%dn”, a%b );
getch();
return 0;
}
When both operands are intergers, the result value always an integer.
/* Program to understand the floating point arithmetic operation */
#include<stdio.h>
int main()
{
float a=12.4, b=3.8;
printf(“Sum=%fn”, a+b );
printf(“Difference=%fn”, a-b );
printf(“Product=%fn”, a*b );
printf(“Quotient=%fn”, a/b );
getch();
return 0;
}
The modulus operator % can’t be used with floating point numbers.
Operator (Assignment = += -= *= /= )
 A value can be stored in a variable using assignment
operator.
 The operand on the left hand side should be a
variable, while the operand on the right hand side
can be any variable, constant or expression. The
value of right hand operand is assigned to the left
hand operand. Here some example :
 x = 8; s = x+y-2; x += 5; a*= b+5;
 Invalid are such that
 8 = x; 5 += x;
Increment / Decrement Operators
 The increment (++) and decrement (--) operators are unary
operators because they operate on a single operand.
 The increment operator increments the value of the variable
by 1, while decrement operator decrements the value of the
variable by 1.
 These operators should be used only with variables ; they can’t
be used with constants or expressions. For example ++5 or
++(x+y+z) are invalid.
 These operators are of two types –
 1.Prefix increment/decrement – operator is written before the
operand (e.g. ++x or --x)
 2.Post increment/decrement – operator is written after the
operand (e.g. x++ or x--)
Prefix Increment/Decrement operator
 First the value of variable is increment/decrement then the
new value is used in the operation.
 The statement y = ++x; means first increment the value of x
by 1, then assign the value of x to y.
 x= x+1;
 y = x;
 Now value of x is 4 and value of y is also 4.
 The statement y = --x; means first increment the value of x by
1, then assign the value of x to y.
 x= x-1;
 y = x;
 Now value of x is 3 and value of y is also 3.
/* Prefix increment /decrement */
#include<stdio.h>
int main()
{
int x = 8;
printf(“x=%dt”, x);
printf(“x=%dt”, ++x);
printf(“x=%dt”, x);
printf(“x=%dt”, --x);
printf(“x=%dn”, x);
return 0;
}
Output :
x = 8 x = 9 x = 9 x = 8 x = 8
In the second printf() statement , first the value of x is incremented and then printed; similarly in
the fourth printf() first the value of x is decremented and then printed.
Postfix Increment/Decrement operator
 First the value of variable is used in the operation and then
increment/decrement performed.
 The statement y = x++; means first the value of x assigned to y
and then x is incremented.
 y = x;
 x= x+1;
 Now value of x is 4 and value of y is 3.
 The statement y = x--; means first the value of x assigned to y
and then x is decremented.
 y = x;
 x= x-1;
 Now value of x is 3 and value of y is 4.
/* Postfix increment /decrement */
#include<stdio.h>
int main()
{
int x = 8;
printf(“x=%dt”, x);
printf(“x=%dt”, x++);
printf(“x=%dt”, x);
printf(“x=%dt”, x--);
printf(“x=%dn”, x);
return 0;
}
Output :
x = 8 x = 8 x = 9 x = 9 x = 8
In the second printf() statement , first the value of x is printed and then incremented; similarly in
the fourth printf() first the value of x is printed and then decremented.
Relational Operators
Relational operators are used to compare value of two expressions. An expression
that contains relational operators is called relational expression. If the relation is true
then the value of relational expression is 1 and if the relation is false then the value of
expression is 0. The relational operators are –
operator Meaning
< less than
<= less than equal to
== equal to
!= Not equal to
> Greater than
>= Greater than or equal to
Variable a = 9 and b = 5
Exopression Relation Value of Expression
a<b False 0
a<=b False 0
a!=b True 1
a==b False 0
a>8 True 1
2>4 False 0
/* Relational Operator */
#include<stdio.h>
int main()
{
int a,b;
printf(“Enter values for a and b :”);
scanf(“%d%d”, &a, &b);
if(a<b)
printf(“%d is less than %dn”, a, b);
if(a<=b)
printf(“%d is less than or equal to %dn”, a, b);
if(a==b)
printf(“%d is equal to %dn”, a, b);
if(a!=b)
printf(“%d is not equal to %dn”, a, b);
if(a>b)
printf(“%d is greater than %dn”, a, b);
if(a>=b)
printf(“%d is greater than or equal to %dn”, a, b);
return 0;
}
Logical Operators
An expression that combines two or more expressions is termed as logical expression.
For combining these expressions we use logical operators.
These operators return 0 for false and 1 for true. The operand may be contants,
variable or expressions. C has three logical operators.
Operator Name
&& AND
|| OR
! NOT
Here logical not is unirary operator while the other two are binary operatons.
In C any non-zero value is regarded as true and zero is regarded as false.
Logical AND (&&)
AND(&&) Operator : This operator gives the net result true if both the condition
are true , otherwise the result is false.
Boolean Table
Let us take three variable a = 10, b = 5, c = 0
Codition1 Condition2 Result
False False False
False True False
True False False
True True True
Expression Boolean Result Expression value
(a==10)&&(b>a) true && false False 0
(b>=a) &&(b==3) false&& false False 0
a && b true&& true True 1
a && c true&& false False 0
Logical OR (||)
OR (||) Operator : This operator gives the net result false, if both the conditions
have the value false , otherwise the result is true.
Boolean Table
Let us take three variable a = 10, b = 5, c = 0
Codition1 Condition2 Result
False False False
False True True
True False True
True True True
Expression Boolean Result Expression value
a || b True || True True 1
a || c True || False True 1
(a<9)||(b>10) False && False False 0
(b!=7)|| c True && False True 1
Logical NOT (!) Operator
This is a unary operator and it negates the value of the condition. If the value of the
condition is false then it gives the result true. If the value of the condition is true then
it gives the result false.
Boolean Table
Let us take three variable a = 10 , b = 5 , c = 0
Condition Result
False True
True False
Expression Result Expression Value
!a !true false 0
!c !false true 1
!(b>c) !true false 0
!(a&&c) !false true 1
Conditional Operators
Conditional operator is a ternary operator (? and :) which requires three expressions
as operands. It is written as –
TestExpression ? expression1 : exprssion 2
Firstly the text expression is evaluated.
(1) If Testexpression is true (nonzero), then expression1 is evaluated and it becomes
the value of the overall conditional expression.
(2) If Testexpression is false (nonzero), then expression2 is evaluated and it becomes
the value of the overall conditional expression.
For example consider the conditional expression –
a>b ? a : b
Suppose a = 5 and b = 8 then
max = a>b ? a : b
/* Conditional Operator */
#include<stdio.h>
int main()
{
int a, b, max ;
printf(“Enter values for a and b :”);
scanf(“%d%d”, &a, &b);
max = a>b ? a : b;
printf(“Larger of %d and %d is %dn”, a, b, max);
return 0;
}
Comma Operators
The comma operator ( , ) is used to permit different expressions to appear in
situations where only one expression would be used. The expression are separated by
the comma operator.
Example : sum = (a = 8 , b = 7 , c = 9 , a+b+c );
#include<stdio.h>
int main()
{
int a, b, c, sum ;
sum =(a = 8, b = 7, c = 9, a+b+c );
printf(“Sum = %dn”, sum);
return 0;
}
Size of Operators
Sizeof is an unary operator that gives the size of its operand in terms of bytes. The
operand can be a variable, constant or any datatype (int, float, char etc.)
For example
sizeof (int) gives the bytes occupied by the int datatype.
#include<stdio.h>
int main()
{
int var;
printf(“Size of int =%u n”, sizeof(int));
printf(“Size of float =%u n”, sizeof(float));
printf(“Size of var =%u n”, sizeof(var));
printf(“Size of an integer constant =%u n”, sizeof(45));
return 0;
}
Decision Making and Branching
 Many situations arise where we may want to execute some statements several times.
 Control statement enable us to specify the order in which the various instrucitons in the
program are to be executed.
 They define how the control is transferred to other parts of the program.
 Different types of control statement in C
 Control statements
 Selection Iterative Jump
 If else while break
 Switch do..while continue
 . for
 COMPOUND STATEMENT : Compound statement or block is a group of statement enclosed
within a pair of curly barces. {}. The statement inside a block are executed sequentially.
 {
 Statement 1;
 Statement 1;
 ………………..;
 ………………..;
 }
If Statement
 The if statement :- C uses the keyword if to implement the
decision control instruction. The general form of if statement
looks like this :
 if (test expression)
 {
 Statement - block ;
 }
 The statement block may be a single statement or a group of
statements. If the test expression is true, the statement block
will be executed, otherwise the statement block will be
skipped and the execution will jump to the next statement.
Remember, when the condition is true both the statement
block and the next statement are executed in sequence.
Condition
End IF
False
True
Statement(S)
Flow chart of if statement
IF-ELSE Statement
 The if-else statement :- This is a bidirectional selection control
statement which is used to take one of the two possible actions.
 Syntax :
 if (expression)
 {
 Statement - block 1;
 }
 else
 {
Statement-block2;
 }
 The expression inside the parantheses is evaluated and if it is true (has a
non zero value) then statement1 is executed and if the expression is false
(zero) then statement2 is executed
 This expression is often called the if condition. Flow chart for if else :

Expression
Next Statement
FalseTrue
Statement 2
Flow chart of if-else statement
Statement 1
/*Program to print the bigger number*/
 #include<stdio.h>
 int main()
 {
 int a,b;
printf(“ Enter two numbers : ”);
scanf(“%d%d”, &a, &b);
if(a>b)
{
printf(“Bigger number = %dn”,a);
}
else
{
printf(“Bigger number = %dn”,b);
}
return 0;
 }
Decision Making and Looping
 Loops are used when we want to execute a part of the program or a
block of statements several times.
 Suppose we want to print “C is best ” 10 times.
 The way is write printf statement 10 times which is not preferable.
 Other way is loop – using loop we can write one loop and one printf
statement, and this approach is definitely better than the first one.
 With the help of loop we can execute a part of the program
repeatedly till some expression is true.
 In C, the loops can be written using these three iterative statements.

 while
 do – while
 for
While Loop
 The syntax of while statement is –
 while(expression)
 {
 statement
 statement
 .…………….
 }
 First the expression is evaluated; if it is true then the stmnts in the body of
loop are executed. After the execution, again the expression is evaluated
and if it is found to be true then again the stmnts in the body of loop are
executed.
 This means that these statements are executed continuosly till the
expression is true and when it becomes false, the loop terminates and the
control comes out of the loop. Each execution of the loop body is known as
iteration, and the expression is known as loop condition.
Expression
Body of Loop
True
False
Next Statement
out of loop
Flow chart of while loop
/* Print no. form 1 to 10 using While Loop */
#include<stdio.h>
int main()
{
int i;
while(i<=10)
{
printf(“%dt”, i);
i = i + 1;
}
printf(“n”);
return 0;
}
Output :
1 2 3 4 5 6 7 8 9 10
/* Print no. in reverse order with a difference of 2*/
#include<stdio.h>
int main()
{
int k=10;
while(k>=2)
{
printf(“%dt”, k);
k = k - 2;
}
printf(“n”);
return 0;
}
Output :
10 8 6 4 2
DO – while Statement
 The syntax of while statement is –
 do
 {
 statement
 statement
 .…………….
 } while(expression);
 Here first the loop body is executed and then the
expression is evaluated. If the expression is true, then
again the loop body is executed and this process
continues as long as the expression is true. When the
expression becomes false the loop terminates.
Difference between while and do-while loop
 In a while loop, first the expression is evaluated and
then the loop body is executed whereas in a do-while
loop, first the loop body is executed and then the
expression is evaluated.
 If intially the condition is false then in the case of
while loop , the loop body will not execute at all while
in the case of do-while loop, the loop body will
execute one time.
 So in a do-while loop, the loop body is guaranteed to
execute at least once.
Expression
Body of Loop
True
False
Next Statement
out of loop
Flow chart of do-while loop
/* Print no. form 1 to 10 using do - while Loop */
#include<stdio.h>
int main()
{
int i=1;
do
{
printf(“%dt”, i);
i = i + 1;
} while(i<=10);
printf(“n”);
return 0;
}
Output :
1 2 3 4 5 6 7 8 9 10
/* Program to find the sum of numbers entered */
#include<stdio.h>
int main()
{
int num, sum=0;
do
{
printf(“Enter a number (enter 0 to stop) :”);
printf(“%d”, &num);
sum = sum + num;
} while(num!=0);
printf(“Sum is %dn”, sum);
return 0;
}
for Statement
 The for statement has three expression, semicolons are used for separating
these expressions.The syntax of for statement is –
 for(expression1;expression2;expression3)
 {
 statement
 statement
 .…………….
 }
 Exp1 is an intialization expression, exp2 is a test expression or condition
and exp3 is an update expression
 Exp1 is executed only once when the loop starts and is used to intialize the
loop variables. This exp is genearally an assignment expression.
 Exp2 is condition and is tested before each iteration of the loop.This
condition generally used relational and logical operators.
 Exp3 is an update expression and is executed each time after the body of
the loop is executed.
Condition
Intialization expression
True
False
Next Statement
out of loop
Update expression
Body of Loop
Flow chart of for loop
/* Print no. form 1 to 10 using for Loop */
#include<stdio.h>
int main()
{
int i;
for(i=1; i<=10 ; i++)
{
printf(“%dt”, i);
}
printf(“n”);
return 0;
}
Output :
1 2 3 4 5 6 7 8 9 10
/* Print no. in reverse order with a difference of 2 using for loop*/
#include<stdio.h>
int main()
{
int k;
for(k=10 ; k>=2 ; k-=2)
{
printf(“%dt”, k);
}
printf(“n”);
return 0;
}
Output :
10 8 6 4 2
Jumps in Loop(Break , Continue and goto)
 Jumps statements:
 break
 continue
 goto
Break
 break statement is used inside loops and switch
statements. Sometimes it becomes necessary to come out
of the loop even before the loop condition becomes false.
In such a situation, break statement is used to terminate
the loop. This statement causes an immediate exit from
that loop in which this statement appears. It can be
written as –
 break;
 When break statement is encountered, loop is terminated
and the control is transferred to the statement
immediately after the loop. The break statement is
generally written along with a condition. If break is
written inside a nested loop structure then it causes exit
from the innermost loop.
#include<stdio.h>
int main()
{
int n;
for(n=1;n<=5;n++)
{
if(n==3)
break;
printf(“Number = %dn”, n);
}
printf(“Out of for loop n”);
return 0;
}
Output
Number = 1
Number = 2
Out of for loop
Continue
 The continue statement is used when we want to go
the next iteration of the loop after skipping some
statements of the loop. The continue statement can
be written simply as –
 It is generally used with a condition. When continue
statement is encountered all the remaining
statements (statements after continue) in the current
iteration are not executed and the loop continue with
the next iteration.
#include<stdio.h>
int main()
{
int n;
for(n=1;n<=5;n++)
{
if(n==3)
continue;
printf(“Number = %dn”, n);
}
printf(“Out of for loop n”);
return 0;
}
Output :
Number = 1
Number = 2
Number = 4
Number = 5
Out of loop
Difference between break and continue
 When break is encountered the loop terminates and
the control is transferred to the next statement
following the loop
 BUT when a continue statement encountered the
loop is not terminated and the next iteration of the
loop starts.
GO-TO Statement
 This is an unconditional control statement that transfers
the flow of control to another part of the program. The
goto statement can be used as –
goto label;
…………….
…………….
label :
statement
…………….
…………….
#include<stdio.h>
int main()
{
int n;
printf(“Enter a number : ”);
scanf(“%d”, &n);
if(n%2==0)
goto even;
else
goto odd;
even :
printf(“Number is even n”);
goto end;
odd :
printf(“Number is odd n”);
goto end;
printf(“n”);
return 0;
}
Goto
Label is any valid C identifier and it is followed by a colon.
The label can be placed anywherer.
If the label is after goto then the control is transferrred
forward and it is known as forward jump or forward goto,
and if the label is before goto then the control is transferred
backward and it is known as backward jump or backward
goto. In forward goto, the statements between goto and
label will not be executed and in backward goto statements
between goto and label will be executed repeatedly. More
than one goto can be assoiciated with the same label but we
can not have same label at more than one place.
for(…………….)
{
while(………)
{
for(………….)
{
if(….)
{
goto stop;
}
}
}
}
flag =0;
for(…………….)
{
while(………)
{
for(………….)
{
if(….)
{
flag = 1;
break;
}
}
if(flag == 1)
break;
} if(flag == 1)
break;
}
THANK YOU

More Related Content

What's hot

Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compilerIffat Anjum
 
Unit 2 introduction to c programming
Unit 2   introduction to c programmingUnit 2   introduction to c programming
Unit 2 introduction to c programmingMithun DSouza
 
Cs6660 compiler design
Cs6660 compiler designCs6660 compiler design
Cs6660 compiler designhari2010
 
C programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer CentreC programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer Centrejatin batra
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programmingRokonuzzaman Rony
 
Chapter 1 1
Chapter 1 1Chapter 1 1
Chapter 1 1bolovv
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoobirbal
 
introduction to C programming (C)
introduction to C programming (C)introduction to C programming (C)
introduction to C programming (C)Abhishek Walia
 
C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance levelsajjad ali khan
 
Features of c language 1
Features of c language 1Features of c language 1
Features of c language 1srmohan06
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & RecoveryAkhil Kaushik
 
Chap 1-language processor
Chap 1-language processorChap 1-language processor
Chap 1-language processorshindept123
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C ProgrammingMOHAMAD NOH AHMAD
 

What's hot (19)

Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
 
Unit 2 introduction to c programming
Unit 2   introduction to c programmingUnit 2   introduction to c programming
Unit 2 introduction to c programming
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
 
Lecture 01 2017
Lecture 01 2017Lecture 01 2017
Lecture 01 2017
 
Cs6660 compiler design
Cs6660 compiler designCs6660 compiler design
Cs6660 compiler design
 
C programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer CentreC programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer Centre
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
C programming part1
C programming part1C programming part1
C programming part1
 
Chapter 1 1
Chapter 1 1Chapter 1 1
Chapter 1 1
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoo
 
introduction to C programming (C)
introduction to C programming (C)introduction to C programming (C)
introduction to C programming (C)
 
C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance level
 
Features of c
Features of cFeatures of c
Features of c
 
C Language
C LanguageC Language
C Language
 
Features of c language 1
Features of c language 1Features of c language 1
Features of c language 1
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
 
Chap 1-language processor
Chap 1-language processorChap 1-language processor
Chap 1-language processor
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 

Similar to Msc prev completed

Similar to Msc prev completed (20)

C Lang notes.ppt
C Lang notes.pptC Lang notes.ppt
C Lang notes.ppt
 
1. introduction to computer
1. introduction to computer1. introduction to computer
1. introduction to computer
 
C language for Semester Exams for Engineers
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers
 
C programming.pdf
C programming.pdfC programming.pdf
C programming.pdf
 
C programming language tutorial for beginers.pdf
C programming language tutorial for beginers.pdfC programming language tutorial for beginers.pdf
C programming language tutorial for beginers.pdf
 
C
CC
C
 
cunit1.pptx
cunit1.pptxcunit1.pptx
cunit1.pptx
 
C notes
C notesC notes
C notes
 
C language ppt
C language pptC language ppt
C language ppt
 
C language
C languageC language
C language
 
history of c.ppt
history of c.ppthistory of c.ppt
history of c.ppt
 
Aniket tore
Aniket toreAniket tore
Aniket tore
 
Introduction%20C.pptx
Introduction%20C.pptxIntroduction%20C.pptx
Introduction%20C.pptx
 
Unit 1 c - all topics
Unit 1   c - all topicsUnit 1   c - all topics
Unit 1 c - all topics
 
C PROGRAMMING LANGUAGE.pptx
 C PROGRAMMING LANGUAGE.pptx C PROGRAMMING LANGUAGE.pptx
C PROGRAMMING LANGUAGE.pptx
 
unit 1 cpds.pptx
unit 1 cpds.pptxunit 1 cpds.pptx
unit 1 cpds.pptx
 
C PADHLO FRANDS.pdf
C PADHLO FRANDS.pdfC PADHLO FRANDS.pdf
C PADHLO FRANDS.pdf
 
C presentation book
C presentation bookC presentation book
C presentation book
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
unit2.pptx
unit2.pptxunit2.pptx
unit2.pptx
 

More from mshoaib15

Msc prev updated
Msc prev updatedMsc prev updated
Msc prev updatedmshoaib15
 
Msc chemistry previous
Msc chemistry previousMsc chemistry previous
Msc chemistry previousmshoaib15
 
Bsc math previous exam quetions
Bsc math previous exam quetionsBsc math previous exam quetions
Bsc math previous exam quetionsmshoaib15
 
Computer lab (programs)
Computer lab (programs)Computer lab (programs)
Computer lab (programs)mshoaib15
 
What is algorithm
What is algorithmWhat is algorithm
What is algorithmmshoaib15
 
Boolean expression org.
Boolean expression org.Boolean expression org.
Boolean expression org.mshoaib15
 
Number system....
Number system....Number system....
Number system....mshoaib15
 

More from mshoaib15 (8)

Msc prev updated
Msc prev updatedMsc prev updated
Msc prev updated
 
Msc chemistry previous
Msc chemistry previousMsc chemistry previous
Msc chemistry previous
 
Bsc math previous exam quetions
Bsc math previous exam quetionsBsc math previous exam quetions
Bsc math previous exam quetions
 
Function
FunctionFunction
Function
 
Computer lab (programs)
Computer lab (programs)Computer lab (programs)
Computer lab (programs)
 
What is algorithm
What is algorithmWhat is algorithm
What is algorithm
 
Boolean expression org.
Boolean expression org.Boolean expression org.
Boolean expression org.
 
Number system....
Number system....Number system....
Number system....
 

Recently uploaded

Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 

Recently uploaded (20)

Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 

Msc prev completed

  • 2. Topics  1.Overview of C  2.Constants  3.Variable and Data Types  4.Operators and Expression  5.Managing Input and Output Operators  6.Decision Making and Branching  7.IF statement  8.IF-ELSE statement
  • 3.  9.GO TO statement  10.Decision Making and Looping  11. WHILE statement  12. DO statement and FOR statement  13. Jumps in Loop (Break and Continue)
  • 4. Overview of C  Low level Language  Translator (Compiler, Interpreter,…)  History of C  Characteristics of C
  • 5. Low Level Languages  The languges in this category are the Machine level language and Assembly language.  1. Machine level Language  2. Assembly Language  3. High-Level Language
  • 6. Machine Level Language  Computers can understand only digital signals, which are in binary digits i.e.(0 or 1)  So the instructions given to the computer can be only in binary codes.  The machine language consists of instructions that are in binary 0 or 1. so computers can understand only machine level language.  Writing a program in machine level language is a dififcult task because to write instructions in binary code is not easy.  It is error-prone and maintenance is very difficult.  Not Portable.  Every computer has its own machine instructions,so the programs written for one computer are not valid for other computers
  • 7. Assembly Level Language  The difficulties faced in machine level language were reduced to some extent by using a modified form of machine level language called Assembly language.  Modified form in English words like MOV,ADD,SUB.  So easy to write and understand assembly programs.  Computer can understand machine language so assembly language program must be translated into machine language.The translator is called “Assembler”.  In this, data is stored in computer registers and each computer has different set of registers.Thus it’s not portable.  The low level languages are related with the hardware, the execution of a low-level program is faster.
  • 8. High Level Language  Portable and Machine Independent.  These are English like language, so easy to write and understand program of high level language.  For translating a high-level language program into machine language, compiler or interpreter is used.  Every language has its own compiler or interpreter.  Some languages in this category are : FORTRAN, COBOL, BASIC,Pascal etc.
  • 9. Translators  Assembler  Compiler  Interpreter  Assembler is used for converting the code of low-level language(assembly language) into machine level language.  Compiler and interpreter are used to convert the code of high-level language into machine language. The high level program is known as source program and the corresponding machine language program is known as object program. Although both compilers and interpreters perform the same task, there is a difference in their working.-----
  • 10. Translator  Question: difference between compiler and interpreter.  A Compiler searches all the errors of program and lists them.If the program is error free then it converts the code of program into machine code and then the program can be executed by separate commands.  An Interpreter chacks the errors of program statement by statement. After checking one statement, it converts that statement into machine code and then executes that statement.This process continues until the last statement of program or an erroneous statement occurs.
  • 11. History of C  C Language was developed by Dennis Ritchie in 1970’s at AT&T Bell laboratories.  Intially it was designed for programming in the OS called UNIX.After advent of C, the whole UNIX OS was rewritten using it.  The C language is derived from the B languge, which was written by Ken Thompson at AT&T Bell laboratories  The B language was adopted from a language called BCPL(Basic Combined Programming Language),which was developed by Martin Richards at Cambirdge University .  C programming is considered as the base for other programming languages, that is why it is known as mother language.
  • 12.  It can be defined by following ways: • Mother language • System programming language • Procedure-oriented programming language • Structured programming language • Mid level programming language
  • 13. Characteristics of C  C is middle level language.  It has the simplicity of a high level language as well as the power of a low level language.  This aspect of C makes it suitable for writing both Application program and System program  It is an excellent,efficient and general-purpose language for most of the applications such as mathematical,scientific, business and system software applications  C is small language,consisting 32 keywords.  The power of C is augmented by the library functions provided with it.the language is extendible since it allows the users to add their own library functions to the library.  C is structured programming language.
  • 14.  C is the widely used language. It provides a lot of features that are given below. • Simple • Machine Independent or Portable • Mid-level programming language • structured programming language • Rich Library • Memory Management • Fast Speed • Pointers • Recursion • Extensible
  • 15. Constants  Constants is a value that cannot be changed during execution of the program. CONSTANT NUMERIC CONSTANT CHARACTER CONSTANT STRING CONSTANT INTERGER CONSTANT REAL CONSTANT DECIMAL OCTAL HEXADECIMAL
  • 16. Numeric Constant  Numeric constants consist of numeric digits, they may or may not have decimal point(.).These are the rules for defining numeric constants:  1. Numeric constant should have at least one digit.  2. No comma or space is allowed within the numeric constant.  3. Numeric constants can either be positive or negative but default sign is always positive.
  • 17. Integer Constants  There are three type of constant based on different number systems(decimal,octal,hexadecimal)  Decimal constants: 0 to 9 (base 10)  Octal constants: 0 to 7 (base 8)  Hexadecimal: 0 to 9 or A,B,C,D,E,F (base 16)  Some valid decimal Integer constants are  0 123 3705 232759  Some invalid decimal Integer constants are  2.5 (illegal character (.)) 3#5(# illegal character) 98 5 (No blank space) 092 (first digit can not be zero) 8,25 (comma is not allowed)  Octal constants: 0 06 077 032 {first digit muse be O}  Hexadeciamal constants:  0x23 0x251 0xac 0xFF {first two characters should be 0x or OX }
  • 18. Real(floating ) Constant  Floating point constants are numeric constants that contain decimal point.some are :  0.5 5.3  400.0 0.002  5478.513  For expressing very large or very small real constant, exponential (scientific) form is used.Here the number is written in the mantissa and exponent form, which are separated by ‘e’ or ‘E’.  The mantissa can be an integer or a real number.  While the exponent can be only an integer (+ or -)  for example: 1800000 can be written as 1.8e6.  Here 1.8 is mantissa and 6 is the exponent.  2500000000 -> 2.5*109 exponent form 2.5e9  -670000 -> -6.7*105 exponent form -6.7E5
  • 19. Character Constant  A character constant is a single character that is enclosed within single quotes. Some are  ‘8’ ‘D’ ‘$’ ‘ ’ ‘#’  Some invalid are  ‘four ’ There should be only one character within quotes  “d” Double quotes are not allowed  ‘’ No character between single quotes  Y Sinlge quotes missing  Every character constant has a unique integer value associated with it.This integer is the value of the character in the machine’s character code.  A-Z ASCII value (65-90)  a-z ASCII value (97-122)  0-9 ASCII value (48-57)  ; ASCII value (59)
  • 20. String Constant  A string constant is enclosed within double quotes (“”). At the end of string, the null character ‘0’ is automatically placed by the compiler. Some are  “kumar”  “594”  “8”  “ ”  “A”  Note that “A” and ‘A’ are different, the first one is a string constant which consist of character A and ‘0’ while the second one is a character constant which represents integer value 65(in ASCII)
  • 21. Identifiers (Variables)  All words in C programs will be either keywords or identifiers.  Identifiers are user defined words and are used to give names to entities like variable, arrays, functions,structures etc.  The rules for naming variables are same as that for naming identifiers.  Variable is a name that can be used to store values, it can take different values but one at a time. These values can be changed during execution of program.
  • 22. Declaration of Variables  It is must to declare a variable before it is used in the program.  Declaration of a variable specifies its name and datatype.  The type and range of values that a variable can store depends upon its datatype.  Syntax: datatype variable name;  int x;  float salary;  char grade;  long y;  short z;  int,float,char,long,short are datatype
  • 23. Initialization of Variable  When a variable is declared it contains undefined value commonly known as garbage value.  If we want we can assign some initial value to the variable during the declaration itself, this is called initialization of the variable.  int a=5;  float x=8.9,y=10.5;  char ch= ‘Y’;  double num=0.15197e-7;  int l,m,n,total=0;  In the last declaration, only the variable total is initialized.
  • 24. Rules for naming identifiers  1.The name should consist of only uppercase and lowercase latters,digits and underscore sign( _ ).  2. First character should be alphabet or underscore.  3. The name should not be a keyword.  4. Since C is case sensitive, the uppercase and lowercase letters are considered different. For example code, Code and CODE are three different identifier.
  • 25. Data Type  A data type is associated with each variable and its decides what values the variable can take.  A data type defines a domain of allowed values and the operation that can be performed on those values.  Storage representation of different data type is different in memory.  There are four fundamental data type in C, which are int,char,float and char.  We can use type qualifiers with these basic types to get some more types.  There are two types of type qualifiers  1. Size qualifiers - short, long  2.Sign qualifiers - signed, unsigned
  • 26. Data Type  The qualifier signed and unsigned can be applied to char and integer types.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 in integers, then by default signed qualifier is assumed.  If the sign qualifier is not mentioned for char type, then whether the char type is signed or unsigned is machine dependent.  The range of values for signed data types is less than that or unsinged type. This is because in signed type.  the leftmost bit is used to represent the sign. While in unsigned type this bit is also used to represent the value.  The qualifier short and long can be applied to int type to get types short int and long int. The qualifier long can be applied to double to get type long double which stores extended precision floating point number.
  • 28. Data Type (Range) {-2 n-1 to 2 n-1 -1} Baic Data Data type with type Size (bytes) Formula Range char signed char 1 -2 8-1 to 28-1 -1 -128 to 127 unsigned char 1 0 to 255 int int or signed int 2 -32768 to 32767 unsigned int 2 0 to 65535 short int or signed short int 1 -128 to 127 unsigned short int 1 0 to 255 long int or signed long int 4 -2147483648 to 2147483647 unsigned long int 4 0 to 4294967295 float float 4 3.4E-38 to 3.4E38 double double 8 1.7E-308 to 1.7E308 long double long double 10 3.4E-4932 to 3.4E4932
  • 29. Operator and Expression  An operator specifies an operation to be performed that yields a value.  The variables, constants can be joined by various operators to form an expression.  An operand is a data item on which an operator acts.  Expression => operand + operator  Several different categories of operators:  1. Arithmetic operators (+ - * / %)  2. Assignment operators (= += -= *= /= %=)  3.Increment and Decrement operators (++ --)  4. Relational operators (> >= < <= == !=)  5. Logical operators (&& || !)  6. Conditional operator (text expressing ? exp1 : exp2)  7. Comma operator (,)  8.Sizeof operator sizeof()  9. Bitwise operators (& | ~ << >> ^ )  10.Other operators
  • 30. Operator (Arithmetic + - * / %)  Arithmetic operators are used for numeric calculations. They are two types  1. Unary arithmetic operator  2. Binary arithmetic operator  Unary operators require only one operand (+x -y).  Binary operators require two operand. (operand )  % (modulus operator ) can not be applied with floating point operands.  Unary plus and unary minus operators are different from the addition and subtraction operators.  When both operands are intergers, the result value always an integer.  When one operand is interger type and the other is of float type, the resulting value is floating type.  The modulus operator % can’t be used with floating point numbers.
  • 31. /* Integer arithmetic operations */ #include<stdio.h> int main() { int a=17, b=4; printf(“Sum=%dn”, a+b ); printf(“Difference=%dn”, a-b ); printf(“Product=%dn”, a*b ); printf(“Quotient=%dn”, a/b ); printf(“Remainder=%dn”, a%b ); getch(); return 0; } When both operands are intergers, the result value always an integer.
  • 32. /* Program to understand the floating point arithmetic operation */ #include<stdio.h> int main() { float a=12.4, b=3.8; printf(“Sum=%fn”, a+b ); printf(“Difference=%fn”, a-b ); printf(“Product=%fn”, a*b ); printf(“Quotient=%fn”, a/b ); getch(); return 0; } The modulus operator % can’t be used with floating point numbers.
  • 33. Operator (Assignment = += -= *= /= )  A value can be stored in a variable using assignment operator.  The operand on the left hand side should be a variable, while the operand on the right hand side can be any variable, constant or expression. The value of right hand operand is assigned to the left hand operand. Here some example :  x = 8; s = x+y-2; x += 5; a*= b+5;  Invalid are such that  8 = x; 5 += x;
  • 34. Increment / Decrement Operators  The increment (++) and decrement (--) operators are unary operators because they operate on a single operand.  The increment operator increments the value of the variable by 1, while decrement operator decrements the value of the variable by 1.  These operators should be used only with variables ; they can’t be used with constants or expressions. For example ++5 or ++(x+y+z) are invalid.  These operators are of two types –  1.Prefix increment/decrement – operator is written before the operand (e.g. ++x or --x)  2.Post increment/decrement – operator is written after the operand (e.g. x++ or x--)
  • 35. Prefix Increment/Decrement operator  First the value of variable is increment/decrement then the new value is used in the operation.  The statement y = ++x; means first increment the value of x by 1, then assign the value of x to y.  x= x+1;  y = x;  Now value of x is 4 and value of y is also 4.  The statement y = --x; means first increment the value of x by 1, then assign the value of x to y.  x= x-1;  y = x;  Now value of x is 3 and value of y is also 3.
  • 36. /* Prefix increment /decrement */ #include<stdio.h> int main() { int x = 8; printf(“x=%dt”, x); printf(“x=%dt”, ++x); printf(“x=%dt”, x); printf(“x=%dt”, --x); printf(“x=%dn”, x); return 0; } Output : x = 8 x = 9 x = 9 x = 8 x = 8 In the second printf() statement , first the value of x is incremented and then printed; similarly in the fourth printf() first the value of x is decremented and then printed.
  • 37. Postfix Increment/Decrement operator  First the value of variable is used in the operation and then increment/decrement performed.  The statement y = x++; means first the value of x assigned to y and then x is incremented.  y = x;  x= x+1;  Now value of x is 4 and value of y is 3.  The statement y = x--; means first the value of x assigned to y and then x is decremented.  y = x;  x= x-1;  Now value of x is 3 and value of y is 4.
  • 38. /* Postfix increment /decrement */ #include<stdio.h> int main() { int x = 8; printf(“x=%dt”, x); printf(“x=%dt”, x++); printf(“x=%dt”, x); printf(“x=%dt”, x--); printf(“x=%dn”, x); return 0; } Output : x = 8 x = 8 x = 9 x = 9 x = 8 In the second printf() statement , first the value of x is printed and then incremented; similarly in the fourth printf() first the value of x is printed and then decremented.
  • 39. Relational Operators Relational operators are used to compare value of two expressions. An expression that contains relational operators is called relational expression. If the relation is true then the value of relational expression is 1 and if the relation is false then the value of expression is 0. The relational operators are – operator Meaning < less than <= less than equal to == equal to != Not equal to > Greater than >= Greater than or equal to Variable a = 9 and b = 5 Exopression Relation Value of Expression a<b False 0 a<=b False 0 a!=b True 1 a==b False 0 a>8 True 1 2>4 False 0
  • 40. /* Relational Operator */ #include<stdio.h> int main() { int a,b; printf(“Enter values for a and b :”); scanf(“%d%d”, &a, &b); if(a<b) printf(“%d is less than %dn”, a, b); if(a<=b) printf(“%d is less than or equal to %dn”, a, b); if(a==b) printf(“%d is equal to %dn”, a, b); if(a!=b) printf(“%d is not equal to %dn”, a, b); if(a>b) printf(“%d is greater than %dn”, a, b); if(a>=b) printf(“%d is greater than or equal to %dn”, a, b); return 0; }
  • 41. Logical Operators An expression that combines two or more expressions is termed as logical expression. For combining these expressions we use logical operators. These operators return 0 for false and 1 for true. The operand may be contants, variable or expressions. C has three logical operators. Operator Name && AND || OR ! NOT Here logical not is unirary operator while the other two are binary operatons. In C any non-zero value is regarded as true and zero is regarded as false.
  • 42. Logical AND (&&) AND(&&) Operator : This operator gives the net result true if both the condition are true , otherwise the result is false. Boolean Table Let us take three variable a = 10, b = 5, c = 0 Codition1 Condition2 Result False False False False True False True False False True True True Expression Boolean Result Expression value (a==10)&&(b>a) true && false False 0 (b>=a) &&(b==3) false&& false False 0 a && b true&& true True 1 a && c true&& false False 0
  • 43. Logical OR (||) OR (||) Operator : This operator gives the net result false, if both the conditions have the value false , otherwise the result is true. Boolean Table Let us take three variable a = 10, b = 5, c = 0 Codition1 Condition2 Result False False False False True True True False True True True True Expression Boolean Result Expression value a || b True || True True 1 a || c True || False True 1 (a<9)||(b>10) False && False False 0 (b!=7)|| c True && False True 1
  • 44. Logical NOT (!) Operator This is a unary operator and it negates the value of the condition. If the value of the condition is false then it gives the result true. If the value of the condition is true then it gives the result false. Boolean Table Let us take three variable a = 10 , b = 5 , c = 0 Condition Result False True True False Expression Result Expression Value !a !true false 0 !c !false true 1 !(b>c) !true false 0 !(a&&c) !false true 1
  • 45. Conditional Operators Conditional operator is a ternary operator (? and :) which requires three expressions as operands. It is written as – TestExpression ? expression1 : exprssion 2 Firstly the text expression is evaluated. (1) If Testexpression is true (nonzero), then expression1 is evaluated and it becomes the value of the overall conditional expression. (2) If Testexpression is false (nonzero), then expression2 is evaluated and it becomes the value of the overall conditional expression. For example consider the conditional expression – a>b ? a : b Suppose a = 5 and b = 8 then max = a>b ? a : b
  • 46. /* Conditional Operator */ #include<stdio.h> int main() { int a, b, max ; printf(“Enter values for a and b :”); scanf(“%d%d”, &a, &b); max = a>b ? a : b; printf(“Larger of %d and %d is %dn”, a, b, max); return 0; }
  • 47. Comma Operators The comma operator ( , ) is used to permit different expressions to appear in situations where only one expression would be used. The expression are separated by the comma operator. Example : sum = (a = 8 , b = 7 , c = 9 , a+b+c ); #include<stdio.h> int main() { int a, b, c, sum ; sum =(a = 8, b = 7, c = 9, a+b+c ); printf(“Sum = %dn”, sum); return 0; }
  • 48. Size of Operators Sizeof is an unary operator that gives the size of its operand in terms of bytes. The operand can be a variable, constant or any datatype (int, float, char etc.) For example sizeof (int) gives the bytes occupied by the int datatype. #include<stdio.h> int main() { int var; printf(“Size of int =%u n”, sizeof(int)); printf(“Size of float =%u n”, sizeof(float)); printf(“Size of var =%u n”, sizeof(var)); printf(“Size of an integer constant =%u n”, sizeof(45)); return 0; }
  • 49. Decision Making and Branching  Many situations arise where we may want to execute some statements several times.  Control statement enable us to specify the order in which the various instrucitons in the program are to be executed.  They define how the control is transferred to other parts of the program.  Different types of control statement in C  Control statements  Selection Iterative Jump  If else while break  Switch do..while continue  . for  COMPOUND STATEMENT : Compound statement or block is a group of statement enclosed within a pair of curly barces. {}. The statement inside a block are executed sequentially.  {  Statement 1;  Statement 1;  ………………..;  ………………..;  }
  • 50. If Statement  The if statement :- C uses the keyword if to implement the decision control instruction. The general form of if statement looks like this :  if (test expression)  {  Statement - block ;  }  The statement block may be a single statement or a group of statements. If the test expression is true, the statement block will be executed, otherwise the statement block will be skipped and the execution will jump to the next statement. Remember, when the condition is true both the statement block and the next statement are executed in sequence.
  • 52. IF-ELSE Statement  The if-else statement :- This is a bidirectional selection control statement which is used to take one of the two possible actions.  Syntax :  if (expression)  {  Statement - block 1;  }  else  { Statement-block2;  }  The expression inside the parantheses is evaluated and if it is true (has a non zero value) then statement1 is executed and if the expression is false (zero) then statement2 is executed  This expression is often called the if condition. Flow chart for if else : 
  • 53. Expression Next Statement FalseTrue Statement 2 Flow chart of if-else statement Statement 1
  • 54. /*Program to print the bigger number*/  #include<stdio.h>  int main()  {  int a,b; printf(“ Enter two numbers : ”); scanf(“%d%d”, &a, &b); if(a>b) { printf(“Bigger number = %dn”,a); } else { printf(“Bigger number = %dn”,b); } return 0;  }
  • 55. Decision Making and Looping  Loops are used when we want to execute a part of the program or a block of statements several times.  Suppose we want to print “C is best ” 10 times.  The way is write printf statement 10 times which is not preferable.  Other way is loop – using loop we can write one loop and one printf statement, and this approach is definitely better than the first one.  With the help of loop we can execute a part of the program repeatedly till some expression is true.  In C, the loops can be written using these three iterative statements.   while  do – while  for
  • 56. While Loop  The syntax of while statement is –  while(expression)  {  statement  statement  .…………….  }  First the expression is evaluated; if it is true then the stmnts in the body of loop are executed. After the execution, again the expression is evaluated and if it is found to be true then again the stmnts in the body of loop are executed.  This means that these statements are executed continuosly till the expression is true and when it becomes false, the loop terminates and the control comes out of the loop. Each execution of the loop body is known as iteration, and the expression is known as loop condition.
  • 57. Expression Body of Loop True False Next Statement out of loop Flow chart of while loop
  • 58. /* Print no. form 1 to 10 using While Loop */ #include<stdio.h> int main() { int i; while(i<=10) { printf(“%dt”, i); i = i + 1; } printf(“n”); return 0; } Output : 1 2 3 4 5 6 7 8 9 10
  • 59. /* Print no. in reverse order with a difference of 2*/ #include<stdio.h> int main() { int k=10; while(k>=2) { printf(“%dt”, k); k = k - 2; } printf(“n”); return 0; } Output : 10 8 6 4 2
  • 60. DO – while Statement  The syntax of while statement is –  do  {  statement  statement  .…………….  } while(expression);  Here first the loop body is executed and then the expression is evaluated. If the expression is true, then again the loop body is executed and this process continues as long as the expression is true. When the expression becomes false the loop terminates.
  • 61. Difference between while and do-while loop  In a while loop, first the expression is evaluated and then the loop body is executed whereas in a do-while loop, first the loop body is executed and then the expression is evaluated.  If intially the condition is false then in the case of while loop , the loop body will not execute at all while in the case of do-while loop, the loop body will execute one time.  So in a do-while loop, the loop body is guaranteed to execute at least once.
  • 62. Expression Body of Loop True False Next Statement out of loop Flow chart of do-while loop
  • 63. /* Print no. form 1 to 10 using do - while Loop */ #include<stdio.h> int main() { int i=1; do { printf(“%dt”, i); i = i + 1; } while(i<=10); printf(“n”); return 0; } Output : 1 2 3 4 5 6 7 8 9 10
  • 64. /* Program to find the sum of numbers entered */ #include<stdio.h> int main() { int num, sum=0; do { printf(“Enter a number (enter 0 to stop) :”); printf(“%d”, &num); sum = sum + num; } while(num!=0); printf(“Sum is %dn”, sum); return 0; }
  • 65. for Statement  The for statement has three expression, semicolons are used for separating these expressions.The syntax of for statement is –  for(expression1;expression2;expression3)  {  statement  statement  .…………….  }  Exp1 is an intialization expression, exp2 is a test expression or condition and exp3 is an update expression  Exp1 is executed only once when the loop starts and is used to intialize the loop variables. This exp is genearally an assignment expression.  Exp2 is condition and is tested before each iteration of the loop.This condition generally used relational and logical operators.  Exp3 is an update expression and is executed each time after the body of the loop is executed.
  • 66. Condition Intialization expression True False Next Statement out of loop Update expression Body of Loop Flow chart of for loop
  • 67. /* Print no. form 1 to 10 using for Loop */ #include<stdio.h> int main() { int i; for(i=1; i<=10 ; i++) { printf(“%dt”, i); } printf(“n”); return 0; } Output : 1 2 3 4 5 6 7 8 9 10
  • 68. /* Print no. in reverse order with a difference of 2 using for loop*/ #include<stdio.h> int main() { int k; for(k=10 ; k>=2 ; k-=2) { printf(“%dt”, k); } printf(“n”); return 0; } Output : 10 8 6 4 2
  • 69. Jumps in Loop(Break , Continue and goto)  Jumps statements:  break  continue  goto
  • 70. Break  break statement is used inside loops and switch statements. Sometimes it becomes necessary to come out of the loop even before the loop condition becomes false. In such a situation, break statement is used to terminate the loop. This statement causes an immediate exit from that loop in which this statement appears. It can be written as –  break;  When break statement is encountered, loop is terminated and the control is transferred to the statement immediately after the loop. The break statement is generally written along with a condition. If break is written inside a nested loop structure then it causes exit from the innermost loop.
  • 71. #include<stdio.h> int main() { int n; for(n=1;n<=5;n++) { if(n==3) break; printf(“Number = %dn”, n); } printf(“Out of for loop n”); return 0; } Output Number = 1 Number = 2 Out of for loop
  • 72. Continue  The continue statement is used when we want to go the next iteration of the loop after skipping some statements of the loop. The continue statement can be written simply as –  It is generally used with a condition. When continue statement is encountered all the remaining statements (statements after continue) in the current iteration are not executed and the loop continue with the next iteration.
  • 73. #include<stdio.h> int main() { int n; for(n=1;n<=5;n++) { if(n==3) continue; printf(“Number = %dn”, n); } printf(“Out of for loop n”); return 0; } Output : Number = 1 Number = 2 Number = 4 Number = 5 Out of loop
  • 74. Difference between break and continue  When break is encountered the loop terminates and the control is transferred to the next statement following the loop  BUT when a continue statement encountered the loop is not terminated and the next iteration of the loop starts.
  • 75. GO-TO Statement  This is an unconditional control statement that transfers the flow of control to another part of the program. The goto statement can be used as – goto label; ……………. ……………. label : statement ……………. …………….
  • 76. #include<stdio.h> int main() { int n; printf(“Enter a number : ”); scanf(“%d”, &n); if(n%2==0) goto even; else goto odd; even : printf(“Number is even n”); goto end; odd : printf(“Number is odd n”); goto end; printf(“n”); return 0; }
  • 77. Goto Label is any valid C identifier and it is followed by a colon. The label can be placed anywherer. If the label is after goto then the control is transferrred forward and it is known as forward jump or forward goto, and if the label is before goto then the control is transferred backward and it is known as backward jump or backward goto. In forward goto, the statements between goto and label will not be executed and in backward goto statements between goto and label will be executed repeatedly. More than one goto can be assoiciated with the same label but we can not have same label at more than one place.