Input-Output in C (CT 101): Complete IOE Notes on printf & scanf
Input-Output in C
Input-Output in C
3 hours | 3 marks

COMPUTER PROGRAMMING (CT 101) – Chapter 4

Handling Input-Output in C is a core skill for any programmer. Every useful program needs to interact with the user, whether it’s reading data from the keyboard (input) or displaying results on the screen (output). C provides a rich set of library functions to manage these operations, which are broadly categorized into unformatted and formatted I/O.

Chapter Information

Chapter 4: Input-Output in C (3 hours) – 3 marks

Course: Computer Programming (CT 101), I Year I Part

Description: This guide provides a complete overview of Input-Output in C, covering formatted (printf, scanf) and unformatted (getchar, gets) I/O functions with examples, as per the IOE syllabus.

Credit: Sujan Karki

Detailed Chapter Notes

4.1 Unformatted I/O

Unformatted I/O functions handle data as a stream of characters without regard for a specific format. They are simple and efficient for reading or writing single characters or entire strings.

4.1.1 Character Input: getchar(), getch(), getche()

These functions are used to read a single character from the keyboard.

FunctionHeaderWaits for Enter?Echoes to Screen?Description
getchar()stdio.hYesYesReads a character after the Enter key is pressed.
getch()conio.hNoNoReads a character immediately but does not display it. Useful for passwords.
getche()conio.hNoYesReads a character immediately and displays (echoes) it on the screen.

4.1.2 String Input: gets()

The gets() function reads a line of text, including spaces, from the keyboard until the Enter key is pressed. It is declared in stdio.h.

Syntax: gets(string_variable);

⚠️ Security Warning: The gets() function is considered unsafe because it doesn’t check buffer boundaries, which can lead to a serious security vulnerability called buffer overflow. It is strongly recommended to use fgets() instead for safer string input.

4.1.3 Character Output: putchar()

The putchar() function displays a single character on the screen. It is the unformatted counterpart to getchar() and is declared in stdio.h.

Syntax: putchar(character_variable);

4.1.4 String Output: puts()

The puts() function displays a string on the screen. After printing the string, it automatically adds a newline character (\n). It is declared in stdio.h.

Syntax: puts(string_variable);

4.2 Formatted I/O

Formatted I/O functions give you precise control over the format of your input and output, using a special “control string” to specify data types, alignment, precision, and more.

4.2.1 Formatted Output: printf()

The printf() function is used to print formatted output to the console. It can display text, variable values, and more, all arranged according to the control string.

Syntax: printf("Control string", arg1, arg2, ...);

4.2.2 Formatted Input: scanf()

The scanf() function is used to read formatted input from the keyboard. It reads data according to the format specifiers in the control string and stores it in the memory locations of the provided arguments.

Syntax: scanf("Control string", &arg1, &arg2, ...);

Important: Note the use of the address-of operator (&) before the variable names, which is necessary to tell scanf() where to store the input value in memory.

4.3 Control String

The control string is the key to formatted I/O. It contains ordinary characters (which are printed as they are) and format specifiers (which define how arguments are handled).

The general syntax for a format specifier is: %[flag][field_width][.precision]specifier

4.3.1 Flags

Flags are optional characters that modify the output format.

  • - : Left-justifies the output within the field. (Default is right-justification).
  • + : Forces a plus (+) or minus (-) sign to be displayed for signed numbers.
  • 0 : Pads the output with leading zeros instead of spaces to fill the field width.
  • # : Used with octal (%o) and hexadecimal (%x) to prefix them with 0 and 0x respectively. For floats, it forces a decimal point.

4.3.2 Field Width

An optional number that specifies the minimum number of characters to be printed. If the value is shorter, it’s padded with spaces (or zeros if the 0 flag is used). If it’s longer, the field width expands to fit.

4.3.3 Precision

An optional number preceded by a period (.). Its meaning varies:

  • For floats (%f): Specifies the number of digits after the decimal point.
  • For integers (%d): Specifies the minimum number of digits to print (pads with zeros if needed).
  • For strings (%s): Specifies the maximum number of characters to print.

4.3.4 Specifier (Conversion Character)

A required character that indicates the data type of the argument.

SpecifierData Type
%cA single character
%d or %iA signed decimal integer
%fA floating-point number (float)
%lfA double-precision floating-point number (double)
%sA string of characters
%uAn unsigned integer
%xA hexadecimal number (lowercase)
%oAn octal number
%pA pointer address
%eA floating-point number in scientific notation
%ldA long signed integer

PDF Notes

×

Disclaimer

The educational materials provided on this website are intended as supplementary resources to support your learning journey. These study materials are sample documents designed to help students understand complex concepts in C Programming.

We have made every effort to ensure the accuracy of the content. However, we recommend students to refer to standard textbooks and consult with professors for authoritative explanations. These materials should be used as references only.

Scroll to Top