Basic Input and Output in C
Basic Input and Output in C
●
●
●
C language has standard libraries that allow input and output in a program.
The stdio.h or standard input output library in C that has methods for input and
output.
printf()
The printf() method, in C, prints the value passed as the parameter to it, on the
console screen.
Syntax:
printf("%X", variableOfXType);
where %X is the format specifier in C. It is a way to tell the compiler what type of
data is in a variable and variableOfXType is the variable to be printed.
● Printing of an Integer
#include <stdio.h>
void main() {
Output:
● Printing of a Character
#include <stdio.h>
void main() {
char ch = 'A';
Output:
The character is: A
● Printing of Float
#include <stdio.h>
void main() {
Output:
#include <stdio.h>
void main() {
int month = 8;
Output:
scanf()
The scanf() method, in C, reads the value from the console as per the type specified
and store it in the given address.
Syntax:
scanf("%X", &variableOfXType);
where %X is the format specifier in C. It is a way to tell the compiler what type of
data is in a variable and & is the address operator in C, which tells the compiler to
change the real value of variableOfXType, stored at this address in the memory.
#include <stdio.h>
void main() {
int num;
scanf("%d", &num);
Output:
Enter an integer: 25
#include <stdio.h>
void main() {
float num;
scanf("%f", &num);
Output:
Enter a float value: 3.14
#include <stdio.h>
void main() {
char ch;
Output:
Enter a character: A
void main() {
Output: