C Programming Language Tutorial
Variables and Data Types
Input/Output
Looping and Selection Structures
Array
Functions
Preprocessing Command
Pointer
Structure
File Operations
Important Knowledge
Reading characters and strings from the user is an essential part of many C programs. This tutorial will show you how to use the standard C library functions getchar
, scanf
, and fgets
to read characters and strings as input.
getchar
The getchar
function reads a single character from the standard input (usually the keyboard) and returns its integer representation. You can store this integer in a variable of type int
and then cast it to a char
to get the actual character.
Example:
#include <stdio.h> int main() { printf("Enter a character: "); int input = getchar(); char character = (char)input; printf("You entered: '%c'\n", character); return 0; }
scanf
The scanf
function is a versatile function that can read various data types from standard input. To read a string, you can use the %s
format specifier. However, scanf
stops reading input when it encounters whitespace, which means it cannot be used to read strings with spaces.
Example:
#include <stdio.h> int main() { char name[20]; printf("Enter your name: "); scanf("%s", name); printf("Hello, %s!\n", name); return 0; }
fgets
The fgets
function can read a string, including spaces, from a given file stream. To read a string from standard input, you can use fgets
with the stdin
file stream. The fgets
function reads a specified number of characters or until it encounters a newline character or end-of-file (whichever comes first). It also includes the newline character in the resulting string, which you may need to remove.
Example:
#include <stdio.h> #include <string.h> int main() { char sentence[50]; printf("Enter a sentence: "); fgets(sentence, sizeof(sentence), stdin); // Remove the newline character, if present size_t len = strlen(sentence); if (sentence[len - 1] == '\n') { sentence[len - 1] = '\0'; } printf("You entered: \"%s\"\n", sentence); return 0; }
In summary, you can use the getchar
function to read single characters and the scanf
or fgets
functions to read strings in C programming. While scanf
is convenient for reading simple strings without spaces, fgets
provides a more robust way to read strings that include spaces and manage buffer sizes.
Reading Characters from stdin in C:
#include <stdio.h> int main() { char ch; printf("Enter a character: "); scanf("%c", &ch); printf("You entered: %c\n", ch); return 0; }
Input Functions for Characters in C Language:
The getchar()
function can be used to read a single character from stdin.
#include <stdio.h> int main() { char ch; printf("Enter a character: "); ch = getchar(); printf("You entered: %c\n", ch); return 0; }
C Program Examples with Character Input:
#include <stdio.h> int main() { char ch1, ch2; printf("Enter two characters separated by space: "); scanf("%c %c", &ch1, &ch2); printf("You entered: %c and %c\n", ch1, ch2); return 0; }
String Input in C Programming:
#include <stdio.h> int main() { char name[50]; printf("Enter your name: "); fgets(name, sizeof(name), stdin); printf("Hello, %s", name); return 0; }
Using scanf
for Character and String Input in C:
#include <stdio.h> int main() { char ch; char name[50]; printf("Enter a character: "); scanf(" %c", &ch); // Note the space before %c to consume any whitespace printf("Enter your name: "); scanf("%s", name); printf("You entered: %c and %s\n", ch, name); return 0; }
Safe String Input Techniques in C:
Using fgets
for string input to avoid buffer overflow.
#include <stdio.h> int main() { char name[50]; printf("Enter your name: "); fgets(name, sizeof(name), stdin); printf("Hello, %s", name); return 0; }
Handling Whitespace in Character and String Input in C:
Using scanf
with %c
to handle whitespace in character input.
#include <stdio.h> int main() { char ch; printf("Enter a character: "); scanf(" %c", &ch); // Note the space before %c to consume any whitespace printf("You entered: %c\n", ch); return 0; }
Character and String Input Validation in C:
#include <stdio.h> int main() { char gender; printf("Enter your gender (M/F): "); scanf(" %c", &gender); if (gender == 'M' || gender == 'F') { printf("Valid gender: %c\n", gender); } else { printf("Invalid gender\n"); } return 0; }