printf() function to display output

printf() is a pre defined or built in function in C to send formatted outputs. We will first try to send a simple message by using printf function like this.
printf("welcome to plus2net");
The above command will print the message welcome to plus2net

Adding a new line or a like break

To above message we will add one like break by adding \n at the end of it. For a new line we have to use \n ( no space between \ and n ). We will print two lines like this
printf("welcome to plus2net \n C tutorial section");
The output of the above command will be
Welcome to plus2net 
C tutorial section

Formatted output using printf

WE can display decimal integers by using %d
We will declare one integer variable first and then we will store a value in it. Then we will display the value using printf command.
Note: The statements written after // are comments for easy understanding , these words will not be executed by C compiler
int main(void){
int num;  // num is declared as integer variable
num=15; // data is stored 
	printf("num = %d ", num);  // final output 
  	return 0;
}
Here we have formatted the display by saying %d , that is we are sending an integer output. The above statement will display
Num = 15
To display a decimal value we will declare a float variable first and then format it to display. Like this ( changes in above code are only shown )
float num; // num is declared as float variable
num=17.873456;     // data is stored
printf("num = %.3f", num); // final output
The output is
num = 17.873
To show 8 places after decimal
float num; // num is declared as float variable
num=17.873456789;     // data is stored
printf("num = %.4f", num); // final output
Output is here
17.8735

Output of a single char

#include<stdio.h>
#include<string.h>

int main()
{
 char str[30]="plus2net";
   printf("%c\n", str[3]);// Output is s
   printf("%c\n",str[4]); // Output is 2
   return 0;
}
We used \n in above code to add line break. The char at position 3 and 4 ( first char is position 0 ) is returned.

Printing in reverse order without using strrev()

#include<stdio.h>
#include<string.h>

int main()
{
 char str[30]="plus2net";
 int str_length;
 str_length=strlen(str); // length of the string
 for(int i=str_length; i>=0; i--){
 printf("%c",str[i]);   // printing the char at position  i
 }
return 0;
}

Displaying string

char var[20] = "Plus2net";
printf(" Welcome to  %s ",var);
Output is here
Welcome to Plus2net

C language is case sensitive, lower and upper case letters are to be treated differently. Printf is not same as printf.

1.Write a program to display sum of two integers

2.Write a program to display a patern like this.
*
**
***
****

Visitors Rating
Your Rating




Pratibha Suralkar

25-09-2016

what is the output of following :

printf("%d",printf("welcome"));

Post your comments , suggestion , error , requirements etc here




HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer