0% found this document useful (0 votes)
32 views16 pages

Pop M1

The document discusses algorithms, flowcharts, pseudocode and the basic structure of C programs. It explains that algorithms are step-by-step procedures to solve problems and lists characteristics of algorithms. It also describes common control structures used in algorithms like sequence, decision and repetition. The document further explains flowcharts, pseudocode and the basic components of C programs like main functions, variables etc.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views16 pages

Pop M1

The document discusses algorithms, flowcharts, pseudocode and the basic structure of C programs. It explains that algorithms are step-by-step procedures to solve problems and lists characteristics of algorithms. It also describes common control structures used in algorithms like sequence, decision and repetition. The document further explains flowcharts, pseudocode and the basic components of C programs like main functions, variables etc.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Module – 1: Introduction to C-

ALGORITHMS: Algorithm is a step-by-step procedure of solving the given problem, which


defines a set of instructions to be executed in a certain order to get the desired output. Algorithms
are generally created independent of underlying languages, i.e. an algorithm can be implemented
in more than one programming language.

An algorithm should have the following characteristics:

 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.
Control Structures Used in Algorithms
 Sequence – each step of the algorithm is executed in the specified order.
 Decision – used when the outcome of the process depends on the condition. A condition
in this context is any statement that may evaluate either to a true or false value.
 Repetition – involves executing one or more steps for a number of times, can be
implemented using constructs such as the while, do-while, and for loops. These loops
execute one or more steps until some condition is true.

Example-1: Algorithm to find area of circle


Input: radius of circle
Output: Area of circle based on given radius
 Step-1: read the radius “r”
 Step-2: Compute the area of circle
Area = 3.14*r*r
 Step-3: print the computed area of circle
Example-2: Algorithm to add two numbers
Input: Two numbers “a” and “b”
Output: Result after addition of two given numbers
 Step-1: read two numbers “a” and “b”
 Step-2: Computation
Result = a+b
 Step-3: print the computed result
FLOWCHART: A flowchart is a type of diagram that represents a workflow or process. A
flowchart can also be defined as a diagrammatic representation of an algorithm, a step-by-
step approach to solving a task.
 The flowchart shows the steps as boxes of various kinds, and their order by connecting the
boxes with arrows. This diagrammatic representation illustrates a solution model to a given
problem.

Basic Flowchart Symbols are as follows:

Example-1: Flowchart to find area of circle


Example-2: Flowchart to add two numbers

Advantages :
 Good communication tools to explain the logic of a system.
 Help to analyse the problem in more effective manner.
 They are used for program documentation.
 They act as a guide or blueprint for the programmers to code the solution in any programming
language.
 They can be used to debug programs that have errors.
Disadvantages :
 Drawing flowchart is a time-consuming activity.
 Flowchart of a complex program becomes complex and clumsy.
 If changes are needed, the flowchart may need to fully redrawn.
 As the flowchart symbols cannot be typed, reproduction of flowchart becomes a problem.
 Factors that affect the sequence are not included.

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.

 The pseudo-code is neither an algorithm nor a program. pseudo-code is a semi-formal


description of the steps to be carried out by the computer, including Steps that are to be
repeated and decisions that are to be made but it constructs/models and maybe even look like
programming code.
Example:

IF (Age >18) THEN


Eligible to Vote
ELSE
Not Eligible to Vote
ENDIF
Advantage of Pseudo Code in C is easier and simpler to understand by the programmers of all
types.

BASIC STRUCTURE OF C-PROGRAM


 Documentation section - The documentation section consists of a set of comment lines
giving the name of the program, the author and other details, which the programmer
would like to use later.

 Link section - The link section provides instructions to the compiler to link functions
from the system library.

 Definition section - The definition section defines all symbolic constants.

 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.

Example-1: C Program to compute area of circle


#include<stdio.h>
int main()
{
float radius, area;
printf("Enter the radius of Circle : \n");
scanf("%d", &radius);
area = 3.14 * radius * radius;
printf("Area of Circle : %f", area);
return 0;
}

Example-2: C Program to compute simple interest


#include<stdio.h>
int main()
{
int p,n,r,si;
printf("Enter Principle, Rate of interest & Time :\n");
scanf("%d %d %d",&p,&r,&n);
si=(p*n*r)/100;
printf("Simple Interest is :%d",si);
return 0;

}
 Keywords - Every word used in a C program is classified as either a keyword or as an
identifier. A keyword in C is a reserved word which has a specific meaning. Keywords
in C cannot be used as identifiers. Keywords serve as the basic building blocks for
program statements
Keywords in C are always in lowercase. ANSI C supports 32 keywords which are
listed below:

 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 are 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.

VARIABLES IN C: A variable is defined as a meaningful name given to a data storage


location in computer memory. When using a variable, we actually refer to address of the
memory where the data is stored.

 Numeric Variables: it can be used to store either integer values or floating point
values. It can also be associated with modifiers, such as short, long, signed and
unsigned. Unsigned variable indicates a variable that can hold only positive numbers.
Signed variable indicates that a variable can hold negative and positive values.

 Character Variables: Single character enclosed within single quotes. These characters
could be any character from the ASCII character set - letters ( ‘a’ , ‘A’ ), numerals
(‘1’) or special characters (‘&’).
Declaring Variables:
 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.
In c variables are declared at three basic places as follows:
 When a variable is declared inside a function it is known as a “local variable”.
 When a variable is declared in the definition of function parameters it is known as a “formal
parameter”.
 When a variable is declared outside all functions it is known as a “global variable”.

Example- int emp_num;


float salary;
char grade;
int acc_no;
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:

1) Primitive or Fundamental data types

2) User-defined data types

3) Derived data types


 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:

1) By using the “typedef” keyword

typedef int rollno;

rollno r1, r2;

2) By using the “enum” keyword

enum identifier{value1, value2… valuen};

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.
Basic Datatypes in C:

Data type Keyword used Size in bytes Range Use

Character char 1 -128 to 127 Store character

Integer Int 2 -32768 to Store whole


32767 numbers

Floating float 4 3.4E-38 to Store decimal


point 3.4E+38 numbers

Double double 8 1.7E-308 to Double-precision


1.7E+308 floating point value

Valueless void 0 valueless -

Detailed Datatypes in C:

OPERATORS in C: An operator is a symbol that tells a computer to perform certain


mathematical or logical or relational operations. Operators are used in programs to manipulate
data and variables. Generally the usage of an operator is as shown below:

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:

 Arithmetic Operators (+,-,*,/,%)

 Relational Operators(<,>,==,!=,>=,<=)

 Equality Operators (== , !=)

 Logical Operators(&&,||,!)

 Unary Operator (++, --, -)


 Conditional Operators (?)
 Bitwise Operators(&,|,^,~,<<,>>)
 Assignment Operators(=,+=,-=,*=,/=,%=)

 Comma Operator (,)

 Sizeof Operator

 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.

 The logical AND operator is represented as the '&&' double ampersand symbol. It
checks the condition of two or more operands by combining in an expression, and if
all the conditions are true, the logical AND operator returns the Boolean value true or
1. Else it returns false or 0.
A B A&&B
0 0 0
0 1 0
1 0 0
1 1 1
 The Logical OR returns a false value if both the operands are false. Otherwise it
returns a true value.

A B A||B
0 0 0
0 1 1
1 0 1
1 1 1
 The Logical NOT takes a single expression and negates the value of the expression.
A !A
0 1
1 0
 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;
 Increment/Decrement Operators - The increment and decrement operators provided by C
are used to increment or decrement the operand by a value of one. Both the increment and
decrement operators are unary operators. There are two variations in increment/decrement
operators

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.
Example: int x =10,y;
y = ++x;
can be written as
x = x+1;
y = x;
 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.
Example: int x=10,y;
y = x++;
can be written as
y = x;
y = x+1;
 Unary minus: The Unary Minus operator is represented using the symbol (-). The
unary operator is used to change the sign of any positive value to a negative value. It
means it changes the positive number to the negative, and a negative number becomes
the positive number using the unary minus operator.
Example: int a,b=10;
a= -(b);

 Conditional Operator - The conditional operator “? :” in C, is a ternary operator, which


operates on three operands. This operator is used to construct conditional expressions of the
form:

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.
Example: large = (a>b) ? a:b
The conditional operator is used to find the larger of two given numbers. First expression1, i.e,
(a>) is evaluated. If a is greater than b, then large = a, else large = b. hence large is equal to either
a or b but not both.

 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. 

 Bitwise AND operator is denoted by the single ampersand sign (&). Two integer
operands are written on both sides of the (&) operator. If the corresponding bits of both
the operands are 1, then the output of the bitwise AND operation is 1; otherwise, the
output would be 0

 The bitwise OR operator is represented by a single vertical sign (|). Two integer operands
are written on both sides of the (|) symbol. If the bit value of any of the operand is 1, then
the output would be 1, otherwise 0.

 Bitwise exclusive OR operator is denoted by (^) symbol. Two operands are written on
both sides of the exclusive OR operator. If the corresponding bit of any of the operand is
1 then the output would be 1, otherwise 0.

 The bitwise complement operator is also known as one's complement operator. It is represented
by the symbol tilde (~). It takes only one operand or variable and performs complement operation
on an operand. When we apply the complement operation on any bits, then 0 becomes 1 and 1
becomes 0.

 Two types of bitwise shift operators exist in C programming. The bitwise shift operators will shift
the bits either on the left-side or right-side. Therefore, we can say that the bitwise shift operator is
divided into two categories:
Left-shift operator: It is an operator that shifts the number of bits to the left-side.
Right-shift operator: It is an operator that shifts the number of bits to the right side.
Example: x is an integer expression with data 1111.
After performing shift operation the result will be:
x << 2 (left shift) = 1111<<2 = 1100
x>>2 (right shift) = 1111>>2 = 0011

The bitwise operators available in C are as shown below:


 Comma Operator - C supports some special operators such as comma “,” operator. The
comma “,” operator is used to combine multiple related expressions together. A comma
separated operands of expressions is evaluated from left to right and the value of the right
most expression is the value of the combined expression.
Example:int a=2, b=3, x=0; x= (++a, b+=a );
Now the value of x=6.


 Equality operator – c language supports two kinds of equality operators to compare their
operands for strict equality or inequality. They are equal to (==) and not equal to (!=)
operators. 
 returns 1 if both operands are equal, others it returns 0.
 (!=) – returns 1 if operands do not have the same value, otherwise it returns 0.
 Sizeof operator - The sizeof operator computes the size of an expression or variable or
constant or a data type. It is a unary operator. It is used to determine the amount of memory
space that the variable/expression/data type will take.
The general syntax of sizeof operator is as shown below:

sizeof(operand);

The operand can be either a value or variable or data type or an expression.


Example: int a=2;
unsigned int result;
result = sizeof(a);

Operator Precedence Chart: Expressions are evaluated based on operator precedence and
associativity rules when an expression contains more than one operator. Every C operator has a
precedence (priority) associated with it. This precedence is used to determine how an expression
involving more than one operator is evaluated.

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.

You might also like