Reading and Printing Data in C Language
Before learning about reading and printing data,
let us first understand format specifiers.
Format Specifiers in C Language
In C programming language, format specifiers are special placeholders.
They are used in input/output functions such as printf() and scanf()
to specify the data type.
They always start with the % symbol, followed by a character that represents
the data type.
Here, we will learn basic format specifiers.
We will study format specifiers in detail later.
Common Format Specifiers in C Language
| Data Type | Specifier | Example |
|---|---|---|
| int | %d | printf("%d", 10); → prints 10 on the screen |
| float | %f | printf("%f", 3.14); → prints 3.140000 on the screen |
| double | %lf | printf("%lf", 3.14159); → prints 3.141590 on the screen |
| char | %c | printf("%c", 'A'); → prints A on the screen |
| string | %s | printf("%s", "Hello"); → prints Hello on the screen |
Printing Data on the Screen (printf() function) in C Language
printf() is the primary function used to display output in C programming.
printf() means “print formatted”.
It displays output on the standard output device (console).
This function is defined in the Standard Input Output library.
Therefore, to use this function, we must include the "stdio.h" header file
in our program.
This is done by writing #include<stdio.h>.
Header files must always be included at the beginning of the program.
Syntax:
printf("format_string", arguments);
- format_string: A string containing plain text and format specifiers.
- arguments: Values or variables that replace the format specifiers.
Features
- Formatted Output: Output can be controlled using width, precision, and alignment.
- Supports Multiple Data Types: Works with integers, floats, doubles, characters, and strings.
Example 1: Printing text only using printf() function in C Language
#include<stdio.h>
int main() {
printf("Welcome to C Programming World");
return 0;
}
Output:
Welcome to C Programming World
Example 2: Printing variable values using format specifiers in printf() function
#include<stdio.h>
int main() {
int quantity = 5;
float price = 10.50;
printf("Quantity: %d, Price: %f", quantity, price);
return 0;
}
Output:
Quantity: 5, Price: 10.500000
Points to Remember about printf()
- You must include <stdio.h> at the beginning of the program.
- Use the correct format specifier for the variable type (e.g., int → %d, float → %f).
- You can print both text and variables in a single printf() call.
- Precision and width can be controlled (e.g., %.2f prints only two digits after the decimal point).
- If you use %.2f instead of %f, the output will be printed as Price: 10.50.
Reading Data from Keyboard (scanf() function) in C Language
scanf() is the main standard library function used to take user input in C.
scanf() means “scan formatted”.
It stores input values in variables so they can be used in the program.
This function is also defined in the Standard Input Output library,
so we must include the "stdio.h" header file to use it.
Syntax:
scanf("format_string", arguments);
- format_string: A string containing format specifiers that describes how input should be read.
- arguments: Variables used to store input values (the variable address must be provided using the & operator).
Features
- Reads Input: Takes data from the user through the keyboard.
- Supports Multiple Data Types: Can read integers, floats, doubles, characters, and strings.
- Requires Address: The variable address (&) must be provided, otherwise incorrect values may be read.
Address-of Operator (&) in C Language
The ampersand (&) is called the address-of operator.
In C language, it is used to find the memory address of a variable.
That means it tells where a variable is stored in memory.
To store a value in a variable using scanf(),
the address of the variable is required.
Therefore, both the variable and its address must be provided to scanf().
Example 1: Printing the address of a variable in C Language
#include<stdio.h>
int main() {
int a = 10;
printf("Value of a: %d\n", a);
printf("Address of a: %p", &a);
return 0;
}
Here,
&a shows the memory location where variable a is stored.
%p is the format specifier used to print a memory address.
Output:
Value of a: 10
Address of a: 000000e52b9ffb2c (address may vary)
Example 2: Reading an integer value from the keyboard using scanf()
#include<stdio.h>
int main() {
int a;
printf("Enter value of a: ");
scanf("%d", &a);
printf("You entered: %d", a);
return 0;
}
Output (User enters 10):
Enter value of a: 10
You entered: 10
Example 3: Reading multiple values from the keyboard using scanf()
#include<stdio.h>
int main() {
int a, b;
printf("Enter values of a and b: ");
scanf("%d %d", &a, &b);
printf("Value a: %d, Value b: %d", a, b);
return 0;
}
Output (User enters 10 and 20):
Enter values of a and b: 10 20
Value a: 10, Value b: 20
Points to Remember about scanf()
- You must include <stdio.h> to use scanf().
- Use the correct format specifier for the variable type.
- The address-of operator (&) must be used with variables (not required for strings).
- Spaces, tabs, and newlines in input affect how scanf() reads data.
Escape Characters in C Language
Escape sequences are special characters used in strings or character constants.
They start with a backslash (\) and represent actions such as newline or tab.
Here, we will learn basic escape characters.
| Escape Character | Meaning | Example Output |
|---|---|---|
| \n | New line (moves cursor to the next line) | New Line |
| \t | Horizontal tab | Tab Space |
| \b | Backspace | Back\bspace |
| \r | Carriage return | Carriage\rReturn |
Example: Printing output in two lines using escape character \n
#include<stdio.h>
int main() {
printf("Welcome,\nC Programming World");
return 0;
}
Output:
Welcome,
C Programming World
(The output is printed in two lines because of \n)
So far, we have learned
- Format Specifiers
- printf() function
- scanf() function
- Address-of operator (&)
- Basic escape characters