CS25C01 Unit 1&2 Notes
CS25C01 Unit 1&2 Notes
Regulation
PROGRAMMING 2025
UNIT I- INTRODUCTION
Evolution of Programming Languages - Programming Paradigms : Structured programming -
Object Oriented programming - Functional programming -Algorithms - Pseudo code and
Flowchart - Structure of C Program - Compilation Process -Preprocessor Directives - C Tokens.
1.1.1 Introduction:
Usually computer software may be classified into application software and system
software. Application Software – Set of programs necessary to carry out operations for specified
applications. System Software – General programs written for the system, which provide the
environment to facilitate the writing of application software.
Programming language translators are the best example of system software.
Programming Paradigm deals with the evolution of programming languages.
The purpose of using computers is to solve problems, which are difficult to solve
manually. So the computer user must concentrate on the development of good algorithms for
solving problems. After preparing the algorithms, the programmer or user has to concentrate the
programming language which is understandable by the computer.
A language that used to set instructions for computer is known as programming language.
It is a set of instructions that tells the computer what to do. Programming languages are used to
create software applications, websites, and other computer programs. The first programming
languages were developed in the 1940s and 1950s. They were very simple and inefficient, but
they were a necessary step in the evolution of computer programming.
The era of machine code marked the beginning of programming languages' development.
The language that the hardware of the computer can understand directly is called machine code.
It is a very basic language that is challenging to write and debug. The first programming-
languages was created using machine code. These languages were very simple and not so
powerful, but they were a necessary step in the progression of computer programming.
The assembly language era followed the machine code period. Assembly language is a
higher-level language than machine code, but it is still a lower level language. Assembly
language uses to represent machine code instructions. This makes assembly language code more
user-friendly and efficient. High-level languages are becoming more popular, while assembly
language is becoming less so.
From 1960s the high-level language periods began. High level languages are easy to
write and debug compare to machine code or assembly language. High level programming
languages are more easily movable than assembly, meaning that they can be used without
rewritten. This is because high level languages are not specific to any architecture, while
assembly language is specific. Some of the most famous high level languages include:
The modern age of programming languages started from 1990s. This era is marked by the
creation of new programming approaches, such as object-oriented and functional programming.
These languages are used to develop a wide range of applications, from web apps to mobile apps
to desktop apps to scientific computing apps.
Some of the most famous programming languages in the modern age are
JAVA
PYTHON
JAVASCRIPT
C#
RUBY
SWIFT
KOTLIN
GO
RUST
***************
Paradigm can also be termed as method to solve some problem or do some task.
Programming paradigm is an approach to solve problem using some programming language or
also we can say it is a method to solve a problem using tools and techniques that are available to
I Year/I Sem Page 3
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025
us following some approach. There are lots for programming language that are known but all of
them need to follow some strategy when they are implemented and this methodology/strategy is
paradigm. Apart from varieties of programming language there are lots of paradigms to fulfil
each and every demand. They are discussed below
Imperative programming is divided into three broad categories: Structured, Procedural and
Object Oriented Programming (OOP). These paradigms are as follows:
Since the invention of the computer, many programming approaches have been tried.
These include techniques such as modular programming, top-down programming, bottom-up
programming and structured programming. The primary motivation in each case has been the
concern to handle the increasing. Complexity of program that is reliable and maintainable. These
techniques became popular among programmers over the last two decades.
With the advent of languages such as C, structured programming became very popular and was
the paradigm of the 1980s. Structured programming proved to be a powerful tool that enabled
programmers to write moderately complex programs fairly easily. However, as the programs
grew larger, even the structured approach failed to show the desired results in terms of bug-free,
easy-to-maintain, and reusable programs. Procedural and Object Oriented Programming (OOP)
Paradigms were derived from Structured Programming Paradigm.
**************
1.3.1. Procedural programming paradigm
This paradigm emphasizes on procedure in terms of under lying machine model. There is
no difference in between procedural and imperative approach. It has the ability to reuse the code
and it was boon at that time when it was in use because of its reusability.
on the objects only. More emphasis is on data rather procedure. It can handle almost all kind of
real life problems which are today in scenario.
Advantages:
Data security
Inheritance
Code reusability
Flexible and abstraction is also present
knowledge base and the problem. The execution of the program is very much like proof of
mathematical statement, e.g. Prolog
**************
1.5. Algorithm
Clear and Unambiguous: The algorithm should be unambiguous. Each of its steps
should be clear in all aspects and must lead to only one meaning.
Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined
inputs. It may or may not take input.
Well-Defined Outputs: The algorithm must clearly define what output will be yielded
and it should be well-defined as well. It should produce at least 1 output.
Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite time.
Feasible: The algorithm must be simple, generic, and practical, such that it can be
executed with the available resources. It must not contain some future technology or
anything.
I Year/I Sem Page 8
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025
***********
1.7. Flowchart and Pseudo code
1.7.1. Flowchart
Flowchart consists of sequentially arranged boxes that depict the process flow.
Since it visually represents an algorithm or workflow, it is easier to interpret and understand.
However, to create an effective Flowchart, certain standardized rules must be followed
Different types of boxes are used to make Flowchart Symbols. All the different kinds of
boxes are connected by arrow lines. Arrow lines are used to display the flow of control.
Table.1.1. Flowchart Symbols
This box is of an oval shape which is used to indicate the start or end of the
program. Every Flowchart diagram has an oval shape that depicts the start of an
algorithm and another oval shape that depicts the end of an algorithm.
Input/output
Process
This is a rectangular box inside which a programmer writes the main course of
action of the algorithm or the main logic of the program. This is the crux of the Flowchart
as the main processing codes is written inside this box. For example: if the programmer
wants to add 1 to the input given by the user, he/she would make the following
Flowchart:
Decision
This is a rhombus-shaped box, control statements like if, condition like a > 0, etc
are written inside this box. There are 2 paths from this one which is “yes” and the other
one is “no”. Every decision has either yes or no as an option, similarly, this box has these
as options. For example: if the user wants to add 1 to an even number and subtract 1 if the
number is odd, the Flowchart would be:
On-Page Reference
This circular figure is used to depict that the Flowchart is in continuation with the
further steps. This figure comes into use when the space is less and the Flowchart is long.
Any numerical symbol is present inside this circle and that same numerical symbol will
be depicted before the continuation to make the user understand the continuation. Below
is a simple example depicting the use of On-Page Reference.
Algorithm:
1. Start
2. Input 2 variables from user
3. Now check the condition If a > b, go to step 4, else go to step 5.
4. Print a is greater, go to step 6
5. Print b is greater
6. Stop
Flowchart:
Problem 2: Draw a Flowchart to check whether the input number is odd or even
Algorithm:
1. Start
2. Put input a
3. Now check the condition if a % 2 == 0, go to step 5. Else go to step 4
4. Now print(“number is odd”) and go to step 6
5. print(“number is even”)
6. Stop
Flowchart:
***********
1.7.2 Pseudo code
Organize the sequence of tasks and write the pseudo code accordingly.
At first, establishes the main goal or the aim.
Example:
This program will print first N numbers.
Standard programming structures such as if-else, for, while, and cases should be
used. Statements if-else, for, while loops should be indented like they are
indented in a program, it helps to comprehend the decision control mechanism. It
also improves readability to a great extent.
Example:
IF “1”
print response
“I AM CASE 1”
IF “2”
print response
“I AM CASE 2”
Appropriate naming conventions should be followed. so the naming must be
simple and distinct.
Reserved commands or keywords must be represented in capital letters.
Example: if you are writing IF…ELSE statements then make sure IF and
ELSE be in capital letters.
Check whether all the sections of a pseudo code are complete, finite, and clear to
understand and comprehend. Also, Mention everything in pseudo code that is
going to happen in the actual code.
Pseudo code should not be written in a programming language. It is necessary
that the pseudo code is simple and easy to understand even for a layman or client.
IF (condition)THEN BEGIN
statement ELSE
ENDIF END IF
END
statement GET n
PRINT i
i=i+1
ENDFOR
END
statement BEGIN
ENDWHILE WHILE(i<=n) DO
PRINT i
i=i+1
ENDWHILE
END
There are no rules to writing There are certain rules for writing pseudo
algorithms code
*************
1.7.2.3. History of ‘C’ Language
‘C’ is one of the most popular programming languages, it was developed by Dennis
Ritchie at AT & T’s Bell Laboratories at USA in 1972. It is an upgraded version of two earlier
languages, called BCPL and B, which were also developed at Bell Laboratories.
The following table illustrates the history of “C” Language.
C stands in between these two categories. It is neither a low level language nor high level
language. It is a middle level language. It means, it performs, the task of low level language as
well as high level language. We can write the codes for Operating System, Application
Programs, and Assembly Language Program in ‘C’ language. UNIX Operating System is written
in ‘C’ language.
‘C’ is a robust language whose rich set of built in functions and operators can be used to
write any complex program.
‘C’ has the ability to extend itself.
‘C’ is well suited for writing system software application software.
‘C’ is a middle level language. i.e., the supports both the low level language and high
level language features.
‘C’ is widely available, commercial ‘C’ compilers are available on most PC’s.
‘C’ programs are fast and efficient.
‘C’ has got rich set of operators.
‘C’ can be applied in System Programming areas like compilers, interpreters and
assemblers, etc.,
Documentation Section: It consists of a set of comment lines used to specify the name
of program, the author and other details etc.,
Comments: Comments are very helpful in identifying the program features and
underlying logic of the program. The lines beginning with ‘/*’ and ending with ‘*/’ are
known as comment lines. These are not executable, the compiler is ignored anything in
between /* and */.
Preprocessor Section: It is used to link system library files, for defining macros and for
defining the conditional inclusion.
Eg., #include<stdio.h> , #define A 10, #if def, #endif, …etc.
Global Declaration Section: The variables that are used in more than one function
throughout the program are called global variables and declared outside of all the
function i.e., before main().
Every ‘C’ program must have one main() function, which specify the starting of ‘C’
program. It contains the following two parts.
Declaration Part: This part is used to declare all the variables that are used in the
executable part of the program and these are called local variables.
Executable Part: It contains at least one valid ‘C’ statement. The execution of a program
begins with opening brace ‘{‘ and ends with closing brace ‘}’. The closing brace of the
main function is the logical end of the program.
**************
1.9. Compilation Process
I. Execution of a C program
Execution is a process of running the program, to execute a ‘C’ program, we need to
follow the steps given below.
1. Creating the program
2. Compiling the program
3. Linking the program with system library
The following illustrates the process of creating, compiling, and executing a ‘C’ program.
Characteristics of a Program
Clarity: It refers to the readability of the program with underlying logic.
Integrity: It refers to the accuracy of the program. If the calculations are not performed
correctly, all other program enhancements will be meaningless.
Simplicity: The clarity and accuracy of the program is usually enhanced by keeping the
things as simple as possible with the overall program objectives.
Efficiency: It refers to the execution speed and efficient memory utilization of the
program.
Modularity: Many programs can be broken down into a series of identifiable sub
programs. Each sub program can be called as functions in C program. The modularity of
the program design, enhances the accuracy and clarity of the program.
Generality: Usually, the program is to be as general as possible, within reasonable units.
****************
A program in C language involves into different processes. Below diagram will help you to
understand all the processes that a C program comes across.
There are 4 regions of memory which are created by a compiled C program. They are,
1. First region – This is the memory region which holds the executable code of the
program.
2. 2nd region – In this memory region, global variables are stored.
3. 3rd region – stack
4. 4th region – heap
Difference between stack and heap memory:
Stack Heap
Stack is a memory region where “local Heap is a memory region which is used by
variables”, “return addresses of function calls” dynamic memory allocation functions at run
and “arguments to functions” are hold while C time.
program is executed.
CPU’s current state is saved in stack memory Linked list is an example which uses heap
memory.
Difference between compilers and interpreters:
Compilers Interpreters
Compiler reads the entire source code of the Interpreter reads the program source code one
program and converts it into binary code. This line at a time and executing that line. This
process is called compilation. process is called interpretation.
Program speed is fast. Program speed is slow.
One time execution. Interpretation occurs at every line of the
Example: C, C++ program. Example: BASIC
Output
Output
Output
Program
Output
Output
Pragma is used to call a function before and after main function in a C program.
Example Program
Output
******************
1.11. Tokens in C
In C programming, tokens are the smallest units in a program that have meaningful
representations. Tokens are the building blocks of a C program, and they are recognized by the C
compiler to form valid expressions and statements. Tokens can be classified into various
categories, each with specific roles in the program.
Identifiers
Keywords
Operators
Strings
Special Characters
Constant
Let’s look at each of these- one by one in detail:
1.11.1. Identifiers
These are used to name the arrays, functions, structures, variables, etc. The identifiers are
user-defined words in the C language. These can consist of lowercase letters, uppercase letters,
digits, or underscores.
We must write identifiers in such a way that it is not only meaningful- but also easy to
read and short.
1.11.2. Keywords
Keywords are predefined or reserved words that have special meanings to the compiler.
These are part of the syntax and cannot be used as identifiers in the program. A list of 32
keywords in C or reserved words in the C programming language is mentioned below:
1.11.3. Operators in C
The operators in C are the special symbols that we use for performing various functions.
Operands are those data items on which we apply the operators. We apply the operators in
between various operands. On the basis of the total number of operands, here is how we classify
the operators:
Unary Operator
Binary Operator
Ternary Operator
Unary Operator
The unary operator in c is a type of operator that gets applied to one single operand, for example:
(–) decrement operator, (++) increment operator, (type)*, sizeof, etc.
Binary Operator
Binary operators are the types of operators that we apply between two of the operands. Here is a
list of all the binary operators that we have in the C language:
Relational Operators
Arithmetic Operators
Logical Operators
Shift Operators
Conditional Operators
Bitwise Operators
Misc Operator
Assignment Operator
Ternary Operator
Using this operator would require a total of three operands. For instance, we can use the ?: in
place of the if-else conditions. Example: C= a<b? a: b ;
1.11.4. Strings
Strings are nothing but an array of characters ended with a null character (‘\0’). This
null character indicates the end of the string. Strings are always enclosed in double quotes.
Whereas a character is enclosed in single quotes in C. Example: “hello”
The following special symbols are used in C having some special meaning and
thus, cannot be used for some other purpose. Some of these are listed below:
() Simple brackets – We use these during function calling as well as during function
declaration. For instance, the function printf() is pre-defined.
[ ] Square brackets – The closing and opening brackets represent the multidimensional
and single subscripts.
(,) Comma – We use the comma for separating more than one statement, separating the
function parameters used in a function call, and for separating various variables when we
print the value of multiple variables using only one printf statement.
{ } Curly braces – We use it during the closing as well as opening of any code. We also
use the curly braces during the closing and opening of the loops.
(*) Asterisk – We use this symbol for representing the pointers and we also use this
symbol as a type of operator for multiplication.
(#) Hash/preprocessor – We use it for the preprocessor directive. This processor basically
denotes that the user is utilizing the header file.
(.) Period – We use the period symbol for accessing a member of a union or a structure.
(~) Tilde – We use this special character in the form of a destructor for free memory.
1.11.6. Constant in C
Constants are fixed values used in a C program. These values do not change during the
execution of the program. Constants can be integers, floating-point numbers, characters, or
strings.. Here are two of the ways in which we can declare a constant:
***********
Each variable in C has an associated data type. It specifies the type of data that the
variable can store like integer, character, floating, double, etc.
Example:
int number;
The above statement declares a variable with name number that can store integer values.
C is a statically type language where each variable’s type must be specified at the declaration
and once specified, it cannot be changed. This is because each data type requires different
amounts of memory and allows type specific operations.
Primitive Data Primitive data types are the most basic data types that int, char, float,
Types are used for representing simple values. double, void
User Defined The user-defined data types are defined by the user structure, union,
Data Types himself. enum
Format specifiers are the symbols that are used for printing and scanning values of
given data types.
Example:
We use int keyword to declare the integer variable:
int val;
A variable of given data type can only contains the values of the same type. So, var can only
store numbers, not text or anything else
The integer data type can also be used as:
1. unsigned int: It can store the data values from zero to positive numbers, but it can’t
store negative values
2. short int: It is lesser in size than the int by 2 bytes so can only store values from -
32,768 to 32,767.
3. long int: Larger version of the int datatype so can store values greater than int.
Note: The size of an integer data type is compiler dependent. We can use sizeof operator to
check the actual size of any data type. In this article, we are discussing the sizes according to
64-bit compilers.
Example:
void fun(int a, int b){
// function body
}
**************
1.11.8. Variables
C variable is a named location in a memory where a program can manipulate the data.
This location is used to hold the value of the variable.
The value of the C variable may get change in the program.
Scope of Variables:
It implies the availability of variables within the program. Variables have two types of
scopes namely local variables and global variables.
Local Variables:
The scope of local variables will be within the function only.
These variables are declared within the function and can’t be accessed outside the
function.
Global Variables:
The scope of global variables will be throughout the program. These variables can be
accessed from anywhere in the program.
This variable is defined outside the main function. So that, this variable is visible to main
function and all other sub functions.
Difference between variable declaration and
definition:
**************
3. In the algorithm, plain text is used. In the flowchart, symbols/shapes are used.
6. The algorithm does not follow any rules. The flowchart follows rules to be constructed.
10. Define preprocessor directive and list out the types of preprocessor directives. (K1)
Before a C program is compiled in a compiler, source code is processed by a program
called preprocessor. This process is called preprocessing. Commands used in
preprocessor are called preprocessor directives and they begin with “#” symbol.
Macro - #define
Header File Inclusion -#include
Conditional Compilation -#if, #else, #endif
Other Directives -#pragma
13. Show the difference between variable declaration and initialization. (K2)
Variables should be declared in the C program before to use. Memory space is not
allocated for a variable while declaration. It happens only on variable definition.
14. What is data type? And list out the types of data type in C. (K1)
C data types are defined as the data storage format that a variable can store a data to
perform a specific operation. Data types are used to define a variable before to use in a
program. Size of variable, constant and array are determined by data types. There are three
data types in C language. They are,
Primitive Data Primitive data types are the most basic data types that int, char, float,
Types are used for representing simple values. double, void
User Defined The user-defined data types are defined by the user structure, union,
Data Types himself. enum
17. Define variable and list out the rules for naming a variable. (K1)
C variable is a named location in a memory where a program can manipulate the data.
This location is used to hold the value of the variable. The value of the C variable may
get change in the program. Rules for naming Variable
Variable name must begin with letter or underscore.
Variables are case sensitive
They can be constructed with digits, letters.
No special symbols are allowed other than underscore.
sum, height, _value are some examples for variable name
Identifiers
Keywords
Operators
Strings
Special Characters
Constant
19. What is an identifier? (K1)
It is used to name the arrays, functions, structures, variables, etc. The identifiers are user-
defined words in the C language. These can consist of lowercase letters, uppercase letters,
digits, or underscores.
Local Variables:
The scope of local variables will be within the function only.
These variables are declared within the function and can’t be accessed outside
the function.
Global Variables:
The scope of global variables will be throughout the program. These variables
can be accessed from anywhere in the program.
This variable is defined outside the main function. So that, this variable is
visible to main function and all other sub functions.
C language is being used since the early days of computer programming and still is
used in wide variety of applications such as:
C is used to develop core components of operating systems such as Windows,
Linux, and macOS.
C is applied to program embedded systems in small devices such as washing
machines, microwave ovens, and printers.
C is utilized to create efficient and quick game engines. For example, the
Doom video game engine was implemented using C.
C is employed to construct compilers, assemblers, and interpreters. For
example, the CPython interpreter is written partially using C.
C is applied to develop efficient database engines. The MySQL database
software is implemented using C and C++.
C is employed to create programs for devices and sensors of Internet of
Things (IoT).
25. What is modifier? And list out the types of modifier. (K1)
Modifiers are prefixed with basic data types to modify (either increase or
decrease) the amount of storage space allocated to a variable.
There are 5 modifiers available in C language. They are,
short
long
signed
unsigned
long long
PART B
PART C
1. Develop algorithm, Flowchart and pseudo code to check whether a number is odd or
even. (K3)
2. Develop a pseudo code for menu driven program to perform various arithmetic
operations. (K3)
3. Develop algorithm, flowchart and pseudo code to find factorial of given number.
(K3)
4. Develop a flowchart for swapping two numbers of variables using third variable.
(K3)
2.1. Expression in C
Example: C= a+b;
1. Arithmetic Expression
2. Relational Expression
3. Logical Expression
4. Conditional Expression
Types of Expressions in C
Example:
c=a+b;
c=a+b*10;
Relational expressions use comparison operators such as '>' (greater than) and '<' (less
than) to compare two operands.
It is a condition that decides whether the action should be taken or not .The result of the
comparison is integer value i.e. 0 (false) or non-zero (true).Relational expressions are used
in decision-making statements and looping statements in the C language .
Relational
Description
Expression
The given condition checks whether the x is an even number or not. This
x%2 = = 0 relational expression shows the result as the value 1 if x is an even number
otherwise as the value 0
Logical expressions in C which use logical operators are a powerful tool for controlling
the logic of the flow of the program. They are made by combining as many relational
expressions as the programmer wants. It then determines if certain statements or groups of
statements should be executed or not.
The conditional expression which uses ternary operator (?:) consists of three operands . It
returns 1 if the condition is true otherwise 0.
Example : c = a<b? a : b;
*****************
2.2. Input / Output Statements
We know that input, process, output are the three essential features of computer program.
The program takes some input data, processes it and gives the output.
We have two methods for providing data to the program.
o Assigning the data to the variables in a program
o By using the input/output statements.
Two types of I/O statements are available in ‘C’ language and all input and output
operations are carried out through the function call. Several functions are available for
input/output operations in ‘C’. These functions are collectively known as the standard I/O
library.
o Unformatted Input / Output statements
o Formatted Input / Output statements
printf( ) function
printf() function is used to print the “character, string, float, integer, octal and
hexadecimal values” onto the output screen.
We use printf() function with %d format specifier to display the value of an integer
variable.
Similarly %c is used to display character, %f for float variable, %s for string
variable, %lf for double and %x for hexadecimal variable.
To generate a newline, we use “\n” in C printf() statement.
Note: C language is case sensitive. For example, printf() and scanf() are different from
Printf() and Scanf(). All characters in printf() and scanf() functions must be in lower case.
scanf( ) function
scanf() function is used to read character, string, numeric data from keyboard.
The format specifier %d is used in scanf() statement. So that, the value entered is
received as an integer and %s for string.
Ampersand is used before variable name “ch” in scanf() statement as &ch.
*****************
2.3. Operators in ‘C’
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 Operators
C language offers many types of operators. They are,
1. Arithmetic operators
2. Assignment operators
3. Relational operators
4. Logical operators
5. Bit wise operators
Assignment operators are used to assign value to a variable. The left side operand of the
assignment operator is a variable and the right side operand of the assignment operator is a
value. The assignment operators can be combined with some other operators in C to provide
multiple operations using single operator. These operators are called compound operators.
S. No. Symbol Operator Description Syntax
The relational operators in C are used for the comparison of the two operands. All these
operators are binary operators that return true or false values as the result of comparison.
#include <stdio.h>
int main() {
int a = 25, b = 5;
return 0;
}
Output
a<b :0
a>b :1
a <= b: 0
a >= b: 1
a == b: 0
a != b : 1
Logical Operators are used to combine two or more conditions. The result of the operation of a
logical operator is a Boolean value either true or false.
Returns true if
&& Logical AND both the operands a && b
1 are true.
Returns true if
|| Logical OR both or any of the a || b
2 operand is true.
#include <stdio.h>
int main() {
int a = 25, b = 5;
return 0;
}
Output
a && b : 1
a || b : 1
!a: 0
The Bitwise operators are used to perform bit-level operations on the operands. The operators
are first converted to bit-level and then the calculation is performed on the operands.
#include <stdio.h>
int main() {
int a = 25, b = 5;
return 0;
}
Output
a & b: 1
a | b: 29
a ^ b: 28
~a: -26
a >> b: 0
a << b: 800
The conditional operator is the only ternary operator in C. It is a conditional operator that we can
use in place of if..else statements. It operates on three operands.
Syntax
expression1 ? Expression2 : Expression3;
Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then we
will execute and return the result of Expression2 otherwise if the condition(Expression1)
is false then we will execute and return the result of Expression3.
The increment and decrement operators are one of the unary operators which are very useful in C
language. It operates on single operand.
They are extensively used in for and while loops. The syntax of the operators is given below
++ variable name (Pre increment)
variable name++ (Post increment)
-–variable name
variable name– –
The increment operator ++ adds the value 1 to the current value of operand and the decrement
operator – – subtracts the value 1 from the current value of operand. ++variable name and
variable name++ mean the same thing when they form statements independently, they behave
differently when they are used in expression on the right hand side of an assignment statement.
Consider the following
M=5;
y = ++m; (prefix)
In this case the value of y and m would be 6
Suppose if we rewrite the above statement as
m =5;
The operator size of gives the size of the data type or variable in terms of bytes occupied in the
memory. The operand may be a variable, a constant or a data type qualifier.
Example
m = sizeof (sum);
n = sizeof (long int);
k = sizeof (235L);
The size of operator is normally used to determine the lengths of arrays and structures when their
sizes are not known to the programmer.
The comma operator can be used to link related expressions together. A comma-linked list of
expressions is evaluated left to right and value of right most expression is the value of the
combined expression.
First assigns 10 to x and 5 to y and finally assigns 15 to value. Since comma has the lowest
precedence in operators the parenthesis is necessary.
structure_variable . member;
structure_pointer -> member;
Addressof operator & returns the address of a variable and the dereference operator
* is a pointer to a variable
**********
Example:
#include <stdio.h>
int main() {
int a = 6, b = 3, c = 4;
int res;
printf("%d", res);
return 0;
}
Output
12
Explanation: The above expression is evaluated as 6 + ( (3 * 4) / 2) = 12, not (6 + 3) * (4 / 2) =
18. So what guided the compiler to evaluate the expression in this way? It is actually the
precedence and associativity of the operators used in this expression .
----------------------------******************-----------------------------
2.5.1 if statement
In “if” control statement, respective block of code is executed when condition is true.
Example Program
Output
Output
Output
}
else {
// Else body
}
where,
condition1, condition2: Contitions to be tested.
statements 1, statements 2: Code block corresponding to each condition.
An if-else ladder can exclude else block.
Example:
#include <stdio.h>
int main() {
int marks = 85;
// Assign grade based on marks
if (marks >= 90) {
printf("A\n");
} else if (marks >= 80) {
printf("B\n");
} else if (marks >= 70) {
printf("C\n");
} else if (marks >= 60) {
printf("D\n");
}
else
{
printf("F\n");
}
return 0;
}
Output:
B
Explanation:
The program checks the value of marks and assigns a grade based on predefined ranges.
It checks from highest to lowest, printing the grade based on the conditions met, or “F” if none
are met.
Example Program
Output
Syntax for each C loop control statements are given in below table with description.
Output
Output
Output
while do…while
Loop is executed only when Loop is executed for first time
condition is true. irrespective of the condition. After
executing while loop for first time,
then condition is checked.
Example Program
Output
Output
Example Program
Output
-------------------------------------****************---------------------------------
PART A
Example: C= a+b;
Formatted Input / Output refers to input and output, that has been arranged in a particular
format. Printf() and scanf() functions are inbuilt library functions which are an example
of Formatted I/O Function Which requires format specifier
Unformatted input and output functions are only work with character data type.
Unformatted input and output functions do not require any format specifiers. Because
they only work with character data type .Example : getchar() and purchar() functions
are inbuilt library functions which are an example of Unformatted I/O Function Which
does not require format specifier
5. How do you read the string with white space from user? (K1)
String with white space cannot be read from user with help of formatted i/p function scanf () .
Whereas this can be done with help of unformatted input function gets ().
6. Define Ternary Operator. (K1)
The conditional operator is the only ternary operator in C. It is a conditional operator that we
can use in place of if..else statements. It operates on three operands.
Syntax
expression1 ? Expression2 : Expression3;
Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then
we will execute and return the result of Expression2 otherwise if the condition(Expression1)
is false then we will execute and return the result of Expression3.
7. Explain all types of Unary Operator. (K2)
The increment and decrement operators are one of the unary operators which are very useful
in C language. It operates on single operand.
They are extensively used in for and while loops. The syntax of the operators is given below
++ variable name (Pre increment)
variable name++ (Post increment)
__variable name(Pre decrement)
variable name– – (Post decrement)
y = ++m; (prefix), y = m++; (post fix)
8. State about sizeof operator. (K2)
The operator sizeof gives the size of the data type or variable in terms of bytes occupied in
the memory. The operand may be a variable, a constant or a data type qualifier.
Example
m = sizeof (sum);
n = sizeof (long int);
k = sizeof (235L);
The size of operator is normally used to determine the lengths of arrays and structures when
their sizes are not known to the programmer.
9. Define precedence and associativity. (K1)
Precedence is used to determine the order in which different operators in complex
expressions are evaluated. The operators of the same precedence evaluated either from left to
right or right to left depending on the level is known as associativity.
10. Explain about Decision Making Statement. (K2)
In decision control statements (if-else and nested if), group of statements are executed when
condition is true. If condition is false, then else part statements are executed. There are 3
types of decision making control statements in C language. They are,
if statements
if else statements
nested if statements
Else if ladder statement
11. Define Dangling-else ambiguity. (K1)
The dangling else problem in syntactic ambiguity. It occurs when we use nested if. When
there are multiple "if" statements, the "else" part doesn't get a clear view with which "if " it
should combine. Dangling else can lead to serious problems. It can lead to wrong
interpretations by the compiler and ultimately lead to wrong results.
Initialize k=0 and o=0
if(ch>=3)
if(ch<=10)
k++;
else
o++;
12. Why Switch statement is called as multi-way decision making statement? (K1)
The switch case statement is a multi-way decision-making statement that chooses the
statement block to be executed by matching the given value with a list of case values.The
switch statement enables us to do several operations for the various possible values of a
single variable known as the switch variable.
13. Differentiate while and do…while looping statements. (K2)
while do…while
Its pre test loop Its post test loop
Body of the Loop is Body of the Loop is executed for
executed only when first time irrespective of the
condition is true. condition. After executing while
loop for first time, then condition is
checked.
The condition which used in if-else can The expression in switch is usually an integral type or an
1
be any Boolean expression. enumerated type.
It can handle multiple conditions using Designed for evaluating a single expression against
2
else if clauses. multiple constants.
if-else doesn't have fall-through behavior switch can have fall-through behavior (case statements
3
by default. without breaks).
Optional else block for handling cases not Optional default case for handling cases not explicitly
4
explicitly covered. covered.
Suitable for complex, non-trivial More concise for handling a series of conditions based
5
conditions. on a single expression.
15. Show the differences between pre test and post test looping statements. (K2)
Pretest loops check the condition before entering the loop's body, while posttest loops
check the condition after executing the loop's body at least once.
Pretest loops may not run the loop's body at all if the condition is false initially, while
posttest loops always run the loop's body at least once.
16. Mention the significance of break statement in loops. (K1)
The break in C is a loop control statement that breaks out of the loop when encountered. It
can be used inside loops or switch statements to bring the control out of the block. The
break statement can only break out of a single loop at a time
17. Show the differences between break and continue statements. (K2)
The primary difference between break and continue statement in C is that the break
statement leads to an immediate exit of the innermost switch or enclosing loop.
On the other hand, the continue statement only skips the current iteration and begins
the next iteration of the while, enclosing for, or do loop
18. Define infinite loop. (K1)
An infinite loop (or, an endless loop) is a never-ending looping construct that executes a set
of statements forever without terminating the loop. It has a true condition that enables a
program to run continuously.
While(1) for(; ;)
{ {
Statement; Statement;
} }
PART B
1. Discuss in detail about various operators used in C and its precedence with an example. (K2)
2. Discuss the various Conditional Branching Statements used in C with its syntax and flow
diagram. (K2)
3. Explain the importance of the following loop control statements with example. (K2)
(i) break statement (5)
(ii) continue statement (4)
(iii) goto statement (4)
4. Discuss the various Input and Output statements used in C with example. (K2)
5. Develop a C program for the following: (K3)
(i) To find the sum of the digits of a number. (123 =>1+2+3=6.) (7)
(ii)To find the sum of all odd / even numbers between 1 and100. (6)
PART C
1. Develop a C program for the following: (K3)
(i) To check whether a number is prime or not. (8)
(ii) To convert the temperature given in Fahrenheit to Celsius and vice versa. (7)
2. Develop a menu driven program in C to perform various arithmetic operations. (K3)
3. Develop a C program for the following: (K3)
4. Distinguish between branching and looping statements used in C. (K3)
(i) To find the power of a Number Using the while loop and using pow () function. (8)
(ii) To print a pattern of Floyd triangle. (7)