Input Output - printf() and scanf() functions

printf() and scanf() Functions in C

printf() and scanf() are inbuilt library functions in the C programming language.
These functions are available in the C standard library by default.

They are declared in the header file:

<stdio.h>

To use these functions in a C program, include the header file:

#include <stdio.h>

printf() Function in C

In C programming, the printf() function is used to display output on the screen.
It can print:

  • Characters

  • Strings

  • Integers

  • Floating-point numbers

  • Octal values

  • Hexadecimal values


Common printf Format Specifiers

%c character %d decimal (integer) number (base 10) %ld long integer %e exponential floating-point number %f floating-point number %lf double number %i integer (base 10) %o octal number (base 8) %s string of characters %u unsigned decimal integer %x hexadecimal number (base 16) %% print a percent sign \% print a percent sign

Controlling Integer Width with printf

Minimum Width

%3d
  • Specifies a minimum width of 3 spaces

  • Output is right-justified by default


Left-Justified Output

%-3d
  • Add a minus sign - after %

  • Output becomes left-justified


Zero-Filled Output

%03d
  • Add 0 after %

  • Empty spaces are filled with zeros


printf Integer Formatting Summary

printf() supports several integer formatting options:

  • Minimum width specification

  • Left-justified output

  • Zero-filled output

  • Showing + sign for positive numbers

These options help control how numbers appear in the output.




Formatting Floating-Point Numbers with printf

The printf() function in C allows you to control how floating-point numbers are displayed.

Below are examples showing different ways to format floating-point output using printf():

printf String Formatting

The printf() function in C can be used to format string output.

Below are several examples that demonstrate how to format strings using printf().



printf Special Characters

The following character sequences have a special meaning when used as printf() format specifiers.


\a
Audible alert
\b
Backspace
\f
Form feed
\r
Carriage return
\?
Question mark
\n
New Line
\t
Tab
\v
Vertical tab
\\
Back slash
\0
ASCII null character
\’
Single quotes
\””
Double quotes
\o
Octal Constant
\x
Hexadecimal constant

Using Backslash and Special Characters in printf()

As seen in the previous example, the backslash (\) character is treated as a special character in C.

To print one backslash in the output, you must type two backslashes (\\) inside the string.


Examples of Using Special Characters

Below are some examples demonstrating how to use these special characters in printf().




scanf() Function in C Language

In C programming, the scanf() function is used to read characters, strings, and numeric data from the keyboard.


Basic Syntax Examples

These examples show how to read different data types:

int x; scanf("%i", &x); // or scanf("%d", &x); float x; scanf("%f", &x); char x; scanf("%c", &x); long int x; scanf("%ld", &x); char str[100]; scanf("%s", str);

Using * in scanf() (Ignoring Input)

Sometimes you may need to exclude characters from user input.

Example

Input: 30/01/2018
Goal: Read day, month, year separately as integers.

int day, month, year; scanf("%d%*c%d%*c%d", &day, &month, &year);
  • %c → reads a character

  • %*c → reads one character but does not store it

Here, the two / characters are ignored.

More Examples

  • %*c → exclude one character

  • %*d → exclude one integer

  • %*f → exclude one float

  • %*s → exclude one word

Note: \n and \t are each single characters.


Limiting Input Size in scanf()

You can limit the number of digits or characters by placing a positive integer inside the format specifier.

Examples

int a; scanf("%5d", &a);
  • Takes only the first 5 digits

  • Extra digits are ignored

  • If fewer than 5 digits are entered, input is taken normally

Other Limits

  • %5c → takes exactly 5 characters

    • Array size must be at least 6 (extra space for \0)

  • %5d → integer up to 5 digits

  • %5f → float with at most 5 characters total

  • %5s → string up to 5 non-whitespace characters


Using [set] in scanf() (Pattern-Based Input)

[set] provides regex-like behavior for character input.
It works only with characters or strings, not integers or floats.


Read an Entire Line

char str[100]; scanf("%[^\n]", str);
  • ^ → means up to

  • \n → newline character

  • %[^\n] → read everything until Enter is pressed


More Pattern Examples

  • %[^5] → read characters until 5 appears

    Input: abcde fghk5k zaq
    Output stored: abcde fghk

  • %[0-9] → read only digits

    Input: 1234abc567
    Output stored: 1234

  • %[a-z] → read only lowercase letters

    Input: abc12ijkl
    Output stored: abc

  • %[123] → read any combination of 1, 2, or 3

    Input: 1123390123
    Output stored: 11233


More Information

For additional details on input/output functions, visit:

https://en.cppreference.com/w/c/io/fscanf

Comments

Popular posts from this blog

Programming in C GXEST204 - KTU 2024 scheme syllabus notes pdf ------- Dr Binu V P

Structure of a C Program

Functions in C