You will learn how to use the functions scanf() and printf() in this lesson to take user input and show the output to the user.
Reading, processing, and writing data are three of a computer’s fundamental tasks, as we are all aware.
Most programs accept data as input and process it before displaying the result, which is known as information. To read and print data in C programming, use the built-in functions scanf() and print().
Standard libraries for the C programming language provide input and output in a program. The C language’s stdio.h library contains functions for input and output.
For transporting data between the computer and the common input and output devices, there are some library functions that are available.
These functions, which may be found in the header file, are connected to the symbolic constants.
Contents
functions of input and output
scanf()
In C, the scanf() function reads the requested kind of value from the console. Syntax:
where%X is the format specifier in C, scanf(“%X”, &variableOfXType); In C, the address operator & instructs the compiler to alter the real value of the variable that is stored at this address in the memory.
It is a means to tell the compiler what kind of data is in a variable.
printf()
The value provided to the printf() function in C is printed on the console screen. Syntax:
Where %X is the format specifier in C, printf(“%X”, variableOfXType); In C, the address operator & instructs the compiler to alter the real value of the variable that is stored at this address in the memory.
It is a means to tell the compiler what kind of data is in a variable.
C Output
Printf() is one of the primary output functions in C programming. The function displays the output that has been prepared. For instance,
Example 1: C Output
#include <stdio.h> int main() { // Displays the string inside quotations printf("C Programming"); return 0; }
Output
C Programming
How does this system operate?
- The main() function must be present in every legitimate C program. The main() function is where the code execution starts.
- A library function called printf() is used to display formatted output on screens. The string is printed within quote marks by the function.
- We must use the #include stdio.h> declaration to include the stdio.h header file in order to use printf() in our application.
- The main() function returns 0; the line represents the program’s “Exit status.” It’s not required.
Example 2: Integer Output
#include <stdio.h> int main() { int testInteger = 5; printf("Number = %d", testInteger); return 0; }
Output
Number = 5
For printing int types, we utilize the %d format specifier. The value of the test integer will be used in place of the%d inside the quotation marks in this case.
Example 3: float and double Output(Input Output)
#include <stdio.h> int main() { float number1 = 13.5; double number2 = 12.4; printf("number1 = %f\n", number1); printf("number2 = %lf", number2); return 0; }
Output
number1 = 13.500000
number2 = 12.400000
We utilize the %f format specifier to print floats. Similarly, to print double values, we use%lf.
Example 4: Print Characters
#include <stdio.h> int main() { char chr = 'a'; printf("character = %c", chr); return 0; }
Output
character = a
We utilize the %c format specifier to output char.
C Input
One of the often used functions in C programming to receive user input is scanf(). Using common input devices like keyboards, the scanf() function reads formatted input.
Example 5: Integer Input Output
#include <stdio.h> int main() { int testInteger; printf("Enter an integer: "); scanf("%d", &testInteger); printf("Number = %d",testInteger); return 0; }
Output
Enter an integer: 4
Number = 4
In order to accept int input from the user, we have utilized the %d format specifier inside the scanf() function. An integer is entered by the user and kept in the testInteger variable.
Note: As you can see, we used &testInteger inside of scanf (). This is so that the value given by the user can be saved in the test integers address, which is obtained by &testInteger.
Example 6: Float and Double Input Output
#include <stdio.h> int main() { float num1; double num2; printf("Enter a number: "); scanf("%f", &num1); printf("Enter another number: "); scanf("%lf", &num2); printf("num1 = %f\n", num1); printf("num2 = %lf", num2); return 0; }
Output
Enter a number: 12.523
Enter another number: 10.2
num1 = 12.523000
num2 = 10.200000
For float and double, we use the format specifiers %f and %lf, respectively.
Example 7: C Character Input Output
#include <stdio.h> int main() { char chr; printf("Enter a character: "); scanf("%c",&chr); printf("You entered %c.", chr); return 0; }
Output
Enter a character: g
You entered g
The character itself is not kept when a character is typed into the application mentioned above by the user. An integer value (ASCII value) is instead kept on file.
The entered character is shown when we display that value using %c text format. When a character is displayed using %d, its ASCII value is printed.
Example 8: ASCII Value
#include <stdio.h> int main() { char chr; printf("Enter a character: "); scanf("%c", &chr); // When %c is used, a character is displayed printf("You entered %c.\n",chr); // When %d is used, ASCII value is displayed printf("ASCII value is %d.", chr); return 0; }
Output
Enter a character: g
You entered g.
ASCII value is 103.
Multiple Values Input Output
Here’s how to display multiple user inputs after accepting numerous inputs.
#include <stdio.h> int main() { int a; float b; printf("Enter integer and then a float: "); // Taking multiple inputs scanf("%d%f", &a, &b); printf("You entered %d and %f", a, b); return 0; }
Output
Enter integer and then a float: -3
3.4
You entered -3 and 3.400000
I/O Format Specifiers(Input Output)
As seen by the instances above, we employ
- for int %d
- for float, %f
- double, %lf
- for char, %c
The most popular C data types and associated format specifiers are shown here.
Data Type | Format Specifier |
---|---|
int | %d |
char | %c |
float | %f |
double | %lf |
short int | %hd |
unsigned int | %u |
long int | %li |
long long int | %lli |
unsigned long int | %lu |
unsigned long long int | %llu |
signed char | %c |
unsigned char | %c |
long double | %Lf |