Unit 4 2
Unit 4 2
Unit-3
Data Input and Output
Formatted Functions
- Formatted functions allow the input from the keyboard or the output displayed on screen
to be formatted according to our requirements.
- Input function: scanf( )
- Output function: printf( ) Formatted functions
Formatted Input
- The well-known function for formatted input is scanf.
- The built-in function scanf() can be used to enter input data into the computer from a
standard input device.
- Its general form is as follows:
scanf(“control string”, arg1, arg2,…..,argn);
Where, control string → format in which data is to be entered.
arg1, arg2,… → location where the data is stored and preceded by ampersand (&)
- The control string consists of individual groups of data formats, with one group for each
input data item.
- Each data format must begin with a percentage sign.
Conversion Specifier:
E.g.
#include<stdio.h>
void main()
{
int i;
printf("Please enter a value:");
scanf("%d", &i);
printf( "\nYou entered: %d", i);
}
getch();
}
Input string:
#include<stdio.h>
#include<conio.h>
void main()
{
char str[20];
printf("Enter your name:");
scanf("%s",&str);
printf("Your name is %s",str);
getch();
}
Reading mixed data types:
- In a single scanf call more than one of data can be read.
- Care should be taken to ensure that the input data items match the control specification.
E.g.
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
int roll;
float marks;
printf("Enter your name, roll number and marks:");
scanf("%s%d%f",&name,&roll,&marks);
printf("Name=%s\nRoll no.=%d\nMarks=%f",name,roll,marks);
getch();
}
Formatted Output
- Refers to the output of data that has been arranged in a particular format.
- printf() is a built in function which is used to output data from the computer onto a
standard device i.e. screen.
- General form:
printf("control string",arg1,arg2,….argn);
- The control string cosists of four types of items:
Character that will be printed on the screen as they appear
Format specifications that define the output format for display of each item
Escape sequence character such as \n,\t etc.
Any combination of characters, format specifications and escape sequences.
4 C Programming Reference Note
Unformatted Functions
- Unformatted functions do not allow user to read or display data in desire format.
- These library functions basically deals with a single character or a string of character.
- The functions getchar(), putchar(), gets(), puts(), getch(), getche(), putche() are
considered as unformatted functions.
getchar()
- Reads a character from a standard input device.
- It takes the form: character_variable = getchar();
- This function reads only single character at a time.
putchar()
- Displays a character to the standard output device.
- Its form: putchar(character_variable)
- This function displays only single character at a time.
E.g.
#include <stdio.h>
void main( )
{
int c;
printf("Enter a character:");
/* Take a character as input and store it in variable c */
c = getchar();
/* display the character stored in variable c */
putchar(c);
}
gets()
- used to read string of text, containing whitespace, until a new line character is
encountered.
- General form: gets(string_variable);
puts()
- Used to display the string onto the terminal
- General form: puts(string_variable);
E.g.
#include<stdio.h>
void main()
{
/* character array of length 100 */
char str[100];
printf("Enter a string:");
gets( str );
printf("The string you entered:");
puts( str );
5 C Programming Reference Note
getch();
}
Or
5 Example: Example:
#include <stdio.h> #include <stdio.h>
#include<conio.h> #include<conio.h>
Main ( ) MAIN ( )
{ {
Char name [20] Char NAME [20]
Print f("enter name:"); PRINT F("what is your name;")
gets (name) ; Gets (name);
puts (name); Puts (name);
} puts (''thank you")
}