- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Input Output
![]() Share with a Friend |
C Programming - C Input Output
C Input and Output (I/O)
In C programming, input and output (I/O) operations are performed using functions from the C standard library, mainly provided in the <stdio.h> header file. The functions for input and output handle the interaction between the program and the user or the system, allowing for data input (from the user, files, etc.) and output (to the screen, files, etc.).
Standard Input and Output Functions
- printf() - Output to the Console (Screen)
- The printf() function is used to display output on the console (screen).
Syntax:
C
int printf(const char *format, ...);
- format: A string that specifies how the output should be formatted. It can contain placeholders (format specifiers) like %d, %f, %s, etc.
- The printf() function returns the number of characters printed.
Example:
C
#include <stdio.h>
int main() {
printf("Hello, World!\n"); // Prints "Hello, World!"
return 0;
}
Output:
Hello, World!
- scanf() - Input from the Console (Keyboard)
- The scanf() function is used to read input from the user.
Syntax:
C
int scanf(const char *format, ...);
- format: A string that specifies the expected type of input. Format specifiers are used to match the input type (e.g., %d for integers, %f for floats, %s for strings).
- The scanf() function returns the number of successful inputs.
Example:
C
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age); // Input an integer
printf("Your age is: %d\n", age);
return 0;
}
Output:
Enter your age: 25
Your age is: 25
Format Specifiers
- %d: Integer (signed decimal)
- %f: Floating-point numbers
- %s: String of characters
- %c: Single character
- %x: Hexadecimal integer
- %p: Pointer address
- %lf: Double precision floating-point number
Formatted Input and Output
You can use the format specifiers with printf() and scanf() to control the formatting of the data being printed or read.
Example: Formatted Output
C
#include <stdio.h>
int main() {
int age = 25;
float height = 5.9;
// Displaying formatted output
printf("Age: %d years\n", age);
printf("Height: %.2f feet\n", height);
return 0;
}
Output:
Age: 25 years
Height: 5.90 feet
Example: Formatted Input
C
#include <stdio.h>
int main() {
int num;
float price;
// Input with formatted specifiers
printf("Enter an integer: ");
scanf("%d", &num);
printf("Enter the price: ");
scanf("%f", &price);
printf("Integer: %d, Price: %.2f\n", num, price);
return 0;
}
Example Input:
Enter an integer: 10
Enter the price: 20.5
Output:
Integer: 10, Price: 20.50
Input and Output with Strings
- gets(): Reads a line of text (deprecated due to security risks like buffer overflow)
- Syntax: char *gets(char *str);
- Reads a string until a newline or the end of the file is encountered.
- It's no longer recommended due to its vulnerability to buffer overflow attacks. Use fgets() instead.
- fgets(): Reads a line of text with a specified limit.
- Syntax: char *fgets(char *str, int n, FILE *stream);
- Safer alternative to gets(). It reads up to n-1 characters and appends a null character at the end.
Example of fgets() usage:
C
#include <stdio.h>
int main() {
char name[100];
printf("Enter your name: ");
fgets(name, sizeof(name), stdin); // Safe input for strings
printf("Hello, %s", name);
return 0;
}
File Input and Output
In addition to standard input and output, C provides functions for file manipulation. These functions allow reading from and writing to files.
- fopen(): Opens a file.
- Syntax: FILE *fopen(const char *filename, const char *mode);
- Modes include:
- "r": Open for reading.
- "w": Open for writing (creates a new file or overwrites an existing file).
- "a": Open for appending data to the end of the file.
- fprintf(): Writes formatted output to a file.
- Syntax: int fprintf(FILE *stream, const char *format, ...);
- fscanf(): Reads formatted input from a file.
- Syntax: int fscanf(FILE *stream, const char *format, ...);
- fclose(): Closes a file.
- Syntax: int fclose(FILE *stream);
Example: Writing to a File
C
#include <stdio.h>
int main() {
FILE *file = fopen("output.txt", "w");
if (file != NULL) {
fprintf(file, "This is written to the file.\n");
fclose(file);
} else {
printf("File opening failed.\n");
}
return 0;
}
Example: Reading from a File
C
#include <stdio.h>
int main() {
FILE *file = fopen("output.txt", "r");
char buffer[100];
if (file != NULL) {
fscanf(file, "%s", buffer);
printf("Read from file: %s\n", buffer);
fclose(file);
} else {
printf("File opening failed.\n");
}
return 0;
}
Conclusion
C provides robust I/O functions, allowing interaction with the user and the system through standard and file-based input/output. Using functions like printf(), scanf(), and file handling functions, C allows for versatile and efficient data manipulation.
- Standard I/O: printf(), scanf()
- File I/O: fopen(), fclose(), fprintf(), fscanf()
- String Input/Output: gets() (deprecated), fgets()
- Formatted I/O: Using format specifiers like %d, %f, %s, etc.
