0% found this document useful (0 votes)
21 views29 pages

3.1 IO, Branch and Looping

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views29 pages

3.1 IO, Branch and Looping

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

UNIT - III

Input Output functions (I/O)

Input, it means to feed some data into a program.


• An input can be given in the form of a file or from the command line.
• C programming provides a set of built-in functions to read the given input and
feed it to the program as per requirement.
Output, it means to display some data on screen, printer, or in any file.
• C programming provides a set of built-in functions to output the data on the
computer screen as well as to save it in text or binary files.
• We can classify I/O function into two categories:
• Console I/O function
• File I/O function
Console Input-Output functions

• 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

• A file represents a sequence of bytes on the disk where a group of


related data is stored.
• File is created for permanent storage of data. It is a ready made
structure.
• In C language, we use a structure pointer of file type to declare a file.
FUNCTION DESCRIPTION
fopen() create a new file or open a existing file
fclose() closes a file
getc() reads a character from a file
putc() writes a character to a file
fscanf() reads a set of data from a file
fprintf() writes a set of data to a file
getw() reads a integer from a file
putw() writes a integer to a file
fseek() set the position to desire point
ftell() gives current position in the file
rewind() set the position to the beginning point
Compound and Conditional Statements
• In a 'C' program are executed sequentially. This happens when there is
no condition around the statements. If you put some condition for a
block of statements the flow of execution might change based on the
result evaluated by the condition. This process is referred to as
decision making in 'C.' The decision-making statements are also
called as control statements.
• In 'C' programming conditional statements are possible with the help
of the following two constructs:
1. If statement
2. If-else statement
If statement

• If statement is responsible for modifying the flow of execution of a


program. If statement is always used with a condition.
• The condition is evaluated first before executing any statement inside
the body of If.
Syntax:
if (condition)
{
instruction;
}
• The condition evaluates to either true or false. True is always a non-
zero value, and false is a value that contains zero.
Example:
#include<stdio.h>
int main()
{
int num1=1;
int num2=2;
if(num1<num2) //test-condition
{
printf("num1 is smaller than num2");
}
return 0;
}
Output:
num1 is smaller than num2
The If-Else statement
• The if-else is statement is an extended version of If.
Syntax:
if (test-expression)
{
True block of statements
}
Else
{
False block of statements
}
Statements;
Example:
#include<stdio.h>
void main()
{
int num=19;
if(num<10)
{
printf("The value is less than 10");
}
else {
printf("The value is greater than 10");
} getch();
}
Output:
The value is greater than 10
While loop
• The while is an entry- controlled loop statement.
• The test -condition is evaluated first and if the condition is true,
then the body of loop is executed. Again the test-condition is tested
and body of the loop will be repeated until the condition becomes
false
• If at the entry condition itself the test-condition is false then
body of the loop will be skipped.
Syntax:
while ( test condition)
{
body of the loop;
}
Example Output
#include<stdio.h> 1
void main() 2
{ 3
int i=1; 4
while(i<=10){ 5
printf("%d \n",i); 6
i++; 7
} 8
getch(); 9
} 10
do while loop
• The do is an exit- controlled loop statement.
• The body of loop will be executed once and the condition is tested if
the condition is true then body will be repeated or else exit from the
loop.
Syntax:
do
{
body of the loop
}
while (test-condition);
Example: Output
//program using the do-while statement The number is one
#include<stdio.h> thank you
void main()
{
int i=1;
do
{
printf(“the number is one\n”);

}
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

The statement block in while The statement block is executed at


statement is executed when the least once irrespective of the values
values of the condition is true of the condition.

The condition is evaluated at the The condition is evaluated at the


beginning of the loop. The end of the loop. The statement
statement block is executed if the block is executed again if the value
value of the condition is true. of the condition is true.
UNIT – III
Completed

You might also like