PPS Module2 CSE
PPS Module2 CSE
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)
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.
Flowchart
A diagrammatical representation of an algorithm is called Flowchart.
Shapes used in Flowchart
Documentation Section
Link Section
Definition Section
Global Declaration Section
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
Example 2: 5.5 e3 [i.e: 5.5 x 103], 6.6 e-4 [i.e: 6.6 x 10-4]
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:
+,_,*,%,/
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,
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
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;
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 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
(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,
Operators Example/Description
sum = 10;
= 10 is assigned to variable sum
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
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
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
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
PROGRAMMING EXAMPLES
Hello world program:
//C hello worldexample
#include <stdio.h>
int main()
{
printf("Hello world\n");
return 0;
}
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("Sum = %d\n",add);
printf("Difference = %d\n",subtract);
printf("Multiplication = %d\n",multiply);
printf("Division = %.2F\n",divide);
return 0;
}
#include <stdio.h>
int main()
{
int i=1;
while(i<10)
{
printf("%d ",i);
i++;
}
}
#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
The input and output functions can be classified into two parts:
(i) Formatted Input Output function.
(ii) Unformatted Input Output 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);