What is output?
Output is an answer for our process. For example , we are adding 2 with 3 and it gives an answer 5. Here we need to tell the user the answer. How? just show it in the monitor. But user won't like to see every process's output. That will make them frustrated. when programming a programmer will decide what to show on screen. That's output of a application. In every language we have some in built functions to display outputs. Those functions will help us to display our outputs.
Printf method in c
The most common method in c language to print something on screen is
printf("string to print");
I'll explain it. printf is a function which will display what we give in first parameter. Now let's try somethingprintf("Hey guys this is my first output");
what if you wanna print something from variable? You can also print from a variable like this.
int a = 2; printf("%d", a);
It will print "2" on screen. In the above example we have printed the 2 from variable a. If you wanna print from another variable.int a = 3, b = 5; printf(" %d , %d ", a, b);
Will print " 3 , 8 ". parameter after the string (from second parameter) will be used to fill the characters which starts with "%". What are they?. "Format specifiers". "%d" means integer. we have declared the variable "a" as integer above. but what if we wanna print it as hexadecimal. how you could do this? it will be done in a simple way by just specifying it's format in "printf" function. "%x" is format specifier for hexadecimal. binary value of the variable will be converted to hexadecimal. Let's see what it will print.int a = 8; printf(" %x ", a);
Output: 0x1000 It is trying to tell us that It is 8 but in hex form by prefixing 0x before the actual value
format specifier is used to convert the binary to specified format. A variable's binary value will be converted to given format.
There is also some other specifiers to use. Let's see them