Unit 2
Unit 2
UNIT - II
Features of C:
• It is a structured programming language.
• It is highly portable.
• It is a middle level language.
• It is a case sensitive language.
• It has rich set of libraries.
• It uses Top-Down approach.
C Tokens
C Tokens
Keywords
Identifiers ConstantsStrings
operatorsspI
symbol
Eg:main, Eg: int, Eg:17,Eg: “ab”Eg: + Eg: #
avg for 15.5 - $%
Identifiers:
• Only the first 31 characters of an identifier are significant and are recognized
by the compiler.
• Cannot use a keywords or reserved word (e.g. main, include, printf & scanf
etc.).
UNIT-II GE6151-COMPUTER PROGRAMMING
Variable:
Eg: x=3
• Here x is variable.
Keywords:
• It is a reserved words.
• Examples:
– int
– while
– for etc,.
Constants:
• It is an entity whose value does not changes during the execution
Types:
Numeric Constant
Character Constant
Constants
Numeric Constants Character Constants
Numeric Constant:
Integer constants
Decimal - 0 to 9 .
Octal - 0 to 7.
Hexa - 0 to 9 ,A to F
Real Constatnts:
Character Constant
String constants:
Executing a C program
Compilation
Linking
Execution
STRUCTURE OF A C PROGRAM:
Preprocessor Commands
Functions
Variables
Comments
UNIT-II GE6151-COMPUTER PROGRAMMING
Documentations
The documentation section consist of a set of comment lines giving the name of the program, the
another name and other details, which the programmer would like to use later.
Preprocessor Statements
The preprocessor statement begins with # symbol and are also called the preprocessor directive.
These statements instruct the compiler to include C preprocessors such as header files and
symbolic constants before compiling the C program. Some of the preprocessor statements are
listed below.
#include <stdio.h>
#include <math.h>
#include <conio.h>
#define X 30
Global Declarations
The variables are declared before the main ( ) functions as well as user defined functions
are called global variables. These global variables can be accessed by all the user defined
functions including main ( ) function.
Each and Every C program should contain only one main ( ). The C program execution
starts with main ( ) function. No C program is executed without the main function. The main ( )
function should be written in small (lowercase) letters and it should not be terminated by
semicolon. Main ( ) executes user defined program statements, library functions and user defined
functions and all these statements should be enclosed within left and right braces.
Braces
Every C program should have a pair of curly braces ({, }). The left braces indicates the
beginning of the main ( ) function and the right braces indicates the end of the main ( ) function.
These braces can also be used to indicate the user-defined functions beginning and ending. These
two braces can also be used in compound statements.
Local Declarations
The variable declaration is a part of C program and all the variables are used in main ( )
function should be declared in the local declaration section is called local variables. Not only
variables, we can also declare arrays, functions, pointers etc. These variables can also be
initialized with basic data types.
void main()
{
Int a, b; // Local Variables
}
These are subprograms, generally, a subprogram is a function and these functions are written by
the user are called user defined functions. These functions are performed by user specific tasks
and this also contains set of program statements. They may be written before or after a main ()
function and called within main () function. This is an optional to the programmer.
void main()
{
UNIT-II GE6151-COMPUTER PROGRAMMING
Data Types
Operators
Arithmetic operator
Relational operator
Logical operator
Assignment operator
Bitwise operator
Conditional operator
Arithmetic Operator
Eg: + , - , * , / etc,
Program:
Relational Operator:
• It is used to compare two or more operands.
Eg : < , > , <= , >=, !=
Logical Operator:
• It is used to combine the result of two or more condition.
AND(&&)
OR (||)
NOT (!) are Logical operators.
Eg: (i>10)&&(j>5).
(i>10)||(j>5) etc,.
Program:
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a=10,b=3,c=5,e;
clrscr( );
if(a>b) // relational operator
{
printf(" \n a is bigger than b");
}
if((a>b)&&(a>c)) //Logical operator
{
printf(" \n a is biggest");
}
getch( );
UNIT-II GE6151-COMPUTER PROGRAMMING
Assignment operator:
• It is used to assign a value or expression etc to a variable.
• Eg: a =10.
a=b
a = b + c etc.,
Compound operator
It is also used to assign a value to a variable.
Eg: x + = y means x = x + y
Nested operator
It is used for multiple assignments.
Eg: i = j = k = 0;
Program:
#include<stdio.h>
#include <conio.h>
int b=10;
void main ( )
{
int a=3,b=5;
clrscr( );
a+=b; // a= a+b
printf(" \n The sum of the two values:%d",a);
getch( );
}
Program:
UNIT-II GE6151-COMPUTER PROGRAMMING
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a=5;
clrscr( );
printf(" \n Post increment Value:%d",a++);
printf(" \n Pre increment Value:%d",++a);
printf(" \n Pre decrement Value:%d",--a);
printf(" \n Post decrement Value:%d",a--);
getch( );
}
Bitwise Operator:
• It is used to manipulate data at bit level.
Program:
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a=5,b=4,c;
//char a=5,b=4,c;
clrscr( );
c = a&b;
printf(" \n value a&b is:%d",c);
getch( );
}
Program:
UNIT-II GE6151-COMPUTER PROGRAMMING
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a=5,b=8,c;
clrscr( );
c = a>b?a:b; //Conditional operator
printf(" \n The Larger Value is%d",c);
getch( );
}
Special Operator:
• comma operator ( , )
• sizeof operator
• pointer operator (& , *) etc,.
Program:
#include<stdio.h>
#include <conio.h>
void main ( )
{
int c;
clrscr( );
printf(" \n size of int is:%d", sizeof( c));
getch( );
}
Expression:
• An expression represent data item such as variable, constant are
interconnected using operators.
• Eg:
Expression C Expression
a+b+c a+b+c
UNIT-II GE6151-COMPUTER PROGRAMMING
Precedence Operator
High *,/,%
Low +,-
Type Conversion:
• Converting the type of an expression from one type to another type.
Eg: x = (int)10.45
Program:
#include<stdio.h>
#include <conio.h>
void main ( )
{
int c;
clrscr( );
c=(int)10.45;
printf("\nOutput is:%d",c);
getch( );
}
OUTPUT
UNIT-II GE6151-COMPUTER PROGRAMMING
Output is: 10
Input/Output
Function
Formatted Unformatted
Output Input Output
Input
tolower(ch)
toupper(ch)
• getchar() :
It accepts one character type data from the keyboard.
Example
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char x;
printf("enter the character:");
x=getchar();
if(islower(x))
putchar(toupper(x));
UNIT-II GE6151-COMPUTER PROGRAMMING
else
putchar(tolower(x));
getch();
}
Output:
enter the character:A
a
• getche() :
It displays the entered character in the screen.
Example:
#include <stdio.h>
#include <conio.h>
void main()
{
char c ;
clrscr();
printf("\nInput a string:");
c = getche();
printf("\nstring is:");
putch(c);
getch();
}
• getch():
getch() accepts only single character from keyboard. The character
entered through getch() is not displayed in the screen (monitor).
• gets(char):
It accepts any line of string including spaces from the standard Input
device (keyboard). gets() stops reading character from keyboard only
when the enter key is pressed.
Example:
#include <stdio.h>
UNIT-II GE6151-COMPUTER PROGRAMMING
#include<conio.h>
void main()
{
char c[80];
clrscr();
printf("Input a string:");
gets(c);
Standard Output
• When you need to display data that is not text, it must be converted into to
the text before it is written to the screen.
• scanf consists of :
A format string .
(“%c….%d…..%f…..”, &a,….&i,…..,&x…..)
Control Structure
Control structures are blocks of code that dictate the flow of control.
• Categories:
– Sequential structure
In which instructions are executed in sequence.
– Selection structure
In which instruction are executed based on the result of some
condition
UNIT-II GE6151-COMPUTER PROGRAMMING
– Iteration structure
In which instruction are executed repeatedly.
Selection Structure:
• It allows the program to make a choice from alternative paths.
• C provide the following selection structures
– IF statement
– IF … ELSE statement
– Nested IF … ELSE statement
– IF … ELSE ladder
IF Statement
Syntax
If
IF (condition is true)
Tr
condition
ue
Statements
{ Fa
ls
e
Statements;
Example:
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a;}
clrscr( );
printf("\nEnter the number:");
scanf("%d",&a);
if(a>10)
{
UNIT-II GE6151-COMPUTER PROGRAMMING
IF…ELSE Statement
Syntax If
IF (condition) Tru
Condition Fal
{ True
e False
se
statements statements
True statements;
}
Example:
ELSE
#include<stdio.h>
#include <conio.h>
{
{
void main ( )
int a;
False statements;
clrscr( );
}
printf("\nEnter the number:");
scanf("%d",&a);
if(a>10)
{
printf(" \n a is greater than 10");
}
else
{
printf(" \n a is less than 10");
}
getch( );}
UNIT-II GE6151-COMPUTER PROGRAMMING
If
Condition
False
1
Tr If Statements
Condition
Tr ue Fa
True 2 False
ue lse
statementsstatements
Syntax
IF (condition1)
{
IF (condition2)
{
True statements;
}
ELSE
{
False statements;
}
}
ELSE
{
False statements;
}
UNIT-II GE6151-COMPUTER PROGRAMMING
IF…ELSE LADDER
Condition
FA
1 Condition
LS
Statements FAE 2
Condition
LS
Statements TR E FA
UE
3 LS
Statements Statements
E
Syntax
IF (condition1)
{
statements;
}
else if (condition2)
{
statements;
}
else if (condition3)
{
statements;
}
else
{
statements;
UNIT-II GE6151-COMPUTER PROGRAMMING
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3;
float avg;
printf("\nEnter the marks:");
scanf("%d%d%d",&m1,&m2,&m3);
avg=(m1+m2+m3)/3;
printf("\n The average is:%f",avg);
printf("\n The Grade is:");
if(avg>=60)
{
printf("First class");
}
else if(avg>=50)
{
printf("Second class");
}
else if(avg>=35)
{
printf("Thrid class");
}
else
{
printf("Fail");
}
getch();
}
Looping structure
UNIT-II GE6151-COMPUTER PROGRAMMING
WHILE Loop
Syntax
.
conditionFa
WHILE (condition) ls
Tr e
{ Bodyue
of The loop
.
Body of the loop;
Example:
.
#include<stdio.h>
}
#include<conio.h>
void main()
{
int i=1,fact=1,n;
printf("\nEnter the Number:");
scanf("%d",&n);
while(i<=n)
{
fact =fact *i;
i++; // To increment i=i+1
}
printf("\n The value of %d! is:%d",n,fact);
getch();
}
UNIT-II GE6151-COMPUTER PROGRAMMING
DO…WHILE Loop
Syntax
do Body of The loop
{
Tru
e condition
Body of the loop Fal
se
FOR Loop
}while (condition);
Syntax
}
UNIT-II GE6151-COMPUTER PROGRAMMING
Initialization
Inc / Decrement
Body of the loop
condition Fa
ls
e
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,fact=1,n;
printf("\nEnter the Number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact =fact *i;
}
printf("\n The value of %d! is:%d",n,fact);
getch();
}
Syntax
for (initialization; condition; Inc/Dec)
{
UNIT-II GE6151-COMPUTER PROGRAMMING
CASE structure
Switch
Case 1
Case 2
Default
case
Syntax
UNIT-II GE6151-COMPUTER PROGRAMMING
switch (expression)
{
case constant 1:
block1;
break;
case constant 2:
block2;
break;
.
.
default :
default block;
break;
}
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
printf("\nEnter the Number:");
scanf("%d",&n);
switch(n)
{
case 1:
{
printf("\n Its in case 1");
break;
}
case 2:
{
printf("\n Its in case 2");
break;
}
default:
{
printf("\n Its in default");
break;
}
UNIT-II GE6151-COMPUTER PROGRAMMING
}
getch();
}
break Statement
• It is used to terminate the loop
• When a break statement is encountered inside a loop, then the loop is
terminated.
While
while(cond)
{
…………
if(cond)
break;
For:
…………
for (initi; condt; Inc/Dec)
}
{
…………
if(cond)
Continue Statement
break;
…………
UNIT-II GE6151-COMPUTER PROGRAMMING
while(cond)
{
…………
if(cond)
continue;
…………
}
do while:
do
{
…………
For:
if(cond)
continue;
…………
UNIT-II GE6151-COMPUTER PROGRAMMING