Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Output using printf() function
2.1.
Syntax
2.1.1.
Example
3.
Input using scanf() function
3.1.
Syntax
3.1.1.
Example
4.
Input a character using getchar()
5.
Output a character using putchar()
5.1.
Example of getchar() & putchar()
6.
Frequently Asked Questions
6.1.
1. Can we specify a variable field width in a scanf() format string?
6.2.
2. In a call to printf() function, the format specifier %b can be used to print the binary equivalent of an integer?
6.3.
3. stderr, stdin, stdout are FILE pointers?
7.
Conclusion
Last Updated: Jun 26, 2024
Easy

Input and Output Functions in C

Author Ravi Khorwal
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

In the C program, three primary functions take area accepting data as Input, processing of data, and generating output. Data recognition refers to Input, while the presentation of data refers to an output. In the C program, it will accept input from the keyboard while displaying output on the screen.

Input and Output Functions in C

Output using printf() function

The printf() function is described within the header file stdio.h, and we use it to display the usual output.

Syntax

printf("format string containing format-specifiers", variable_list);

 

Example

#include <stdio.h>
int main()
{
    int num1, num2, sum;
    /* Simple message to user */
    printf("Enter two numbers: ");
    /* Input value in two variables */
    scanf("%d%d", &num1, &num2);
    /* Find sum */
    sum = num1 + num2;
    /* Print sum */
    printf("Sum = %d", sum);
    return 0;
} 

 

You can also read about the jump statement, Tribonacci Series

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Input using scanf() function

We use the scanf() function each time we want the program to get hold of inputs from us. Thus, while this system receives Input from the user, it stores the input values into any variable.

Syntax

scanf("format-specifiers", var1-memory-address, ... , varN-memory-address);

 

Example

#include <stdio.h>
int main() 
{
    int input_from_user;
    printf(“ Enter a digit here: “);
    scanf(“%d”, &input_from_user);
    printf(“Entered number is: %d”, input_from_user);
    return 0;
}

 

Output

Enter a digit here: 20
The digit that you entered is: 20

Input a character using getchar()

In C, we use the getchar() function to study a single individual from the keyboard. The getchar() function returns an integer i.e. getchar() function waits for Input till the user presses any character. It accepts any individual as an input. For this reason, be cautious while using getchar(). As non-printable characters like blank area, new lines, tab, etc., are also considered Input to getchar().

Output a character using putchar()

We use C's putchar() function to show or output a character on the console (display). The putchar() function takes an integer argument and displays its character representation on the console.

Example of getchar() & putchar()

#include <stdio.h>
int main()
{
    char grade;
    printf("Enter student grade: ");
    /* Input character using getchar() and store in grade */
    grade = getchar();
    /* Output grade using putchar() */
    putchar(grade);
    return 0;
}

Frequently Asked Questions

1. Can we specify a variable field width in a scanf() format string?

Ans: In scanf(), a * in a format string after a % sign is used for the suppression of an assignment. That's, The current input field is scanned but not STORED.

2. In a call to printf() function, the format specifier %b can be used to print the binary equivalent of an integer?

Ans: There is no format specifier named %b in c.

3. stderr, stdin, stdout are FILE pointers?

Ans: Yes, these will be declared like

The corresponding stdio.h variable is FILE* stdin;

The corresponding stdio.h variable is FILE* stdout;

The corresponding stdio.h variable is FILE* stderr;

Conclusion

We hope you have gained some insights on Input and Output Functions in C through this article. The scanf() function is to take Input from the user and the printf() function is used to display output to the user and learned about the getchar() & putchar() objects. You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass