Notes PPS
Notes PPS
KARMNAGAR
(Approved by AICTE, New Delhi and- Affiliated
505481 to JNTU, Hyderabad)
I B.Tech I- Semester
UNIT - I
Computer Systems
A computer is a system which has two major components: hardware and software.
The computer hardware is the physical equipment. The software is the collection of
programs (instructions) that allow the hardware to do its job.
Computer Hardware
The hardware of the computer system is group of five parts: input devices, central
processing unit (CPU), primary storage, output devices, and auxiliary storage devices.
Generally a keyboard is the popular input device. The programs and data are entered into the
computers using key board. Other input devices are mouse, a pen or stylus, a touch screen,
or an audio input unit.
The central processing unit (CPU) is executes instructions such as arithmetic calculations.
Present generation computers may have one, two, or more CPUs.
Primary storage is also known as main memory. This is a place where the programs and
data are stored temporarily during processing. The data in primary storage is removed when
we turn off a computer.
The output device is generally a monitor or a printer. The output is shown on the monitor is
called as soft copy. If output is a printed one is called hard copy.
Auxiliary storage, (secondary storage), is used for both input and output. It is the place
where the programs and data are stored permanently. When we turn off the computer the
programs and data remain in the secondary storage which can be used again when user need
them.
System software manages the computer resources. It provides the interaction between the
hardware and the users. These programs are divided into three classes: the operating system,
system support, and system development.
The operating system provides services such as a user interface, file and database access,
and interfaces to communication systems such as Internet protocols.
System support software provides system utilities and other operating services.
Computing Environments
The different types of computing environments are
1. Personal Computing Environment
2. Time-Sharing Environment
If we are using a personal computer, all of the computer hardware components are
grouped together in our personal computer (PC).
2. Time-Sharing Environment
Employees in large companies often work in what is known as a time-sharing
environment. In the times-sharing environment, many users are connected to one or more
computers. These computers may be minicomputers or central mainframes. The terminals
they use are often nonprogrammable, although today we see more and more
microcomputers being used to simulate terminals. Also, in the time-sharing environment,
the output devices and auxiliary storage devices are shared by all of the users. A typical
college lab in which a minicomputer is shared is shared by many students is shown in
figure:
In the time-sharing environment, all computing must be done by the central computer. The
central computer has many duties: It must control the shared resources; it must manage the
shared data and printing and it must do the computing.
3. Client/Server Environment
4. Distributed Computing
A Distributed Computing environment provides an integration of computing
functions between different servers and clients .The internet provides connectivity to
different servers throughout the world. For example eBay uses several computers to provide
its auction services. This environment provides a reliable, scalable, and highly available
network.
Machine Languages
In the early days of computers, the programming languages available were only
machine languages. Each computer has its own machine language, which is made by
grouping 0’s and 1’s. The only language understood by computer hardware is machine
language.
Symbolic Languages:
In early 1950’s Admiral Grace Hopper, A mathematician and naval officer
developed Symbolic Language; a special computer program that would convert symbolic
code into machine language is called Assembler.
Symbolic language uses symbols or mnemonics to represent the various, machine language
instructions.
High Level Languages:
High level languages are portable to many different computers. High level languages
share one thing with symbolic languages; They must be converted into machine language.
The process of converting them is known as compilation.
The first widely used high-level languages, FORTRAN (FORmula TRANslation)
was created by John Backus and an IBM team in 1957. After FORTRAN was COBOL
(Common Business-Oriented Language). Admiral Hopper was played a key role in the
development of the COBOL Business language.
C is a high-level language used for system software and new application code.
Creating and Running Programs
Computer hardware understands a program only if it is coded in its machine
language. It is the job of the programmer to write and test the program .There are four steps
in this process:
1. Writing and editing the program
2. Compiling the program
3. Linking the program with the required library modules
4. Executing the program.
Compiling Programs:
The code in a source file must be translated into machine language. This is the job of
the compiler. The c compiler is two separate programs-preprocessor and the translator.
The preprocessor reads the source code by scanning preprocessor commands and
replacing with special code libraries by finally preparing it for the translator.
After the preprocessor has prepared the code for compilation, the translator does the
actual work of converting the program into machine language.
Linking Programs:
A C program is made up of many functions. We write some of these functions, and
they are a part of our source program. There are other functions, such as input/output
processes and, mathematical library functions that exist elsewhere and must be attached to
our program. The linker assembles all of these functions, ours and systems into our final
executable program.
Executing Programs:
Once program has been linked, it is ready for execution. To execute a program we
use an operating system command, such as run, to load the program into primary memory
and execute it. Getting the program into memory is the function of an operating system
program known as the loader.
After the program processes the data, it prepares the output and the output can be to the
user’s monitor or to a file.
We can use flowchart or pseudo code to complete the design of your program.
1. Black box testing: This is done by the system test engineer and the user. Black box
testing is the programs are tested without knowing what is inside it, without knowing
how it works. Black box test plans are developed by looking only the requirements
statement. The test engineer uses these requirements to develop test plans.
2. White box testing: This is the responsibility of the programmer. White box testing
assumes that the tester knows everything about the program.
ALGORITHM /PSEUDOCODE:
1. Pseudo Code:
Pseudo-code is the way of writing the steps to solve a problem in any spoken language like
English. Pseudo-code is informal language that used to develop algorithms. It is not a
computer programming language like C. It helps the programmer to develop programs.
2. Algorithm:
Steps in the algorithm: An algorithm can be divided into three basic categories.
The algorithm which as best space complexity and/or time complexity should be selected to
solve the problem based on context.
FLOW CHART:
A Flow chart is a Graphical representation of an Algorithm. Flow charts are drawn
using certain special purpose symbols such as Rectangles, Diamonds, Ovals and small
circles. These symbols are connected by arrows called flow lines.
(or)
The diagrammatic representation of way to solve the given problem is called flow chart.
C language was developed by Dennis Ritchie, in 1972 at AT&T (American Telephones &
Telegraphs) Bell Labs, New-Jersy, USA.
C language has rich set of built-in functions and operations that can be used to write
any difficult program.
C language is suitable for writing both system software and business packages.
Programs written in ‘C’ are efficient and fast. This is due to its variety of data types
and powerful operators.
There are only 32 keywords in ANSI C and its strength lies in its build-in functions.
C is highly portable. This means that C programs written for one computer can be
run on another computer with little or no modifications.
C language is well suited for structured programming. User can solve problems in
terms of function modules or blocks.
Documentation Section
Link Section
Definition Section
Global Declaration
Section
Main () Function Section
{
Declaration part
Executable part
}
Prepared by Dr. V.Neelima, Associate Professor, CSE Dept Page 11
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY AND SCIENCE
KARMNAGAR
(Approved by AICTE, New Delhi and- Affiliated
505481 to JNTU, Hyderabad)
The documentation section consists of a set of comment lines such as giving the name of the
program, the author and other details. All the comments of a C program must be written
between /* and */.
Eg: /* This is my first program */
The link section provides instructions to the compiler to link functions from the system
library.
The global declaration section is used declare global variables of a C program. Global
variables are used at any part of the program.
Every C program must have one main () function section. This section contains two parts,
declaration part and process part. The declaration part is used to declare all the variables
used in the process part. There must be atleast one statement in the process part. These two
parts must appear between the opening and the closing braces. All statements in the
declaration and executable parts end with a semicolon ; .
The subprogram section contains all the user-defined functions that are called in the main()
function.
All sections, except the main() function section may be absent in the program when they are
not required.
There are four steps in this process of creating and running programs:
A text editor is software used to write or modify the program. The program file is saved to
disk with a file name of extension .C. For example, add.c. This file is also known as source
file.
Compiling Programs:
Compiler is a software used to convert C program source file into machine language. The
compiler checks the program for any syntax errors. The c compiler is two separate programs
preprocessor and the translator. The preprocessor reads the source code by scanning for
special instructions known as preprocessor commands(contains # symbol). These commands
tell the preprocessor to look for special code libraries, make substitutions in the code, and
generate .obj file (object file).Then the translator convert .obj file into machine readable file
called executable file.
Linking Programs:
In a C program, we may write some of the functions like printf, scanf, sqrt etc. The linker
links these functions to our program from header files. Assemble all of these functions,
related to program and system into our final executable program.
Executing Programs:
Once program has been linked, it is ready for execution. To execute a program we use an
operating system command, such as run.
CHARACTER SET:
The following are the character that can be used to form words, numbers and expressions
depend upon the computer on the program is run. They are…
Alphabets (A – Z and a – z)
Numerals (0 – 9)
Special Characters ( ~ ! # $ % ^ & * ( ) _ - + = [ ] { } | \ ; : ‘ “ , < . > / ?)
1. C TOKENS:
C tokens are the basic buildings blocks in C language which are constructed together to
write a C program.
Each and every smallest individual units in a C program are known as C tokens.
Words are broadly categorized into two types. They are i) Reserved words and ii) User-
defined words.
User-defined words: User-defined words are the words which are defined by the user for
his specific purpose.
1.1. Identifiers: An identifier is a name, which was given to the element of a program
such as names of the variables, constants, functions, structures, unions pointers, files
etc.,
Variables: A variable is an identifier, which reserves some space in the memory, which can
change its value during the execution of a program.
1.2. Constants: A constant is an identifier, which reserves some space in the memory,
which cannot change its value during the execution of a program. Constants can be
broadly categorized into two types. They are i) Character constants ii) Numeric
constants.
Character constants: Character constants can be formed with the help of valid ‘C’
character set. They can be a) Single character constants and b) String constants.
Single character constants: Single character constants are the single characters enclosed
within single quotations.
Eg: ‘a’, ‘*’, ‘5’
String constants: String constants are the collection of characters enclosed within double
quotations.
Numeric constants: Numeric constants can be formed with the help of numbers only. They
can a) Integer constants b) Real Constants.
Integer constants: An integer constant is a sequence of digits with or without sign, without
decimal point or exponentiation.
Backslash Character Constants: C supports some special back slash character constants
that are used in output functions. These characters combinations are known as ‘escape
sequences’.
Constant Meaning
‘\a’ Audible alert
(bell)
‘\b’ Back space
‘\f’ Form feed
‘\n’ New line
‘\r’ Carriage return
‘\t’ Horizontal tab
‘\v’ Vertical tab
‘\’’ Single quote
‘\”’ Double quote
‘\?’ Question mark
‘\\’ Backslash
‘\0’ Null
DATA TYPES:
C supports different types of data, each of which may be represented differently within a
computer's memory. Data type is used to determine what type of value a variable or a
constant can contain throughout the program. Data types of the ‘C’ language are…
Primary Data Types: All C compilers support four fundamental data types such as
Character (char), integer (int), floating point (float) and double floating point (double).
Character data type: Character data type is used to represent character type of information.
Different character data types, their size and memory requirements are…
Memory
Sl. No. Data Type Range of values
Size
1 Signed char -128 to 127 1 byte
2 Unsigned char 0 to 255 1 byte
Memory
Sl. No. Data Type Range of Values
Size
Signed Short int (Short
1 -128 to 127 1 byte
int)
2 Unsigned Short int 0 to 255 1 byte
3 Signed int (int) -32768 to 32767 2 bytes
4 Unsigned int 0 to 65535 2 bytes
Signed Long int (Long -2147483648 to
5 4 bytes
int) 2147483647
6 Unsigned Long int 0 to 4294967295 8 bytes
Floating Point Data Types: Floating point is a sequence of digits, with or without sign,
with a decimal point or exponentiation. Floating point numbers are stored in 32 bits with 6
digits of precision. Different floating point data types, their size and memory requirements
are…
Memory
Sl. No. Data Type Range of Values
Size
1 Floating point (float) 3.4E-38 to 3.4E+38 4 bytes
Double Floating Point Data Types: Double floating point is a sequence of digits, with or
without sign, with a decimal point or exponentiation. Double floating point numbers are
stored in 64 bits with 14 digits of precision. Different double floating point data types, their
size and memory requirements are…
Memory
Sl. No. Data Type Range of Values
Size
Double floating point
1 1.7E-308 to 1.7E+308 8 bytes
(double)
3.4E-4932 to
2 Long double 10 bytes
1.1E+4932
User-defined Data types: User defined data types of ‘C’ language are typedef and enum.
Typedef: C supports a feature known as “type definition” that allows users to define an
identifier that would represent an existing data type. It can be done as…
Enum: C provides another user-defined data type is enumerated (enum). It is defined as…
The identifier is user-defined enumerated data type which can be used to declare variable
that can have one of the values enclosed within the braces. We can declare variables to be
of this ‘new’ type as…
The enumerated variables var-1, var-2, …, var-n can only have one of the values value-1,
value-2, … value-n. It can be done as…
Derived Data Types: Derived data types of ‘C’ language are arrays, functions, structures,
unions, pointers and files.
Empty Data Set: Empty data set of the ‘C’ language is ‘void’. Void means nothing.
DECLARATION OF VARIABLES:
After designing suitable variable names, we must declare them to the compiler. Declaration
does two things.
Var-1, var-2, …, var-n are the names of variables. Variables are separated by commas. A
declaration statement must end with a semicolon.
Eg: x = 0;
y = 100;
balance = 75.45;
It is also possible to assign a value to a variable at the time the variable is declared as…
Eg: int x = 0;
char yes = ‘x’;
double balance = 75.45;
C permits the initialization of more than one variables in one statement using multiple
assignment operators as…
Eg: p = q = r = 0;
Input means which the computer reads, and output means which the computer writes. The
C language input and output functions are broadly classified into 3 types. They are…
Console I/O functions are used to transfer the data between the program and console. Disk
I/O functions are used to transfer the data between the program and hard disk. Port I/O
functions are used to transfer the data between the program and computer port.
Console I/O functions are broadly classified into 2 types. Formatted console I/O functions
and unformatted console I/O functions.
Formatted Console I/O Functions: Formatted console I/O functions are used to read /
write mixed type from / to the console. The 2 formatted console I/O functions are printf( )
and scanf( ) functions.
printf() function: The printf() function is used to write mixed type data to the console.
The control string indicates how many arguments follow and what their types are. The
arguments arg-1, arg-2, …, arg-n are the variables whose values are formatted and printed
according to the specifications of the control string. Commonly used printf format codes
are…
scanf() function: The scanf() function is used to read mixed type data from the console.
The control string specifies the field format in which the data is to be entered and the
arguments arg-1, arg-2, …, arg-n specify the address of locations where the data is stored.
Commonly used scanf() format codes are…
Unformatted Console I/O Functions: Unformatted console I/O functions are used to read
/ write character type of data from / to the console. The 7 unformatted console I/O functions
are getchar(), putchar(), getch(), putch(), getche(), gets(), puts().
getchar(): The getchar() function is used to read a single character from the console.
The symbols which are used to perform logical and mathematical operations in a C
program are called C operators.
These C operators join individual constants and variables to form expressions.
Operators, functions, constants and variables are combined together to form
expressions.
Consider the expression A + B * 5. where, +, * are operators, A, B are variables, 5
is constant and A + B * 5 is an expression.
Types of C operators:
1. Arithmetic operators
2. Assignment operators
3. Relational operators
4. Logical operators
5. Bit wise operators
6. Conditional operators (ternary operators)
7. Increment/decrement operators
8. Special operators
Arithmetic Operators in C:
In this example program, two values “40” and “20” are used to perform arithmetic
operations such as addition, subtraction, multiplication, division, modulus and output
is displayed for each operation.
#include <stdio.h>
int main()
{
int a=40,b=20, add,sub,mul,div,mod;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is : %d\n", add);
printf("Subtraction of a, b is : %d\n", sub);
printf("Multiplication of a, b is : %d\n", mul);
printf("Division of a, b is : %d\n", div);
Prepared by Dr. V.Neelima, Associate Professor, CSE Dept Page 22
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY AND SCIENCE
KARMNAGAR
(Approved by AICTE, New Delhi and- Affiliated
505481 to JNTU, Hyderabad)
Output:
Addition of a, b is : 60
Subtraction of a, b is : 20
Multiplication of a, b is : 800
Division of a, b is : 2
Modulus of a, b is : 0
Assignment operators in C:
In C programs, values for the variables are assigned using assignment operators.
For example, if the value “10” is to be assigned for the variable “sum”, it can be
assigned as “sum = 10;”
Other assignment operators in C language are given below.
In this program, values from 0 – 9 are summed up and total “45” is displayed as
output.
Assignment operators such as “=” and “+=” are used in this program to assign the
values and to sum up the values.
# include <stdio.h>
int main()
{
int Total=0,i;
for(i=0;i<10;i++)
{
Output:
Total = 45
Relational operators in C:
Relational operators are used to find the relation between two variables. i.e. to
compare the values of two variables in a C program.
In this program, relational operator (==) is used to compare 2 values whether they
are equal are not.
If both values are equal, output is displayed as ” values are equal”. Else, output is
displayed as “values are not equal”.
Note : double equal sign (==) should be used to compare 2 values. We should not
single equal sign (=).
Logical operators in C:
These operators are used to perform logical operations on the given expressions.
There are 3 logical operators in C language. They are, logical AND (&&), logical
OR (||) and logical NOT (!).
a b && || !a
False(0) False(0) 0 0 1
False(0) True(1) 0 1 1
True(1) False(0) 0 1 0
True(1) True(1) 1 1 0
0 1
1 0
#include <stdio.h>
int main() {
int a,b,c;
scanf("%d%d",&a,&b);
c=(a>b || a==b);
c=!(a>b);
return 0;
Output:
logical or result : 0
#include <stdio.h>
int main()
{
int m=40,n=20;
int o=20,p=30;
if (m>n && m !=0)
{
printf("&& Operator : Both conditions are true\n");
}
if (o>p || p!=20)
{
printf("|| Operator : Only one condition is true\n");
}
if (!(m>n && m !=0))
{
printf("! Operator : Both conditions are true\n");
}
else
{
printf("! Operator : Both conditions are true. " \
"But, status is inverted as false\n");
}
}
Output:
In this program, operators (&&, || and !) are used to perform logical operations on
the given expressions.
&& operator – “if clause” becomes true only when both conditions (m>n and m!
=0) is true. Else, it becomes false.
|| Operator – “if clause” becomes true when any one of the condition (o>p ||
p!=20) is true. It becomes false when none of the condition is true.
! Operator – It is used to reverses the state of the operand.
If the conditions (m>n && m!=0) is true, true (1) is returned. This value is inverted
by “!” operator.
So, “! (m>n and m! =0)” returns false (0).
These operators are used to perform bit operations. Decimal values are converted
into binary values which are the sequence of bits and bit wise operators work on
these bits.
Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise
OR), ^ (XOR), << (left shift) and >> (right shift).
Consider x=40 and y=80. Binary form of these values are given below.
x = 00101000
y= 01010000
Note:
In this example program, bit wise operations are performed as shown above and
output is displayed in decimal format.
Prepared by Dr. V.Neelima, Associate Professor, CSE Dept Page 27
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY AND SCIENCE
KARMNAGAR
(Approved by AICTE, New Delhi and- Affiliated
505481 to JNTU, Hyderabad)
#include <stdio.h>
int main() {
int x=42,y=20;
printf("bitwise and=%d\n",x&y);
printf("bitwise or=%d\n",x|y);
printf("bitwise xor=%d\n",x^y);
printf("bitwise leftshift=%d\n",x<<1);
printf("bitwise rightshift=%d\n",x>>1);
printf("bitwise complement=%d\n",~x);
return 0;
}
Output:
bitwise and=0
bitwise or=62
bitwise xor=62
bitwise leftshift=84
bitwise rightshift=21
bitwise complement=-43
#include <stdio.h>
int main()
{
int m = 40,n = 80,AND_opr,OR_opr,XOR_opr,NOT_opr ;
AND_opr = (m&n);
OR_opr = (m|n);
NOT_opr = (~m);
XOR_opr = (m^n);
printf("AND_opr value = %d\n",AND_opr );
printf("OR_opr value = %d\n",OR_opr );
printf("NOT_opr value = %d\n",NOT_opr );
printf("XOR_opr value = %d\n",XOR_opr );
printf("left_shift value = %d\n", m << 1);
printf("right_shift value = %d\n", m >> 1);
}
Output:
AND_opr value = 0
OR_opr value = 120
NOT_opr value = -41
XOR_opr value = 120
left_shift value = 80
right_shift value = 20
In above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if
else conditional statements.
#include <stdio.h>
int main()
{
int x=1, y ;
y = ( x ==1 ? 2 : 0 ) ;
printf("x value is %d\n", x);
printf("y value is %d", y);
}
Output:
x value is 1
y value is 2
C – Increment/decrement Operators
Increment operators are used to increase the value of the variable by one and
decrement operators are used to decrease the value of the variable by one in C
programs.
Syntax:
In this program, value of “i” is incremented one by one from 1 up to 9 using “i++”
operator and output is displayed as “1 2 3 4 5 6 7 8 9”.
#include <stdio.h>
Output:
123456789
In this program, value of “I” is decremented one by one from 20 up to 11 using “i–”
operator and output is displayed as “20 19 18 17 16 15 14 13 12 11”.
#include <stdio.h>
int main()
{
int i=20;
while(i>10)
{
printf("%d ",i);
i--;
}
}
Output:
20 19 18 17 16 15 14 13 12 11
Below table will explain the difference between pre/post increment and decrement
operators in C.
Output:
1234
#include <stdio.h>
int main()
{
int i=0;
while(i++ < 5 )
{
printf("%d ",i);
}
return 0;
}
Output:
12345
Above 3 steps are continued until while expression becomes false and output is
displayed as “1 2 3 4 5”.
Output:
9876
#include <stdio.h>
int main()
{
int i=10;
while(i-- > 5 )
{
printf("%d ",i);
}
return 0;
}
Output:
98765
Special Operators in C:
In this program, “&” symbol is used to get the address of the variable and “*”
symbol is used to get the value of the variable that the pointer is pointing to.
#include <stdio.h>
int main()
{
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}
Output:
50
sizeof() operator is used to find the memory space allocated for each C data types.
#include <stdio.h>
#include <limits.h>
int main()
{
int a;
char b;
float c;
double d;
Output:
Operator Precedence in C
Operator precedence determines the grouping of terms in an expression and decides how an
expression is evaluated. Certain operators have higher precedence than others; for example,
the multiplication operator has a higher precedence than the addition operator.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher
precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the
lowest appear at the bottom. Within an expression, higher precedence operators will be
evaluated first.
Precedence and Associativity are two characteristics of operators that determine the
evaluation order of subexpressions in absence of brackets.
1) Associativity is only used when there are two or more operators of same precedence.
The point to note is associativity doesn’t define the order in which operands of a single
operator are evaluated. For example consider the following program, associativity of the +
operator is left to right, but it doesn’t mean f1() is always called before f2(). The output of
following program is in-fact compiler dependent.
4) Comma has the least precedence among all operators and should be used carefully
For example consider the following program, the output is 1.
#include<stdio.h>
int main()
{
int a;
a = 1, 2, 3; // Evaluated as (a = 1), 2, 3
printf("%d", a);
#include <stdio.h>
int main()
{
int a = 10, b = 20, c = 30;
Expression evaluation
Priority
Associativity
Example 1:
Example 2:
Implicit casting
Explicit casting
Implicit casting doesn't require a casting operator. This casting is normally used when
converting data from smaller integral types to larger or derived types to the base type.
int x = 123;
double y = x;
In the above statement, the conversion of data from int to double is done implicitly, in other
words programmer don't need to specify any type operators.
Explicit casting
Explicit casting requires a casting operator. This casting is normally used when converting a
double to int or a base type to a derived type.
double y = 123;
int x = (int)y;
In the above statement, we have to specify the type operator ( int) when converting from
double to int else the compiler will throw an error.
Conversion operators
Conversion operators help to cast user-defined types from one to the other much like the
basic types. For implicit or explicit conversion, we have to create a static method in the
corresponding class with method name as the type it returns including the keyword that says
implicit or explicit.
The usual arithmetic conversions are not performed for the assignment operators, nor for the
logical operators && and ||.
#include <stdio.h>
main() {
int i = 17;
char c = 'c'; /* ascii value is 99 */
float sum;
sum = i + c;
printf("Value of sum : %f\n", sum );
}
When the above code is compiled and executed, it produces the following result –
Value of sum : 116.000000
Control Statements
Condition: A condition is an expression built with relational operators along with logical
operators.
(a >= b) condition using relational operators
Every condition gives only one result that is either true or false (Boolean).
It has 4 forms…
simple if
if…else
nested if
else if ladder
Simple if: In case of simple if, a condition is tested, if the condition is true then the
statement followed by “if” will be executed.
Flowchart:
Example:
#include <stdio.h>
int main()
{
int m=40,n=40;
if (m == n)
printf("m and n are equal");
}
Output:
if else statement: Using “if else” a condition is tested, if the condition is true then the
statement next to “if” will be executed.
If the condition is false then the statement statement after “else” will be executed.
Any one of the statement parts will be executed but not both.
Flowchart:
Example:
#include <stdio.h>
int main()
{
int age;
int m=40,n=20;
if (m == n)
printf("m and n are equal");
else
printf("m and n are not equal");
}
Output:
nested if.. else statement: If an “if .. else” statement is enclosed within another “if”
statement, then it is called nested if .. else statement.
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m>n)
printf("m is greater than n");
else
if(m<n)
printf("m is less than n");
else
printf("m is equal to n");
}
Output:
m is greater than n
Example:
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m>n)
printf("m is greater than n");
else if(m<n)
Prepared by Dr. V.Neelima, Associate Professor, CSE Dept Page 42
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY AND SCIENCE
KARMNAGAR
(Approved by AICTE, New Delhi and- Affiliated
505481 to JNTU, Hyderabad)
Output:
m is greater than n
Example:
#include <stdio.h>
int main()
{
int choice;
printf("Select any option number from 1 to 4 : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("You have selected first option");
break;
case 2:
printf("You have selected second option");
break;
case 3:
printf("You have selected third option");
break;
case 4:
printf("You have selected fourth option");
break;
default:
printf("Invalid option selected");
break;
}
return 0;
}
Menu
1. addition
2. subtraction
3. multiplication
4. quotient division
5. remainder division
Loop: A loop can be defined as a repetition of statements some given number of times.
Loops can be divided into 2 types.
In case of unconditional loops, the programmer does not know how many times the loop is
to be repeated.
In case of conditional loops, the programmer knows how many times the loop is to be
repeated.
The 3 types of conditional loops are: while loop, do…while loop, and for loop.
Example:
Void main()
{
int count=1;
while (count <=5)
{
printf("%d ", count);
count++;
}
}
Output:
1 2 3 4 5
Syntax: Ex:
do n=1;
{ do
Prepared by Dr. V.Neelima, Associate
Statement part; Professor, CSE Dept { Page 45
} while(condition); printf(“\n n = %d”, n);
} while(n<=10);
next statement;
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY AND SCIENCE
KARMNAGAR
(Approved by AICTE, New Delhi and- Affiliated
505481 to JNTU, Hyderabad)
Example:
Void main()
{
int count=1;
do
{
printf("%d ", count);
count++;
} while (count <=5);
}
Output:
1 2 3 4 5
for loop: for is a conditional repetitive control structure. It is an entry control structure.
The statement in the loop will be repeated until the condition remains true. The for loop
includes initialization, test expression, increment / decrement statements into a single
structure.
Example:
Void main()
{
int count;
for(count=0;count<=5,count++)
printf("%d", count);
}
Output:
1 2 3 4 5
Syntax: goto label; Ex: goto JITS; Syntax: label: Ex: JITS:
Example: printf(“Hello”);
......... ......... statement;
int .main()
........ ......... ......... .........
{ label: JITS: ......... .........
int age;
statement; printf(“Hello”); goto label; goto JITS;
Vote:
printf("you are eligible for voting");
NoVote:
printf("you are not eligible to vote");
return 0;
}
continue
The continue statement in ‘C’ programming forces the next iteration of the loop to take
place, by skipping any code in between.
For the for loop, continue statement causes the conditional test and increment portions of the
loop to execute. For the while and do...while loops, continue statement causes the program
control to pass to the conditional tests.
Output:
0 1 2 3 4 6 7 8 9 10
Output:
0 1 2 3 4 6 7 8 9 10
Output:
0 1 2 3 4 6 7 8 9 10
#include <stdio.h>
int main () {
/* do loop execution */
do {
if( a == 15) {
/* skip the iteration */
a = a + 1;
continue;
}
} while( a < 20 );
return 0;
}
Output
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
Break:
The break statement in C programming has the following two usages −
It can be used to terminate a case in the switch statement (covered in the next
chapter).
In nested loops, the break statement will stop the execution of the innermost loop and start
executing the next line of code after the block.
Syntax:
The syntax for a break statement in C is as follows −
break;
Flow Diagram:
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* while loop execution */
while( a < 20 )
{
printf("value of a: %d\n", a);
a++;
if( a > 15)
{
/* terminate the loop using break statement */
break;
}
}
return 0;
}
When the above code is compiled and executed, it produces the following result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
Explanation :
Comma as Separator:
int num1 = 1, num2 = 2;
1. Function calls
2. Function definitions
3. Variable declarations
4. Enum declarations
Comma as Operator:
res = (num1, num2);
In this case value of rightmost operator will be assigned to the variable. In this case value of
num2 will be assigned to variable res.
Examples of comma operator:
Comma Operator have lowest priority in C Programming Operators. All possible operations
that can be performed on comma operator are summarized below –
Output :
Explanation :
i = 1,2,3;
2. If we check precedence table then we can say that “Comma” operator has lowest
#include<stdio.h>
#include< conio.h>
void main()
{
clrscr();
printf("Computer","Programming");
getch();
}
Output :
Computer
You might feel that answer of this statement should be “Programming” because comma operator
always returns rightmost operator, in case of printf statement once comma is read then it will
consider preceding things as variable or values for format specifier.
Output :
Babo
Output :
Value of J : 0
Value of J : 1
Value of J : 2
Value of J : 3
Value of J : 4