Input and output functions in c pdf
File Name: input and output functions in c .zip
- C programming Input Output functions (I/O): printf() and scanf()
- C Programming/Simple input and output
- C Input and Output
Input, process and output processed data is the main goal of every computer program. A program without data is a useless program. C provides a header file stdio.
Actually, every operating system has its own functions for reading and displaying input-output to and from devices. Hence, the developers of C compilers write programs that would link the C compiler to those input-output function of the operating system. The major difference is that formatted function allows us to format the input from the keyboard and the output to be displayed on the screen. Format control string in printf function describes the output format which consists of conversion specifiers, precisions, flags, field widths and literal characters.
C programming Input Output functions (I/O): printf() and scanf()
In other words, there is no keyword like read or write. Instead, it left the IO to the compiler as external library functions such as printf and scanf in stdio library.
In input operations, data bytes flow from an input source such as keyboard, file, network or another program into the program. In output operations, data bytes flow from the program to an output sink such as console, file, network or another program. Streams acts as an intermediaries between the programs and the actual IO devices, in such the way that frees the programmers from handling the actual devices, so as to archive device independent IO operations.
In formatted or high-level IO, bytes are grouped and converted to types such as int , double , string or user-defined types. In unformatted or low-level IO, bytes are treated as raw bytes and unconverted.
Most of the template classes take two type parameters. For example,. You can also overload these operators for your own user-defined types. It contains two set of output functions: formatted output and unformatted output. It converts a numeric value from its internal representation to the text form. It also supports formatted input and unformatted input. It performs formatting by converting the input texts into the internal representation of the respective types.
The ostream 's member function put can be used to put out a char. The flags are:. However, it is more convenience to use these public member functions of ios class:.
They are:. The default field-width is 0, i. The setw operation is non-sticky. That is, it is applicable only to the next IO operation, and reset back to 0 after the operation. The field-width property is applicable to both output and input operations. Except setw , all the other IO manipulators are sticky , i. The internal alignment left-align the sign, but right-align the number, as illustrated.
You can also use ostream 's member function width e. Floating point number can be display in 3 formatting modes: default fixed scientific. The precision is interpreted differently in default and non-default modes due to legacy. You can also use ostream 's member function precision n e. File IO requires an additional step to connect the file to the stream i. By default, opening an output file creates a new file if the filename does not exist; or truncates it clear its content and starts writing as an empty file.
The available file mode flags are:. You can set multiple flags via bit-or operator, e. For output, the default is ios::out ios::trunc. For input, the default is ios::in. Random access file is associated with a file pointer, which can be moved directly to any location in the file. Random access is crucial in certain applications such as databases and indexes. You can position the input pointer via seekg and output pointer via seekp. Each of them has two versions: absolute and relative positioning.
The string streams is based on ostringstream subclass of ostream , istringstream subclass of istream and bi-directional stringstream subclass of iostream. IO operations are defined for each of the type.
If IO operations are not defined for a particular type, compiler will generate an error. The same set of operations can be applied to different types of IO devices.
Connect Associate the stream object to an actual IO device e. Some functions convert the data between the external format and internal format formatted IO ; while other does not unformatted or binary IO.
Disconnect Dissociate the stream to the actual IO device e. Free the stream object. Take note that istream and ostream are unidirectional streams; whereas iostream is bidirectional. The unformatted output functions e. It uses the terminating null character to decide the end of the char array. In unformatting input, such as get , getlin , read , it reads the characters as they are, without conversion. It uses whitespace as delimiter and adds a terminating null character to the C-string.
The flags are: eofbit : set when an input operation reaches end-of-file. However, it is more convenience to use these public member functions of ios class: good : returns true if goodbit is set i. By default, the values are displayed with a field-width just enough to hold the text, without additional leading or trailing spaces.
You need to provide spaces between the values, if desired. For integers, all digits will be displayed, by default. This default precision of 6 digits include all digits before and after the decimal point, but exclude the leading zeros.
Scientific notation E-notation will be used if the exponent is 6 or more or -5 or less. In default mode neither fixed nor scientific used , a floating-point number is displayed in fixed-point notation e.
The precision in default mode includes digits before and after the decimal point but exclude the leading zeros. Fewer digits might be shown as the trailing zeros are not displayed. The default precision is 6. See the earlier examples for default mode with default precision of 6. As mentioned, the trailing zeros are not displayed in default mode, you can use manipulator showpoint noshowpoint to show or hide the trailing zeros. In both fixed e. The default precision is also 6. Negative hex and oct are displayed in 2's complement format.
It does not convert characters or strings to uppercase! These manipulators are sticky. However, they are not as user-friendly as using manipulators as discussed above. File Output The steps are: Construct an ostream object. Connect it to a file i. Disconnect close the file which flushes the output buffer and free the ostream object. The available file mode flags are: ios:: in - open file for input operation ios:: out - open file for output operation ios:: app - output appends at the end of the file.
File Input The steps are: Construct an istream object. Disconnect close the file and free the istream object.
C Programming/Simple input and output
They are classified into two broad categories. Keyboard and screen together called console. This is the behind the name of these functions. They provide the flexibility to receive the input in some fixed format and to give the output in desired format. As I already explained them in one previous article so I will not discuss them here.
In C Language input and output function are available as C compiler function or C library provided with each C compiler implementation. These are as:. Input and Output statement are used to read and write the data in C programming. These are embedded in stdio. These are discussed as:.
It is this standardized version that is covered in the rest of the article. Most of the classes in the library are actually very generalized class templates. Each template can operate on various character types, and even the operations themselves, such as how two characters are compared for equality, can be customized. However, the majority of code needs to do input and output operations using only one or two character types, thus most of the time the functionality is accessed through several typedefs , which specify names for commonly used combinations of template and character type. The classes in the library could be divided into roughly two categories: abstractions and implementations. Classes, that fall into abstractions category, provide an interface which is sufficient for working with any type of a stream.
Therefore, for effecting input/output functions, C program must include the statement. #include. This statement will tell the compiler to search for this.
C Input and Output
Each program requires at least one header file to work. Before we begin our discussion, it is important to understand, what are preprocessor directives? Before the source code is compiled, it gets automatically processed due to the presence of preprocessor directives. Stay updated with latest technology trends Join DataFlair on Telegram!!
Machines process things. We feed stuff into a machine and get different stuff out. A saw turns trees into planks.