c unit II
c unit II
Note: int main() is an old style declaration that indicates main takes an
unspecified number of arguments.
3.The gets() function: This function reads all characters entered from
the keyboard until the ENTER key or RETURN key is pressed.
Syntax: gets(string);
Unformatted o/p functions: These functions are used to print the
character type data on the monitor. The putchar( ) and puts( ) are used
for this purpose. Both these functions are defined in the header file
stdio.h.
1.The putchar()function: This function points a single character
on the screen. The variable to be displayed is of type char.
Syntax: putchar (var);
Where: var specifies a character type variable which is enclosed within the
parenthesis.
A program to illustrate the use of getchar and putchar
#include<std
io.h> main( )
{
char ch;
ch=getc
har( );
putchar
(ch);
}
2.The puts( ): This function prints a set of characters on the screen.
Syntax:puts (string); where: string specifies a sequence of characters.
WAP to read and print a string using gets( ) and puts( )
#include<stdi
o.h> main( )
{
char name[20];
printf(“enter your name:\n”);
gets(n
ame);
puts(n
ame);
getcha
r( );
}
Functio Descript
n ion
Reads a single character from the user at
getch()
the console, without echoing it.
Reads a single character from the user at the
getche()
console, and echoing it.
Reads a single character from the user at the
getchar(
) console and echoing it, but needs an Enter key to
be pressed at the end.
gets() Reads a single string entered by the user at the
console.
puts() Displays a single string's value at the console.
putchar( Displays a single character value at the console.
)
Syntax: if(condition)
if
statement 1; false CondD true
else
statement 2; Statement 2
If condition is true statement 1 is executed Statement 1
and skips statement 2. If condition is false,
statement 2 is executed and skips Statement after if
statement1.
Program to find greatest of 2 numbers.
#include<stdio.h>
Program to find the largest of 2 numbers
main()
{
int a,b;
printf(“enter two numbers”); scanf(“%d%d”,&a,&b);
if(a>b)
printf(“%d is greater”,a); enter the value of a and b
else 15 35
35 is greater
printf(“(“%d is greater”,b);
getch();
}
#include<stdio.h>
main()
{
int a; enter a number
15
15 is positive
printf(“enter a number”); scanf(“%d”,&a);
if(a>0)
printf(“%d is positive”,a);
else
printf(“%d is negative”,a);
}
true
Cond-1
false
true
Cond-2
St-1
false
St-2
Cond-3
false true
St-4 St-3
next
LOOPS
The process of repeating the execution of a certain set of statements
again & again is called looping. C has 3 looping statements they are
1. while
2. do while
3. for
The while and do while are used when the programmer does not know
exactly the number of executions. Thus the execution will be repeated until
the condition become false. The for loop is normally used when the number
of repetitions is know in advance.
Syntax:
while ( condition )
true
{
Statement ;
statement 1;
statement 2;
…………….; Inc/dec
statement n;
} Next statement
statement 1;
statement 2;
Next statement
…………….;
statement n; false
}
while ( condition);
Working:
1. expression1is first evaluated i.e. initialize values for declared variables
2. expression 2: checks the condition; suppose the condition is true it enter
the loop.
Otherwise it skips the loop.
3. expression 3: Control send back to beginning of the structure, the value
of variable is incremented or decremented depending on
the expression.
for (initialization; condition; updation)
false true
Statements;
Next statement
Sl.N
while do while
o
1. This is pre- tested loop This is post tested loop
#include<stdio.h>
main()
{
int a=1,n;
clrscr();
printf(“enter number”);
scanf(“%d”,&n);
while (a<=n)
{
printf(“%d”,a);
a++;
}
getch();
}Program using do while
#include<stdio.h>
main()
{
int a=1,n;
clrscr();
printf(“enter number”);
scanf(“%d”,&n);
do
{
printf(“%d”,a);
a++;
Program
} using for loop
while (a<=n);
#include<stdio.h>
getch();
main()
}
{
int a,n;
clrscr();
printf(“enter number”);
scanf(“%d”,&n);
for(a=1;a<=n;a++)
printf(“%d\n”,a);
getch();
}
#include<stdio.h> 1 2
main() 1 2 3
{ 1 2 3 4
int r, c, n;
clrscr();
for(r=1;r<=n;r++)
{
for(c=1;c<=r;C++)
printf("%d \t”,c);
}
printf("\n");
}
Wap to generate the following pattern 1
2 3
4 5 6
7 8 9 10
1. break
2. continue
3. exit()
4. goto
5. return.
goto and return statements are used anywhere in the program while break
and continue are used inside the loop. In addition the function exit() is also
used inside the loop.
The continue statement : When the key word continue is encountered the
control automatically pass brak to the beginning of the loop
Syntax:continue;
#include<stdio.h>
main()
{
int i; o/p
for(i=1;i<=5;i++)
{
if(i==3)
continue;
printf(“%d\n”,i);
}
printf("KARNATAKA \n");
}
goto statement