What is Formatted Input and Output Function in C Language

What is Formatted Input and Output Function in C Language

Input and Output in C Programming – Input refers to the act of providing data to a software. A file or the command line can be used to provide an input. The built-in functions in C programming make it possible to read the input and pass it along to the program as needed.

The term “output” refers to the presentation of data in a file, on the screen, or on a printer. A collection of built-in functions for C programming are available to output data to the computer screen as well as save it to text or binary files.

You must test the C Language code provided in this lesson in your code editor to validate it. However, if you prefer to execute this code online, we also provide a free C Online Compiler for testing your C Language code.

Check out these tutorials to better grasp input and output before continuing with the C language:

Standard Files

Every device is treated as a file in C programming. In order to allow access to the keyboard and screen while a program runs, the following three files are automatically opened. Devices like the display are thus accessed in the same way as files.

What Do Computers Actually Do?
What Do Computers Actually Do?
Standard FileFile PointerDevice
Standard inputstdinKeyboard
Standard outputstdoutScreen
Standard errorstderrYour screen

The mechanism of accessing a file for reading and writing purposes are file pointers. The methods for reading values from the screen and printing the outcome on the screen are covered in this section.

Output in C

Printf() is one of the primary output functions in C programming. The function displays output that has been prepared. For instance,

Example 1 of Output in C

#include <stdio.h>    
int main()
{ 
    // Displays the string inside quotations
    printf("C Programming Tutorial in Input and Output");
    return 0;
}

Output of the above example

C Programming Tutorial in Input and Output

How does this program work?

  • The main() function must be present in every legitimate C program. The main() function is where the code execution starts.
  • A library function called printf() is used to display formatted output on screens. The string is printed within quote marks by the function.
  • We must use the #include stdio.h> declaration to include the stdio.h header file in order to use printf() in our application.
  • The main() function’s return 0; line represents the program’s “Exit status.” It’s not required.

You can try to test here the example above!

Example 2 of Integer Output in C

#include <stdio.h>
int main()
{
    int exampleInteger = 45;
    printf("Number = %d", exampleInteger);
    return 0;
}

Output of the above example

Number = 45

To print int types, we utilize the percent d format specifier. Here, the value of exampleInteger will be used in place of the percentage d enclosed in quote marks.

`You can try to test here the example above!

Example 3 of float and double Output in C

#include <stdio.h>
int main()
{
    float testnum1 = 45.5;
    double testnum2 = 35.4;
    printf("number1 = %f\n", testnum1);
    printf("number2 = %lf", testnum2);
    return 0;
}

Output of the above example

number1 = 45.500000
number2 = 35.400000

We utilize the percent %f format specifier to output float. In a same manner, we print double numbers by using percent %lf.

You can try to test here the example above!

Example 4 of Print Characters in C Output

#include <stdio.h>
int main()
{
    char chr = 'x';    
    printf("character = %c", chr);  
    return 0;
} 

Output of the above example

character = x

We employ the percent %C format specifier to print char.

You can try to test here the example above!

Input in C

One of the often used functions in C programming to receive user input is scanf(). Using common input devices like keyboards, the scanf() function reads formatted input.

Example 5 of Integer Input and Output in C

#include <stdio.h>
int main()
{
    int exampleInteger;
    printf("Enter an integer: ");
    scanf("%d", &exampleInteger);  
    printf("Number = %d",exampleInteger);
    return 0;
}

Output of the above example!

Enter an integer:30
Number = 30

In order to accept int input from the user, we have utilized the percent d format specifier inside the scanf() function. An integer is entered by the user and kept in the testInteger variable.

As you can see, we used &exampleInteger inside of scanf (). This is so that the value given by the user can be saved in exampleInteger address, which is obtained by &exampleInteger.

You can try to test here the example above!

Example 6 of Float and Double Input and Output in C

#include <stdio.h>
int main()
{
    float number1;
    double number2;
    printf("Enter a number: ");
    scanf("%f", &number1);
    printf("Enter another number: ");
    scanf("%lf", &number2);
    printf("number1= %f\n", number1);
    printf("number2= %lf", number2);
    return 0;
}

Output of the above example!

Enter a number:45
Enter another number:69
number1= 45.000000
number2= 69.000000

For float and double, we utilize the percent %f and percent %lf format specifiers, respectively.

You can try to test here the example above!

Example 7 of Character Input and Output in C

#include <stdio.h>
int main()
{
    char character;
    printf("Enter a character: ");
    scanf("%c",&character);     
    printf("You entered %c.", character);  
    return 0;
}   

Output of the above example!

Enter a character: x
You entered x.

The character itself is not kept when a character is typed into the application mentioned above by the user. An integer value (ASCII value) is instead kept on file.

The entered character is seen when we use the %c text format to display that value. When a character is displayed using %d, its ASCII value is printed.

You can try to test here the example above!

Example 8 of ASCII Value in C

#include <stdio.h>
int main()
{
    char character;
    printf("Enter a character: ");
    scanf("%c", &character);     
    // When %c is used, a character is displayed
    printf("You entered %c.\n",character);  
    // When %d is used, ASCII value is displayed
    printf("ASCII value is %d.", character);  
    return 0;
}

Output of the above example!

Enter a character: i
You entered i.
ASCII value is 105.

You can try to test here the example above!

Input and Output Multiple Values in C Program

Here’s how to display multiple user inputs after accepting numerous inputs. Most of the time, novice programmers are looking for methods to have a function return more than one value. Unfortunately, C directly prohibits this. Fortunately, we can easily accomplish this with a little bit of clever programming. The techniques to return multiple values from a function in C are listed below:

Example 9 of Input and Output Multiple Values in C

#include <stdio.h>
int main()
{
    int x;
    float y;
    printf("Enter integer and then a float: ");
  
    // Taking multiple inputs
    scanf("%d%f", &x, &y);
    printf("You entered %d and %f", x, y);  
    return 0;
}

Output of the above example!

Enter integer and then a float: 45
5.9
You entered 45 and 5.900000

You can try to test here the example above!

Format Specifiers for Input and Output in C

From the above examples, you can see that we use

  • %d for int
  • %f for float
  • %lf for double
  • %c for char

This section lists the most popular C data types and the format specifiers used with them.

Data TypeFormat Specifier
int%d
char%c
float%f
double%lf
short int%hd
unsigned int%u
long int%li
long long int%lli
unsigned long int%lu
unsigned long long int%llu
signed char%c
unsigned char%c
long double%Lf

The getchar() and putchar() Functions in C Program

An integer is returned by the int getchar(void) function whenever it is called, and that value is the next character that can be read from the screen. It is only possible to read one character at a time with this function. If you want to read more than one character at a time from the screen, use this method in a loop.

The function int putchar(int c) returns the same character that it displays on the screen. This operation can only insert a single character at a time. If you need to show more than one character at a time, utilize this technique in the loop.

Example 10 of getchar() and putchar() Functions in C

#include <stdio.h>
int main( ) {
   int x;
   printf( "Enter a value :");
   x = getchar( );
   printf( "\nYou entered: ");
   putchar( x );
   return 0;
}

The above code, when compiled and run, will prompt you for some text. Input text, press enter, and the program will read the first character and display it in the format shown below.

Enter a value : example of c program
You entered: e

You can try to test here the example above!

The gets() and puts() Functions in C Language

When called, the char *gets(char *s) function reads a line from stdin into the buffer indicated by s until either a terminating newline or end-of-file is encountered (End of File).

When called, the int puts(const char *s) function outputs the string’s‘ followed by a newline to stdout.

It is recommended that you use fgets() rather than gets(), as the use of gets has been deprecated

Example 11 of gets() and puts() Functions in C

#include <stdio.h>
int main( ) {
   char str[100];
   printf( "Enter a value :");
   gets( str );
   printf( "\nYou entered: ");
   puts( str );
   return 0;
}

Output of the above example!

Enter a value :example of c programming
You entered: example of c programming

The scanf() and printf() Functions in C Language

Standard input (stdin) is read by the int scanf(const char *format,…) function, which then scans the data according to the given format.

The int printf(const char *format,…) function produces output in the specified format and writes it to the standard output stream stdout.

The format may be a single constant string, or you may use the notation %s, %d, %c, etc. to indicate that you want to print or read a string, an integer, a character, or a float. Many other formatting choices are at your disposal and can be employed as needed.

Example 12 of scanf() and printf() Functions

#include <stdio.h>
int main( ) {
   char str[100];
   int x;
   printf( "Enter a value :");
   scanf("%s %d", str, &x);
   printf( "\nYou entered: %s %d ", str, x);
   return 0;
}

Output of the above example!

Enter a value :69
You entered: 69 0

It is important to note that scanf() requires inputs of the same type as %s and %d, so inputs like “string integer” are not acceptable. Providing “string string” or “integer integer” as input will be treated as invalid. Second, scanf() stops reading a string when it hits a space, so “programming is best” is actually three strings.

You can try to test here the example above!

Summary

In this course, we learned about the C language’s input and output. We learned how to utilize the printf() and scanf() functions in code to display output to and collect input from users, respectively.

A few other functions for displaying results and accepting user input were also covered.

Your 15 seconds will motivate us to put forth even more effort.

Inquiries

If you have any questions or suggestions about What is Formatted Input and Output Function in C Language, please feel free to leave a comment below.

About Next Tutorial

In the next following post, I’ll create a File Input and Output in C and try to explain its many components in full details. I hope you enjoy this post on What is Formatted Input and Output Function in C Language, in which I attempt to illustrate the language’s core grammar.


Leave a Comment