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.
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( "\nYou 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 −
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.
NOTE: Though it has been deprecated to use gets() function, Instead of using gets, you want to use fgets()
#include <stdio.h>
int main( ) {
char str[100];
printf( "Enter a value :");
gets( str );
printf( "\nYou 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 −
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( "\nYou 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 −
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().
MCQ on Input and Output
1. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int i = 10, j = 2;
printf("%d\n", printf("%d %d ", i, j));
}
a) Compile time error
b) 10 2 4
c) 10 2 2
d) 10 2 5
2. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int i = 10, j = 3;
printf("%d %d %d", i, j);
}
a) Compile time error
b) 10 3
c) 10 3 some garbage value
d) Undefined behaviour
3. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int i = 10, j = 3, k = 3;
printf("%d %d ", i, j, k);
}
a) Compile time error
b) 10 3 3
c) 10 3
d) 10 3 somegarbage value
4. What will be the output of the following C code?
#include <stdio.h>
int main()
{
char *s = "myworld";
int i = 9;
printf("%*s", i, s);
}
a) myworld
b) myworld(note: spaces to the left of myworld)
c) myworld (note:followed by two spaces after myworld)
d) Undefined
5. What will be the output of the following C code?
#include <stdio.h>
int main(int argc, char** argv)
{
char *s = "myworld";
int i = 3;
printf("%10.*s", i, s);
}
a) myw b) myworld(note:2 spaces before myworld) c) myworld (note:2 spaces after myworld) d) myw(note:6 spaces after myworld) r
6. Escape sequences are prefixed with ________
a) %
b) /
c) ”
d) None of the mentioned
7. What is the purpose of sprintf?
a) It prints the data into stdout
b) It writes the formatted data into a string
c) It writes the formatted data into a file
d) None of the mentioned
8. The syntax to print a % using printf statement can be done by ________ a) % b) \% c) ‘%’ d) %%
1. Which among the following is the odd one out?
a) printf
b) fprintf
c) putchar
d) scanf
2. For a typical program, the input is taken using _________
a) scanf
b) Files
c) Command-line
d) All of the mentioned
3. What does the following command line signify?
prog1|prog2
a) It runs prog1 first, prog2 second
b) It runs prog2 first, prog1 second
c) It runs both the programs, pipes output of prog1 to input of prog2
d) It runs both the programs, pipes output of prog2 to input of prog1
4. What is the default return-type of getchar()?
a) char
b) int
c) char *
d) reading character doesn’t require a return-type
5. What is the value of EOF?
a) -1
b) 0
c) 1
d) 10
6. What is the use of getchar()?
a) The next input character each time it is called
b) EOF when it encounters end of file
c) The next input character each time it is called EOF when it encounters end of file
d) None of the mentioned
7. Which of the following statement is true?
a) The symbolic constant EOF is defined in <stdio.h>
b) The value is -1
c) The symbolic constant EOF is defined in <stdio.h> & value is -1
d) Only value is -1
8. What is the return value of putchar()? a) The character written b) EOF if an error occurs c) Nothing d) Both character written & EOF if an error occurs
1. Which is true about function tolower?
a) The function tolower is defined in <ctype.h>
b) Converts an uppercase letter to lowercase
c) Returns other characters untouched
d) None of the mentioned
2. What will be the output of the following C code?
#include <stdio.h>
int main()
{
char c = '�';
putchar(c);
}
a) Compile time error
b) Nothing
c) 0
d) Undefined behaviour
3. putchar(c) function/macro always outputs character c to the __________
a) screen
b) standard output
c) depends on the compiler
d) depends on the standard
4. What will be the output of the following C code if following commands are used to run (considering myfile exists)?
gcc -otest test.c
./test < myfile
#include <stdio.h>
int main()
{
char c = 'd';
putchar(c);
}
a) Compile time error (after first command)
b) d in the myfile file
c) d on the screen
d) Undefined behaviour
5. What will be the output of the following C code if following commands are used to run (considering myfile exists)?
gcc -otest test.c
./test > myfile
#include <stdio.h>
int main(int argc, char **argv)
{
char c = 'd';
putchar(c);
printf(" %d\n", argc);
}
a) d 2 in myfile b) d 1 in myfile c) d in myfile and 1 in screen d) d in myfile and 2 in screen r
6. What will be the output of the following C code if following commands are used to run and if myfile does not exist?
gcc -o test test.c
./test > myfile
#include <stdio.h>
int main(int argc, char **argv)
{
char c = 'd';
putchar(c);
printf(" %d\n", argc);
}
a) d 2 in myfile
b) d 1 in myfile
c) Depends on the system
d) Depends on the standard
7. The statement prog < infile causes _________ a) prog to read characters from infile b) prog to write characters to infile c) infile to read characters from prog instead d) nothing
1. What is the meaning of the following C statement?
printf(“%10s”, state);
a) 10 spaces before the string state is printed
b) Print empty spaces if the string state is less than 10 characters
c) Print the last 10 characters of the string
d) None of the mentioned
2. What are the Properties of the first argument of a printf() functions?
a) It is defined by a user
b) It keeps the record of the types of arguments that will follow
c) There may no be first argument
d) None of the mentioned
3. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int i = 10, j = 2;
printf("%d\n", printf("%d %d ", i, j));
}
a) Compile time error
b) 10 2 4
c) 10 2 2
d) 10 2 5
4. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int i = 10, j = 3;
printf("%d %d %d", i, j);
}
a) Compile time error
b) 10 3
c) 10 3 some garbage value
d) Undefined behaviour
5. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int i = 10, j = 3, k = 3;
printf("%d %d ", i, j, k);
}
a) Compile time error
b) 10 3 3
c) 10 3
d) 10 3 somegarbage value
6. What will be the output of the following C code?
#include <stdio.h>
int main()
{
char *s = "myworld";
int i = 9;
printf("%*s", i, s);
}
a) myworld
b) myworld(note: spaces to the left of myworld)
c) myworld (note:followed by two spaces after myworld)
d) Undefined
7. What will be the output of the following C code?
#include <stdio.h>
int main(int argc, char **argv)
{
char *s = "myworld";
int i = 3;
printf("%10.*s", i, s);
}
a) myw b) myworld(note:2 spaces before myworld) c) myworld (note:2 spaces after myworld) d) myw(note:6 spaces after myworld)
댓글