Data Input and Output
By Paribesh Sapkota
Input / Output (I/O) Functions : In „C‟ language, two types of Input/Output functions are available, and all input and output operations are carried out through function calls. Several functions are available for input / output operations in „C‟. These functions are collectively known as the standard i/o library.
Input: In any programming language input means to feed some data into program. This can be given in the form of file or from command line.
Output: In any programming language output means to display some data on screen, printer or in any file.
The Standard Files
C programming treats all the devices as files. So devices such as the display are addressed in the same way as files and the following three files are automatically opened when a program executes to provide access to the keyboard and screen.
Input / Output functions are classified into two types
Formated I/O Functions: formatted I/O functions operates on various types of data.
1 : printf() : output data or result of an operation can be displayed from the computer to a standard output device using the library function printf(). This function is used to print any combination of data.
Syntax : printf(“control string “, variable1, variable2, ———–, variablen);
Ex : printf(“%d”,3977); // Output: 3977
printf() statement another syntax :
Syntax : printf(“fomating string”);
Formating string : it prints all the character given in doublequotes (“ “) except formatting specifier.
C PROGRAMMING Page 29
Ex : printf(“ hello “);-> hello
printf(“a”); -> a
printf(“%d”, a); -> a value
printf(“%d”); -> no display
scanf() : input data can be entered into the computer using the standard input „C‟ library function called scanf(). This function is used to enter any combination of input.
Syntax : scanf(“control string “,&var1, &var2,—-, &varn);
The scanf() function is used to read information from the standard input device (keyboard). Ex : scanf(“ %d “,&a);-> hello
Each variable name (argument) must be preceeded by an ampersand (&). The (&) symbol gives the meaning “address of “ the variable.
Unformatted I/O functions:
- a) Character I/O
- b) String I/O
- a) character I/O:
- getchar(): Used to read a character from the standard input
- putchar(): Used to display a character to standard output
- getch() and getche(): these are used to take the any alpha numeric characters from the standard input
getche() read and display the character
getch() only read the single character but not display
- putch(): Used to display any alpha numeric characters to standard output a) String I/O:
- gets(): Used for accepting any string from the standard input(stdin) eg:gets()
- puts(): Used to display a string or character array Eg:puts() 3. Cgets():read a string from the console eg; cgets(char *st) 4. Cputs():display the string to the console eg; cputs(char *st)
conversion specification
Format specifiers in C are used to take inputs and print the output of a type. The symbol we use in every format specifier is %. Format specifiers tell the compiler about the type of data that must be given or input and the type of data that must be printed on the screen.
List of Format Specifiers in C
The below table contains the most commonly used format specifiers in C
Format Specifier |
Description |
---|---|
%c |
For character type. |
%d |
For signed integer type. |
%e or %E |
For scientific notation of floats. |
%f |
For float type. |
%g or %G |
For float type with the current precision. |
%i |
Unsigned integer |
%ld or %li |
Long |
%lf |
Double |
%Lf |
Long double |
%lu |
Unsigned int or unsigned long |
%lli or %lld |
Long long |
%llu |
Unsigned long long |
%o |
Octal representation |
%p |
Pointer |
%s |
String |
%u |
Unsigned int |
%x or %X |
Hexadecimal representation |
%n |
Prints nothing |
%% | Prints % character |
Examples of Format Specifiers in C
1. Character Format Specifier – %c in C
The %c is the format specifier for the char data type in C language. It can be used for both formatted input and formatted output in C language.
Syntax:
scanf("%d...", ...); printf("%d...", ...);
// C Program to illustrate the %c format specifier. #include <stdio.h> int main() { char c; // using %c for character input scanf("Enter some character: %c", &c); // using %c for character output printf("The entered character: %c", &c); return 0; }
Input:
Enter some character: A
Output:
The entered character: A
2. Integer Format Specifier (signed) – %d in C
We can use the signed integer format specifier %d in the scanf() and print() functions or other functions that use formatted string for input and output of int data type.
Syntax:
scanf("%d...", ...); printf("%i...", ...);
Example:
// C Program to demonstrate the use of %d and %i #include <stdio.h> // Driver code int main() { int x; // taking integer input scanf("Enter the two integers: %d", &x); // printing integer output printf("Printed using %%d: %d\n", x); printf("Printed using %%i: %3i\n", x); return 0; }
Input:
Enter the integer: 45
Output:
Printed using %d: 45 Printed using %i: 45
3. Unsigned Integer Format Specifier – %u in C
The %u is the format specifier for the unsigned integer data type. If we specify a negative integer value to the %u, it converts the integer to its first complement.
Syntax:
printf("%u...", ...); scanf("%u...", ...);
Example: The following C Program demonstrates how to use %u in C.
// C Program to illustrate the how to use %u #include <stdio.h> // driver code int main() { unsigned int var; scanf("Enter an integer: %u", &var); printf("Entered Unsigned Integer: %u", var); // trying to print negative value using %u printf("Printing -10 using %%u: %u\n", -10); return 0; }
Input:
Enter an integer: 25
Output:
Entered unsigned integer: 25 Printing -10 using %u: 4294967286
4. Floating-point format specifier – %f in C
The %f is the floating point format specifier in C language that can be used inside the formatted string for input and output of float data type. Apart from %f, we can use %e or %E format specifiers to print the floating point value in the exponential form.
Syntax:
printf("%f...", ...); scanf("%e...", ...); printf("%E...", ...);
Example:
// C program to demonstrate the use of %f, %e and %E #include <stdio.h> // driver code int main() { float a = 12.67; printf("Using %%f: %f\n", a); printf("Using %%e: %e\n", a); printf("Using %%E, %E", a); return 0; }
Output
Using %f: 12.670000 Using %e: 1.267000e+01 Using %E, 1.267000E+01
String Format Specifier – %s in C
The %s in C is used to print strings or take strings as input.
Syntax:
printf("%s...", ...); scanf("%s...", ...);
Example:
// C program to illustrate the use of %s in C #include <stdio.h> int main() { char a[] = "Hi Geeks" ; printf ( "%s\n" , a); return 0; } |
Output
Hi Geeks
Example: The working of %s with scanf() is a little bit different from its working with printf().
Let’s understand this with the help of the following C program.
// C Program to illustrate the working of %s with scanf() #include <stdio.h> int main() { char str[50]; // taking string as input scanf ( "Enter the String: %s" , str); printf ( "Entered String: %s" , str); return 0; } |
Input
Enter the string: Hi Geeks
Output
Hi
Input and Output Formatting
C language provides some tools using which we can format the input and output. They are generally inserted between the % sign and the format specifier symbol Some of them are as follows:
- A minus(-) sign tells left alignment.
- A number after % specifies the minimum field width to be printed if the characters are less than the size of the width the remaining space is filled with space and if it is greater then it is printed as it is without truncation.
- A period( . ) symbol separates field width with precision.
Precision tells the minimum number of digits in an integer, the maximum number of characters in a string, and the number of digits after the decimal part in a floating value.
Example of I/O Formatting
// C Program to demonstrate the formatting methods. #include <stdio.h> int main() { char str[] = "geeksforgeeks"; printf("%20s\n", str); printf("%-20s\n", str); printf("%20.5s\n", str); printf("%-20.5s\n", str); return 0; }
Output
geeksforgeeks geeksforgeeks geeks geeks