Monday, September 29, 2014

MANAGING INPUT AND OUTPUT OPERATORS in C

Explain briefly about the input and output function in "C". (MAY 2009/FEB 2009)

printf() is actually a function (procedure) in C that is used for printing variables and text. Where text appears in double quotes “”, it is printed without modification. There are some exceptions however.
This has to do with the \ and % characters. These characters are modifiers, and
for the present the \ followed by the n character represents a newline character.
Example:
#include <stdio.h>
main()

{
printf(“Programming in C is easy.\n”);
printf(“And so is Pascal.\n”);
}
@ Programming in C is easy. And so is Pascal.
FORMATTERS for printf are, Cursor Control Formatters
\n newline
\t tab
\r carriage return
\f form feed
\v vertical tab
2. Scanf ():
Scanf () is a function in C which allows the programmer to accept input from a
keyboard.
Example:
#include <stdio.h>
main() /* program which introduces keyboard input */
{
int number;
printf(“Type in a number \n”);
scanf(“%d”, &number);
printf(“The number you typed was %d\n”, number);
}
FORMATTERS FOR scanf()
The following characters, after the % character, in a scanf argument, have the
following effect.
D read a decimal integer o read an octal value
x read a hexadecimal value h read a short integer
l read a long integer
f read a float value
e read a double value
c read a single character
s read a sequence of characters

[...] Read a character string. The characters inside the brackets

3. ACCEPTING SINGLE CHARACTERS FROM THE KEYBOARD 
Getchar, Putchar
getchar() gets a single character from the keyboard, and putchar() writes a single character from the keyboard.
Example:
The following program illustrates this,
#include <stdio.h>
main()
{
int i;
int ch;
for( i = 1; i<= 5; ++i ) { ch = getchar(); putchar(ch);
}
}
The program reads five characters (one for each iteration of the for loop) from the keyboard. Note that getchar() gets a single character from the keyboard, and putchar() writes a single character (in this case, ch) to the console screen.

No comments:

Post a Comment