In C programming, the printf function is employed to showcase output by presenting the specified statement on the console. This function is extensively utilized in C programming and has the flexibility to accept multiple arguments. With printf, you can output text, numeric values, and variables to the standard output stream.
Syntax:
The format of the printf function is shown as follows:
printf("format string", argument_list);
In this particular syntax,
- The format string serves the purpose of specifying the output structure and incorporating various format specifiers like %d (integer), %c (character), %s (string), %f (float), and more.
- The argument_list is utilized for transmitting the arguments (values of variables) that will substitute the format specifiers.
Printf Function Example in C
Let's consider a scenario to demonstrate the printf function in the C programming language.
Example
#include <stdio.h>
int main() { //main function
int stu_age = 24; //using integer
float pi = 3.14159; //using float with 2 decimals
char grade = 'A'; //using char
char stu_name[] = "Michael Bravo";
printf("Name: %s\n", stu_name);
printf("Age: %d\n", stu_age);
printf("Grade: %c\n", grade);
printf("Pi (2 decimals): %.2f\n", pi);
return 0;
}
Output:
Name: Michael Bravo
Age: 24
Grade: A
Pi (2 decimals): 3.14
Explanation:
In this instance, we've selected four distinct data types along with their respective values: integer, floating-point number, character, and string. Subsequently, we employ the printf function to exhibit the values of these various data types in a structured manner.
ImportanLogic Practices about the printf function
Several importanLogic Practices about the printf function in the C programming language are as follows:
- The printf function is declared in the stdio.h header file. It is used to display the output in the console.
- It is also used to print a different type of data format on the output string.
- We need to use the "\n" format specifiers in the printf statement to print a new line on the console.
Format Specifier in C
In C programming, the format specifier serves as a string utilized within input and output functions that are formatted. C offers a variety of format specifiers, with some commonly used ones outlined below:
| Format Specifier | Meaning | Results |
|---|---|---|
%d |
It is used to print the integer value. | 10 |
%f |
It is used to print the floating-point value. | 3.141593 |
%.2f |
It is used to print the float with 2 decimal values. | 3.14 |
%c |
It is used to print the character value. | A |
%s |
It is used to print the string. | Hello |
%u |
It is used to print the unsigned integer value. | 25 |
%x |
It is used to print the hexadecimal value. | 1a |
%o |
It is used to print the octal value. | 12 |
Scanf Function
In C programming, the scanf function is employed to receive user input by reading data from the console. This function is versatile, capable of reading various data types like integers, decimals, strings, characters, and more. To use scanf, provide a format string along with the memory addresses (using the & symbol) where the input will be stored in C.
Syntax:
It has the following syntax:
scanf("format string", argument_list);
In this arrangement,
- Formatting string: Its purpose is to specify the type of input and contain various format placeholders like %d (integer), %c (character), %s (string), %f (float), and more.
- Argument list: This section is responsible for supplying the memory locations of variables (using &) where the input data will be saved.
Scanf Function Example in C
Let's consider a scenario to demonstrate the Scanf function in C programming. In this instance, we'll retrieve user input and display the cube of the provided number.
Example
#include<stdio.h>
int main(){ //main function
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Cube of a number is: %d ", num*num*num);
return 0;
}
Output:
Enter a number:15
Cube of a number is:3375
Explanation:
In this instance, we can infer that the scanf("%d", &num) function retrieves an integer input from the console and saves it in the variable named number. Subsequently, the printf("cube of number is: %d ", numnumnum) function displays the cube of the input number on the console.
C Program to print the sum of 2 numbers using both printf and scanf functions
Let's consider a basic example in the C programming language that displays the total of two numbers.
Example
#include<stdio.h>
int main(){ //main function
int x=0, y=0, result=0;
printf("Enter first number: ");
scanf("%d", &x);
printf("Enter second number: ");
scanf("%d", &y);
result = x + y;
printf("sum of 2 numbers: %d ", result);
return 0;
}
Output:
Enter first number: 15
Enter second number: 20
sum of 2 numbers: 35
Explanation:
In this instance, we determine the total of two numerical values by employing the printf and scanf functions. In this scenario, we define 3 integer variables: x, y, and result. Subsequently, the scanf function acquires input for x and y, computes their sum, and presents the result using the printf function.
C Program to Swap Two Numbers
Let's consider an illustration to demonstrate the process of exchanging two values in C programming.
Example
#include <stdio.h>
int main() { //main function
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Output:
Enter two numbers: 10
21
After swapping: a = 21, b = 10
Explanation:
In this instance, a pair of integer variables are assigned user-input values via the scanf function. These values are then interchanged by employing a temporary variable. Subsequently, the printf function is utilized to exhibit the exchanged values.
Conclusion
In summary, the printf function is employed to showcase information on the console. Conversely, the scanf function is used to obtain user input. These functions are fundamental for handling input/output tasks in C programming. Through the utilization of format specifiers, these functions can manage various data types like integers, floating-point numbers, characters, and strings. This versatility is crucial for developing interactive software applications.