Knowlet

Unit 3: Unformatted and Formatted I/O

1. Introduction to I/O Functions

Input/Output (I/O) functions are essential for interaction between the user and the computer. In C, these functions are categorized based on whether they allow the user to control the format of the data being transferred.

  • Input: Sending data into a program from a keyboard or file.
  • Output: Displaying or storing the results of a program.

2. Formatted I/O: printf() and scanf()

Formatted functions allow the programmer to specify exactly how the data should be read or displayed using format specifiers.

printf() Function

Used to send formatted output to the standard output device (usually the screen).

 printf("format string", argument_list); // Example: printf("Age is %d", age); 

scanf() Function

Used to read formatted input from the standard input device (usually the keyboard).

 scanf("format string", &argument_list); // Example: scanf("%d", &age); 

Common Format Specifiers

Specifier Data Type
%d Signed Decimal Integer
%f Floating-point number
%c Single Character
%s String (Character Array)

3. Unformatted I/O: Character and String Functions

Unformatted I/O functions work with individual characters or strings of characters without using format specifiers.

Character I/O

  • getchar(): Reads a single character from the keyboard.
  • putchar(): Displays a single character on the screen.

String I/O

  • gets(): Reads a string (including spaces) from the keyboard until a newline is reached.
  • puts(): Displays a string on the screen and automatically moves the cursor to a new line.

4. Expressions in C

An expression is a combination of operators, constants, and variables arranged according to the rules of the language.

Definition: An expression evaluates to a single value. For example, a + b is an arithmetic expression.
  • Operands: The data elements (variables or constants) on which operations are performed.
  • Operators: Symbols that trigger specific mathematical or logical actions.

5. Exam Focus Enhancements

Exam Tips

  • The Ampersand (&): Always remember to use the address operator (&) in scanf() for non-string variables.
  • gets vs scanf: Use gets() if you need to input a string that contains spaces; scanf("%s") stops at the first space.
  • Header Files: Ensure #include <stdio.h> is at the top of your program to use these functions.

Common Mistakes

  • Forgetting the & in scanf(). This will cause the program to crash or behave unpredictably.
  • Confusing printf with puts. puts() only works for strings and adds a newline, while printf() is versatile but requires format specifiers.
  • Assuming getchar() will wait for 'Enter'. It captures the very first keystroke it receives.

Frequently Asked Questions

Q: What is the primary difference between formatted and unformatted I/O?
A: Formatted I/O uses format specifiers (%d, %f) to handle different data types, whereas unformatted I/O handles raw characters and strings.

Q: Why do we use putchar() instead of printf()?
A: putchar() is faster and more efficient when you only need to output a single character.

Did this resource help you study?

Share feedback or report issues to help improve this resource.