10/24/2011

C Programming - Managing Input and Output Operations


C Programming - Managing Input and Output Operations

In this tutorial you will learn about Single character input output, String input and output, Formatted Input For Scanf, Input specifications for real number, Input specifications for a character, Printing One Line, Conversion Strings and Specifiers, Specifier Meaning

Introduction


One of the essential operations performed in a C language programs is to provide input values to the program and output the data produced by the program to a standard output device. We can assign values to variable through assignment statements such as x = 5 a = 0 ; and so on. Another method is to use the Input then scanf which can be used to read data from a key board. For outputting results we have used extensively the function printf which sends results out to a terminal. There exists several functions in ‘C’ language that can carry out input output operations. These functions are collectively known as standard Input/Output Library. Each program that uses standard input / out put function must contain the statement.

# include < stdio.h >

at the beginning.


Single character input output:


The basic operation done in input output is to read a characters from the standard input device such as the keyboard and to output or writing it to the output unit usually the screen. The getchar function can be used to read a character from the standard input device. The scanf can also be used to achieve the function. The getchar has the following form.

Variable name = getchar:


Variable name is a valid ‘C’ variable, that has been declared already and that possess the type char.





Example program :

# include < stdio.h > // assigns stdio-h header file to your program
void main ( ) // Indicates the starting point of the program.
{
char C, // variable declaration
printf (“Type one character:”) ; // message to user
C = getchar () ; // get a character from key board and
Stores it in variable C.
Printf (” The character you typed is = %c”, C) ; // output
} // Statement which displays value of C on
// Standard screen.


The putchar function which in analogus to getchar function can be used for writing characters one at a time to the output terminal. The general form is

putchar (variable name);

Where variable is a valid C type variable that has already been declared Ex:-

Putchar ( );


Displays the value stored in variable C to the standard screen.

Program shows the use of getchar function in an interactive environment.


#include < stdio.h > // Inserts stdio.h header file into the Pgm
void main ( ) // Beginning of main function.
{
char in; // character declaration of variable in.
printf (” please enter one character”); // message to user
in = getchar ( ) ; // assign the keyboard input value to in.
putchar (in); // out put ‘in’ value to standard screen.
}


String input and output:

The gets function relieves the string from standard input device while put S outputs the string to the standard output device. A strong is an array or set of characters.

The function gets accepts the name of the string as a parameter, and fills the string with characters that are input from the keyboard till newline character is encountered. (That is till we press the enter key). All the end function gets appends a null terminator as must be done to any string and returns.

The puts function displays the contents stored in its parameter on the standard screen.

The standard form of the gets function is

gets (str)

Here "str" is a string variable.
NOTE: It is recommended NOT to make use of gets() function. It is unsafe and can even crash the C program itself. Above description is for theoretical illustration purpose only.

The standard form for the puts character is

puts (str)

Where str is a string variable.

Example program (Involving both gets and puts)

# include < stdio.h >
Void main ( )
{
char s [80];
printf (“Type a string less than 80 characters:”);
gets (s);
printf (“The string types is:”);
puts(s);
}

 

 

Formatted Input For Scanf:


The formatted input refers to input data that has been arranged in a particular format. Input values are generally taken by using the scanf function. The scanf function has the general form.

Scanf (“control string”, arg1, arg2, arg3 ………….argn);


The format field is specified by the control string and the arguments
arg1, arg2, …………….argn specifies the address of location where address is to be stored.

The control string specifies the field format which includes format specifications and optional number specifying field width and the conversion character % and also blanks, tabs and newlines.

The Blanks tabs and newlines are ignored by compiler. The conversion character % is followed by the type of data that is to be assigned to variable of the assignment. The field width specifier is optional.

The general format for reading a integer number is

% x d

Here percent sign (%) denotes that a specifier for conversion follows and x is an integer number which specifies the width of the field of the number that is being read. The data type character d indicates that the number should be read in integer mode.

Example :


scanf (“%3d %4d”, &sum1, &sum2);

If the values input are 175 and 1342 here value 175 is assigned to sum1 and 1342 to sum 2. Suppose the input data was follows 1342 and 175.

The number 134 will be assigned to sum1 and sum2 has the value 2 because of %3d the number 1342 will be cut to 134 and the remaining part is assigned to second variable sum2. If floating point numbers are assigned then the decimal or fractional part is skipped by the computer.

To read the long integer data type we can use conversion specifier % ld & % hd for short integer.

Input specifications for real number:

Field specifications are not to be use while representing a real number therefore real numbers are specified in a straight forward manner using % f specifier.

The general format of specifying a real number input is


Scanf (% f “, &variable);

Example:


Scanf (“%f %f % f”, &a, &b, &c);

With the input data


321.76, 4.321, 678 The values
321.76 is assigned to a , 4.321 to b & 678 to C.

If the number input is a double data type then the format specifier should be % lf instead of %f.

Input specifications for a character.


Single character or strings can be input by using the character specifiers.
The general format is


% xc or %xs

Where C and S represents character and string respectively and x represents the field width.
The address operator need not be specified while we input strings.

Example :


Scanf (“%C %15C”, &ch, nname):

Here suppose the input given is a, Robert then a is assigned to ch and name will be assigned to Robert.

Printing One Line:


printf();

The most simple output statement can be produced in C’ Language by using printf statement. It allows you to display information required to the user and also prints the variables we can also format the output and provide text labels. The simple statement such as

Printf (“Enter 2 numbers”);

Prompts the message enclosed in the quotation to be displayed.

A simple program to illustrate the use of printf statement:-


#include < stdio.h >
main ( )
{
printf (“Hello!”);
printf (“Welcome to the world of Engineering!”);
}


Output:

Hello! Welcome to the world of Engineering.

Both the messages appear in the output as if a single statement. If you wish to print the second message to the beginning of next line, a new line character must be placed inside the quotation marks.


For Example :

printf (“Hello!\n”);

OR

printf (“\n Welcome to the world of Engineering”);

Conversion Strings and Specifiers:

The printf ( ) function is quite flexible. It allows a variable number of arguments, labels and sophisticated formatting of output. The general form of the printf ( ) function is

Syntax


Printf (“conversion string”, variable list);

The conversion string includes all the text labels, escape character and conversion specifiers required for the desired output. The variable includes all the variable to be printed in order they are to be printed. There must be a conversion specifies after each variable.


0 komentar:

Posting Komentar