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.

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.

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.

How to take input and output of basic types in C?

The basic type in C includes types like int, float, char, etc. Inorder to input or output the specific type, the X in the above syntax is changed with the specific format specifier of that type. The Syntax for input and output for these are:

Input: scanf("%d", &intVariable); Output: printf("%d", intVariable);
Input: scanf("%f", &floatVariable); Output: printf("%f", floatVariable);
Input: scanf("%c", &charVariable); Output: printf("%c", charVariable);

Please refer Format specifiers in C for more examples.


Output
Enter the integer: 10 Entered integer is: 10 Enter the float: 2.5 Entered float is: 2.500000 Enter the Character: A Entered Character is: A

How to take input and output of advanced type in C?

The advanced type in C includes type like String. In order to input or output the string type, the X in the above syntax is changed with the %s format specifier. The Syntax for input and output for String is:

Input: scanf("%s", stringVariable); Output: printf("%s", stringVariable);

Example:


Output
Enter the Word: GeeksForGeeks Entered Word is: GeeksForGeeks Enter the Sentence: Geeks For Geeks Entered Sentence is: Geeks For Geeks