Basic Input and Output in C
scanf()
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 this variable, stored at this address in the memory.
printf()
printf(“%X”, variableOfXType);
How to take input and output of basic types in C?
Integer:
Input: scanf("%d", &intVariable);
Output: printf("%d", intVariable);
Float:
Input: scanf("%f", &floatVariable);
Output: printf("%f", floatVariable);
Character:
Input: scanf("%c", &charVariable);
Output: printf("%c", charVariable);
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);
List of Format Specifiers in C
Format
Description Data Type
Specifier
%d Integer int , short , long
unsigned int , unsigned short ,
%u Unsigned Integer
unsigned long
Format
Description Data Type
Specifier
%f Floating Point float , double
%lf Double Precision Floating Point double
%c Character char
%s String char*
%p Pointer Address Any Pointer
%x , %X Hexadecimal Integer int , unsigned int
%o Octal Integer int , unsigned int
%e , %E Scientific Notation (Exponential) float , double
%g , %G Compact Format (Float or Exponential) float , double
Integer (Auto-detecting decimal, octal,
%i int , unsigned int
or hexadecimal)
printf in C
In C language, printf() function is used to print formatted output to the standard output stdout (which is
generally the console screen). The printf function is a part of the C standard library <stdio.h> and it
can allow formatting the output in numerous ways.
Syntax of printf
printf ( "formatted_string", arguments_list);
Parameters
formatted_string: It is a string that specifies the data to be printed. It may also contain a format
specifier to print the value of any variable such as a character and an integer.
arguments_list: These are the variable names corresponding to the format specifier.
Return Value
printf() returns the number of characters printed after successful execution.
If an error occurs, a negative value is returned.
scanf in C
In C programming language, scanf is a function that stands for Scan Formatted String. It is used to
read data from stdin (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 also uses format specifiers like printf.
scanf Syntax
The syntax of scanf() in C is similar to the syntax of printf().
int scanf( const char *format, ... );
Here,
int is the return type.
format is a string that contains the format specifiers(s).
“…” indicates that the function accepts a variable number of arguments.
Return Value of scanf
The scanf in C returns three types of values:
> 0: The number of values converted and assigned successfully.
0: No value was assigned.
< 0: Read error encountered or end-of-file(EOF) reached before any assignment was made.
Why &?
While scanning the input, scanf needs to store that input data somewhere. To store this input data,
scanf needs to known the memory location of a variable. And here comes the ampersand to rescue.
& is also called as address of the operator.
For example, &var is the address of var.