Formated OutPut in C/C++

Printf Function In C/C++ Language

Before we use the printf and scanf function we must be know that :
=>C programming has several built functions to perform input and output tasks. Two commonly used functions for Input and Output are: printf() and scanf()
The printf() function sends formatted output to the standard output (screen).

Now we take an example
Example 1:


#include <stdio.h>      
int main()
{
    printf("Hello C Programming");  
    return 0;
}

Explanation of Above program
#include
The printf() function is declared in "stdio.h" header file and #include is a preprocessor directive to paste the code from the header file when necessary.
When the compiler encounters printf() function and doesn't find stdio.h header file, compiler shows error.

Note : if you are using turbo c editor the don't use this include statement but if you are using any c++ editor the its must be declare before the writing the main function.
printf("Hello C Programming");
The printf() is a library function to send formatted output to the screen. where " Hello c Programming " is our text (string constant ) which we want to display.
return 0;
This statement is the "Exit status" of the program. In simple terms, program ends.

Example 2: Integer Output


#include <stdio.h>
int main()
{
int marks = 500;
printf("Your Marks are = %d "    ,   marks);
return 0;
}

Explanation of Above program
In this program printf function passing two argument. in the inverted commas "Your Marks=" is the text (string constant ) print same on the screen. and 2nd one is marks (variable).
 %d is format specifiers which tell to complier that the variable marks is integer variable and print at that place where the %d is place.
its means the above program display the result on the screen as
Your Marks are =500
Following are the some Format specifiers.

%d

Integer

%f

Float

%e

Exponent

%g

General

%c

Character

%s

Siring

%u

Unsigned

%1

Long

%o

Octal

%x

Hexa

we cal also use [Escape Sequences] Back slash Character Constants For example \n which control the cursor position . if we want to display the result on next line then we use \n before displaying the result.
printf(" Hello") ; printf("\n C leraninghints.com");
The result of above tow statement is :

Hello C leraninghints.com

but if we use :

printf(" Hello") ;
printf("C leraninghints.com");

The result of above tow statement is:

Hello C leraninghints.com