- 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 User Input
![]() Share with a Friend |
C Programming - C User Input
C User Input
In C programming, user input allows the program to take data from the user during runtime. The scanf() function is most commonly used for this purpose, but other methods, such as using file handling or command-line arguments, are also available for more advanced use cases.
- Using scanf() for User Input
The scanf() function reads input from the standard input (usually the keyboard). It is included in the <stdio.h> header file.
Syntax
scanf("format_specifier", &variable);
- format_specifier: Specifies the type of data to be input (e.g., %d for integers, %f for floats).
- &variable: Address-of operator (&) is used to store the input value in the variable.
Example: Reading User Input
#include <stdio.h>int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age); // Reads an integer input from the user
printf("You are %d years old.\n", age);
return 0;
}
- Reading Multiple Inputs
You can read multiple inputs using multiple format specifiers in a single scanf() call.
Example
#include <stdio.h>int main() {
int num1, num2;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2); // Reads two integers
printf("You entered %d and %d.\n", num1, num2);
return 0;
}
- Input with Strings
For strings, the %s format specifier is used. Strings do not require the address-of operator (&) in scanf() because they are already pointers.
Example
#include <stdio.h>int main() {
char name[50];
printf("Enter your name: ");
scanf("%s", name); // Reads a string (stops at whitespace)
printf("Hello, %s!\n", name);
return 0;
}
Note: scanf("%s") reads until the first whitespace. To read a full line of input, use fgets().
- Using fgets() for Safer Input
The fgets() function is preferred over scanf("%s") for reading strings because it allows spaces in the input and prevents buffer overflow.
Syntax
fgets(variable, size, stdin);
- variable: Array to store the input string.
- size: Maximum size of the input (including the null character).
- stdin: Standard input (keyboard).
Example
#include <stdio.h>int main() {
char name[50];
printf("Enter your full name: ");
fgets(name, sizeof(name), stdin); // Reads a full line of input
printf("Hello, %s", name);
return 0;
}
- Reading Character Input
The %c format specifier is used for single character input.
Example
#include <stdio.h>int main() {
char grade;
printf("Enter your grade: ");
scanf(" %c", &grade); // Note the space before %c to consume any leftover newline
printf("Your grade is %c.\n", grade);
return 0;
}
- Handling Input Errors
When using scanf(), you can check the return value to ensure the input was successful.
Example
#include <stdio.h>int main() {
int num;
printf("Enter an integer: ");
if (scanf("%d", &num) == 1) { // Checks if one integer was successfully read
printf("You entered %d.\n", num);
} else {
printf("Invalid input.\n");
}
return 0;
}
- Limitations of scanf()
- Does not handle whitespace well: Stops reading strings at the first space.
- Buffer overflow risk: Can lead to undefined behavior if the input exceeds the allocated size.
To overcome these limitations, use fgets() or input validation.
Key Points
| Method | Best for | Example |
|---|---|---|
scanf() |
Simple input of numbers or single words |
scanf("%d", &num); |
fgets() |
Reading full lines or safer input |
fgets(name, sizeof(name), stdin); |
getchar() |
Single character input |
char c = getchar(); |
By using the appropriate method based on the type and complexity of input, you can effectively handle user input in your C programs.
