Tutorial 3: Output and Input

Outputting Information Using printf

printf is a standard library function which can be used to output information to the display screen. It function is to output whatever information contained between the parentheses. For example,
printf"C program example\n");
The printf is used to print everything between the quotation marks on monitor screen. Notice that there is a ‘\n’ character at the end of the printf statement. This character is called newline escape sequence. The function of this escape sequence is to print a newline. There are more escape sequences available in C. We will be looking at the escape sequences later.

The function printf() can also be used to print the value stored in a variable. For example,
printf"C program example\n");
This printf has two arguments. "%d\n" and num. The first argument consists of conversion specifier "%d" indicates that an integer is to be displayed on the monitor screen. The second argument contains the variable that stores the value to be printed which in this case variable num. In the previous tutorial we have discussed the basic data types which are integer, floating point and character

Integer
Integers can be expressed in base other than decimal (base 10). If 0 is preceding the integer value, the integer is considered to be in octal notation (base 8). For example, to express 17 in octal which is equivalent to 15 in decimal, the notation is 017. An octal value can be printed by specifying its conversion specification, %o in the first argument of printf function. Hexadecimal notation (base 16) of a number is preceded by a zero and the letter x or X. For example, to express 17 in hexadecimal which is equivalent to 23 in decimal, the notation is 0x17. A hexadecimal value can be printed by specifying its conversion specification, %x in the first argument of printf function. The following two statements print variable V in octal and hexadecimal notation.
printf("The value of V is %o\n", V);
printf("The value of V is %x\n", V);
Output The value of V is 17 The value of V is f  

Floating point
Floating point is a number with decimal point (radix point) such as 3.1417, 0.158 and -2.718. Floating point number also can be expressed in exponential notation. For example, 299,792,458 can be represented as 2.997925e8 which is equivalent to 2.997925 x 108. To print floating point number, conversion specification %f is specified in the printf first argument. Conversion specification %e is used to print in exponential notation, %g is to print either in normal or exponential notation. If the value is within the range of [-4,5], %f is used, otherwise %e is used.  

Character
A character in C includes letters, numerics, punctuations and control characters. Punctuations are symbols that indicates the structure and organization of written language such as colon (:), comma (,) and apostrophe ('). Control characters do not correspond to any particular symbol in natural language. Examples of control characters are carriage return, tab and backspace. Variables that are declared as character can store a single character. A character constant is formed by enclosing the character with a pair of single quotation marks such as 'A', ';' and '1'. A conversion specification, %c is specified in the first argument of printf function. The following statements assign a character to a char variable and print its value by using %c.
lttr = 'Z';
printf("lttr is %c\n", lttr);
Inputting Information Using scanf

scanf is a standard library function which is used to input information from keyboard. It takes whatever entered from the keyboard and interprets it as specified by the conversion specifier and stores the result in a variable. The following is an example of scanf function statement.
printf"C program example\n");
The first argument consists of conversion specifier "%d" indicates that an integer is to be read from the standard input which is usually the keyboard. The second argument is where we specify the variable to be used to store the input integer. Notice that the variable name starts with the & (ampersand) character. The & is the address-of operator in C programming. It tells the scanf function the location (address) of the memory is stored in memory.

Lets look at an example of I/O operations
#include <stdio.h>

int main()
{
 int v1, v2;
 printf("Enter two numbers: \n");
 scanf("%d", &v1);
 scanf("%d", &v2);
 printf("v1 is %d\n", v1);
 printf("v2 is %d\n", v2);
 return 0;
}
Sample output:

Enter two numbers:
5
7
v1 is 5
v2 is 7


Explanation:
int v1, v2;
We begin with integer variables declaration namely v1 and v2.
printf("Enter two numbers: \n");
Then we display a prompt message asking user to input two numbers.
scanf("%d", &v1);
scanf("%d", &v2);
These statements read the user inputs from the keyboard and store the values in variable v1 and v2. In the sample output, the entered number are 5 and 7. So 5 is stored in v1 and 7 is stored in v2.
printf("v1 is %d\n", v1);
printf("v2 is %d\n", v2);
The first argument of printf statement consists of string to be displayed and a conversion specifier. The second argument specifies the variable that stores the value to be printed.

No comments:

Post a Comment