You are on page 1of 15

Unit 4 Data 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 predefined 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 1 2 3 4 5 6 7 8 9 10 11 12 13 Format Specifier %d %i %o %u %x %X %f %e %E %g %G %x %s Output Type Signed decimal integer Signed decimal integer Unsigned octal integer Unsigned decimal integer Unsigned hexadecimal (small letter) Unsigned hexadecimal (capital letter) Integer including decimal point Signed floating point (using e notation) Signed floating point (using E notation) The shorter between %f and %e The shorter between %f and %E Character String Output Example 76 76 134 76 9c 9C 76.0000 7.6000e+01 7.6000E+01 76 76 7 76'

Tells the printf() function the format of the output to be printed put.

Escape Sequence
Escape Sequence \a \b \f \n \r \t \v \\ \ \o \x \O Effect Beep sound Backspace Formfeed (for printing) New line Carriage return Tab Vertical tab Backslash sign Octal decimal Hexadecimal NULL

Escape sequence is used in the printf() function to do something to the 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); }

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

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;

10

getchar to input a single character requires you to hit enter.

#include <stdio.h> int main() {

char c; printf("Please type ONE character : "); c=getchar(); printf("You typed : %c\n", c); return 0;

getch to input a single character reads a key hit without waiting for you to press enter.

#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; }

11

#include <stdio.h>

puts outputs a string as a whole

int main() {

char a[80]; puts("Type some words :"); gets(a); puts(a); return 0;

#include <stdio.h>

printf to output a string

int main() {

char a[80]="abcd"; printf("%s\n", a); return 0;

#include <stdio.h>

putchar outputs characters individually

int main() {

char letter; for (letter='A'; letter<='Z'; letter++) putchar (letter); printf("\n"); return 0; 12

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.
13

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
14

String Input and Output (cont.)


#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 jaya petaling jaya petaling petaling jaya petaling jaya Press any key to continue
15

You might also like