printf and scanf function in C
printf and scanf function in C
C is a popular programming language that is widely used for system programming, embedded systems, and scientific computing. One of the key features of C is its ability to perform input and output operations using the standard input/output functions provided by the language. In this article, we will explore two of the most commonly used input/output functions in C: printf and scanf.
Printf Function in C
The printf function is used to output data to the console or other output devices. It is defined in the stdio.h header file, which must be included at the beginning of the program.
The general syntax of the printf function is as follows:
printf(“format string”, argument1, argument2, …);
The format string is a string literal that contains format specifiers, which are used to format the output. The arguments are the values to be printed, which are inserted into the format string according to the format specifiers.
Here is an example of using printf function in C:
#include <stdio.h> int main() { int a = 5, b = 10; printf("The value of a is %d and the value of b is %d\n", a, b); return 0; }
In this program, we declare two integer variables, initialize them, and then use the printf function to print their values to the console. The %d format specifier is used to print integer values.
The output of the program will be:
The value of a is 5 and the value of b is 10
Scanf Function in C
The scanf function is used to read input from the console or other input devices. It is also defined in the stdio.h header file and has a similar syntax to the printf function:
scanf(“format string”, argument1, argument2, …);
The format string contains format specifiers that indicate the type and format of the input. The arguments are pointers to the variables where the input values will be stored.
Here is an example of using scanf function in C:
#include <stdio.h> int main() { int age; printf("Enter your age: "); scanf("%d", &age); printf("Your age is %d\n", age); return 0; }
In this program, we declare an integer variable age and use the printf function to prompt the user to enter their age. We then use the scanf function to read the input value from the console and store it in the age variable. Finally, we use the printf function to print the value of the age variable.
When you run this program, you should see the following output:
Enter your age: 30 Your age is 30
Conclusion
The printf and scanf functions are two of the most commonly used input/output functions in C language. They offer a simple and versatile method for performing input and output tasks in C. You can create more effective and practical C programmes by being familiar with the syntax and application of these functions.
So, in this tutorial you have learned printf and scanf function in C programming
with its examples. I hope you understood this tutorial well, and if you have any doubt about regarding this then you can ask in the comment section.
Learn More;
3 Comments
Æthelburg Knight · August 8, 2023 at 5:37 pm
Ahaa, its nice conversation on the topic of this paragraph here at this blog, I have read all that, so at this time me also commenting here.
tlovertonet · May 7, 2024 at 11:49 am
Wonderful web site. Plenty of useful info here. I’m sending it to a few friends ans also sharing in delicious. And naturally, thanks on your sweat!
smortergiremal · November 4, 2024 at 4:00 am
I like this web site its a master peace ! Glad I observed this on google .