chapter4 io
DESCRIPTION
sTRANSCRIPT
Unit 4Data Input and Output
Basic Functions
• A C program consists of one or more functions that contain a group of statements which perform a specific task.
• A C program must at least have one function: the function main.
• We can create our own function or use the functions that has been created in the library, in which case we have to include the appropriate header file (example: stdio.h).
• 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 used with printf() and scanf().
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
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+01
10 %g The shorter between %f and %e 7611 %G The shorter between %f and %E 7612 %x Character ‘7’13 %s String ‘76'
Tells the printf() function the format of the output to be printed put.
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.
scanf()
• Read data from the standard input device (usually keyboard) 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 (notice the & sign) instead of the variable itself to the second function argument.
• Example:
int age;printf(“Enter your age: “);scanf(“%d”, &age);
getchar() and putchar()
• getchar() - read a character from standard input• putchar() - write a character to standard output• Example:
#include <stdio.h>
void main(void){ char my_char; printf(“Please type a character: “); my_char = getchar(); printf(“\nYou have typed this character: “); putchar(my_char);}
9
String Input and Output
• To get input string:– gets ( )– scanf ( )– getchar ( )– getch ( )
• To produce string output– puts ( )– printf ( )– putchar ( )
• Note: program must include <stdio.h> file
10
• gets– a function that
will get a string of characters
• scanf– to input
individual words from a line of text
– blank spaces act as delimiters
#include <stdio.h>
int main(){
char s[80];printf("Please type in some words : ");gets(s);printf("You typed : %s\n", s);return 0;
}
#include <stdio.h>
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;
}
11
• getchar– to input a single
character– requires you to hit
enter.
• getch– to input a single
character– reads a key hit
without waiting for you to press enter.
#include <stdio.h>
int main(){
char c;printf("Please type ONE character : ");c=getchar();printf("You typed : %c\n", c);return 0;
}
#include <stdio.h>#include <conio.h>int main(){
char c;printf("Please type 1 character : ");c=getch();printf("You typed : %c\n", c);return 0;
}
12
• puts– outputs a string
as a whole
• printf– to output a string
• putchar– outputs
characters individually
#include <stdio.h>
int main(){
char a[80];puts("Type some words :");gets(a);puts(a);return 0;
}
#include <stdio.h>
int main(){
char a[80]="abcd";printf("%s\n", a);return 0;
}
#include <stdio.h>
int main(){
char letter;for (letter='A'; letter<='Z'; letter++)
putchar (letter);printf("\n");return 0;
}
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.
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 is not duplicated by the input functions scanf() and gets()– scanf() reads a set of characters up to either a
blank space or a newline character•scanf("%s",message); //No & is required
– gets() stops accepting characters only when a newline is detected
15
#include <stdio.h>#include <string.h>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.)