Basic Input & Output in C Programming | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

When we say Input, it means to feed some data into a program. An input can be given in the form of a file or from the command line. C programming provides a set of built-in functions to read the given input and feed it to the program as per requirement.

When we say Output, it means to display some data on screen, printer, or in any file. C programming provides a set of built-in functions to output the data on the computer screen as well as to save it in text or binary files.

The Standard Files

C programming treats all the devices as files. So devices such as the display are addressed in the same way as files and the following three files are automatically opened when a program executes to provide access to the keyboard and screen.

Standard FileFile PointerDevice
Standard inputstdinKeyboard
Standard outputstdoutScreen
Standard errorstderrYour screen

The file pointers are the means to access the file for reading and writing purpose. This section explains how to read values from the screen and how to print the result on the screen.

The getchar() and putchar() Functions

The int getchar(void) function reads the next available character from the screen and returns it as an integer. This function reads only single character at a time. You can use this method in the loop in case you want to read more than one character from the screen.

The int putchar(int c) function puts the passed character on the screen and returns the same character. This function puts only single character at a time. You can use this method in the loop in case you want to display more than one character on the screen. Check the following example −

#include <stdio.h>
int main( ) {

int c;

printf( "Enter a value :");
c = getchar( );

printf( "
You entered: ");
putchar( c );

return 0;
}

When the above code is compiled and executed, it waits for you to input some text. When you enter a text and press enter, then the program proceeds and reads only a single character and displays it as follows −

$./a.out
Enter a value : this is test
You entered: t

The gets() and puts() Functions

The char *gets(char *s) function reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF (End of File).

The int puts(const char *s) function writes the string 's' and 'a' trailing newline to stdout.

#include <stdio.h>
int main( ) {

char str[100];

printf( "Enter a value :");
gets( str );

printf( "
You entered: ");
puts( str );

return 0;
}

When the above code is compiled and executed, it waits for you to input some text. When you enter a text and press enter, then the program proceeds and reads the complete line till end, and displays it as follows −

$./a.out
Enter a value : this is test
You entered: this is test

The scanf() and printf() Functions

The int scanf(const char *format, ...) function reads the input from the standard input stream stdin and scans that input according to the format provided.

The int printf(const char *format, ...) function writes the output to the standard output stream stdout and produces the output according to the format provided.

The format can be a simple constant string, but you can specify %s, %d, %c, %f, etc., to print or read strings, integer, character or float respectively. There are many other formatting options available which can be used based on requirements. Let us now proceed with a simple example to understand the concepts better −

#include <stdio.h>
int main( ) {

char str[100];
int i;

printf( "Enter a value :");
scanf("%s %d", str, &i);

printf( "
You entered: %s %d ", str, i);

return 0;
}

When the above code is compiled and executed, it waits for you to input some text. When you enter a text and press enter, then program proceeds and reads the input and displays it as follows −

$./a.out
Enter a value : seven 7
You entered: seven 7

Here, it should be noted that scanf() expects input in the same format as you provided %s and %d, which means you have to provide valid inputs like "string integer". If you provide "string string" or "integer integer", then it will be assumed as wrong input. Secondly, while reading a string, scanf() stops reading as soon as it encounters a space, so "this is test" are three strings for scanf().

The document Basic Input & Output in C Programming | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Basic Input & Output in C Programming - Programming and Data Structures - Computer Science Engineering (CSE)

1. What is the purpose of input and output in C programming?
Ans. In C programming, input and output are essential for interacting with the user and the external world. Input allows the program to receive data or instructions from the user or other sources, while output enables the program to display results or communicate information to the user or external devices.
2. How can I take input from the user in C programming?
Ans. To take input from the user in C programming, you can use the "scanf" function. This function allows you to specify the format of the input and store it into variables. For example, to take an integer input, you can use scanf("%d", &variable_name), where "variable_name" is the name of the variable where you want to store the input value.
3. How can I display output in C programming?
Ans. In C programming, you can use the "printf" function to display output. This function takes a format string as an argument, which specifies how to format and display the output. You can include placeholders in the format string, such as "%d" for integers, "%f" for floats, and "%s" for strings, to display the corresponding values.
4. Can I perform input and output operations simultaneously in C programming?
Ans. Yes, you can perform input and output operations simultaneously in C programming. C provides functions like "scanf" and "printf" that can be used together to take input and display output in the same program. However, it's important to ensure proper synchronization and order of operations to avoid any unexpected behavior.
5. How can I handle errors or invalid input during input operations in C programming?
Ans. In C programming, you can use conditional statements and loops to handle errors or invalid input during input operations. For example, you can use a loop to repeatedly ask for input until a valid value is provided. Additionally, you can use conditional statements to check if the input meets certain criteria and prompt the user to re-enter if it is invalid.
119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

study material

,

Basic Input & Output in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

,

Free

,

Semester Notes

,

MCQs

,

Exam

,

Objective type Questions

,

practice quizzes

,

mock tests for examination

,

video lectures

,

Previous Year Questions with Solutions

,

Extra Questions

,

past year papers

,

Important questions

,

Viva Questions

,

shortcuts and tricks

,

ppt

,

Summary

,

Basic Input & Output in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

,

pdf

,

Sample Paper

,

Basic Input & Output in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

;