C Language provides some ready to use builtin functions in the C-library for input and output. Input is taken from input devices (files, keyboard etc.), and output is sent to an output device (Monitor, files, printer etc.)
Standard input (stdin), Standard output (stdout), and Standard error (stderr) are called standard streams and they are defined when program starts executing. Streams are sequence of bytes (You can imagine a file, which is a sequence of characters). The Input/Output functions access these streams to read and display data. Let us look at a small example
Standard input (stdin), Standard output (stdout), and Standard error (stderr) are called standard streams and they are defined when program starts executing. Streams are sequence of bytes (You can imagine a file, which is a sequence of characters). The Input/Output functions access these streams to read and display data. Let us look at a small example
Printf()
#include <stdio.h> int main() { int x, y; float a, b; x = 5; y = x / 2; printf ("%d\n", y); printf ("%3d\n", y); printf ("%03d\n", y); a = 22.3; b = a / 4; printf ("%3.3f\n", b); return 0; }
The output of the following program is as follows:
2 2 002 5.575
We've declared 2 integer variables, and 2 floating point variables. Integer 'x' is assigned value 5, and float 'a' is assigned a value 22.3. A division operator '/' divides 5 by 2 and stores the result in 'y', and similarly, 22.3 is divided by 4 and the result is stored in 'b'
Examine the following:
printf ("%d\n", y); printf ("%3d\n", y); printf ("%03d\n", y);
Here, %d in printf function denotes that, we intend to print an 'integer'. Variable y is sent as an argument to the function printf, saying that you want to print the value of variable in %d(integer) format. %d is called a format specifier, and C offers few more format specifiers to display numbers of different data types in various formats.
Few of them are:
- %i or %d for int
- %f for float
- %d for double
- %c for char (character)
- %s for sequence of characters or a string
- %o for an octal value
- %0x for a hexadecimal value (useful to print addresses)
- %l for a long value
- %ld for a long double value
'\n' stands for newline. It is called an escape sequence. It tells the compiler to print a newline character in the output stream.
Other Escape sequences include:
- \a (raise an audible alert)
- \t (print a tab)
- \v (print a vertical tab)
- \f (new page)
- \b (backspace)
- \r (carriage return)
%3d tells the compiler to print an integer value atleast 3 digits wide, so if you provide a value of 2, it is right justified to fit atleast 3 digits. %03d tells the compiler to fill any spaces with 0. You can understand what's happening by looking at the output above.
But there's something wrong with the output. 5/2 gives 2.5, then why does the output show 2? The result is of type float (2.5), and as we are storing it in an integer, the fractional part .5 is simply discarded or truncated. So, only the integral part 2 is displayed.
printf ("%3.3f\n", b);
%3.3f indicates a floating point value, atleast 3 digits wide, and displaying exactly 3 digits after the decimal
The output 5.575 is 5 characters wide and not 4, because the decimal point (.) is also counted as a character during printing, as expected.
You can experiment further with different format specifiers and escape sequences to understand each of their functionality.
Scanf()
Let us look at an example the uses scanf()
#include<stdio.h> int main() { int x; printf ("Enter a value: \n"); scanf ("%d", &x); printf ("You entered %d", x); return 0; }
Output:
Enter a value: 6 You entered 6
When you execute this program, the program waits for an input. When an integer is supplied as input, it is stored in the variable 'x'. Note the ampersand(&) before x. It denotes that the value from the input should be stored in the address of variable 'x'. Try printing '&x' in hexadecimal format(%0x) and you get the address of variable x. So, '&' should be used with the arguments in scanf(), when you are scanning a value from input stream and storing it in an address for future reference.
All the format specifiers and Escape sequences are available for use in scanf() function
Usage of other format specifiers
Here is a program that uses few of other format specifiers:#include<stdio.h> int main() { printf ("Welcome to %s\n", "doitinc"); printf ("Hexadecimal: %x\n", 255); printf ("This is a number: %05i\n", 305); printf ("Unsigned value: %u\n", 200); printf ("Floating point number: %3.2f\n", 3.14159); printf ("Octal: %o\n", 255); printf ("This is double quote: \" This is percent sign %% \n", 10); return 0; }
Output:
Welcome to doitinc Hexadecimal: ff This is a number: 00305 Unsigned value: 200 Floating point number: 3.14 Octal: 377 This is double quote: " This is percent sign %
And another lesson ends here. Few more I/O functions will be discussed shortly.
A point to note, Do not just read this tutorial, make sure you are coding besides reading this tutorial This way, you can learn the content faster and remember it longer.
Sign up here with your email
ConversionConversion EmoticonEmoticon