Module-1 PPS Notes
Module-1 PPS Notes
• Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or phases),
and their inputs/outputs should be clear and must lead to only one meaning.
• Input − an algorithm should have 0 or more well-defined inputs.
• Output − an algorithm should have 1 or more well-defined outputs, and should match the
desired output.
• Finiteness − Algorithms must terminate after a finite number of steps.
• Feasibility − should be feasible with the available resources.
• Independent − an algorithm should have step-by-step directions, which should be
independent of any programming code.
PSEUDO-CODE: The pseudo code in C consists of words and phrases that make pseudo
code looks similar to the program but not a program. Pseudo codes are written with respect to a
programming language, but programming language syntax or grammar is not strictly followed.
• Pseudocode is made up of two words, ‘pseudo’ and ‘code’. Pseudo means imitation and code
refer to instructions, written in a programming language.
Example:
• Link section - The link section provides instructions to the compiler to link functions
from the system library.
• Global declaration section - There are some variables that are used in more than one
function. Such variables are called global variables and are declared in the global
declaration section that is outside of all the functions. This section also declares all the
user-defined functions.
• main () function section - Every C program must have one main function section. This
section contains two parts; declaration part and executable part. The declaration part
declares all the variables used in the executable part. There must be at least one
statement in the executable part. These two parts must appear between the opening and
closing braces. The program execution begins at the opening brace and ends at the
closing brace. The closing brace of the main function is the logical end of the program.
All statements in the declaration and executable part end with a semicolon.
• Identifiers - refer to the names of variables, functions and arrays. These are user-
defined names and consist of sequence of letters and digits, with a letter as a first
character. Both uppercase and lowercase letters can be used, although lowercase letters
are generally used. The underscore character is also permitted in identifiers.
There certain rules while writing identifiers. They are as follows:
1) First character must be an alphabet or underscore.
2) Must consist of only letters, digits or underscore.
3) Only first 31 characters are significant.
4) Cannot use a keyword.
5) Must not contain white space.
• Constants in C- Constants are fixed values, which do not change during the execution
of a program. C supports several types of constants, which are as shown below:
“Integer Constant” refers to a sequence of digits. Generally in programs, the number
systems used are: decimal, octal and hexadecimal. “Real Constants” integer numbers are
not sufficient to represent quantities that vary continuously, such as distances, height,
prices etc. These quantities are represented by numbers containing fractional parts like
25.234. Such numbers are called as real or floating point constants.
For using variables in our programs, there are essentially two steps:
• Before using a variable in the program, we have to declare the variable. The syntax for
declaring a variable in a program is as shown below:
datatype variable-name;
• The “type” in the above syntax represents the data type. The “variable-name” is the
identifier. There are certain rules that must be followed while writing the variable name.
• Rules for Variables Name –
➢ A variable name must always start with an alphabet (letter) or an underscore ( _ ).
➢ The variable name must not be more than 31 characters. The suggested length of a
variable name is 8 characters.
➢ C is case sensitive. So, the variable name “average” is different from
“AVERAGE”.
➢ Keywords must not be used for declaring variables.
➢ White spaces are not allowed within the variable name.
• Initializing a Variable – After declaring the variable, we can assign a value to the variable.
This process of assigning a value to the variable is known as initialization. Syntax for
initializing a variable is as shown below:
variable-name = value;
The value we assign to the variable depends on the data type of the variable.
int a;
a=10;
The declaration and initialization can be combined into a single line as shown below:
int a=10;
DATA TYPES IN C: A data type specifies the type of value that we use in our programs. A
data type is generally specified when declaring variables, arrays, functions etc.
In ANSI C, the data types are divided into three categories. They are:
• Primitive or Fundamental data types - The primitive data types in ANSI C are as shown in
the below diagram:
• User-Defined data types - ANSI C allows the users to define identifiers as their own data
types, based on the already existing primitive or fundamental data types. This concept is
known as “type definition” and the data types thus created are known as user-defined data
types.
We can create user-defined data types in two ways:
enum days{sun,mon,….sat}
• Derived data types - The data types which are created using the already existing primitive or
fundamental types are known as derived data types. Examples of derived data types in C are:
Arrays, Functions, Structures, Unions & Pointers.
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.
There can be any types of constants like integer, float, octal, hexadecimal, character constants
etc. In C program we can define constants in two ways as shown below:
• #define preprocessor directive - This directive is used to declare constant variable or any
value. We can use this to declare a constant as shown below:
• const keyword - Using const keyword to define constants is as simple as defining variables,
the difference is you will have to precede the definition with a const keyword.
The value of data may be altered by some external factors. When we declare a variable as
volatile, the compiler will examine the value of the variable each time it is encountered to see
any external alteration has changed the value.
Overflow and Underflow: Problem of data overflow and underflow occurs when the value
of a variable is either too big or too small for the type to hold. The largest value that a variable
can hold also depends on the machine. An overflow normally results in the largest value,
whereas an underflow results in zero. C does not provide any warning or indication of integer
overflow. It simply gives incorrect results.
Operand1 Op Operand2
Operand1 and operand2 can be either data or variables or expressions. Op is the operator. In C,
based on the number of operands on which an operator can operate, the operators are divided
into three types namely:
1) Unary
2) Binary
3) Ternary
Different Types of Operators: In C, based on the functionality, operators are classified into
8 categories. They are:
2) Relational Operators(<,>,==,!=,>=,<=)
3 )Logical Operators(&&,||,!)
4) Assignment Operators(=,+=,-=,*=,/=,%=)
6) Conditional Operators(?)
7) Bitwise Operators(&,|,^,~,<<,>>)
• Arithmetic Operators - C provides all the basic arithmetic operators as shown below. The
arithmetic operators can operate on any built-in data type in C.
• Relational Operators - In C, whenever there is a need to compare two values and make a
decision based on the outcome of the comparison, we use relational operators. The relational
operators are generally used in decision making statements like if, else if and in looping
statements like for, while, do while etc. Relational operators always evaluates to 0 (false) or 1
(true).
• Logical Operators - The relational operators are used to compare at most two values i.e.
testing one condition. To test more than one condition, we use logical operators along with
relational operators. The logical operators always evaluates to either 0 or 1 like relational
operators.
• Assignment Operators - The assignment operators are used to assign value of an expression
to a variable. The general assignment operator is = (equal). In C, there are some special
assignment operators known as shorthand operators. The syntax of shorthand operators is as
shown below:
var op=exp;
1) pre-increment/decrement(++var/--var)
2) post-increment/decrement(var++/var--)
When pre increment is applied, the value of the variable is incremented by one first and then
that value is used for evaluation of the expression. When post increment is applied, the value of
the variable is used in the evaluation of the expression and after the expression is evaluated, the
value of the variable is incremented by a value of one.
exp1?exp2:exp3;
In the above syntax, exp1, exp2 and exp3 refer to expressions.It evaluates the exp1 first and then
based on the result of the exp1 it evaluates either exp2 or exp3. If the result of exp1 is true or
non-zero, then exp2 is executed or if the result of exp1 is false or zero, then exp3 is executed.
• Bitwise Operator’s - C supports a set of operators which operate at bit-level. These operators
are known as bitwise operators. The bitwise operators are used for testing a bit, setting a bit,
complementing a bit or for shifting the bits to left or right. The bitwise operators available in
C are as shown below:
• Special Operator’s - C supports some special operators such as comma “,” operator, sizeof
operator, address “&” operator, pointer operator “*” and some others. The comma “,”
operator is used to combine multiple related expressions together. A comma separated list of
expressions is evaluated from left to right and the value of the right most expression is the
value of the combined expression. The sizeof operator computes the size of an expression or
variable or constant or a data type.
sizeof(operand);
The operators at the higher level of precedence are evaluated first. The operators in the same
level of precedence are evaluated from left to right or from right to left, based on
the associativity property of an operator.
In the below table we can look at the precedence levels of operators and also the associativity of
the operators within the same level. Rank 0 indicates the lowest precedence and Rank 14
indicates highest precedence.
TYPE CASTING/ CONVERSION: Typecasting is converting one data type into another
one. It is also called as data conversion or type conversion.
1) Type Conversion
2) Type Casting
• Type Conversion – C automatically converts any intermediate values to the proper type so
that the expression can be evaluated without losing any significance.
• Type casting - There are some scenarios in which we may have to force type conversion.
Suppose we have a variable div that stores the division of two operands which are declared as
an int data type.
(Type_name) expression;
z = (int)a + b
Type casting or conversion hierarchy:
MATHEMATICAL FUNCTIONS:
INPUT-OUTPUT FUNCTIONS: There are two types of a console I/O functions:
1) Formatted input-output function
The major difference is that formatted function allows us to format the input from the keyboard
and the output to be displayed on the screen.
• gets() function can read a full string even blank spaces presents in a string. But, the scanf()
function leave a string after blank space space is detected. The gets() function is used to get
any string from the user.
gets(var_name);
• puts() function prints the charater array or string on the console. The puts() function is
similar to printf() function, but we cannot print other than characters using puts() function.
puts(var_name);
Example:
char c[25];
printf("Enter your Name : ");
gets(c);
puts(c);
Syntax: printf(“string”);
Example: printf(“GOD”);
• That has literal string, conversion specifier and any or all of the following: variables,
constant and expressions values to be printed.
Example: printf(“%d%f”,x,y);
C provides scanf function in the stdio.h header file. Using this function we can read values from
the keyboard and assign the values to variables. The syntax of scanf function is as shown below:
scanf(“control string”,&var1,&var2……)
The control string specifies the type of value to read from the keyboard and the ampersand
symbol & is an operator to specify the address the variable(s).
scanf(“%c”,&h);
scanf(“%s”, name);
scanf(“%d”,&a[i]);
SELECTION OR BRANCHING STATEMENTS: The C statements that transfer the
control from one place to other place in the program with or without any condition are called
branching or selection statements.
1] Conditional Control /Branch Statements: Conditional branching statements that alter the
sequence of execution of the program based on some condition are called Conditional branching
statements/ selection statements /decision statements.
Conditional Control /Branch Statements are as follows: simple if (single selection), if-else (two
way selection), Nested if (multiple if statements), else –if ladder (multi-way selection), switch (
multi-way selection).
2] Unconditional Control /Branch Statement: The statements that alter the sequence of
execution of the program based on some uncondition are called Unconditional branching
statement.
Unconditional Control /Branch Statements are as follows: Goto Statement, break Statement,
continue Statement and return Statement.
void main()
int age;
scanf(“%d”,&age);
if(age>=18)
b) If-else Statement: : It is an extension of if statement .It is used to execute any one set of
two set of statements at a time. If condition is true it executes one set of statements otherwise
it executes another set of statements.
Example: Program to determine whether a person is eligible to vote using if-else statement.
#include<stdio.h>
void main()
{
int age;
scanf("%d",&age);
if(age>=18)
else
printf("not eligible");
c) Nested If-else Statement: : When a series of decisions are involved we may have to use
more than one if else statement in nested form. The nested if else statements are multi
decision statements which consist of if else control statement within another if or else control
statement.
The syntax and flow chart of nested if-else statement is as illustrated below:
Example: Program to find the largest of three numbers using Nested if else statement
#include<stdio.h>
void main()
int a,b,c ;
scanf("%d %d %d",&a,&b,&c);
if (a>b)
if (a>c)
else
else
if(b>c)
else
When an action has to be performed based on many decisions, then this Statement is used when
more than one if-else statements are used in a sequence, it is called as if-else ladder.
if( condition-1)
statement-1;
else if( condition-2)
statement-2;
statement-3;
statement-n;
else
default-statement;
Statement-x;
• Disadvantage: difficult to understand and modify when problem is big, which can be
overcome by using switch Statement.
Example: Program to print the grade obtained by the student based on the given table using else-
if ladder.
#include<stdio.h>
void main()
int marks;
scanf("%d",&marks);
printf("Grade F\n");
printf("Grade E\n");
else if (marks>=50 && marks<=59)
printf("Grade D\n");
printf("Grade B\n");
printf("Grade A\n");
printf("Outstanding\n");
else
switch (choice/expression)
case value1:block1;
break;
break;
break;
default : default_block ;
next Statement;
The value of choice or expression is always an integer value/Character. If the value of choice or
expression is 1 then block1 statements will get executed. After executing the respective block the
control comes outside the switch statement because of the break statement. If no case values
matches with the value of the choice, then the default block will get executed before exiting
switch statement.
#include<stdio.h>
void main( )
{
int num;
printf(“Enter number: ”);
scanf(“%d”, &num);
if(num%2==0)
{
printf(“Even Number”);
}
else
{
printf(“Odd Number”);
}
}