Aug
08
Accepting user input from the console in a C program
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: Read more