Chapter 4

 

Displaying variables-

The function printf is used to display output on the standard output devices.

 

/* An example illustrating an integer output  */

 

#include<stdio.h>

void main()

{

int num;

num=10;

printf (“%d”, num);

}

 

The first line in the program starts with /* and ends with */. Any thing written between /* and */ is called comment. In the C language, comments are an aid to the programmer to read and understand the program. It is not the statement of the language. The compiler ignores the comments.

 

The second line i.e. #include<stdio.h>

This is called the preprocessor directive. It is usually written in the beginning of the program. It commands that the contents of the file stdio.h should be included in the compiled machine code at the place where #include appears. The file stdio.h contains the standard input output routines.

 

The next line is main (). It is a function in C. The main () function is required in all C programs. The execution of program starts from the main (). It is like the main () entry gate for the execution of the program. Note that main () is not followed by a comma or semicolon.

 

The next line is opening brace { . All the C statements must be enclosed within opening brace and closing brace.

 

 

In C, all the variables must be declared before using them. The next line in the program is int num;

The statement is called declaration of the integer type of variable. It informs compiler that num is a variable name and a memory location must be reserved for it in memory. In fact, this statement allocates one memory location named num.

 

The next statement num=10; assigns integer value 10 to the memory location num having certain address.

 

The next statement is printf(“%d”, num);.

This is an output function or output statement in C, It displays the content of memory location num at the standard output device VDU. The %d following the left parentheses is known as the conversion specification. Its role is to specify that the number is to be converted to decimal notation before being printed.

 

Observe, every statement is terminated by semicolon (;). When C compiler comes across with this terminator, it will come to know that it is the end of the statement.

 

The general form of printf statement is

printf(“conversion specifier”, List of variables);

Format string could be:

%f for printing real variables

%d for printing integer values

%c for printing character values

 

 

Note: main () is the only function with return 0, at the end. When control crosses the ending} of main (), it returns control to the operating system by returning 0 to it. The OS normally treats return 0 as the successful termination of the program. Some compiler may accept void main () or any other return type.

 

 

/* An example illustrating integer and floating output */

 

#include<stdio.h>

void main ()

{

int num1;

float num2;

num1=10;

num2=35.32;

printf(“num1 is %d and num2 is %f”,num1, num2);

}

 

Output:

num1 is 10 and num2 is 35.32

 

Two variables, namely num1 of type int and num2 of type float are declared. These statements create two memory locations of type int and float.

 

The next two statements sum1=10 and num2=35.32 assign 10 to the integer type of memory location num1 and 35.32 to float type.

 

 

Inside printf statement, the format %d and %f specify the type of variables to be output. They are called conversion specifier. To first conversion specifier, %d corresponds to the first parameter num1. It informs printf that num1 is an integer type of data. Similarly, the second conversion specifier, %f corresponds to the second parameter num2 and informs printf that num2 is a float type of data.

printf(“num1 is %d and num2 is %f”,num1,num2);

 

%d is replaced by the value of the parameter num1 and %f by the value num2.

 

 

 

Reading variables

 

The scanf() function reads the values of variables from the standard device(keyboard) and stores them in variables. The example below reads the two values from the keyboard and displays it on the standard output device.

 

/* an example illustrating reading of variables */

 

#include<stdio.h>

void main()

{

int num1,num2;

printf(“Enter the two number”);

scanf(“%d%d”,&num1,&num2);

printf(“The numbers are %d and %d”, num1,num2);

}

 

Output:

Enter the two numbers

24 45

The numbers are 24 and 45

 

After the execution of the statement int num1, num2; two memory location namely num1 and num2 of type integer are allocated.

 

The statement printf (“Enter the two numbers”); displays string on the monitor.

scanf (“%d %d”, &num1, &num2);

 

The scanf function stores the input value in memory. To perform this, it must know the name and address of the variable in memory.

The first conversion specifier %d corresponds to num1 preceded by an ampersand and second %d corresponds to num2 preceded by an ampersand for storing integer type of data. Here ampersand is address operator which is used for sending address of variables num1 and num2 to scanf ().

Again the statement printf (“….”) displays the content of memory at the standard output device.

 

 Escape Sequences and control characters

 

Certain ASCII characters are unprintable which means they are not displayed on the screen. Some of the more commonly used escape sequences.

 

Escape Sequence                                           Meaning

\a                                                                     alert (bell)

\f                                                                      form feed

\b                                                                     back space

\n                                                                     new line

\r                                                                      carriage return

\t                                                                      horizontal tab

\v                                                                     vertical tab

\\                                                                      backslash

\?                                                                     Question mark

\’                                                                      single quote

\”                                                                     double quote

 

 Full Width


Post a Comment

0 Comments