Hey there!
Welcome to ClearUrDoubt.com.
In this post, we will look at a C program to accept user input from the console and print it back on the console.
Here is the C program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
/* This program written to accept input from the user and print it back on the console. */ #include <stdio.h> int main() { // integer values int a; printf("Enter a number: "); // ask user to enter a number scanf("%d", &a); // read the user entered value /* if the user enters a value other than a number, output will be unpredictabe. */ printf("You have entered: %d.\n\n", a); // print it back on the console. // floating point values float b; printf("Enter a floating point number: "); // ask user to enter a floating point number scanf("%f", &b); // read the user entered value /* if the user enters a value other than a number, output will be unpredictabe. */ printf("You have entered: %f.\n", b); // print it back on the console. printf("After considering only two decimal points: %.2f.\n\n", b); // print it back on the console using a CONTROL STRING. // char values int c[10]; printf("Enter a string: "); // ask user to enter some text scanf("%s", &c); // read the user entered text /* Character array will read anything from user. So, the output will be same as entered by the user. */ printf("You have entered: %s.\n\n", c); // print it back on the console. return 0; } |
Output:
Let’s go through each line and explore the details.
1 2 3 |
int a; float b; int c[10]; |
These lines declare the datatypes of the variables. the variables a, b and c are declared as an integer, a floating point, and a char array type respectively.
1 2 3 |
scanf("%d", &a); // read the user entered value scanf("%f", &b); // read the user entered value scanf("%s", &c); // read the user entered text |
Like “printf()” function, “scanf()” is also an inbuilt function defined in “stdio.h”. this function will read the input entered on the console by the user and stores the value in the memory location pointed by the corresponding argument. Here “a” is a variable and “&a” denotes the memory location it points to. Similarly “b” and “&b”, “c” and “&c” denote variables and their memory locations.
The “%d”, “%f” & “%s” are called Control Strings in C language. They denote integer, floating point, and character string datatypes respectively.
The format of a Control String:
%[width][.precision][modifier]type
width: specifies the number characters it has to read/write
precision: specifies the number of characters that need to be printed after the precision
modifier: specifies the additional info on the type i.e., if the type is an integer (%i or %d) the applicable modifiers are [h, l]. %hd denotes it is a short int and %ld denotes it is a long int.
1 2 3 4 |
printf("You have entered: %d.\n\n", a); // print it back on the console. printf("You have entered: %f.\n", b); // print it back on the console. printf("After considering only two decimal points: %.2f.\n\n", b); // print it back on the console using a CONTROL STRING. printf("You have entered: %s.\n\n", c); // print it back on the console. |
We are going to print the user input values on the console using Control Strings in the printf() method. As shown in the output, the control string “%.2f”, will print only two decimal points after the precision by rounding up the value.
We have covered the explanation of other lines in the below post. Happy Learning.!! 🙂
Please leave a reply in case of any queries.