You are on page 1of 15

EC201 - FUNDAMENTAL PROGRAMMING 4.

INPUT OUTPUT
Course Lecturer: Norzaiwin B Zainal Abidin

Output Statements in C programming language:


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

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

printf Conversion Letters and Matching Types


Letter % d, i u o x X f, F e, E g, G a, 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", 17 ); printf( "%u", 17u ); printf( "%o", 17 ); printf( "%x", 26 ); printf( "%X", 26 ); printf( "%f", 3.14 ); printf( "%e", 31.4 ); printf( "%g, %g", 3.14, 0.0000314 ); printf( "%a", 31.0 ); printf( "%c", 65 ); printf( "%s", "Hello" ); int a = 1; printf( "%p", &a ); int a; printf( "ABC%n", &a ); Output % 17 17 21 1a 1A 3.140000 3.140000e+01 3.14, 3.14e-05 0x1.fp+0 A Hello 0064FE00 ABC (a==3)

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

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

In the fourth printf statement we want to print a float. 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). The \n used in the printf statements is called an escape sequence. In this case it represents a newline character. After printing something to the screen you usually want to print something on the next line. If there is no \n then a next printf command will print the string on the same line.

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)

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

The output of the example above: :Hello, world!: : Hello, world!: :Hello, wor: :Hello, world!: :Hello, world! : :Hello, world!: : Hello, wor: :Hello, wor :

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

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

Syntax scanf("conversion specifier", &variable);


Common Conversion Specifiers Used with Scanf %d Receives integer value %f Receives floating-point numbers %c Receives character

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

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 >>); gets(name); printf (Good morning ); puts(name); printf(Welcome); Output Enter your name>>Abu Good morning Abu Welcome

You might also like