C programming | Printf and scanf ||
Programming Codester
Printf and scanf function with C language:
We learnt fundamentals of C in previous tutorial.
If you haven't read the previous tutorial, I mentioned the link below here.
So now it's time to learn print and scanf function.
Both printf and scant functions are inbuilt library functions, defined in stdio.h (header file).
Printf;
The printf functions create and output strings formatted at runtime. They are part of the standard C library. Additionally, the printf functionality is implemented in other languages (such as Perl). These functions allow a programmer to create a string based on a format string and a variable number of arguments.
In shot Printf function is used to print any statements in C language.
The syntax of printf function:
Printf(“any statement you want”);
Example syntax of printf:
Printf(“programming codester”);
Scanf:
In C programming language, scanf is a function that stands for Scan Formatted String. It reads data from include<stdio> (standard input stream i.e., usually keyboard) and then writes the result into the given arguments. It accepts character, string, and numeric data from the user using standard input.
Scanf function used to read any num, character, string, etc.
The syntax of scanf function:
scanf(“%x”,&variable);
where %X is the format specifier which tells the compiler what type of data is in a variable.
& refers to address of “variable” which is directing the input value to an address returned by &variable.
Format specifier | Supported data types
| Example
| Description
|
%d
| Integer
| scanf(“%d”, &a)
| Accept integer values such as 1, 5, 25, 105 etc.
|
%f
| Float
| scanf(“%f”, &b)
| Accept floating values such as 1.5, 15.20 etc.
|
%c
| Character
| scanf(“%c”, &c)
| Accept character values such as a, f, j, W, Z etc.
|
%s
| String
| scanf(“%s”, &d)
| Accept string value such as diet, India etc. |
Now I give example of printing and scanning statement:
Whenever we use printf for output we can’t use & with variable.
Find the error of this program:
#include<stdio.h>
int main()
{
int a,b;
printf(Enter value of A and B);
scanf("%d %d",&a,&b);
}
So, I hope you all understand about printf and scanf.
So, if you didn’t read my previous vlog, I will mention all links. Watch and learn!
Learn and explore!!
Comments
Post a Comment