› Module 3: Input and Output › 3.3 Character I/O: getchar(), putchar(), gets()/fgets()
M O D U L E

Module 3: Input and Output

Reading data from the user and displaying results using C's standard I/O library.

3.1 printf() and scanf()

printf() writes formatted output to the console; scanf() reads formatted input from the keyboard into variables. scanf() requires the address-of operator (&) for normal variables so it can write directly into that memory location — this is a very common source of bugs for beginners who forget the &.

How scanf() interacts with memory:

  • In C, function arguments are passed by value (copy). To allow scanf() to modify the caller's variable, you must provide the memory address where the entered value should be stored using the & operator.
  • Forgetting the & in scanf("%d", age) passes the current uninitialized value of age as an arbitrary memory address, causing illegal memory writes and immediate segmentation faults (crashes).
  • Strings (character arrays like char name[50];) already decay to memory addresses, so scanf("%s", name) does not use an explicit &.
int age;
printf("Enter age: ");
scanf("%d", &age);            // &age = address of age
printf("You are %d years old
", age);

3.2 Format Specifiers

Format specifiers tell printf/scanf how to interpret the bytes of a variable: %d/%i for int, %f for float, %lf for double (in scanf), %c for char, %s for strings, %u for unsigned int, %x for hexadecimal, %p for pointers. Using the wrong specifier for a type is undefined behaviour.

Specifier Target Data Type Usage Details
%d or %iintSigned decimal integer
%ffloatStandard 4-byte decimal floating point
%lfdoubleLong float / double precision (mandatory in scanf)
%ccharSingle character
%schar[] / stringSequence of characters terminated by whitespace
%uunsigned intNon-negative unsigned decimal integer
%x / %Xint in hexHexadecimal representation (lowercase/uppercase)
%pvoid* / pointerHexadecimal memory address prefixed by 0x

3.3 Character I/O: getchar(), putchar(), gets()/fgets()

getchar() reads a single character from input; putchar() writes a single character. For whole lines, gets() (unsafe, removed from modern C) has been replaced by fgets(), which takes a buffer size and prevents overflow — always prefer fgets() over gets().

  • getchar() & putchar(): Read and output one character at a time from stdin/stdout. getchar() returns an int so it can capture the special EOF (End-Of-File) marker (-1).
  • Why gets() was abolished: gets() had no parameter to bound user input length. If a user entered more characters than the destination array size, it silently overflowed the buffer, overwriting the return address on the call stack — a notorious vector for buffer overflow exploits.
  • Safe Alternative fgets(): fgets(buffer, sizeof(buffer), stdin) reads up to sizeof(buffer) - 1 characters and automatically appends the null terminator .
MODULE COMPREHENSIVE QUIZ

Module 3 Final Quiz

Test your understanding across all 3 topics of this module with interview-grade questions, tricky edge cases, and code output predictions.

Take Module 3 Final Quiz →
Yukti Mascot