Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Formatted I/O in C Language

Introduction

Formatted Input/Output in C allows the programmer to control the input and output formatting of the data. This is particularly useful for creating user-friendly interfaces and for ensuring that data is correctly interpreted by the program. The standard I/O functions for formatted input and output are printf() and scanf().

Formatted Output using printf()

The printf() function is used to print formatted output to the standard output (screen). The general syntax of printf() is:

printf("format string", argument_list);

The format string contains text to be printed along with format specifiers that begin with a '%' character. These specifiers are replaced by the respective arguments in the argument_list.

Common Format Specifiers

  • %d - Integer
  • %f - Floating-point number
  • %c - Character
  • %s - String
  • %x - Hexadecimal integer
  • %o - Octal integer

Example

#include <stdio.h>

int main() {
    int age = 25;
    float height = 5.9;
    char grade = 'A';
    char name[] = "John Doe";

    printf("Name: %s\n", name);
    printf("Age: %d\n", age);
    printf("Height: %.2f\n", height);
    printf("Grade: %c\n", grade);

    return 0;
}

Name: John Doe

Age: 25

Height: 5.90

Grade: A

Formatted Input using scanf()

The scanf() function is used to read formatted input from the standard input (keyboard). The general syntax of scanf() is:

scanf("control string", argument_list);

The control string contains format specifiers that dictate the type of data to be read and stored in the variables listed in the argument_list. Note that the addresses of the variables are passed to scanf().

Example

#include <stdio.h>

int main() {
    int age;
    float height;
    char grade;
    char name[50];

    printf("Enter your name: ");
    scanf("%s", name);
    printf("Enter your age: ");
    scanf("%d", &age);
    printf("Enter your height: ");
    scanf("%f", &height);
    printf("Enter your grade: ");
    scanf(" %c", &grade); // Note the space before %c to consume any leftover newline

    printf("\nName: %s\n", name);
    printf("Age: %d\n", age);
    printf("Height: %.2f\n", height);
    printf("Grade: %c\n", grade);

    return 0;
}

Enter your name: John

Enter your age: 25

Enter your height: 5.9

Enter your grade: A

Name: John

Age: 25

Height: 5.90

Grade: A

Advanced Formatting

Both printf() and scanf() offer advanced formatting options:

Width and Precision

In printf(), you can control the width and precision of the output using the format specifiers. For example:

printf("%10d", 123); // Width of 10
printf("%.2f", 3.14159); // Precision of 2 decimal places

123

3.14

Reading Strings with Spaces

By default, scanf() stops reading a string at the first whitespace. To read strings with spaces, use the %[^\n] specifier:

char fullName[100];
scanf("%[^\n]", fullName);

Conclusion

Formatted I/O is a powerful feature in C that allows you to control how data is read and displayed. By understanding and utilizing format specifiers, you can create more user-friendly and robust programs. Remember to always validate the input to ensure that your program behaves as expected.