Tuesday, 18 March 2014

Input/Output in C

Input and Output in C Programming


It is all very well to store data in the computer and make calculations with it, but you also need to be able to type new data into the computer and print out the results of your calculations.

Printf( ) Function

We have been using printf( ) up to now without too much explanation of all its possibilities. lets take a closer look.

Format Specifiers

A Format Specifiers is used to control what format will be used by printf( ) to print out a particular variable.
Here is a list of the format specifiers for printf( ).
%c  Single Character
%s  String
%d  Signed Decimal Integer
%f  Floating Point (decimal notation)
%e  Floating Point (exponential notation)
%g  Floating Point (%f or %ew whichever is shorter)
%u  Unsigned Decimal Integer
%x Unsigned hexadecimal Integer (uses "abcdef")
%o  Unsigned Octal Integer

Escape Sequences

Escape sequences cause an escape from the normal interpretation of a string, so that the next character is recognized as having a special meaning..
The following list shows the common escape sequences.
\n  Newline
\t  Tab
\b  Backspace
\r  Carriage return
\f  Formfeed
\'  Signle Quote
\"  Double Quote
\\  Backslash

Scanf ( ) Function

Scanf( ) is an input function in C.Scanf( ) function asks for values at run time.
Here's a program that uses scanf( ).
Program
void main ( )
{
int num;
printf("enter number:");
scanf("%d", & num);
printf("The number you entered is: %d",num);
}
Output
enter number:5
The number you entered is:5
The format specifers for scanf( ) are similar to those for printf( ).

The Address Operator (&)

The scanf( ) function uses the ampersand (&) sign preceding the variable names as arguments.
scanf("%d", & num);

The getche( ) function









No comments:

Post a Comment