--> Sayadasite: MANAGING INPUT OUTPUT STATEMENTS

Multiple Ads

Search

Menu Bar

MANAGING INPUT OUTPUT STATEMENTS

 CHAPTER - 3 : MANAGING INPUT – OUTPUT STATEMENTS

C provides a library of functions. This library is called a standard input output library. It is denoted by stdio. The header file containing such library functions is called stdio.h.

There are two types of input-output functions. They are :

1.         Formatted I/O functions

2.         Unformatted I/O functions

 

FORMATTED OUTPUT STATEMENT

C provides the printf( ) function to display the data on the monitor. The printf( ) is included in stdio.h. It is used to display the results and messages on the screen. The general form of printf( ) statement is :

printf(“control string”, varlist);

where,

control string – specifies the type and format of the values to be displayed. varlist – a list of variables to be displayed.

Example :        printf(“Programming is an Art”); printf(“%d”,sum);

printf(%f%f”,p,q); printf(“\nSum=%d”, sum);

 

The following are different format descriptors: Conversion Character                                                                   Meaning

%d                                           Print & Read a decimal integer

%u                                           Print & Read a unsigned integer

%s                                           Print & Read a string

%f                                           Print & Read a floating point number

%e                                           Print & Read a exponential floating point number

%c                                           Print & Read a single character

%g                                           Print & Read a floating point number

%i                                           Print & Read a decimal or octal or hexadecimal

%x                                           Print & Read a hexadecimal number

%h                                           Print & Read a short integer number

%o                                           Print & Read octal integer number

%p                                           Print & Read pointer

 

FORMATTED INPUT STATEMENT

To read the values for the variables in a program from the keyboard, C provides a function called scanf( ). This is used to accept numeric, character and string type of data. The address operator & (ampersand) is used to locate the values into variable memory.

Syntax :


 

where,


scanf(“control string”, address_list);

 

control string – It is a sequence of one or more character group. Each character is a combination of % symbol and a conversion character.

address_list – Address of memory locations where the values of input variables should be stored.


Example :


 

scanf(“%d”,&num);

scanf(“%d%f%c”,&a,&b,&c); scanf(“%d%s”,&number, name);


Program to add two numbers. #include<stdio.h> #include<conio.h>

main()

{

int a,b,sum; clrscr();

printf(“Enter two numbers\n”); scanf(“%d%d”,&a,&b); sum=a+b

printf(“\nSum = %d”,sum); getch();

}

 

Program to accept three numbers and compute their sum and average.

#include<stdio.h> #include<conio.h> main()

{

int num1,num2,num3,sum; float average;

clrscr();

printf(“Enter three numbers\n”); scanf(“%d%d%d”,&num1,&num2,&num3); sum=num1+num2+num3; average=sum/3;

printf(“\nThe Sum = %d”,sum); printf(“\nThe Average = %f”,average); getch();

}

 

Program to find the simple interest : #include<stdio.h> #include<conio.h>

main()

{

int p,t,r; float si; clrscr();

printf(“\nEnter the Principal :”); scanf(“%d”,&p);

printf(“\nEnter the Term :”); scanf(“%d”,&t); printf(“\nEnter the Rate :”); scanf(“%d”,&r); si=(p*t*r)/100;

printf(“\nThe Simple Interest = %f”,si); getch();

}


Program to accept the temperature in Fahrenheit and convert it into Celsius.

#include<stdio.h> #include<conio.h> main()

{

float ct, ft; clrscr();

printf(“Enter the temperature in Fahrenheit\n”); scanf(“%f”,&ft);

ct=(ft-32.0)/1.8;

printf(“Fahrenheit temperature = %6.2f\n”, ft); printf(“Celsius temperature = %6.2f\n”, ct); getch();

}

 

UNFORMATTED INPUT FUNCTIONS

These functions are primarily concerned with reading the character type data from the keyboard. The getchar( ) and get( ) functions are used for this purpose. Since, they are included in the stdio.h, the C programs that use these functions should exclusively have the following preprocessor statement.

#include<stdio.h>

THE getchar( ) FUNCTION

This function reads a single character from the standard input device. There is no parameter within the parentheses. Its syntax is as follows :

ch_var = getchar( );

Where, ch_var is a character type variable to which an accepted character is assigned.

Example :       void main( )

{

char letter;

letter = getchar( );

printf(“Your character = %c”, letter);

}

 

THE gets( ) FUNCTION

This function reads in everything you enter from the keyboard until the ENTER key or RETURN is pressed. Here, everything means a string which is a sequence of all printable ASCII characters. The RETURN key that you pressed will not be stored at the end of the string. It overcomes the limitation of the scanf( ) statement with %s option. The syntax of gets( ) is

gets(string);

Where, string is a sequence of characters and it is of type char.

 

Example :

void main( )

{

char name[25]; printf(“Enter your name\n”); gets(name);

printf(“Your Name = %s”, name);

}


UNFORMATTED OUTPUT FUNCTIONS

These functions are mainly concerned with displaying or printing the character type data on the monitor. The putchar( ) and puts( ) functions are used for this purpose. Both these functions are defined in the header file <stdio.h>

 

THE putchar( ) FUNCTION

This function prints a single character on the screen. The character to be displayer is of type char. Its syntax is as follows.

putchar(ch_var);

Where, ch_var is a character variable which is enclosed within the parentheses.

Example :

void main( )

{

char letter;

letter = getchar( ); putchar(letter);

}

 

THE puts( ) FUNCTION

This function prints a string of characters on the screen. The newline character that signals the end of the string will not be displayed. The syntax of puts( ) is as follows:

puts(string);

Where, string is a sequence of characters.

 

Example :

void main( )

{

char message[20];

printf(“Enter the message to motivate students\n”); gets(message);

puts(message);

}

 


No comments: