Read and print one character using getchar() and putchar().
Characters, getchar(), and putchar().
This program reads one character from the keyboard using getchar(). The character is stored in a variable of type char. Then putchar() prints the same character to the screen. This is useful when working with single characters instead of full strings.
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
ch = getchar();
printf("You entered: ");
putchar(ch);
printf("\n");
return 0;
}
Input: A
Output: A
Read and print a string using fgets() and puts().
Character arrays, fgets(), puts(), and safe line input.
A string in C is stored as an array of characters. We declare a character array like char str[100];. The fgets() function reads a line of text safely from input and stores it in the array. puts() prints the string to the output screen. This is a safe way to read text with spaces.
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("You entered: ");
puts(str);
return 0;
}
Input: Hello C
Output: Hello C
Convert a lowercase character to uppercase using character operations.
Character codes and character arithmetic.
In C, each character has an ASCII value. Lowercase letters and uppercase letters differ by a fixed value: 'a' - 'A' = 32. So to convert a lowercase letter to uppercase, we subtract 32 from its ASCII value. This works for lowercase letters a to z.
#include <stdio.h>
int main() {
char ch;
printf("Enter a lowercase letter: ");
scanf("%c", &ch);
if (ch >= 'a' && ch <= 'z') {
ch = ch - 32;
}
printf("Uppercase: %c\n", ch);
return 0;
}
Input: m
Output: M