-
Notifications
You must be signed in to change notification settings - Fork 0
/
13In_Out.txt
48 lines (35 loc) · 2.61 KB
/
13In_Out.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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 File File Pointer Device
Standard input stdin Keyboard
Standard output stdout Screen
Standard error stderr Your 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.
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.
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().