In this article printf function in c we give the information about We can read and write a letter on the screen using the printf () and scanf () functions but this does not apply in all situations.
printf function in c:
Formatted Output in C:
Reading, processing, and writing data are the three essential functions of a computer program. Most programs take some data as input and display the processed data on the appropriate medium, which is known as information or result. Two methods of assigning data to program variables.
- One method is to assign values to variables through the assignment statements such as x=5; a=0; and so on.
- Another way is to use the input function scanf which can read data from keywords.
- All input / output operations are performed by function calls such as printf and scanf. These functions are collectively known as the standard I / O library.
- There is a way to use an assignment statement like x = 5; a = 0; Etc.
Reading Character: getchar()
We can read and write a letter on the screen using the printf () and scanf () functions but this does not apply in all situations. There are some functions available in C programming language that read letters or number of letters directly from the keyboard.
This is a default C language function available in the stdio.h header file. Using this function we can read the characters on the keyboard and store the characters in variables. Store all data in a character array when you want to read multiple character form keyboards.
getchar takes the following forms:
variable_name = getchar ();
Variable_name is a valid C name declared as four types. When this statement appears, the computer pauses until the key is pressed and then assigns the value to the value of the getchar function.
Since getchar is used on the right side of the assignment statement, the character value of getchar is assigned to the variable name on the left instead.
Name = getchar ();
When we press the H key on the keyboard, we assign the letter ‘H’ to the name of the variable.
int main ()
printf(“Enter character: “);
c = getchar();
printf(“Character entered: “);
Enter character: a
Character entered: a
Formatted input in C:
The Formatted input refers to input data presented in a specific format. For example, consider the following data:
15.75 123 Rajveer
This line consists of three pieces of data, arranged in a special format. Such data must be read in the format for which it is intended.
For example, the first part of the data must be read in a variable float. The second part is in int and the third part is in char. This is possible by using the scanf function in C. (Scanf means scan format)
scanf (“control string”, arg1, arg2, ………, argn);
The control string specifies the field format in which the data is to be entered and the arguments arg1, arg2, …… argn specify the address of the places where the data is stored.
Control strings and arguments are separated by commas.
The control string contains field specifications, which guide the interpretation of the input data.
These may include:
1. Field specification, which contains conversion character%, data type character, and an optional number, specifying the width of the field.
2. Space, tab or new line.
Spaces, tabs or new lines are ignored. The data type character indicates the data type to assign the variable to the corresponding argument. Specified field width is optional.
int length, breadth, area;
printf(“\nEnter the Length of Rectangle : “);
printf(“\nEnter the Breadth of Rectangle : “);
area = length * breadth;
printf(“\nArea of Rectangle : %d”, area);
Enter the Length of Rectangle : 5
Enter the Breadth of Rectangle : 4
Area of Rectangle : 20