You are on page 1of 15

EC201 - FUNDAMENTAL PROGRAMMING 4.

INPUT OUTPUT
Course Lecturer: Norzaiwin B Zainal Abidin

• If a program uses for input or output function. Some important functions for output are as follows: • The functions used for input and output are stored in the header file stdio. . puts(). 3. (2+3) ). C language provides many functions to show output to the user. 2. it is necessary to include this header file in the program. A call to printf looks like this: printf( "format-string".h. The term standard output refers to the output showed on the monitor.. The output is mostly showed on monitor. ).. The result of a program is the output of the program.Output Statements in C programming language: The process of getting something from the computer is known as output. Example printf( "%i + %i = %i\n". will produce the following output 2+3=5 . printf(). expression.

Puts() Will print a string to the screen and automatically put a new line \n after the string. printf(“Welcome”). Example printf(“Enter your name >>”). Output Enter your name>>Abu Good morning Abu Welcome . puts(name). gets(name). printf (“Good morning “).

printf( "%a".14.0 ). 26 ). 31. printf( "%c". int a = 1.0000314 ). printf( "%g. printf( "%u". 31. 17 ).14e-05 0x1. &a ). F e. printf( "%X".printf Conversion Letters and Matching Types Letter % d.4 ).140000e+01 3. int a. i u o x X f. printf( "%p". E g. 65 ). 3. &a ). 3. "Hello" ). 17 ). 3. 17u ).14. %g". printf( "%o".14 ). Output % 17 17 21 1a 1A 3. A c s p n Type of Matching Argument none int unsigned int (Converts to decimal) unsigned int (Converts to octal) unsigned int (Converts to lower-case hex) unsigned int (Converts to upper-case hex) double double double double int string void* int* Example printf( "%%" ). printf( "%i". printf( "ABC%n".fp+0 A Hello 0064FE00 ABC (a==3) .140000 3. printf( "%x". G a. printf( "%s". 26 ). printf( "%e". printf( "%f". 0.

} Output of the source above: 7 7 007 5. printf("%3.2f\n".b). d = c / 3.10 .d). a = 15.b).h> main() { int a.3. printf("%03d\n".d. printf("%d\n".• • • • • • • • • • • • • • • • • • • • Let’s take a look at an example of printf formatted output: #include<stdio.b. printf("%3d\n".b). float c. c = 15. b = a / 2.

. The result is that two “space characters” are placed before printing the character. Print the output with a width of three digits. but fill the space with 0. but we use a width (%3d) to say that we want three digits (positions) reserved for the output. In the third printf statement we say almost the same as the previous one. In the second printf statement we print the same decimal.As you can see in the first printf statement we print a decimal.

The \n used in the printf statements is called an escape sequence.In the fourth printf statement we want to print a float. If there is no \n then a next printf command will print the string on the same line. . In this printf statement we want to print three position before the decimal point (called width) and two positions behind the decimal point (called precision). After printing something to the screen you usually want to print something on the next line. In this case it represents a newline character.

Commonly used escape sequences are: \n (newline) \\ (backslash) \t (tab) \’ (sign ‘) \v (vertical tab) \a (beep sound) \f (new page) \” (sign”) \b (backspace) \r (carriage return) \0 (zero) \xhh (Number kexsadecimal) \ooo (number oktal) .

world!"). Take a look at the following example: • #include<stdio. • printf(":%-10s:\n". • printf(":%-15s:\n". "Hello. "Hello.10s:\n". but there is one type that is a little different and that are string format conversions. • printf(":%15s:\n". • printf(":%.15s:\n". "Hello.10s:\n". • printf(":%. "Hello. • printf(":%15. world!").10s:\n". "Hello. "Hello. world!").• Formatting Strings • By now you have seen most of the format conversion possible. • printf(":%-15. world!"). world!"). world!"). • } . world!"). "Hello.h> • main() • { • printf(":%s:\n". world!"). "Hello.

• • • • • • • • • The output of the example above: :Hello. world!: :Hello. world!: :Hello. world!: : Hello. wor: :Hello. world! : :Hello. wor: :Hello. wor : . world!: : Hello.

some vital functions are for inputs are as follows: scanf() gets() .Input Statements in C programming language: The process of giving something to the computer is known as Input. A program may need certain inputs from the user for working properly. C language provides many functions to get input from the users. The input is mostly given by keyboard. The term standard input refers to the input using keyboard.

• Additional arguments must be pointers. . • scanf returns the number of successful inputs. • scanf starts with a string argument and may contain additional arguments. The conversion specifier argument tells scanf how to convert the incoming data.The scanf function: read information from a standard input device (keyboard).

Syntax scanf("conversion specifier". &variable). Common Conversion Specifiers Used with Scanf %d Receives integer value %f Receives floating-point numbers %c Receives character .

printf("\nOutput:\n"). int k = 0. &k ). &i . n). int n = 0. &j.h> int main(void) { int i = 0. n = scanf(" %d %x %o". i. printf("\ni = %d j = %d k = %d\n". . int j = 0. } return 0. printf("%d values read. k ).". j.#include <stdio. printf("Input:\n").

printf(“Welcome”). printf (“Good morning “). Output Enter your name>>Abu Good morning Abu Welcome .gets() Function gets() will read a string from the keyboard and automaticly will end the string with a character \0 Example printf(“Enter your name >>”). puts(name). gets(name).