Input and Output in C Programming
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);
No comments:
Post a Comment