INPUT AND OUTPUT OPERATIONS
ANSI standard has defined many library functions for input and output in C language.
printf() and scanf() are the most commonly used to display output and take input respectively.
Let us consider an example:
#include <stdio.h> //This is needed to run printf() function.
int main()
{
printf("My course BCA"); //displays the content inside quotation
return 0;
}
Output
My course BCA
In the above program
printf() is a library function to display output which only works if #include<stdio.h>is included at the beginning.
Here, stdio.h is a standard input output header file .
#include is command to paste the code from the header file whenever necessary.
When compiler encounters printf() function and doesn't find stdio.h header file, compiler shows error.
I/O of integers in C
#include<stdio.h>
int main()
{
int c=5;
printf()("Number=%d",c);
return 0;
}
Output
Number=5
Inside quotation of printf() there, is a conversion format string "%d" (for integer). If this conversion format string matches with remaining argument,i.e, c in this case, value of c is displayed.
#include<stdio.h>
int main()
{
int c;
printf()("Enter a number\n");
scanf()("%d",&c);
printf()("Number=%d",c);
return 0;
}
Output
Enter a number
4
Number=4
The scanf() function is used to take input from user.
In this program, the user is asked a input and value is stored in variable c.
Note the '&' sign before c. &c denotes the address of c and value is stored in that address.
Programming In c is easy you should store your values in variables, then later perform various operations on them and finally display the values from the variables.
Try various programs by changing data types and format specifiers.
ANSI standard has defined many library functions for input and output in C language.
printf() and scanf() are the most commonly used to display output and take input respectively.
Let us consider an example:
#include <stdio.h> //This is needed to run printf() function.
int main()
{
printf("My course BCA"); //displays the content inside quotation
return 0;
}
Output
My course BCA
In the above program
printf() is a library function to display output which only works if #include<stdio.h>is included at the beginning.
Here, stdio.h is a standard input output header file .
#include is command to paste the code from the header file whenever necessary.
When compiler encounters printf() function and doesn't find stdio.h header file, compiler shows error.
I/O of integers in C
#include<stdio.h>
int main()
{
int c=5;
printf()("Number=%d",c);
return 0;
}
Output
Number=5
Inside quotation of printf() there, is a conversion format string "%d" (for integer). If this conversion format string matches with remaining argument,i.e, c in this case, value of c is displayed.
#include<stdio.h>
int main()
{
int c;
printf()("Enter a number\n");
scanf()("%d",&c);
printf()("Number=%d",c);
return 0;
}
Output
Enter a number
4
Number=4
The scanf() function is used to take input from user.
In this program, the user is asked a input and value is stored in variable c.
Note the '&' sign before c. &c denotes the address of c and value is stored in that address.
Programming In c is easy you should store your values in variables, then later perform various operations on them and finally display the values from the variables.
Try various programs by changing data types and format specifiers.
No comments:
Post a Comment