DEV Community

Cover image for Formatted Input and Output Functions in C - Basic Guide
jiresimon
jiresimon

Posted on

Formatted Input and Output Functions in C - Basic Guide

If you're a beginner in the C programming language, then here's an important concept for you to learn.

You might have wondered - how does the computer receive an input of characters, read and print the input of characters in C?

In C programming, the scanf() and printf() functions are two basic formatted input and output functions.

Why use a formatted input and output function in C?

These basic functions accept arguments containing a format specification string and a variable or multiple variables.

What does that mean? It means the functions can allow multiple inputs and outputs.

printf("Hello world");

Hmm, Is that the only thing you can do with the "printf() function"?

As you continue to read on, you'll see multiple interesting ways to use that function.

formatted-input-and-output-in-c-basic-guide

Format specifiers in C

Here are four common format specifiers in C for a formatted input and output in C:

Format Specifiers Functions
%d or %i integers
%c characters
%f floating point values
%s strings

Other format specifiers are %u, %lu, %lf etc...To find out more on format specifiers, check out "The C Programming Language by Brian W.Kernighan and Dennis M. Ritchie"

Or read this article on format specifiers in C

The printf() function

The output function in C programming is called "printf." This function converts, formats, and prints to the standard output(screen).

Basic syntax of the printf() function

--> printf(format, var1, var2,...)

How to use the printf() function in C

Step1: Include the header file

#include <stdio.h>

The header file contains the printf() function - a basic I/O(standard input and output) function.

Step2: Write the program inside the main function

int main(){program codes}

//Here's a simple program in C using the printf() function:

#include <stdio.h>

int main(){
     printf("Welcome to my tech blog, and hope you're having a fun-time reading my article");
}
Enter fullscreen mode Exit fullscreen mode

Adding format specifiers to the printf() function

Here are examples of using the format specifier with the printf() function:

#include <stdio.h>

int main(){

    int num1 = 1234; //declare and initialize the variable


    //NOTE: The colon is just to serve as a boundary to see the start and end
    //Every example has an output and reason stated together

    //Example1
    printf(":%d:\n", num1);
    //output>> :1234:
    //Reason>> prints the variable

    //Example2
    printf(":%10d:\n", num1);
    //output>> :      1234:
    //Reason>> allocates 10 digits to the variable...The number has 4digits
    //and that's why there are 6empty digits

    //Example3
    printf(":%010d:\n", num1);
    //output>> :0000001234:
    //Reason>> The 6empty digits gets filled with zeros

    //Example4
    printf(":%-10d:\n", num1);
    //output>> :1234      :
    //Reason>> variable is shifted to the left and the other digits are empty

    //Example5
    printf(":%-15.10i:\n", num1); 
    //output>> :0000001234     :
    //Reason>> left justified and the empty digits are filled with zeros


    //Example6
    printf(":%.10d:\n", num1);
    //output>> :0000001234:
    //Reason>> Same output as Ex3 since the datatype isn't a float


    //Example7
    printf(":%15.10d:\n", num1);
    //output>> :     0000001234:
    //Reason>> 15 digits but only 10 gets filled up...4digits to the number(1234) and 6digits are filled up with zero

}
Enter fullscreen mode Exit fullscreen mode

The scanf() function

The scanf() function reads characters from the standard input(keyboard) and converts them based on the format specification string.

The inputs are also stored in the memory addresses through the other arguments.

Basic Syntax

scanf(format, &var1, &var2)

How to use the scanf() function

Apart from including the header file and the main() function, here's a rule for using the scanf() function in your program.

If your variable is a string or character datatype, there's no need to add the "&" symbol. Other datatypes will require the "&" symbol to store the inputs.

//Example1

#include <stdio.h>

int main(){
     int birth_month;

     printf("Enter your birth month in figures:>>> ");
     scanf("%d", &birth_month);
     printf("Your birth month is, %d", birth_month);
}

//Output

Enter your birth month in figures:>>> 10
Your birth month is, 12
Enter fullscreen mode Exit fullscreen mode

Other formatted input and output functions

sprintf() and sscanf() functions are other notable functions in C but differ slightly in usage.

You can read about the sprinf() function and sscanf() function here.

Conclusion

Formatted input and output help to modify outputs, inputs, and allow multiple variables.

To read more about formatted input and output, read the chapter1 (variables and arithmetic expressions, and chapter7) of "The C Programming Language by Brian W.Kernighan, and Dennis M. Ritchie"

Top comments (2)

Collapse
 
pauljlucas profile image
Paul J. Lucas

Brain W. Kernighan

His name is Brian.

Collapse
 
jiresimon profile image
jiresimon

Thanks, Paul for the correction.