chapter4 input and output
TRANSCRIPT
-
8/12/2019 Chapter4 Input and Output
1/15
Unit 4Data Input and Output
-
8/12/2019 Chapter4 Input and Output
2/15
Basic Functions
A C program consists of one or more functions thatcontain a group of statements which perform a specifictask.
A C program must at least have one function: the functionmain .
We can create our own function or use the functions thathas been created in the library, in which case we have toinclude the appropriate header file (example: stdio.h).
-
8/12/2019 Chapter4 Input and Output
3/15
In this section, we will learn a few functions that are pre-defined in the header file stdio.h These functions are:
printf() scanf()
getchar() & putchar() In addition to those functions, we will also learn about
Format Specifier and Escape Sequence which are usedwith printf() and scanf().
-
8/12/2019 Chapter4 Input and Output
4/15
printf() Used to send data to the standard output (usually the
monitor) to be printed according to specific format. General format:
printf(control string, variables); Control string is a combination of text, format specifier and
escape sequence. Example:
printf(Thank you); printf (Total sum is: %d \ n, global_var);
%d is a format specifier
\n is an escape sequence
-
8/12/2019 Chapter4 Input and Output
5/15
Format Specifier
No Format Specifier Output Type Output Example1 %d Signed decimal integer 762 %i Signed decimal integer 763 %o Unsigned octal integer 1344 %u Unsigned decimal integer 765 %x Unsigned hexadecimal (small letter) 9c6 %X Unsigned hexadecimal (capital letter) 9C7 %f Integer including decimal point 76.00008 %e Signed floating point (using e notation) 7.6000e+019 %E Signed floating point (using E notation) 7.6000E+0110 %g The shorter between %f and %e 7611 %G The shorter between %f and %E 7612 %x Character 713 %s String 76'
Tells the printf() function the format of the output to be printed put.
-
8/12/2019 Chapter4 Input and Output
6/15
Escape Sequence
Escape Sequence Effect\a Beep sound\b Backspace\f Formfeed (for printing)\n New line\r Carriage return
\t Tab\v Vertical tab\\ Backslash\ sign\o Octal decimal\x Hexadecimal
\O NULL
Escape sequence is used in the printf() function to do something tothe output.
-
8/12/2019 Chapter4 Input and Output
7/15
scanf()
Read data from the standard input device (usuallykeyboard) and store it in a variable. General format:
scanf(Control string, &variable); The general format is pretty much the same as printf()
except that it passes the address of the variable (noticethe & sign) instead of the variable itself to the secondfunction argument.
Example:
int age; printf(Enter your age: );scanf %d, &a e ;
-
8/12/2019 Chapter4 Input and Output
8/15
getchar() and putchar()
getchar() - read a character from standard input putchar() - write a character to standard output Example:
#include
void main(void){
char my_char;
printf(Please type a character: ); my_char = getchar();printf(\nYou have typed this character: ); putchar(my_char);
}
-
8/12/2019 Chapter4 Input and Output
9/15
9
String Input and Output
To get input string: gets ( ) scanf ( ) getchar ( )
getch ( ) To produce string output
puts ( ) printf ( )
putchar ( ) Note: program must include file
-
8/12/2019 Chapter4 Input and Output
10/15
10
gets a function that
will get a stringof characters
scanf to input
individual wordsfrom a line oftext
blank spacesact as delimiters
#include
int main(){
char s[80];printf("Please type in some words : ");gets(s);printf("You typed : %s\n", s);return 0;
}
#include
int main(){
char word1[80], word2[80];printf("Please type TWO words : ");scanf("%s %s", &word1, &word2);printf("You typed : %s and %s\n", word1, word2);return 0;
}
-
8/12/2019 Chapter4 Input and Output
11/15
11
getchar to input a single
character requires you to hit
enter.
getch to input a single
character reads a key hit
without waiting foryou to press enter.
#include
int main(){
char c;printf("Please type ONE character : ");c=getchar();printf("You typed : %c\n", c);return 0;
}
#include #include int main(){
char c;printf("Please type 1 character : ");c=getch();printf("You typed : %c\n", c);return 0;
}
-
8/12/2019 Chapter4 Input and Output
12/15
12
puts outputs a string as
a whole
printf to output a string
putchar outputs
charactersindividually
#include
int main(){
char a[80];puts("Type some words :");gets(a);puts(a);return 0;
}
#include
int main(){
char a[80]="abcd";printf("%s\n", a);return 0;
}
#include
int main(){
char letter;for (letter='A'; letter
-
8/12/2019 Chapter4 Input and Output
13/15
13
String Input and Output (cont.)
Sample run:Enter a string:This is a test input of a string of characters.The string just entered is:This is a test input of a string of characters.
-
8/12/2019 Chapter4 Input and Output
14/15
14
String Input and Output (cont.)
A printf() function call can be used in place of a puts() function call printf("%s\n",message); puts(message);
This correspondence between the output functions isnot duplicated by the input functions scanf() andgets()
scanf() reads a set of characters up to either ablank space or a newline character
scanf("%s",message); //No & isrequired
gets() stops accepting characters only when anewline is detected
-
8/12/2019 Chapter4 Input and Output
15/15
15
#include
#include void main (){
char a[20], b[20];
scanf("%s", a);fflush(stdin);gets(b);printf("%s\n", a);printf("%s\n", b);puts(b);
}
petaling jayapetaling jayapetalingpetaling jayapetaling jayaPress any key to continue
String Input and Output (cont.)