3.1 IO, Branch and Looping
3.1 IO, Branch and Looping
• Console simply means screen and keyboard. There are two types of a
console I/O functions:
• Formatted input-output function
• Unformatted input-output function
• The major difference is that formatted function allows us to format the
input from the keyboard and the output to be displayed on the screen.
The scanf() and printf() Functions
• The int scanf(const char *format, ...) function reads the input from the
standard input stream stdin and scans that input according to
the format provided.
• The int printf(const char *format, ...) function writes the output to the
standard output stream stdout and produces the output according to the
format provided.
• The format can be a simple constant string, but you can specify %s,
%d, %c, %f, etc., to print or read strings, integer, character or float
respectively.
#include <stdio.h>
int main( )
{
char str[100];
int i;
printf( "Enter a value :");
scanf("%s %d", str, &i);
printf( "\nYou entered: %s %d ", str, i);
return 0;
}
Output:
Enter a value : seven 7
You entered: seven 7
The getchar() and putchar() Functions
• The int getchar(void) function reads the next available character from
the screen and returns it as an integer.
• This function reads only single character at a time.
• You can use this method in the loop in case you want to read more than
one character from the screen.
• The int putchar(int c) function puts the passed character on the screen
and returns the same character.
• This function puts only single character at a time.
• You can use this method in the loop in case you want to display more
than one character on the screen. Check the following example −
#include <stdio.h>
int main( )
{
int c;
printf( "Enter a value :");
c = getchar( );
printf( "\nYou entered: ");
putchar( c );
return 0;
}
Output:
Enter a value : this is test
You entered: t
The gets() and puts() Functions
• The char *gets(char *s) function reads a line from stdin into the buffer
pointed to by s until either a terminating newline or EOF (End of File).
• The int puts(const char *s) function writes the string 's' and 'a' trailing
newline to stdout.
#include <stdio.h>
int main( )
{
char str[100];
printf( "Enter a value :");
gets( str );
printf( "\nYou entered: "); puts( str );
return 0;
}
Output:
Enter a value : this is test
You entered: this is test
File Input/Output in C
}
while(i>1);
printf(“thank you”);
}
For loop
• For loop is used to execute and repeat a block of statements depending
on a condition.
Syntax:
for(intialization;test -condition;increment)
{
body of the loop;
}
• Intialization is the assignment expression which initializes the value of
a variable .
• Test -condition is a releational or logical expression which will have the
value true or false.
• Increment is the increment value of the variable which will be added
every time
Example: Output
#include<stdio.h> 1
int main(){ 2
int i=0; 3
for(i=1;i<=10;i++) 4
{ 5
printf("%d \n",i); 6
} 7
return 0; 8
} 9
10
While statement do-while statement