0% found this document useful (0 votes)
45 views

PPS Module2 CSE

The document provides an overview of the C programming language, including its history and basic structure. It discusses algorithms and flowcharts, C tokens like keywords and identifiers, variable data types, operators and expressions. It also covers input/output operations and examples of writing algorithms.

Uploaded by

Sachin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

PPS Module2 CSE

The document provides an overview of the C programming language, including its history and basic structure. It discusses algorithms and flowcharts, C tokens like keywords and identifiers, variable data types, operators and expressions. It also covers input/output operations and examples of writing algorithms.

Uploaded by

Sachin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Module II: Overview of C

Overview of C: Representation of Algorithm/Pseudo code and flowchart with examples,


Basic Structure of C Program, C-tokens, variable and data types, Operators, and
expressions, Managing input and output operations.

INTRODUCTION to C LANGUAGE
C Language was designed and developed by Dennis Ritchie at BELL Laboratories in 1972. C
programming language is derived from earlier languages B, BCPL (Basic Combined
Programming Language) and CPL (Combined Programming Language).

ALGORITHM
An algorithm is a step by step procedure to solve a given problem. The good algorithm
should contain the following
• Finite number of steps.
• Each step should be clear and meaningful.
• The input and output should be defined precisely.
• An algorithm should not have computer code. ( C program)

Example1: Write an algorithm to multiply 2 numbers entered by user.


Step 1: Start
Step 2: Declare variables nuM1, nuM2 andmul.
Step 3: Read values of nuM1 and nuM2.
Step 4: Multiply nuM1 & nuM2 and assign the result to mul.
mul = nuM1+nuM2
Step 5: Display mul
Step 6: Stop

Example 2: Write an algorithm to find area and perimeter of a rectangle.


Step 1: Start
Step 2: Declare variables length, breadth, areaand perimeter.
Step 3: Read values of length and breadth.
Step 4: Multiply length & breadth and assign the result to area.
area= length*breadth
Step5: Compute the perimeter using the formula
perimeter= 2* (length+breadth)
Step6: Displayareaand perimeter.
Step7: stop.

Example 3: Write an algorithm to compute simple interest.


Step 1: Start
Step 2: Declare variables p, r, t and I.
Step 3: Read values of p, r and t.
Prepared By : CSE Page 1
Step 4: Multiply p, r & t and assign the result to I.
I=p*r*t
Step5: Display I.
Step7: stop.

Example 4: Write an algorithm to find sum and average of given three numbers.
Step 1: Start
Step 2: Declare variables a, b, c, sum and avg.
Step 3: Read values of a, b and c.
Step 4: Find the sum of a, b, c.
sum= a+b+c
Step 5: Find the average of a, b, c.
avg= sum/3;
Step 6: Displaysum, avg.
Step 7: Stop.

Example 5: Write an algorithm to find the largest of three numbers.


Step 1: Start
Step 2: Declare variables a, b, c.
Step 3: Read values of a, b and c.
Step 4: Comparison
if ((a>b) and (a>c)) then
display a is greatest.
else if ((b>a) and (b>c)) then
display b isgreatest.
else
display c is greatest.
Step 5: Stop.

Prepared By : CSE Page 2


Example 6: Write an algorithm to swap the two variables using third variable.
Step 1: Start
Step 2: Declare variables a and b.
Step 3: Read values of a and b.
Step 4: Exchange a & b
temp=a
a=b
b=temp
Step 5: Display a and b.
Step 6: Stop.

Flowchart
A diagrammatical representation of an algorithm is called Flowchart.
Shapes used in Flowchart

THE STRUCTURE OF THE C PROGRAM

Documentation Section
Link Section
Definition Section
Global Declaration Section

Prepared By : CSE Page 3


main () function section
{
Declaration part;
Executable part;
}
Subprogram section
Function 1
Function 2
.
.
.
.
Function n
(i) Documentation Section
Documentation section consists of set of comment lines. This comment lines
indicate name of the program, author and other details.

Prepared By : CSE Page 4


(ii) Link Section
The Like section provides instruction to the compiler to link functions from the
system library such as using the #include directive.
(iii) Definition Section
The definition section defines all symbolic constants such as using the #define
directive.
(iv) 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. This section also
declares all the user-defined functions.
(v) Main() function Section
Every C program must have one main function section. This section contains two
parts:
a) Declaration Part:
The declaration part declares all the variables used in the executable part.

b) Executable part:
There is 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 the statements in the declaration
and executable part end with a semicolon.
(vi) Subprogram section:
If the program is a multi function program then the subprogram section contains all
the user defined functions that are called in the main() function. User defined functions are
generally placed immediately after the main () function, although they may appear in any
order.

Example:
//Documentation section
/* Program Name: Sample Program
Author:aaa
*/
//Link section
#include <stdio.h>
// Definition Section
#define MAX_ARRAY_LENGTH 20
// Global declaration section
int max_input_length=15;
// Declaring user definedfunction
float addNumbers(float a, float b);
//Main() function section
int main()
{
int localvariable=50; //Declaration part
clrscr();
Prepared By : CSE Page 5
//Executable part
printf(“Maxarraylengthis%d\n”, MAX_ARRAY_LENGTH);
printf (“Max input length is %d\n”, max_input_length);
printf(Sum of 5.5 and 8.5=%f\n”, addNumbers(5.5,8.5));
return 0;
}
//defining an user definedfunction which has been declared.
float addNumbers(float a, float b)
{
return (a+b);
}
ALPHABET
The various categories of characters are used while writing a program is called alphabet.
The alphabet may be letter or digit or any special symbol. There are 96 symbols in C.

Example:
• Letters A-X, a-z, both upper and lower
• Digits 0-9
• Symbols such as + - * /%
• White spaces

C TOKENS
A token is a smallest or basic unit of a C program. One or more characters are grouped in
sequence to form meaningful words. These meaningful words are called tokens. C tokens
are classified as follows:
(i) Keywords
(ii) Identifiers
(iii) Constants
(iv) Operators
(v) Special symbols

(i) Keywords:
The words which have predefined meaning in C language are called keyword. And
this keywords are reserved for specific purpose in C language. They are also called
reserved words.
Example:
int, float, if, else, etc…

(ii) Identifiers:
Identifiers are the names given to program elements such as variables, constants,
function names, array names, etc.. It consists of sequence of one or more letters or digits
along with “_” (underscore). Rules to be followed to frame an identifier are:
• The first character in the identifier should be a letter or “_” (underscore) and can be
followed by any number of letters or digits or underscores.
Prepared By : CSE Page 6
• Noextra symbols areallowed other than letters, digits and“_”.
• The length of an identifier can be maximum of 31 characters for external names.
Such as function names and global variables.
• The length of an identifier can be maximum of 63 characters for internal names such
as local variables.
• Keywords cannot be used asidentifiers.
• Identifiers are case sensitive.

Example:
n, add, sum, sub
iii. Constants:
Constants are values that will not change during execution of the program.
Constants are classified as
(a) Integer Constants
(b) Floating point constants
(c) Character constants
(d) String constants
(e) Enumeration constants

(a) Integer constants:


Integer constants are numeric values without any fractional part or exponential
part. The integer constants are classified into 3 types.
• Decimal constants
The decimal constants can be formed using the numbers 0,1,2,3,4,5,6,7,8,9.
Example: 55, -66
• Octal constants
The octal constants can be formed using the numbers 0, 1,2,3,4,5,6,7.
Example: 055,022
• Hexadecimal constants
Thehexadecimal constants can be formed using the numbers
0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F.
Example: 0021, 034A, 056B

(b) Floating point constants:


Floating point constants are numeric values that have either fractional form or
exponent form.
Example 1: 5.5, -6.6, +7.7

Example 2: 5.5 e3 [i.e: 5.5 x 103], 6.6 e-4 [i.e: 6.6 x 10-4]

(c) Character constants:


Character constants contain only one character enclosed within a pair of single
quote marks. Example: ‘J’, ‘S’

(d) String Constants:


Prepared By : CSE Page 7
String constants contain zero or more characters which are enclosed within a pair of
double quote marks.
Example:
“ “  NULL string constants.
“ “  String constants of 5 white space.
“J” String constants has single character
“SSCE”  String constants has one word.
“welcome to ssce”  String constants has one sentence.

(e) Enumeration constants:


The collection of named integer contants defined using the keyword “enum’ are
called enumeration constants.

Example:
enum days{sun, mon, tue, wed, thur, fri, sat};

Where
• enum is keyword.
• Days is variable name
• sun, mon, tue, wed, thur, fri, sat are enumeration constants having value
0,1,2,3,4,5,6 respectively by default.
(iii) Operators
Operators aresymbols which perform the operation such asaddition, substraction, etc
between values or variables.
Example:
+,_,*,%,/

(iv) Special symbols


The special symbols are used in c program for specific reasons. There following special
symbols are used in cprogram.
a. Braces{}
The blocks of code written in the program between braces.
b. Parentheses()
The parentheses are used to indicate function calls and function parameters.
c. Brackets[]
The brackets are used as array element reference.

DECLARATION

What is variable?
Variable is an identifier whose value can be changed during execution of the
program. When we declare variable the memory location will be allocated based on data
type of that particular variable. We can store and manipulate values in that memory
location.
Declaration of the variable:
Prepared By : CSE Page 8
The declaration tells to the C compiler
- What is the name of the variable?
- What is the data type of the variable?
Initialization of the variable:
The variables can be initialized when it has been declared.
Syntax:
data type V1=VALUe;

Example:
int m=10;

ASSIGNMENT STATEMENT
Assignment statement is used to assign the value for a given variable at any point of time in
the given program.
Syntax:
variable name= value (or) expression;

Example:
c=30;
c=a+b;
What is multiple assignment statement?
Multiple assignment statement is used to assign single value to more than one
variable at anytime and in a single statement.
Example:
a=b=30;
d=c=a+b;

DATA TYPES
C data types are defined as the data storage format. The variable can store a data to
perform a specific operation. Data types are used to define a variable before to use in a
program. There are four data types in C language. They are,

Types Data Types

A. Basic data types int, char, float, double

B. Enumeration data type Enum

C. Derived data type pointer, array, structure, union

D. Void data type Void

A. Basic data types in c language:


Integer data type:
Integer data type allows a variable to store numeric values. “int” keyword is used to refer
Prepared By : CSE Page 9
integer data type. The storage size of int data type is 2 or 4 or 8 byte.
Character data type:
Character data type allows a variable to store only one character. Storage size of character
data type is 1. We can store only one character using character data type. “char” keyword is
used to refer character data type.

Floating point data type:


Floating point data type consists of 2 types. They are,
a. float
b. double
a. FLOAT:
Float data type allows a variable to store decimal values. Storage size of float data type is
3. This also varies depend upon the processor in the CPU. We can use up-to 6 digits after
decimal using float data type.
b. DOUBLE:
Double data type is also same as float data type which allows up-to 10 digits after decimal.
The range for double data type is from 1E–37 to 1E+37.

Modifiers in c language:
The amount of memory space to be allocated for a variable is derived by modifiers.
Modifiers are prefixed with basic data types to modify (either increase or decrease) the
amount of storage space allocated to a variable. For example, storage space for int data type
is 4 byte for 32 bit processor. We can increase the range by using long int which is 8
byte. We can decrease the range by using short int which is 2 byte.
There are 5 modifiers available in C language. They are,
1. short
2. long
3. signed
4. unsigned
5. long long
Below table gives the detail about the storage size of each C basic data type in 16 bit
processor. Please keep in mind that storage size and range for int and float datatype will
vary depend on the CPU processor (8, 16, 32 and 64 bit)

CDatatypes/ storage
Size Range

char / 1 –127 to 127

int / 2 –32,767 to 32,767

1E–37 to 1E+37 with six digits of


float / 4 precision

1E–37 to 1E+37 with ten digits of


double / 8 precision

Prepared By : CSE Page 10


1E–37 to 1E+37 with ten digits of
long double / 10 precision

long int / 4 –2,147,483,647 to 2,147,483,647

short int / 2 –32,767 to 32,767

unsigned short int / 2 0 to 65,535

signed short int / 2 –32,767 to 32,767

long long int / 8 –(2POWer(63) –1) to 2(power)63 –1

signed long int / 4 –2,147,483,647 to 2,147,483,647

unsigned long int / 4 0 to 4,294,967,295

unsigned long long int /


8 2(power)64 –1

B. Enumeration data type in c language:


Enumeration data type consists of named integer constants as a list. It start with 0 (zero)
by default and value is incremented by 1 for the sequential identifiers in the list.
Enum syntax in C:
enum identifier [optional{ enumerator-list }];
Enum example in C:
enum month { Jan, Feb, Mar }; or
/* Jan, Feb and Mar variables will be assigned to 0, 1 and 2 respectively by default */
C. Derived data type in C language:
Array, pointer, structure and union are called derived data type in C language.
D. Void data type in C language:
Void is an empty data type that has no value. This can be used in functions and pointers.

OPERATORS
An operator is a symbol which helps the user to command the computer to do a certain
mathematical or logical manipulations.
1. Arithmetic operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increments and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators

1. Arithmetic Operators
Prepared By : CSE Page 11
The operators which are used to perform arithmetic operation such as addition,
subtraction, multiplication, divisionandmodulus operation arecalledarithmetic operators.
The operator and meaning of the operator is shown below:
+ addition
- subtraction
* multiplication
/ division
% modulus
Example:
c=a+b;
d=e-f;

The arithmetic operator is classified into three groups:


A. Integer Arithmetic
When an arithmetic operation is performed on two whole numbers or integers then
this operation is called as integer arithmetic. It always gives an integer as the result.
Example:
Let x = 32 and y = 5 be TWO integer numbers then
x + y = 37
x – y = 27
B. Floating point arithmetic
When an arithmetic operation is performed on two real numbers or fraction
numbers then this operation is called floating point arithmetic.
Example:
Let x = 24.0 and y = 4.0 be TWO real numbers then
x + y = 28.0
x – y = 20.0
C. Mixed mode arithmetic
If the arithmetic operation is performed on ONE real number and ONE integer
number then it is called as mixed mode arithmetic. The result of the mixed mode
arithmetic operation is always real number.
Example:
Let x=55 and y=10.0 then
x/y => 55/10.0 = 5.5

2. Relational Operators
Relational Operators are used to compare the relationship between operands and bring out
a decision.

Operators Example/Description

> x > y (x is greater than y)

< x < y (x is less than y)

>= x >= y (x is greater than or equal to y)


Prepared By : CSE Page 12
<= x <= y (x is less than or equal to y)

== x == y (x is equal to y)

!= x != y (x is not equal to y)

3. Logical Operators
• These operators are used to perform logical operations on the given expressions and
produce the results.
• There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||)
and logical NOT (!).
Operators Example/Description

&& (logical (x>5)&&(y<5)


AND) It returns true when both conditions are true

(x>=10)||(y>=10)
|| (logical It returns true when at-least one of the condition is
OR) true
!((x>5)&&(y<5))
It reverses the state of the operand “((x>5) &&
(y<5))”
! (logical If “((x>5) && (y<5))” is true, logical NOT operator
NOT) makes it false

4. Assignment Operators
In C programs, values for the variables are assigned using assignment operators.
For example, if the value “20” is to be assigned for the variable “sum”, it can be assigned
as “sum = 20;”
There are 2 categories of assignment operators in C language. They are,

1. Simple assignment operator (Example: =)


2. Compound assignment operators (Example: +=, -=, *=, /=, %=, &=, ^=)

Operators Example/Description

sum = 10;
= 10 is assigned to variable sum

sum += 10;
+= This is same as sum = sum + 10

Prepared By : CSE Page 13


sum -= 10;
-= This is same as sum = sum – 10

sum *= 10;
*= This is same as sum = sum * 10

sum /= 10;
/= This is same as sum = sum / 10

sum %= 10;
%= This is same as sum = sum % 10

sum&=10;
&= This is same as sum = sum & 10

sum ^= 10;
^= This is same as sum = sum ^ 10

5. Increments and DecrementOperators


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:
Increment operator: ++var_name; (or) var_name++;
Decrement operator: – -var_name; (or) var_name – -;
Example:
Increment operator : ++i ; i ++;
Decrement operator : – – i ; i – – ;

6. Conditional Operators
Conditional operators return one value if condition is true and returns another value is
condition is false. This operator is also called as ternary operator.
Syntax : (Condition? true_value: false_value);
Example: (A > 500 ? 0 : 1);
In above example, if A is greater than 500, 0 is returned else 1 is returned. This is equal to if
else conditional statements.

7. Bitwise Operators
These operators are used to perform bit operations. First 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 languageare

• & – Bitwise AND


• | – Bitwise OR
• ~ – Bitwise NOT
• ^ – XOR
• << – Left Shift
Prepared By : CSE Page 14
• >> – Right Shift

Example:
Consider x=40 and y=80. Binary forms of these values are given below.
x = 00101000
y= 01010000
All bit wise operations for x and y are given below.
• x&y = 00000000 (binary) = 0 (decimal)
• x|y = 01111000 (binary) = 120(decimal)
• ~x = 11111111111111111111111111
11111111111111111111111111111111010111 = -41 (decimal)
• x^y = 01111000 (binary) = 120(decimal)
• x << 1 = 01010000 (binary) = 80 (decimal)
• x >> 1 = 00010100 (binary) = 20 (decimal)

8. Special Operators
Below are some of the special operators that the C programming language offers.
Operators Description

This is used to get the address of the


variable.
& Example : &a will give address of a.

This is used as pointer to a variable.


Example : * a where, * is pointer to
* the variable a.

This gives the size of the variable.


Sizeof () Example : size of (char) will give us 1.

C EXPRESSIONS
• A sequence of operand and operators that reduces to a single value after evaluation
is called expression.
• There are 5 types of expression in C language:
1. Arithmetic Expression
Prepared By : CSE Page 15
a. Single Mode Arithmetic Expression
b. Mixed Mode Arithmetic Expression
2. Relational Expression
3. Logical Expression
4. Assignment Expression
5. Short hand assignment expression

1. Arithmetic Expression:
• Arithmetic expression is a combination of variables, arithmetic operators and
constants.
• The arithmetic expression produces a single value when it evaluated.
• Example: x + y
Where,
x and y are called operands
+ is called operator
• There are 2 types of Arithmetic Expression
a. Single Mode Arithmetic Expression
b. Mixed Mode Arithmetic Expression

a. Single Mode ArithmeticExpression:


• In single mode arithmetic expression, all operands should be same datatype.
• The datatype of result is same as datatype of operand.
• There 2 types of single mode arithmetic expression
(i) Integer mode Arithmetic Expression
(ii) Floating mode Arithmetic Expression
(i) Integer mode ArithmeticExpression:
• In this type of expression all the operands or constants are integer datatype and
result is also integer datatype.
• Example:
4+4=8
5*5=25
(ii) Floating mode ArithmeticExpression:
• In this type of expression all the operands or constants are float datatype and result
is also float datatype.
• Example:
4.55-3.45=1.1
8.5 / 2.2= 3.86
b. Mixed Mode Arithmetic Expression:
• In this type of expression all the operands or constants contain both the integer and
Real Datatype (i.e. float datatype (or) double datatype) and result is always real
datatype.
• In mixedmode arithmetic expression, integer operands are converted into real
datatype before doing any computation.
• Example:

Prepared By : CSE Page 16


2+ 2.5= 4.5
1 / 2.0 = 0.5
2. Relational Expression:
• Thistype of expression contains relational operators, variables andconstants. The
result of the relational expression is either TRUE or FALSE.
• Example:
(a+b) > (c+d)
(b*b-4*a*c) >= 0
3. Logical Expression:
• Thistype of expression contains logical operators andrelational expressions. The
result of the logical expression is either TRUE or FALSE.
• Example:
(a<=b) && (c>=d)
4. Assignment Expression:
• This type of expression contains assignment operator, variables and constants.
• Example:
c = a+b

5. Short hand assignmentexpression:


• Thistype of expression contains shorthand assignment operator, variables and
constants.
• Example:
a+=5

PROGRAMMING EXAMPLES
Hello world program:
//C hello worldexample
#include <stdio.h>
int main()
{
printf("Hello world\n");
return 0;
}

Addition of TWO numbers:


#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter twonumberstoadd\n");
scanf("%d%d",&a,&b);

c = a + b;

printf("Sumofenterednumbers=%d\n",c);
Prepared By : CSE Page 17
return 0;
}
Addition, Subtraction, Multiplication and Division:
#include <stdio.h>
int main()
{
int first, second, add, subtract, multiply;
float divide;

printf("Enter two integers\n");


scanf("%d%d", &first, &second);

add = first + second;


subtract = first - second;
multiply = first* second;
divide = first / (float)second; //typecasting

printf("Sum = %d\n",add);
printf("Difference = %d\n",subtract);
printf("Multiplication = %d\n",multiply);
printf("Division = %.2F\n",divide);

return 0;
}

Increment and Decrement Operator Program:


//Example for increment operators

#include <stdio.h>
int main()
{
int i=1;
while(i<10)
{
printf("%d ",i);
i++;
}
}

//Example for decrement operators

#include <stdio.h>
int main()
{
int i=20;
while(i>10)
Prepared By : CSE Page 18
{
printf("%d ",i);
i--;
}
}

Conditional Operator:
#include <stdio.h>
void main()
{
int a = 10, b = 11;
int c;
c = (a < b)? a : b;
printf(“%d”, c);
}

Bitwise AND
#include <stdio.h>
Void main()
{
int a=12, b=25;
printf(“Output=%d”, a&b);
}

Output:
Output=8

MANANGINI INPUT AND OUTPUT FUNCTIONS

The input and output functions can be classified into two parts:
(i) Formatted Input Output function.
(ii) Unformatted Input Output Function.

(i) Formatted Input Output Function


(a) printf () function
(b) scanf() function

(a)printf() function:
The output of the C program is printed using printf() statement. The printf statement tells to
the compiler to display / print the string or expression value or variable value in the output
device (monitor).
Example 1: [print only strings]
printf(“Welcome to C program”);
Example 2: [print string and values]
Prepared By : CSE Page 19
printf(“%d”, c);
here %d is called format specifier or conversion specifier.

(b)scanf () function:
The input (data) of the program can be given by keyboard or data file. When we want to give
input through the keyboard we can use scanf statements. The format specifier (or)
conversion specifier used in the printf statement can be used in scanf statement also.
syntax:
scanf(“Format specifier”, address list);
Example:
Scanf(“%d%d”, &n,&m);

(ii) Unformatted Input Output function:


(a) getchar() and putchar()
(b) getch() and putch()
(c) gets() and puts()
(d) getche()

(a) getchar() and putchar() function:


The getchar() function is used to read ONE character (letter) from the keyboard and stores
in the memory. The putchar() function is used to display character on the screen.
Example:
char ch;
ch=getchar();
putchar(ch);

(b) getch() and putch() function:


The getch() function is used to read ONE character from the keyboard without echo (i.e: The
typed character will not be display on the output screen). The putch() function is used to display
characters which is stored in the memory location.

(c) gets() and puts() function:


The gets() function is used to read a string from the keyboard and stores in the memory. The
puts() function is used to display a string which is stored in the memory.

(d) getche() function:


The getche() function is used to read ONE character from the keyboard with echo. (i.e: The
typed character will be displayed on the screen).

Prepared By : CSE Page 20

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy