SlideShare a Scribd company logo
1 of 383
Unit 1
2016
• What are the basic data types and their
associated range? (2.5 marks)
2015
What do you mean by the term data type?
Explain any four data types with examples.(2.5
marks)
Data Types in C
• Each variable in C has an associated data type.
• Each data type requires different amounts of
memory.
• Following are the examples of some very
common data types used in C:
• char: The most basic data type in C. It stores a
single character.
• int: As the name suggests, an int variable is
used to store an integer.
• float: It is used to store decimal numbers
(numbers with floating point value) with single
precision.
• double: It is used to store decimal numbers
(numbers with floating point value) with
double precision.
• Different data types also have different
ranges upto which they can store numbers.
• These ranges may vary from compiler to
compiler.
• Below is list of ranges along with the memory
requirement and format specifiers on 32 bit
gcc compiler.
• We can use the sizeof() operator to check the
size of a variable.
• See the following C program for the usage of
the various data types:
Table of Data type and ranges
2’s complement form
• For signed Numbers : (-2^(n-1)) to (2 ^(n-1)-1)
For example Range for singed char :
n=8 bits
(-2^(8-1)) to (2^(8-1)-1)
(-2^(7)) to (2^(7)-1)
-128 to 127
• For unsigned Numbers: (0 to (2^n)-1)
For example Range for unsigned char
0 to ((2^8)-1)
0 to 256-1
0 to 255
output
2017
What are keywords in C? Give examples (2.5
marks)
Keywords and Variables
• Keywords are specific reserved words in C
each of which has a specific feature associated
with it.
Variables
• A variable in simple terms is a storage place
which has some memory allocated to it.
• Basically, a variable used to store some form
of data.
• Different types of variables require different
amounts of memory
• Example :
• Char c; c is a variable which takes 2 bytes in
memory
Variable Declaration Rules in C
• Every variable name should start with alphabets
or underscore (_).
• No spaces are allowed in variable declaration.
• Except underscore (_) no other special symbol are
allowed in the middle of the variable declaration
• Maximum length of variable is 8 characters
depend on compiler and operation system.
• Every variable name always should exist in the
left hand side of assignment operator
(invalid -> 10=a; valid -> a=10;).
• Variable Declaration:
A typical variable declaration is of the form:
• type variable_name;
or for multiple variables:
• type variable1_name, variable2_name,
variable3_name;
• A variable name can consist of alphabets
(both upper and lower case), numbers and the
underscore ‘_’ character.
• However, the name must not start with a
number.
output
• Difference b/w variable declaration and
definition
Variable declaration refers to the part where
a variable is first declared or introduced
before its first use.
• Variable definition is the part where the
variable is assigned a memory location and a
value.
• Most of the times, variable declaration and
definition are done together.
Note
• Is it possible to have separate declaration and
definition?
It is possible in case of extern variables
2019
• Write a C program to test weather a given
integer is a prime number or not. Display an
appropriate message in the output(5.5
marks)
output
2015
• Write a program to swap two numbers
without using third variable.(2.5 marks)
Arrays in C
• An array is a collection of items stored at
contiguous memory locations and elements
can be accessed randomly using index's of an
array.
• They are used to store similar type of
elements as in the data type must be the
same for all elements.
• Given below is the picturesque representation
of an array.
2017
What is an array? Write a program to print
average of all elements of an array.(6.5 marks)
OUTPUT
Constants
Constants in C
• As the name suggests the name constants is
given to such variables or values in C
programming language which cannot be
modified once they are defined.
• They are fixed values in a program.
• There can be any types of constants like
integer, float, octal, character constants etc.
Defining Constants:
In C/C++ program we can define constants in
two ways as shown below:
1. Using #define preprocessor directive
2. Using a const keyword
• Let us now learn about above two ways in
details:
1. Using #define preprocessor directive:
• We can use this to declare a constant as
shown below:
#define identifierName value
– identifierName: It is the name given to constant.
– value: This refers to any value assigned to
identifierName.
output
2. using a const keyword:
The qualifier const can be applied to the
declaration of any variable to specify that its
value will not be changed
Declaration and definition of var variable in same line
#include<conio.h>
#include<stdio.h>
OUTPUT
2015
• What is Enumerated Data type? Explain with
suitable example.(2.5 marks)
Enumeration (or enum) in C
• Enumeration (or enum) is a user defined data
type in C.
• It is mainly used to assign names to integral
constants, the names make a program easy to
read and maintain.
OUTPUT
2017
What are different type of operators in c?
Briefly explain. (6 marks)
2016
Define operators. What are the different types
of operators (2.5 marks)
2019
Discuss the various operators in C. (5 marks)
• Operators allow us to perform different kinds
of operations on operands.
C Programming
Lecture 4
Operators
Part 1
In C, operators can be categorized in following
categories:
1. Arithmetic Operators
Binary operators : (+, -, *, /, %)
Unary Operators : (post-increment, pre-
increment, post-decrement, pre-decrement)
(Arithmetic Operators)
• The binary operators falling in this category
are:
– Addition: The ‘+’ operator adds two operands.
For example, x+y.
– Subtraction: The ‘-‘ operator subtracts two
operands.
For example, x-y.
– Multiplication: The ‘*’ operator multiplies two
operands.
For example, x*y.
– Division: The ‘/’ operator divides the first operand
by the second.
For example, x/y.
– Modulus: The ‘%’ operator returns the remainder
when first operand is divided by the second.
For example, x%y.
Example of Binary Operators
END
C Programming
Lecture 5
Modulo operator restriction
Restrictions of the modulo operator
• The % operator cannot be applied to floating-
point numbers i.e float or double.
• If you try to use the modulo operator with
floating-point constants or variables, the
compiler will produce a error:
END
C Programming
Lecture 6
Operators
part 2
In C, operators can be categorized in following
categories:
1. Arithmetic Operators
Binary operators : (+, -, *, /, %)
Unary Operators : (post-increment, pre-
increment, post-decrement, pre-decrement)
(Arithmetic Operators)
• The ones falling into the category of unary
arithmetic operators are:
• Increment: The ‘++’ operator is used to
increment the value of an integer.
• When placed before the variable name (also
called pre-increment operator), its value is
incremented instantly.
• For example, ++x.
And when it is placed after the variable name
(also called post-increment operator), its
value is preserved temporarily until the
execution of this statement and it gets
updated before the execution of the next
statement. For example, x++.
• Decrement: The ‘ – – ‘ operator is used to
decrement the value of an integer.
• When placed before the variable name (also
called pre-decrement operator), its value is
decremented instantly.
• For example, – – x.
And when it is placed after the variable name
(also called post-decrement operator), its
value is preserved temporarily until the
execution of this statement and it gets
updated before the execution of the next
statement.
• For example, x – –.
OUTPUT
END
Relational operator
Relational Operators
Relational operators
• Relational operators are used for comparison
of two values.
For example,
less than, greater than, equal to etc.
• Let’s see them one by one
1. Equal to operator:
Represented as ‘==’, the equal to operator
checks whether the two given operands are
equal or not.
If so, it returns true.
Otherwise it returns false.
For example,
1. 5==5 will return true.
2. 6==5 will return false.
2. Not equal to operator:
Represented as ‘!=’, the not equal to operator
checks whether the two given operands are
equal or not.
If not, it returns true.
Otherwise it returns false.
It is the exact boolean complement of
the ‘==’ operator.
For example,
1. 5!=5 will return false.
2. 6!=5 will return true
3. Less than operator:
Represented as ‘<‘, the less than operator
checks whether the first operand is lesser than
the second operand.
If so, it returns true.
Otherwise it returns false.
For example,
1. 5<5 will return false.
2. 5<6 will return true.
4. Less than or equal to operator:
Represented as ‘<=’, the less than or equal
tooperator checks whether the first operand is
less than or equal to the second operand.
• If so, it returns true
• else false.
For example,
1. 2<=5 will return true
2. 6<=5 will return false.
3. 5<=5 will return true.
5. Greater than operator:
• Represented as ‘>’, the greater than operator
checks whether the first operand is greater
than the second operand or not.
• If so, it returns true.
• Otherwise it returns false.
For example,
1. 6>5 will return true
2. 5>5 will return false
6. Greater than or equal to operator:
Represented as ‘>=’, the greater than or equal to
operator checks whether the first operand is
greater than or equal to the second operand.
• If so, it returns true
• else it returns false.
For example,
7>=5 will return true.
6>=8 will return false.
5>=5 will return true.
OUTPUT
Operators
Logical Operators
1. Logical AND operator:
• The ‘&&’ operator returns true when both the
conditions under consideration are satisfied.
Otherwise it returns false.
• For example, a && b returns true when both a
and b are true (i.e. non-zero).
A B A AND B
0 0 0
0 1 0
1 0 0
1 1 1
Using 2 variables we can have 4 combinations
2. Logical OR operator:
The ‘||’ operator returns true even if one (or
both) of the conditions under consideration is
satisfied. Otherwise it returns false.
For example, a || b returns true if one of a or b
or both are true (i.e. non-zero).
Of course, it returns true when both a and b are
true.
A B A OR B
0 0 0
0 1 1
1 0 1
1 1 1
Using 2 variables we can have 4 combinations
3. Logical NOT operator:
• Logical NOT is denoted by exclamatory
characters (!), it is used to check the opposite
result of any given test condition.
• If any condition's result is non-zero (true), it
returns 0 (false) and if any condition's result is
0(false) it returns 1 (true).
A !A
0 1
1 0
Using 1 variables we can have 2 combinations
output
END
Assignment Operator
Assignment Operators in C
• Assignment operators are used to assign
value to a variable.
• The left side operand of the assignment
operator is a variable and right side operand
of the assignment operator is a value.
• For example :
int a; // variable a is declare and define
a= 5; // a is assigned value 5.
• Different types of assignment operators are
shown below:
1. “=”:
This is the simplest assignment operator.
This operator is used to assign the value on the
right to the variable on the left.
For example :
int a; a = 10;
2. “+=”:
• This operator is combination of ‘+’ and ‘=’
operators.
+= is known as compound assignment.
• This operator first adds the current value of
the variable on left to the value on the right
and then assigns the result to the variable on
the left.
For Example
(a += b) can be written as (a = a + b)
If initially value stored in a is 5 and b is 6
Then (a += 6) OR (a=a+6)
a=5+6=11
3. “-=”
• This operator is combination of ‘-‘ and ‘=’
operators
-= is known as compound assignment
• This operator first subtracts the current value
of the variable on left from the value on the
right and then assigns the result to the
variable on the left.
For Example:
(a -= b) can be written as (a = a - b)
If initially value stored in a is 8 and b is 6
Then (a -= 6) OR a=a-6
a=8-6 = 2
4. “*=”
This operator is combination of ‘*’ and ‘=’
operators
*= is known as compound assignment
• This operator first multiplies the current
value of the variable on left to the value on
the right and then assigns the result to the
variable on the left.
Example:
• (a *= b) can be written as (a = a * b)
• If initially value stored in a is 5 and b is 6
• Then (a *= 6) = 30.
5. “/=”
• This operator is combination of ‘/’ and ‘=’
operators
/= is known as compound assignment
• This operator first divides the current value
of the variable on left by the value on the
right and then assigns the result to the
variable on the left.
Example:
• (a /= b) can be written as (a = a / b)
• If initially value stored in a is 6 and b is 2
• Then (a /= 2) a=(6/2)=3.
6. “%=”
• This operator is combination of ‘%’ and ‘=’
operators
%= is known as compound assignment
• This operator first modulus the current value
of the variable on left by the value on the
right and then assigns the result to the
variable on the left.
Example:
• (a %= b) can be written as (a = a % b)
• If initially value stored in a is 6 and b is 2
• Then (a %= 2) a=(6%2)=0.
OUTPUT
Assignment Operator
(Modulus OPERATOR)
“%=”
• This operator is combination of ‘%’ and ‘=’
operators
%= is known as compound assignment
• This operator first modulus the current value
of the variable on left by the value on the
right and then assigns the result to the
variable on the left.
Example:
• (a %= b) can be written as (a = a % b)
• If initially value stored in a is 6 and b is 2
• Then (a %= 2) a=(6%2)=0.
2017
• Explain bitwise operators available in C. (2.5
marks
2016
what are bitwise operators?( 2.5 marks)
C Programming
Lecture 8
Operators
part 4
Bitwise Operators in C
• The & (bitwise AND) in C takes two numbers
as operands and does AND on every bit of
two numbers. The result of AND is 1 only if
both bits are 1.
Example :
2 in binary 1 0
3 in binary 1 1
2&3 ____________________________
1 0 = 2
_____________________________
LSB
MSB
A B A AND B
0 0 0
0 1 0
1 0 0
1 1 1
Truth table
Bitwise Operators in C
• The | (bitwise OR) in C takes two numbers as
operands and does OR on every bit of two
numbers.
• The result of OR is 1 if any of the two bits is 1.
Example :
2 in binary = 1 0
3 in binary = 1 1
2 | 3 ____________________
1 1 = 3
______________________
LSB
MSB
A B A OR B
0 0 0
0 1 1
1 0 1
1 1 1
Truth table
Bitwise Operators in C
• The ^ (bitwise XOR) in C takes two numbers as
operands and does XOR on every bit of two
numbers. The result of XOR is 1 if the two bits
are different.
Example :
2 in binary = 1 0
3 in binary = 1 1
2^3 ___________________
0 1 = 1
____________________
A B A XOR B
0 0 0
0 1 1
1 0 1
1 1 0
MSB LSB
Truth table
Note:
When both the operand
are same we get 0
Otherwise 1
output
Bitwise Operators
1. Bitwise Left Shift Operator <<
2. Bitwise Right Shift Operator >>
3. Bitwise One's Complement ~
Bitwise Operators in C
Bitwise Operators in C
• The << (left shift) in C takes two numbers.
• left shift operator shifts the bits of the first
operand and the second operand decides the
number of places to shift.
Example :
• First Operand : 8
8 in binary = 1 0 0 0
• Second Operand : 3 ( so shift the operand by 3
bits)
=Shift 8 by 3 bits left = 8<<3
= 8*(2 pow 3)
=8*8
=64 in binary = 1 0 0 0 0 0 0
Short cut trick is append
3 0’s in right side of first
operand
• The >> (right shift) in C takes two numbers.
• right shift operator shifts the bits of the first
operand and the second operand decides the
number of places to shift.
Example :
• First Operand : 8
8 in binary = 1 0 0 0
• Second Operand : 3 (so shift the operand by 3
bits)
=Shift 8 by 3 bits right = 8>>3
= 8/(2 pow 3)
=8/8
=1 in binary = 0 0 0 1
Bitwise Operators in C
• The ~ (bitwise 1’s complement) in C takes
one number and inverts all bits of it
Example :
Number 8 in binary 0 1 0 0 0
1’s complement 1 0 1 1 1
of 8 is (-8)
-8 in binary = 10111
Negative numbers are stored in 2’s
complement form :
• Short trick to calculate 1’s complement :
~n=-(n+1)
• n=8 = - (8+1) = -9
MSB bit is 0 means number is
positive
MSB bit is 1 means number is
negative
output
Ternary Operator
Conditional or Ternary Operator (?:)
Working:
Here, Expression1 is the condition to be evaluated.
If the condition(Expression1) is True then Expression2 will be
executed and the result will be returned.
Otherwise, if the condition(Expression1) is false
then Expression3 will be executed and the result will be
returned.
Program to find MAX of 2 numbers
output
output
Decision Making in C (if , if..else,
Nested if, if-else-if )
if statement in C
• if statement is the most simple decision
making statement.
• It is used to decide whether a certain
statement or block of statements will be
executed or not i.e
• if a certain condition is true then a block of
statement is executed otherwise not.
• Here, condition after evaluation will be either
true or false.
• if statement accepts boolean values – if the value
is true then it will execute the block of statements
below it otherwise not.
• If we do not provide the curly braces ‘{‘ and ‘}’
after if(condition) then by default if statement
will consider the first immediately below
statement to be inside its block.
if-else in C
• The if statement alone tells us that if a
condition is true it will execute a block of
statements and if the condition is false it
won’t.
• But what if we want to do something else if
the condition is false.
• Here comes the C else statement.
• We can use the else statement
with if statement to execute a block of code
when the condition is false.
output
2019
• Explain the if..else statement with all its
variations. Compare it with switch case.
Write a c program that demonstrate the
difference between the two.(7 marks)
if-else-if ladder in C
• Here, a user can decide among multiple
options.
• The C if statements are executed from the top
down.
• As soon as one of the conditions controlling
the if is true, the statement associated with
that if is executed, and the rest of the C else-if
ladder is bypassed.
• If none of the conditions are true, then the
final else statement will be executed.
output
output
• What are nested control statements? Give
examples(2.5 marks)
nested-if in C
• A nested if in C is an if statement that is the
target of another if statement.
• Nested if statements means an if statement
inside another if statement.
• Yes, both C and C++ allows us to nested if
statements within if statements, i.e, we can
place an if statement inside another if
statement.
Jump Statements in C/C++
• These statements are used in C orC++ for
unconditional flow of control through out the
funtions in a program.
• They support four type of jump statements:
2017
What is the function of break and continue
statement in C? (2.5 marks)
2016
Differentiate between break and continue.
• C break:
• This loop control statement is used to terminate
the loop.
• As soon as the break statement is encountered
from within a loop, the loop iterations stops
there and control returns from the loop
immediately to the first statement after the loop.
Syntax:
break;
• Basically break statements are used in the
situations when we are not sure about the actual
number of iterations for the loop or we want to
terminate the loop based on some condition.
2. C continue: This loop control statement is just
like the break statement.
The continue statement is opposite to that of
break statement, instead of terminating the
loop, it forces to execute the next iteration of
the loop.
As the name suggest the continue statement
forces the loop to continue or execute the next
iteration.
When the continue statement is executed in the
loop, the code inside the loop following the
continue statement will be skipped and next
iteration of the loop will begin.
OUTPUT
The GOTO STATEMENT
• A goto statement 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 in next slide
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 jump is known as a backward jump
• On the other hand, if the label : is placed after
goto label; some statements will be skipped
and the jump is known as a forward jump.
2016
• Why does goto statement is not preferred in
c programming (2.5 marks)
Why Goto should be avoided
Due to the unconditional goto statement at the
end, the control is always transferred back to
the input statement.
In fact, program puts the computer in a
permanent loop known as an infinite loop.
The computer goes round and round until we
take some special steps to terminate the loop.
Such infinite loops should be avoided.
No break condition from loop using goto
break condition from loop using goto
2015
• Difference between break and exit
statements.(2.5 marks)
output
• In C, exit() terminates the calling process
without executing the rest code which is after
the exit() function.
output
0 1 2
• C return:
• The return in C or C++ returns the flow of the
execution to the function from where it is
called.
• This statement does not mandatorily need any
conditional statements.
• As soon as the statement is executed, the
flow of the program stops immediately and
return the control from where it was called.
• The return statement may or may not return
anything for a void function, but for a non-
void function, a return value is must be
returned.
Syntax: return[expression];
output
2017
• Explain loop statements in c. (6.5 marks)
2016
• List the different types of loop control
statements and explain them with suitable
examples( 6 marks)
2019
• Explain the different loop statements in c
through suitable examples.(5 marks)
Types of C Loops
• There are three types of loops in C
language that is given below:
1. do while
2. while
3. for
do-while loop in C
• The do-while loop continues until a given
condition satisfies.
• It is also called post tested loop.
• It is used when it is necessary to execute the
loop at least once (mostly menu driven
programs).
do-while loop Syntax
C Programming
Lecture 14
Decision Making
using IF…ELSE statement
Decision making using if-else
statement
• In if statement if a condition is true it will
execute a block of statements.
• But what if we want to execute the statements
if the condition is false.
• So we require else statement.
Syntax of if-else statement
OUTPUT
END
output
END
output
END
C Programming
Lecture 15
Decision Making
using Nested-IF statement
• Nested if statement is If statement inside
another if statement.
Decision making using Nested if statement
Syntax of nested-if statement
OUTPUT
END
C Programming
Lecture 16
Decision Making
using if-else-if ladder
• In if-else-if ladder statement, if a condition is
true then the statements defined in the if
block will be executed.
• otherwise if some other condition is true then
the statements defined in the else-if block will
be executed.
• at the last if none of the condition is true then
the statements defined in the else block will
be executed.
• There are multiple else-if blocks possible.
Syntax of if-else-if ladder
output
END
C Programming
Lecture 17
Decision Making
using Switch Statement
• The switch statement is a multiway branch
statement.
• It provides an easy way to dispatch execution
to different parts of code based on the value
of the expression.
• Switch is a control statement that allows a
value to change control of execution
Rules for switch statement
1) The switch expression must be of an integer
or character type.
2) The case value must be an integer or
character constant.
3) The case value can be used only inside the
switch statement.
4) The break statement in switch case is
optional.
• If there is no break statement found in the
case, all the cases will be executed present
after the matched case.
5) The default statement is executed if no case
constant-expression value is equal to the value
of expression
Syntax of Switch Statement
output
END
C Programming
Lecture 18
Decision Making
using JUMP Statement
• The goto statement is known as jump
statement.
• As the name suggests, goto is used to transfer
the program control to a predefined label.
• The goto statment can be used to repeat some
part of the code for a particular condition.
• Goto is known as unconditional Jump
Syntax of goto statement
Implementation of GOTO Statement
Statements are
ignored
Implementation of GOTO Statement
Statements are
repeated
END
C Programming
Lecture 18
Decision Making
using JUMP Statement
C Programming
Lecture 19
Decision Making
using JUMP Statement
Part 2
Jump Statements
1. goto
2. break
3. continue
4. return
• The break is a keyword in C which is used to
bring the program control out of the loop.
• The break statement is used inside loops or
switch statement.
Break statement
Syntax of break statement
output
END
C Programming
Lecture 20
Decision Making
using JUMP Statement
Part 3
Jump Statements
1. goto
2. break
3. continue
4. return
• The continue statement brings the program
control to the starting of the loop.
• The continue statement skips some lines of
code inside the loop and continues with the
next iteration.
• It is mainly used for a condition so that we can
skip some code for a particular condition.
Syntax of continue
output
END
C Programming
Lecture 21
Introduction to
Decision Making
using looping
Part 1
• Loops in programming come into use when
we need to repeatedly execute a block of
statements.
• For example:
• Suppose we want to print “Hello World” 10
times.
• This can be done in two ways as shown below
1. Iterative Method
2. Using Loops
1. Iterative Method
• An iterative method can be used to write the
printf() statement 10 times.
output
2. Using Loops
• In Loop, the statement needs to be written
only once and the loop will be executed n
times
• In our case printf(“Hellon”) is written only
ones and Hello is printed 10 times using loop.
• A loop is a sequence of instructions that is
repeated until a certain condition is reached.
There are mainly two types of loops:
1. Entry Controlled loops: In this type of loops
the test condition is tested before entering
the loop body.
• For Loop and While Loop are entry controlled
loops.
2. Exit Controlled Loops: In this type of loops
the test condition is tested at the end of loop
body.
• Therefore, the loop body will execute atleast
once, irrespective of whether the test
condition is true or false.
• do – while loop is exit controlled loop.
1. for Loop
• A for loop is a repetition control structure
which allows us to write a loop that is
executed for a specific number of times.
Syntax of for loop
1 2
3
4
output
END
C Programming
Lecture 22
Decision Making
using looping
Part 2
Types of C Loops
• There are three types of loops in C
language that is given below:
1. for
2. do-while
3. while
2. do-while loop in C
• The do-while loop continues until a given
condition satisfies.
• It is used when it is necessary to execute the
loop at least once
• do-while loop is a exit controlled Loop.
Syntax of do-while loop
output
END
C Programming
Lecture 23
Decision Making
using looping
Part 3
Types of C Loops
• There are three types of loops in C
language that is given below:
1. for
2. do-while
3. while
Syntax of while loop
Program 1
output
Program 2
Program 3
END
EXAMPLE
Initialization
Test Expression
Update Expression
OUTPUT
C Programming
Lecture 24
Infinite Loop in C
What is infinite loop?
• An infinite loop is a looping construct that
does not terminate the loop and executes the
loop forever.
• It is also called an indefinite loop or
an endless loop.
• We can create an infinite loop using following
ways:
1. for loop
2. while loop
3. do-while loop
4. go to statement
5. C macros
1. For loop
all the parts of the 'for' loop are optional,
and in the above for loop, we have not
mentioned any condition; so, this loop will
execute infinite times.
Syntax
output
2. while loop
output
3. do..while loop
output
4. goto statement
output
5. Macros
• We can also create the infinite loop with the
help of a macro constant.
• Let's understand through an example.
output
END
C Programming
Lecture 24
Type Conversion in C
• A type cast is basically a conversion from one
type to another.
• There are two types of type conversion:
1. Implicit Type Conversion
2. Explicit Type Conversion
• Also known as ‘automatic type conversion’.
• Done by the compiler on its own.
• All the data types of the variables are
upgraded to the data type of the variable with
largest data type.
1. Implicit Type Conversion
output
2. Explicit Type Conversion
• This process is also called type casting and it is
user defined.
• Here the user can type cast the result to make
it of a particular data type.
• Data can be lost when explicit conversion is
done.
Lower
data type
Higher data
type
Syntax of Explicit Type Conversion
Program without explicit typecasting
output
Program with explicit typecasting
output
END
C Programming
Lecture 25
Test -1
Question 1
Solution
• In program "Expression syntax" error occur
because the while() loop must have
conditional expression.
Question 2
• The break statement is used to take control
out of switch and continue statement is used
to take control of the beginning of the switch?
• The above statement is true or false
Solution
• No, because continue statement can work
only with loops in C-programming and not
with switch.
Question 3
A character variable can store ___ character(s)
at a time.
Solution
• A character variable can at a time store only
one character. In fact, if we execute the
following statements, what gets stored in
variable ch is not really the character constant,
but the ASCII value of 'A', which is 65.
Question 4
The C variables are case insensitive?
SOLUTION
• The C variables are case sensitive.
• This means that variables abc, Abc and ABC
would be treated as different variables in C.
END
C Programming
Lecture 25
Functions in C
• A function is a set of statements that take
inputs, do some specific computation and
produces output.
• The idea is to put some commonly or
repeatedly done task together and make a
function so that instead of writing the same
code again and again for different inputs, we
can call the function.
syntax of creating function
Types of Functions
• There are two types of functions in C
programming:
• Library Functions: are the functions which are
declared in the C header files such as scanf(),
printf(), gets(), puts(), ceil(), floor() etc.
• User-defined functions: are the functions
which are created by the C programmer, so
that he/she can use it many times. It reduces
the complexity of a big program and optimizes
the code.
Different aspects of function calling
1. function without arguments and without
return value
2. function without arguments and with return
value
3. function with arguments and without return
value
4. function with arguments and with return
value
for Loop
• A for loop is a repetition control structure
which allows us to write a loop that is
executed a specific number of times.
• The loop enables us to perform n number of
steps together in one line.
• In for loop, a loop variable is used to control
the loop.
• Initialization Expression: In this expression we
have to initialize the loop counter to some
value.
• For example: int i=1;
• Test Expression: In this expression we have to
test the condition.
• If the condition evaluates to true then we will
execute the body of loop and go to update
expression otherwise we will exit from the for
loop.
For example: i <= 10;
• Update Expression:
• After executing loop body this expression
increments/decrements the loop variable by
some value.
• For example: i++;
2018
Explain while loop and do-while loop with an
example. Elaborate difference between them(5
marks).
2016
Differentiate between entry control loop and
exit control loop.(2.5 marks)
while loop
• A while loop is a control flow statement that
allows code to be executed repeatedly based on
a given Boolean condition.
• The while loop can be thought of as a repeating if
statement. and therefore is an example of Entry
Control Loop.
Syntax :
while (boolean condition)
{
loop statements...
}
do-while loop
• do while loop is similar to while loop with the
only difference that it checks for the condition
after executing the statements, and therefore is
an example of Exit Control Loop.
Syntax:
do
{
statements..
}
while (condition);
2015
• Write a program to display first n prime
numbers(6 marks)
output
Switch Statement in C
• Switch case statements are a substitute for
long if statements that compare a variable to
several integral values
• The switch statement is a multiway branch
statement.
• It provides an easy way to dispatch execution
to different parts of code based on the value
of the expression.
• Switch is a control statement that allows a
value to change control of execution.
Important Points about Switch Case
Statements:
1. The expression provided in the switch should
result in a constant value otherwise it would
not be valid.
Valid expressions for switch:
1. Duplicate case values are not allowed.
• The default statement is optional. Even if the
switch case statement do not have a default
statement,
it would run without any problem.
2. The break statement is used inside the switch to
terminate a statement sequence.
• When a break statement is reached, the switch
terminates, and the flow of control jumps to the
next line following the switch statement.
3. The break statement is optional. If omitted,
execution will continue on into the next case.
• The flow of control will fall through to
subsequent cases until a break is reached.
4. Nesting of switch statements are allowed,
which means you can have switch statements
inside another switch.
However nested switch statements should be
avoided as it makes program more complex and
less readable.
output
2018
Explain the switch statement, break statement
and continue statement with an example (6
marks)
OUTPUT
OUTPUT
2016
Write a program to display the following
pattern as the output (6.5 marks)
OUTPUT
2015
Explain the use of continue statements?
2016
• WAP to reverse the given integer number(6.5
marks)
OUTPUT
2019
What do you understand by type casting? Why
is it required? Explain using an example. (6
marks)
Type Conversion in C
• A type cast is basically a conversion from one
type to another.
• There are two types of type conversion:
1. Implicit Type Conversion
• Also known as ‘automatic type conversion’.
• Done by the compiler on its own, without any
external trigger from the user.
• Generally takes place when in an expression
more than one data type is present.
• In such condition type conversion (type
promotion) takes place to avoid lose of data.
• All the data types of the variables are
upgraded to the data type of the variable
with largest data type.
• bool -> char -> short int -> int -> unsigned int
-> long -> unsigned -> long long -> float ->
double -> long double
Example of Type Implicit Conversion:
output
2. Explicit Type Conversion
• This process is also called type casting and it is
user defined.
• Here the user can type cast the result to make
it of a particular data type.
The syntax in C:
(type) expression
OUTPUT
Advantages of Type Conversion
• This is done to take advantage of certain
features of type hierarchies or type
representations.
• It helps us to compute expressions containing
variables of different data types.
2018
Write a program to find largest number among
the three numbers(8 marks)
Comma in C
1) Comma as an operator:
The comma operator (represented by the token,
) is a binary operator that evaluates its first
operand and discards the result, it then
evaluates the second operand and returns this
value (and type).
• The comma operator has the lowest
precedence of any C operator, and acts as
a sequence point.
C Language Part 1
C Language Part 1
C Language Part 1

More Related Content

Similar to C Language Part 1

Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...Blue Elephant Consulting
 
Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Blue Elephant Consulting
 
C sharp part 001
C sharp part 001C sharp part 001
C sharp part 001Ralph Weber
 
c_programming.pdf
c_programming.pdfc_programming.pdf
c_programming.pdfHome
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingEric Chou
 
M.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageM.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageDr.Florence Dayana
 
Introduction to c
Introduction to cIntroduction to c
Introduction to cAjeet Kumar
 
Lamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ckLamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ckseidounsemel
 
B.sc CSIT 2nd semester C++ Unit2
B.sc CSIT  2nd semester C++ Unit2B.sc CSIT  2nd semester C++ Unit2
B.sc CSIT 2nd semester C++ Unit2Tekendra Nath Yogi
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Rasan Samarasinghe
 
Cpu-fundamental of C
Cpu-fundamental of CCpu-fundamental of C
Cpu-fundamental of CSuchit Patel
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt8759000398
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)sachindane
 

Similar to C Language Part 1 (20)

Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
 
Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...
 
C sharp part 001
C sharp part 001C sharp part 001
C sharp part 001
 
c_programming.pdf
c_programming.pdfc_programming.pdf
c_programming.pdf
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary Programming
 
M.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageM.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C Language
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
Lamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ckLamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ck
 
C program
C programC program
C program
 
Chapter 2.datatypes and operators
Chapter 2.datatypes and operatorsChapter 2.datatypes and operators
Chapter 2.datatypes and operators
 
B.sc CSIT 2nd semester C++ Unit2
B.sc CSIT  2nd semester C++ Unit2B.sc CSIT  2nd semester C++ Unit2
B.sc CSIT 2nd semester C++ Unit2
 
lec 2.pptx
lec 2.pptxlec 2.pptx
lec 2.pptx
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
 
c-programming
c-programmingc-programming
c-programming
 
C basics
C basicsC basics
C basics
 
C basics
C basicsC basics
C basics
 
Cpu-fundamental of C
Cpu-fundamental of CCpu-fundamental of C
Cpu-fundamental of C
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)
 

More from Thapar Institute

More from Thapar Institute (19)

Digital Electronics Unit_4_new.pptx
Digital Electronics Unit_4_new.pptxDigital Electronics Unit_4_new.pptx
Digital Electronics Unit_4_new.pptx
 
Digital Electronics Unit_3.pptx
Digital Electronics Unit_3.pptxDigital Electronics Unit_3.pptx
Digital Electronics Unit_3.pptx
 
Digital Electronics Unit_2.pptx
Digital Electronics Unit_2.pptxDigital Electronics Unit_2.pptx
Digital Electronics Unit_2.pptx
 
Digital Electronics Unit_1.pptx
Digital Electronics Unit_1.pptxDigital Electronics Unit_1.pptx
Digital Electronics Unit_1.pptx
 
Compiler Design Introduction
Compiler Design Introduction Compiler Design Introduction
Compiler Design Introduction
 
Web Technology Part 4
Web Technology Part 4Web Technology Part 4
Web Technology Part 4
 
Web Technology Part 3
Web Technology Part 3Web Technology Part 3
Web Technology Part 3
 
Web Technology Part 2
Web Technology Part 2Web Technology Part 2
Web Technology Part 2
 
Web Technology Part 1
Web Technology Part 1Web Technology Part 1
Web Technology Part 1
 
TOC Introduction
TOC Introduction TOC Introduction
TOC Introduction
 
CSS Introduction
CSS Introduction CSS Introduction
CSS Introduction
 
COA (Unit_4.pptx)
COA (Unit_4.pptx)COA (Unit_4.pptx)
COA (Unit_4.pptx)
 
COA(Unit_3.pptx)
COA(Unit_3.pptx)COA(Unit_3.pptx)
COA(Unit_3.pptx)
 
COA (Unit_2.pptx)
COA (Unit_2.pptx)COA (Unit_2.pptx)
COA (Unit_2.pptx)
 
COA (Unit_1.pptx)
COA (Unit_1.pptx)COA (Unit_1.pptx)
COA (Unit_1.pptx)
 
Software Testing Introduction (Part 4))
 Software Testing Introduction (Part 4)) Software Testing Introduction (Part 4))
Software Testing Introduction (Part 4))
 
Software Testing Introduction (Part 3)
 Software Testing Introduction (Part 3) Software Testing Introduction (Part 3)
Software Testing Introduction (Part 3)
 
Software Testing Introduction (Part 2)
Software Testing Introduction (Part 2)Software Testing Introduction (Part 2)
Software Testing Introduction (Part 2)
 
Software Testing Introduction (Part 1)
Software Testing Introduction (Part 1)Software Testing Introduction (Part 1)
Software Testing Introduction (Part 1)
 

Recently uploaded

Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 

Recently uploaded (20)

Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 

C Language Part 1

  • 2. 2016 • What are the basic data types and their associated range? (2.5 marks) 2015 What do you mean by the term data type? Explain any four data types with examples.(2.5 marks)
  • 3. Data Types in C • Each variable in C has an associated data type. • Each data type requires different amounts of memory.
  • 4. • Following are the examples of some very common data types used in C: • char: The most basic data type in C. It stores a single character. • int: As the name suggests, an int variable is used to store an integer. • float: It is used to store decimal numbers (numbers with floating point value) with single precision. • double: It is used to store decimal numbers (numbers with floating point value) with double precision.
  • 5. • Different data types also have different ranges upto which they can store numbers. • These ranges may vary from compiler to compiler. • Below is list of ranges along with the memory requirement and format specifiers on 32 bit gcc compiler. • We can use the sizeof() operator to check the size of a variable. • See the following C program for the usage of the various data types:
  • 6. Table of Data type and ranges
  • 7. 2’s complement form • For signed Numbers : (-2^(n-1)) to (2 ^(n-1)-1) For example Range for singed char : n=8 bits (-2^(8-1)) to (2^(8-1)-1) (-2^(7)) to (2^(7)-1) -128 to 127 • For unsigned Numbers: (0 to (2^n)-1) For example Range for unsigned char 0 to ((2^8)-1) 0 to 256-1 0 to 255
  • 8.
  • 10. 2017 What are keywords in C? Give examples (2.5 marks)
  • 12. • Keywords are specific reserved words in C each of which has a specific feature associated with it.
  • 13. Variables • A variable in simple terms is a storage place which has some memory allocated to it. • Basically, a variable used to store some form of data. • Different types of variables require different amounts of memory • Example : • Char c; c is a variable which takes 2 bytes in memory
  • 14. Variable Declaration Rules in C • Every variable name should start with alphabets or underscore (_). • No spaces are allowed in variable declaration. • Except underscore (_) no other special symbol are allowed in the middle of the variable declaration • Maximum length of variable is 8 characters depend on compiler and operation system. • Every variable name always should exist in the left hand side of assignment operator (invalid -> 10=a; valid -> a=10;).
  • 15. • Variable Declaration: A typical variable declaration is of the form: • type variable_name; or for multiple variables: • type variable1_name, variable2_name, variable3_name; • A variable name can consist of alphabets (both upper and lower case), numbers and the underscore ‘_’ character. • However, the name must not start with a number.
  • 16.
  • 18. • Difference b/w variable declaration and definition Variable declaration refers to the part where a variable is first declared or introduced before its first use. • Variable definition is the part where the variable is assigned a memory location and a value. • Most of the times, variable declaration and definition are done together.
  • 19. Note • Is it possible to have separate declaration and definition? It is possible in case of extern variables
  • 20. 2019 • Write a C program to test weather a given integer is a prime number or not. Display an appropriate message in the output(5.5 marks)
  • 21.
  • 22.
  • 24. 2015 • Write a program to swap two numbers without using third variable.(2.5 marks)
  • 25.
  • 26.
  • 27. Arrays in C • An array is a collection of items stored at contiguous memory locations and elements can be accessed randomly using index's of an array. • They are used to store similar type of elements as in the data type must be the same for all elements. • Given below is the picturesque representation of an array.
  • 28.
  • 29.
  • 30.
  • 31. 2017 What is an array? Write a program to print average of all elements of an array.(6.5 marks)
  • 32.
  • 33.
  • 36. Constants in C • As the name suggests the name constants is given to such variables or values in C programming language which cannot be modified once they are defined. • They are fixed values in a program. • There can be any types of constants like integer, float, octal, character constants etc.
  • 37. Defining Constants: In C/C++ program we can define constants in two ways as shown below: 1. Using #define preprocessor directive 2. Using a const keyword
  • 38. • Let us now learn about above two ways in details: 1. Using #define preprocessor directive: • We can use this to declare a constant as shown below: #define identifierName value – identifierName: It is the name given to constant. – value: This refers to any value assigned to identifierName.
  • 39.
  • 41. 2. using a const keyword: The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed Declaration and definition of var variable in same line
  • 44. 2015 • What is Enumerated Data type? Explain with suitable example.(2.5 marks)
  • 45. Enumeration (or enum) in C • Enumeration (or enum) is a user defined data type in C. • It is mainly used to assign names to integral constants, the names make a program easy to read and maintain.
  • 46.
  • 47.
  • 49. 2017 What are different type of operators in c? Briefly explain. (6 marks) 2016 Define operators. What are the different types of operators (2.5 marks) 2019 Discuss the various operators in C. (5 marks)
  • 50. • Operators allow us to perform different kinds of operations on operands.
  • 52.
  • 53. In C, operators can be categorized in following categories: 1. Arithmetic Operators Binary operators : (+, -, *, /, %) Unary Operators : (post-increment, pre- increment, post-decrement, pre-decrement) (Arithmetic Operators)
  • 54. • The binary operators falling in this category are: – Addition: The ‘+’ operator adds two operands. For example, x+y. – Subtraction: The ‘-‘ operator subtracts two operands. For example, x-y. – Multiplication: The ‘*’ operator multiplies two operands. For example, x*y. – Division: The ‘/’ operator divides the first operand by the second. For example, x/y.
  • 55. – Modulus: The ‘%’ operator returns the remainder when first operand is divided by the second. For example, x%y.
  • 56. Example of Binary Operators
  • 57.
  • 58. END
  • 59. C Programming Lecture 5 Modulo operator restriction
  • 60. Restrictions of the modulo operator • The % operator cannot be applied to floating- point numbers i.e float or double. • If you try to use the modulo operator with floating-point constants or variables, the compiler will produce a error:
  • 61.
  • 62.
  • 63. END
  • 65.
  • 66. In C, operators can be categorized in following categories: 1. Arithmetic Operators Binary operators : (+, -, *, /, %) Unary Operators : (post-increment, pre- increment, post-decrement, pre-decrement) (Arithmetic Operators)
  • 67. • The ones falling into the category of unary arithmetic operators are: • Increment: The ‘++’ operator is used to increment the value of an integer. • When placed before the variable name (also called pre-increment operator), its value is incremented instantly. • For example, ++x. And when it is placed after the variable name (also called post-increment operator), its value is preserved temporarily until the execution of this statement and it gets updated before the execution of the next statement. For example, x++.
  • 68. • Decrement: The ‘ – – ‘ operator is used to decrement the value of an integer. • When placed before the variable name (also called pre-decrement operator), its value is decremented instantly. • For example, – – x. And when it is placed after the variable name (also called post-decrement operator), its value is preserved temporarily until the execution of this statement and it gets updated before the execution of the next statement. • For example, x – –.
  • 69.
  • 70.
  • 72. END
  • 75. Relational operators • Relational operators are used for comparison of two values. For example, less than, greater than, equal to etc. • Let’s see them one by one
  • 76. 1. Equal to operator: Represented as ‘==’, the equal to operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise it returns false. For example, 1. 5==5 will return true. 2. 6==5 will return false.
  • 77. 2. Not equal to operator: Represented as ‘!=’, the not equal to operator checks whether the two given operands are equal or not. If not, it returns true. Otherwise it returns false. It is the exact boolean complement of the ‘==’ operator. For example, 1. 5!=5 will return false. 2. 6!=5 will return true
  • 78. 3. Less than operator: Represented as ‘<‘, the less than operator checks whether the first operand is lesser than the second operand. If so, it returns true. Otherwise it returns false. For example, 1. 5<5 will return false. 2. 5<6 will return true.
  • 79. 4. Less than or equal to operator: Represented as ‘<=’, the less than or equal tooperator checks whether the first operand is less than or equal to the second operand. • If so, it returns true • else false. For example, 1. 2<=5 will return true 2. 6<=5 will return false. 3. 5<=5 will return true.
  • 80. 5. Greater than operator: • Represented as ‘>’, the greater than operator checks whether the first operand is greater than the second operand or not. • If so, it returns true. • Otherwise it returns false. For example, 1. 6>5 will return true 2. 5>5 will return false
  • 81. 6. Greater than or equal to operator: Represented as ‘>=’, the greater than or equal to operator checks whether the first operand is greater than or equal to the second operand. • If so, it returns true • else it returns false. For example, 7>=5 will return true. 6>=8 will return false. 5>=5 will return true.
  • 82.
  • 83.
  • 84.
  • 87.
  • 88. Logical Operators 1. Logical AND operator: • The ‘&&’ operator returns true when both the conditions under consideration are satisfied. Otherwise it returns false. • For example, a && b returns true when both a and b are true (i.e. non-zero). A B A AND B 0 0 0 0 1 0 1 0 0 1 1 1 Using 2 variables we can have 4 combinations
  • 89. 2. Logical OR operator: The ‘||’ operator returns true even if one (or both) of the conditions under consideration is satisfied. Otherwise it returns false. For example, a || b returns true if one of a or b or both are true (i.e. non-zero). Of course, it returns true when both a and b are true. A B A OR B 0 0 0 0 1 1 1 0 1 1 1 1 Using 2 variables we can have 4 combinations
  • 90. 3. Logical NOT operator: • Logical NOT is denoted by exclamatory characters (!), it is used to check the opposite result of any given test condition. • If any condition's result is non-zero (true), it returns 0 (false) and if any condition's result is 0(false) it returns 1 (true). A !A 0 1 1 0 Using 1 variables we can have 2 combinations
  • 91.
  • 92.
  • 94. END
  • 96.
  • 97. Assignment Operators in C • Assignment operators are used to assign value to a variable. • The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value. • For example : int a; // variable a is declare and define a= 5; // a is assigned value 5.
  • 98. • Different types of assignment operators are shown below: 1. “=”: This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left. For example : int a; a = 10;
  • 99. 2. “+=”: • This operator is combination of ‘+’ and ‘=’ operators. += is known as compound assignment. • This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left. For Example (a += b) can be written as (a = a + b) If initially value stored in a is 5 and b is 6 Then (a += 6) OR (a=a+6) a=5+6=11
  • 100. 3. “-=” • This operator is combination of ‘-‘ and ‘=’ operators -= is known as compound assignment • This operator first subtracts the current value of the variable on left from the value on the right and then assigns the result to the variable on the left. For Example: (a -= b) can be written as (a = a - b) If initially value stored in a is 8 and b is 6 Then (a -= 6) OR a=a-6 a=8-6 = 2
  • 101. 4. “*=” This operator is combination of ‘*’ and ‘=’ operators *= is known as compound assignment • This operator first multiplies the current value of the variable on left to the value on the right and then assigns the result to the variable on the left. Example: • (a *= b) can be written as (a = a * b) • If initially value stored in a is 5 and b is 6 • Then (a *= 6) = 30.
  • 102. 5. “/=” • This operator is combination of ‘/’ and ‘=’ operators /= is known as compound assignment • This operator first divides the current value of the variable on left by the value on the right and then assigns the result to the variable on the left. Example: • (a /= b) can be written as (a = a / b) • If initially value stored in a is 6 and b is 2 • Then (a /= 2) a=(6/2)=3.
  • 103. 6. “%=” • This operator is combination of ‘%’ and ‘=’ operators %= is known as compound assignment • This operator first modulus the current value of the variable on left by the value on the right and then assigns the result to the variable on the left. Example: • (a %= b) can be written as (a = a % b) • If initially value stored in a is 6 and b is 2 • Then (a %= 2) a=(6%2)=0.
  • 104.
  • 105.
  • 106. OUTPUT
  • 108. “%=” • This operator is combination of ‘%’ and ‘=’ operators %= is known as compound assignment • This operator first modulus the current value of the variable on left by the value on the right and then assigns the result to the variable on the left. Example: • (a %= b) can be written as (a = a % b) • If initially value stored in a is 6 and b is 2 • Then (a %= 2) a=(6%2)=0.
  • 109.
  • 110.
  • 111. 2017 • Explain bitwise operators available in C. (2.5 marks 2016 what are bitwise operators?( 2.5 marks)
  • 113.
  • 114. Bitwise Operators in C • The & (bitwise AND) in C takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. Example : 2 in binary 1 0 3 in binary 1 1 2&3 ____________________________ 1 0 = 2 _____________________________ LSB MSB A B A AND B 0 0 0 0 1 0 1 0 0 1 1 1 Truth table
  • 115. Bitwise Operators in C • The | (bitwise OR) in C takes two numbers as operands and does OR on every bit of two numbers. • The result of OR is 1 if any of the two bits is 1. Example : 2 in binary = 1 0 3 in binary = 1 1 2 | 3 ____________________ 1 1 = 3 ______________________ LSB MSB A B A OR B 0 0 0 0 1 1 1 0 1 1 1 1 Truth table
  • 116. Bitwise Operators in C • The ^ (bitwise XOR) in C takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different. Example : 2 in binary = 1 0 3 in binary = 1 1 2^3 ___________________ 0 1 = 1 ____________________ A B A XOR B 0 0 0 0 1 1 1 0 1 1 1 0 MSB LSB Truth table Note: When both the operand are same we get 0 Otherwise 1
  • 117.
  • 118. output
  • 120. 1. Bitwise Left Shift Operator << 2. Bitwise Right Shift Operator >> 3. Bitwise One's Complement ~ Bitwise Operators in C
  • 121. Bitwise Operators in C • The << (left shift) in C takes two numbers. • left shift operator shifts the bits of the first operand and the second operand decides the number of places to shift. Example : • First Operand : 8 8 in binary = 1 0 0 0 • Second Operand : 3 ( so shift the operand by 3 bits) =Shift 8 by 3 bits left = 8<<3 = 8*(2 pow 3) =8*8 =64 in binary = 1 0 0 0 0 0 0 Short cut trick is append 3 0’s in right side of first operand
  • 122. • The >> (right shift) in C takes two numbers. • right shift operator shifts the bits of the first operand and the second operand decides the number of places to shift. Example : • First Operand : 8 8 in binary = 1 0 0 0 • Second Operand : 3 (so shift the operand by 3 bits) =Shift 8 by 3 bits right = 8>>3 = 8/(2 pow 3) =8/8 =1 in binary = 0 0 0 1 Bitwise Operators in C
  • 123. • The ~ (bitwise 1’s complement) in C takes one number and inverts all bits of it Example : Number 8 in binary 0 1 0 0 0 1’s complement 1 0 1 1 1 of 8 is (-8) -8 in binary = 10111 Negative numbers are stored in 2’s complement form : • Short trick to calculate 1’s complement : ~n=-(n+1) • n=8 = - (8+1) = -9 MSB bit is 0 means number is positive MSB bit is 1 means number is negative
  • 124.
  • 125. output
  • 127.
  • 128. Conditional or Ternary Operator (?:) Working: Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then Expression2 will be executed and the result will be returned. Otherwise, if the condition(Expression1) is false then Expression3 will be executed and the result will be returned.
  • 129. Program to find MAX of 2 numbers
  • 130. output
  • 131.
  • 132. output
  • 133. Decision Making in C (if , if..else, Nested if, if-else-if )
  • 134.
  • 135. if statement in C • if statement is the most simple decision making statement. • It is used to decide whether a certain statement or block of statements will be executed or not i.e • if a certain condition is true then a block of statement is executed otherwise not.
  • 136.
  • 137. • Here, condition after evaluation will be either true or false. • if statement accepts boolean values – if the value is true then it will execute the block of statements below it otherwise not. • If we do not provide the curly braces ‘{‘ and ‘}’ after if(condition) then by default if statement will consider the first immediately below statement to be inside its block.
  • 138.
  • 139. if-else in C • The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. • But what if we want to do something else if the condition is false. • Here comes the C else statement. • We can use the else statement with if statement to execute a block of code when the condition is false.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144. output
  • 145. 2019 • Explain the if..else statement with all its variations. Compare it with switch case. Write a c program that demonstrate the difference between the two.(7 marks)
  • 146. if-else-if ladder in C • Here, a user can decide among multiple options. • The C if statements are executed from the top down. • As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. • If none of the conditions are true, then the final else statement will be executed.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151. output
  • 152.
  • 153.
  • 154. output
  • 155. • What are nested control statements? Give examples(2.5 marks)
  • 156. nested-if in C • A nested if in C is an if statement that is the target of another if statement. • Nested if statements means an if statement inside another if statement. • Yes, both C and C++ allows us to nested if statements within if statements, i.e, we can place an if statement inside another if statement.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161. Jump Statements in C/C++ • These statements are used in C orC++ for unconditional flow of control through out the funtions in a program. • They support four type of jump statements:
  • 162. 2017 What is the function of break and continue statement in C? (2.5 marks) 2016 Differentiate between break and continue.
  • 163. • C break: • This loop control statement is used to terminate the loop. • As soon as the break statement is encountered from within a loop, the loop iterations stops there and control returns from the loop immediately to the first statement after the loop. Syntax: break; • Basically break statements are used in the situations when we are not sure about the actual number of iterations for the loop or we want to terminate the loop based on some condition.
  • 164.
  • 165. 2. C continue: This loop control statement is just like the break statement. The continue statement is opposite to that of break statement, instead of terminating the loop, it forces to execute the next iteration of the loop. As the name suggest the continue statement forces the loop to continue or execute the next iteration. When the continue statement is executed in the loop, the code inside the loop following the continue statement will be skipped and next iteration of the loop will begin.
  • 166.
  • 167.
  • 168. OUTPUT
  • 169. The GOTO STATEMENT • A goto statement 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 in next slide
  • 170.
  • 171. 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.
  • 172. • 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 jump is known as a backward jump • On the other hand, if the label : is placed after goto label; some statements will be skipped and the jump is known as a forward jump.
  • 173. 2016 • Why does goto statement is not preferred in c programming (2.5 marks)
  • 174. Why Goto should be avoided Due to the unconditional goto statement at the end, the control is always transferred back to the input statement. In fact, program puts the computer in a permanent loop known as an infinite loop. The computer goes round and round until we take some special steps to terminate the loop. Such infinite loops should be avoided.
  • 175. No break condition from loop using goto
  • 176.
  • 177. break condition from loop using goto
  • 178.
  • 179. 2015 • Difference between break and exit statements.(2.5 marks)
  • 180.
  • 181. output
  • 182. • In C, exit() terminates the calling process without executing the rest code which is after the exit() function.
  • 183.
  • 185. • C return: • The return in C or C++ returns the flow of the execution to the function from where it is called. • This statement does not mandatorily need any conditional statements. • As soon as the statement is executed, the flow of the program stops immediately and return the control from where it was called. • The return statement may or may not return anything for a void function, but for a non- void function, a return value is must be returned. Syntax: return[expression];
  • 186.
  • 187. output
  • 188. 2017 • Explain loop statements in c. (6.5 marks) 2016 • List the different types of loop control statements and explain them with suitable examples( 6 marks) 2019 • Explain the different loop statements in c through suitable examples.(5 marks)
  • 189. Types of C Loops • There are three types of loops in C language that is given below: 1. do while 2. while 3. for
  • 190. do-while loop in C • The do-while loop continues until a given condition satisfies. • It is also called post tested loop. • It is used when it is necessary to execute the loop at least once (mostly menu driven programs).
  • 192. C Programming Lecture 14 Decision Making using IF…ELSE statement
  • 193. Decision making using if-else statement • In if statement if a condition is true it will execute a block of statements. • But what if we want to execute the statements if the condition is false. • So we require else statement.
  • 194. Syntax of if-else statement
  • 195.
  • 196. OUTPUT
  • 197. END
  • 198.
  • 199. output
  • 200. END
  • 201.
  • 202. output
  • 203. END
  • 204. C Programming Lecture 15 Decision Making using Nested-IF statement
  • 205. • Nested if statement is If statement inside another if statement. Decision making using Nested if statement Syntax of nested-if statement
  • 206.
  • 207.
  • 208. OUTPUT
  • 209. END
  • 210. C Programming Lecture 16 Decision Making using if-else-if ladder
  • 211. • In if-else-if ladder statement, if a condition is true then the statements defined in the if block will be executed. • otherwise if some other condition is true then the statements defined in the else-if block will be executed. • at the last if none of the condition is true then the statements defined in the else block will be executed. • There are multiple else-if blocks possible.
  • 213.
  • 214.
  • 215. output
  • 216. END
  • 217. C Programming Lecture 17 Decision Making using Switch Statement
  • 218. • The switch statement is a multiway branch statement. • It provides an easy way to dispatch execution to different parts of code based on the value of the expression. • Switch is a control statement that allows a value to change control of execution
  • 219. Rules for switch statement 1) The switch expression must be of an integer or character type. 2) The case value must be an integer or character constant. 3) The case value can be used only inside the switch statement. 4) The break statement in switch case is optional. • If there is no break statement found in the case, all the cases will be executed present after the matched case.
  • 220. 5) The default statement is executed if no case constant-expression value is equal to the value of expression
  • 221. Syntax of Switch Statement
  • 222.
  • 223.
  • 224. output
  • 225. END
  • 226. C Programming Lecture 18 Decision Making using JUMP Statement
  • 227. • The goto statement is known as jump statement. • As the name suggests, goto is used to transfer the program control to a predefined label. • The goto statment can be used to repeat some part of the code for a particular condition. • Goto is known as unconditional Jump
  • 228. Syntax of goto statement
  • 229. Implementation of GOTO Statement Statements are ignored
  • 230. Implementation of GOTO Statement Statements are repeated
  • 231.
  • 232.
  • 233. END
  • 234. C Programming Lecture 18 Decision Making using JUMP Statement
  • 235. C Programming Lecture 19 Decision Making using JUMP Statement Part 2
  • 236. Jump Statements 1. goto 2. break 3. continue 4. return
  • 237. • The break is a keyword in C which is used to bring the program control out of the loop. • The break statement is used inside loops or switch statement. Break statement
  • 238. Syntax of break statement
  • 239.
  • 240. output
  • 241. END
  • 242. C Programming Lecture 20 Decision Making using JUMP Statement Part 3
  • 243. Jump Statements 1. goto 2. break 3. continue 4. return
  • 244. • The continue statement brings the program control to the starting of the loop. • The continue statement skips some lines of code inside the loop and continues with the next iteration. • It is mainly used for a condition so that we can skip some code for a particular condition.
  • 246.
  • 247. output
  • 248. END
  • 249. C Programming Lecture 21 Introduction to Decision Making using looping Part 1
  • 250. • Loops in programming come into use when we need to repeatedly execute a block of statements. • For example: • Suppose we want to print “Hello World” 10 times. • This can be done in two ways as shown below 1. Iterative Method 2. Using Loops
  • 251. 1. Iterative Method • An iterative method can be used to write the printf() statement 10 times.
  • 252.
  • 253. output
  • 254. 2. Using Loops • In Loop, the statement needs to be written only once and the loop will be executed n times • In our case printf(“Hellon”) is written only ones and Hello is printed 10 times using loop. • A loop is a sequence of instructions that is repeated until a certain condition is reached.
  • 255. There are mainly two types of loops: 1. Entry Controlled loops: In this type of loops the test condition is tested before entering the loop body. • For Loop and While Loop are entry controlled loops. 2. Exit Controlled Loops: In this type of loops the test condition is tested at the end of loop body. • Therefore, the loop body will execute atleast once, irrespective of whether the test condition is true or false. • do – while loop is exit controlled loop.
  • 256. 1. for Loop • A for loop is a repetition control structure which allows us to write a loop that is executed for a specific number of times. Syntax of for loop 1 2 3 4
  • 257.
  • 258. output
  • 259. END
  • 260. C Programming Lecture 22 Decision Making using looping Part 2
  • 261. Types of C Loops • There are three types of loops in C language that is given below: 1. for 2. do-while 3. while
  • 262. 2. do-while loop in C • The do-while loop continues until a given condition satisfies. • It is used when it is necessary to execute the loop at least once • do-while loop is a exit controlled Loop. Syntax of do-while loop
  • 263.
  • 264. output
  • 265. END
  • 266. C Programming Lecture 23 Decision Making using looping Part 3
  • 267. Types of C Loops • There are three types of loops in C language that is given below: 1. for 2. do-while 3. while
  • 270. output
  • 273. END
  • 275. OUTPUT
  • 277. What is infinite loop? • An infinite loop is a looping construct that does not terminate the loop and executes the loop forever. • It is also called an indefinite loop or an endless loop.
  • 278. • We can create an infinite loop using following ways: 1. for loop 2. while loop 3. do-while loop 4. go to statement 5. C macros
  • 279. 1. For loop all the parts of the 'for' loop are optional, and in the above for loop, we have not mentioned any condition; so, this loop will execute infinite times. Syntax
  • 280.
  • 281. output
  • 283.
  • 284. output
  • 286.
  • 287. output
  • 289.
  • 290. output
  • 291. 5. Macros • We can also create the infinite loop with the help of a macro constant. • Let's understand through an example.
  • 292.
  • 293. output
  • 294. END
  • 295. C Programming Lecture 24 Type Conversion in C
  • 296. • A type cast is basically a conversion from one type to another. • There are two types of type conversion: 1. Implicit Type Conversion 2. Explicit Type Conversion
  • 297. • Also known as ‘automatic type conversion’. • Done by the compiler on its own. • All the data types of the variables are upgraded to the data type of the variable with largest data type. 1. Implicit Type Conversion
  • 298.
  • 299. output
  • 300. 2. Explicit Type Conversion • This process is also called type casting and it is user defined. • Here the user can type cast the result to make it of a particular data type. • Data can be lost when explicit conversion is done. Lower data type Higher data type
  • 301. Syntax of Explicit Type Conversion
  • 302. Program without explicit typecasting
  • 303. output
  • 304. Program with explicit typecasting
  • 305. output
  • 306. END
  • 309. Solution • In program "Expression syntax" error occur because the while() loop must have conditional expression.
  • 310. Question 2 • The break statement is used to take control out of switch and continue statement is used to take control of the beginning of the switch? • The above statement is true or false
  • 311. Solution • No, because continue statement can work only with loops in C-programming and not with switch.
  • 312. Question 3 A character variable can store ___ character(s) at a time.
  • 313. Solution • A character variable can at a time store only one character. In fact, if we execute the following statements, what gets stored in variable ch is not really the character constant, but the ASCII value of 'A', which is 65.
  • 314. Question 4 The C variables are case insensitive?
  • 315. SOLUTION • The C variables are case sensitive. • This means that variables abc, Abc and ABC would be treated as different variables in C.
  • 316. END
  • 318. • A function is a set of statements that take inputs, do some specific computation and produces output. • The idea is to put some commonly or repeatedly done task together and make a function so that instead of writing the same code again and again for different inputs, we can call the function.
  • 319. syntax of creating function
  • 320. Types of Functions • There are two types of functions in C programming: • Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc. • User-defined functions: are the functions which are created by the C programmer, so that he/she can use it many times. It reduces the complexity of a big program and optimizes the code.
  • 321. Different aspects of function calling 1. function without arguments and without return value 2. function without arguments and with return value 3. function with arguments and without return value 4. function with arguments and with return value
  • 322.
  • 323. for Loop • A for loop is a repetition control structure which allows us to write a loop that is executed a specific number of times. • The loop enables us to perform n number of steps together in one line.
  • 324. • In for loop, a loop variable is used to control the loop. • Initialization Expression: In this expression we have to initialize the loop counter to some value. • For example: int i=1; • Test Expression: In this expression we have to test the condition. • If the condition evaluates to true then we will execute the body of loop and go to update expression otherwise we will exit from the for loop. For example: i <= 10;
  • 325. • Update Expression: • After executing loop body this expression increments/decrements the loop variable by some value. • For example: i++;
  • 326.
  • 327. 2018 Explain while loop and do-while loop with an example. Elaborate difference between them(5 marks). 2016 Differentiate between entry control loop and exit control loop.(2.5 marks)
  • 328. while loop • A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. • The while loop can be thought of as a repeating if statement. and therefore is an example of Entry Control Loop. Syntax : while (boolean condition) { loop statements... }
  • 329.
  • 330. do-while loop • do while loop is similar to while loop with the only difference that it checks for the condition after executing the statements, and therefore is an example of Exit Control Loop. Syntax: do { statements.. } while (condition);
  • 331.
  • 332.
  • 333.
  • 334.
  • 335. 2015 • Write a program to display first n prime numbers(6 marks)
  • 336.
  • 337.
  • 338. output
  • 339. Switch Statement in C • Switch case statements are a substitute for long if statements that compare a variable to several integral values • The switch statement is a multiway branch statement. • It provides an easy way to dispatch execution to different parts of code based on the value of the expression. • Switch is a control statement that allows a value to change control of execution.
  • 340. Important Points about Switch Case Statements: 1. The expression provided in the switch should result in a constant value otherwise it would not be valid. Valid expressions for switch:
  • 341. 1. Duplicate case values are not allowed. • The default statement is optional. Even if the switch case statement do not have a default statement, it would run without any problem. 2. The break statement is used inside the switch to terminate a statement sequence. • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. 3. The break statement is optional. If omitted, execution will continue on into the next case. • The flow of control will fall through to subsequent cases until a break is reached.
  • 342. 4. Nesting of switch statements are allowed, which means you can have switch statements inside another switch. However nested switch statements should be avoided as it makes program more complex and less readable.
  • 343.
  • 344.
  • 345. output
  • 346.
  • 347.
  • 348. 2018 Explain the switch statement, break statement and continue statement with an example (6 marks)
  • 349.
  • 350.
  • 351.
  • 352. OUTPUT
  • 353.
  • 354. OUTPUT
  • 355. 2016 Write a program to display the following pattern as the output (6.5 marks)
  • 356.
  • 357. OUTPUT
  • 358. 2015 Explain the use of continue statements?
  • 359.
  • 360.
  • 361. 2016 • WAP to reverse the given integer number(6.5 marks)
  • 362.
  • 363. OUTPUT
  • 364. 2019 What do you understand by type casting? Why is it required? Explain using an example. (6 marks)
  • 365. Type Conversion in C • A type cast is basically a conversion from one type to another. • There are two types of type conversion:
  • 366. 1. Implicit Type Conversion
  • 367. • Also known as ‘automatic type conversion’. • Done by the compiler on its own, without any external trigger from the user. • Generally takes place when in an expression more than one data type is present. • In such condition type conversion (type promotion) takes place to avoid lose of data. • All the data types of the variables are upgraded to the data type of the variable with largest data type.
  • 368. • bool -> char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -> long double
  • 369. Example of Type Implicit Conversion:
  • 370. output
  • 371. 2. Explicit Type Conversion
  • 372. • This process is also called type casting and it is user defined. • Here the user can type cast the result to make it of a particular data type. The syntax in C: (type) expression
  • 373.
  • 374. OUTPUT
  • 375. Advantages of Type Conversion • This is done to take advantage of certain features of type hierarchies or type representations. • It helps us to compute expressions containing variables of different data types.
  • 376. 2018 Write a program to find largest number among the three numbers(8 marks)
  • 377.
  • 378.
  • 379.
  • 380. Comma in C 1) Comma as an operator: The comma operator (represented by the token, ) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type). • The comma operator has the lowest precedence of any C operator, and acts as a sequence point.