Lecture 4.
Input Output Functions
Deepthi, Faculty
Department of CSE, NITK
Input Output (I/O) Functions
Input and Output (I/O) Functions: Used to handle data exchange between a program and
external sources like the console (or terminal), files, or other devices.
• Standard I/O Library: The functions for I/O operations are defined in the <stdio.h>
header file.
• Input Functions: Allows a program to read input from users or files.
• Output Functions: Allows a program write output to the console or files.
Two type of I/O Functions:
1. Unformatted I/O Functions
2. Formatted I/O Functions
1
Unformatted I/O Functions
getchar() : Reads a single character from standard input. This function don‘t require
any arguments.
Syntax: variable_name = getchar( );
Example: char ch;
ch = getchar();
putchar() : Writes a single character to the standard output.
Syntax: putchar(variable_name);
Example: char ch = ‘A’;
putchar(ch);
2
Example Program
#include <stdio.h>
int main()
{
char ch;
printf(“Enter a character: ”);
ch = getchar(); // Read a character
putchar(ch); // Print the character
return 0;
}
3
Unformatted I/O Functions
gets() : This function is previously supported. However it is deprecated and removed in
C11, Alternative is fgets().
fgets() : Reads a string from standard input. A string is nothing but a sequence of
characters (Example: "Hello World!“).
Syntax: char *fgets(char *str, int n, FILE *stream);
Example: char buffer[100]; //declare a character array (covered in future)
fgets(buffer, 100, stdin); // Reads a line from the user
puts() : Writes a string to the standard output.
Syntax: int puts(char* str);
Example: puts("Hello, World!");
4
Example Program
#include <stdio.h>
int main()
{
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin); //Read a string
printf("You entered: ");
puts(str); // Output string
return 0;
}
5
Character Test Functions
Defined in <ctype.h> header file.
Function Purpose Returns Example
Non-zero (true) if letter;
isalpha() Checks if the character is an alphabetic letter. isalpha('A')
0 otherwise
Non-zero (true) if digit;
isdigit() Checks if the character is a digit (0-9). isdigit('5')
0 otherwise
Non-zero (true) if lowercase;
islower() Checks if the character is a lowercase letter. islower('b')
0 otherwise
Non-zero (true) if uppercase;
isupper() Checks if the character is an uppercase letter. isupper('G')
0 otherwise
Checks if the character is a whitespace Non-zero (true) if whitespace; 0
isspace() isspace(' ')
character. otherwise
Checks if the character is printable, including Non-zero (true) if printable;
isprint() isprint('#')
space. 0 otherwise
Non-zero (true) if hex digit;
isxdigit() Checks if the character is a hexadecimal digit. isxdigit('a')
0 otherwise
Lowercase equivalent if uppercase;
tolower() Converts an uppercase letter to lowercase. tolower('A')
otherwise unchanged
Uppercase equivalent if lowercase;
toupper() Converts a lowercase letter to uppercase. toupper('b')
otherwise unchanged
6
Example Program
#include <stdio.h>
#include <ctype.h>
int main() {
char char1 = 'A', char2 = '9', char3 = ' ';
printf("Character: '%c'\n", char1);
printf(" - isalpha: %d\n", isalpha(char1));
printf(" - isdigit: %d\n", isdigit(char1)); printf("\n");
printf("Character: '%c'\n", char2);
printf(" - isalpha: %d\n", isalpha(char2));
printf(" - isdigit: %d\n", isdigit(char2)); printf("\n");
printf("Character: '%c'\n", char3);
printf(" - isspace: %d\n", isspace(char3));
printf(" - isprint: %d\n", isprint(char3)); printf("\n");
return 0;} 7
Formatted I/O Functions
• Used to handle input and output with specific formatting.
• Used for accepting/printing single or multiple inputs/outputs.
• The most commonly used functions are
1. printf
2. scanf
8
Formatted I/O Functions
scanf() : Used for receiving single or multiple values as input from the user at the
console.
Syntax:
scanf (format_specifiers, &variable_1, &variable_2,……);
- & refers to the address operator
Format Specifier Description
%d or %i Integer
%ld Long Integer
%f Floating-point number
%lf Double
%c Character
%s String
%x Hexadecimal integer
%o Octal integer 9
Example Program
#include <stdio.h>
Input: 25 1.75 Alice
int main()
{ %d reads the integer 25 and stores it in age.
int age;
float height;
char name[50]; %f reads the float 1.75 and stores it in height.
printf("Enter your age, height, and name: "); %s reads the string Alice and stores it in
scanf("%d %f %s", &age, &height, name); name.
return 0;
} Note: There is no & for name variable.
10
Formatted I/O Functions
printf() : Used to display single or multiple values as output to the user at the console.
Syntax:
printf (format_specifiers, variable_1, variable_2,……);
Format Specifier Description
%d or %i Integer
%ld Long Integer
%f Floating-point number
%lf Double
%c Character
%s String
%x Hexadecimal integer
%o Octal integer
11
Example Program
#include <stdio.h>
int main() {
int age=25;
printf(“%d\n", age);
printf("%f\n", 3.14);
printf("%c\n", 'A');
printf("%s\n", "Hello");
printf("%x\n", 11);
printf("%o\n", 10);
printf("%p\n", &age);
printf("%u\n", 4294967295U);
printf("\n %d \t %f \t %c \t %s\n", 42, 3.14, 'A', "Hello");
return 0;
}
12
Formatting Options in Printf & Scanf
Width: Minimum number of characters to print. If the number is shorter, it will be
padded with spaces.
int num = 5;
printf("|%5d|\n", num); // Output: | 5|
Precision: Number of digits to display after the decimal point for floats, or maximum
number of characters for strings.
float pi = 3.14159;
printf("%.2f\n", pi); // Output: 3.14
char str[] = "Hello";
printf("%.3s\n", str); // Output: Hel
Width & Precision Together:
float pi = 3.14159;
printf("%10.2f\n", pi); // Output: | 3.14|
13
Formatting Options in Printf & Scanf
Flags
- : Left-justify the output within the field width.
int num = 42;
printf("|%-5d|\n", num); // Output: |42 |
0 : Pad the number with leading zeros.
int num = 42;
printf("|%05d|\n", num); // Output: |00042|
+ : Always print a sign for numbers.
int num = 42;
printf("%+d\n", num); // Output: +42
14
Example Program
#include <stdio.h>
int main() {
int num = 123;
float pi = 3.14159;
printf("Formatted integer: |%5d|\n", num); // Outputs: | 123|
printf("Formatted float: |%10.2f|\n", pi); // Outputs: | 3.14|
printf("Left-justified: |%-10s|\n", "Hello"); // Outputs: |Hello |
printf("Zero-padded: |%05d|\n", num); // Outputs: |00123|
return 0;
}
15