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.
*
**
***
****


Example 1: Printing Integer and Float Values

This example demonstrates how to print integer and float values using printf() with format specifiers.

#include <stdio.h>

int main() {
    int num = 25;
    float pi = 3.14159;
    
    printf("Integer: %d\n", num);
    printf("Float: %.2f\n", pi);  // 2 decimal places
    return 0;
}
Output:
Integer: 25
Float: 3.14
---

Example 2: Printing Characters and Strings

This example shows how to use printf() to print characters and strings.

#include <stdio.h>

int main() {
    char ch = 'A';
    char name[] = "plus2net";
    
    printf("Character: %c\n", ch);
    printf("String: %s\n", name);
    return 0;
}
Output:
Character: A
String: plus2net
---

Example 3: Formatting Width and Precision

This example demonstrates how to format output with specific width and precision.

#include <stdio.h>

int main() {
    float num = 123.456;
    
    printf("Fixed width (10 characters): %10.2f\n", num);  // Total width 10, 2 decimals
    return 0;
}
Output:
Fixed width (10 characters):     123.46
---

Example 4: Printing Special Characters

This example shows how to print special characters like newlines and tabs using printf().

#include <stdio.h>

int main() {
    printf("This is line 1.\nThis is line 2.\n");
    printf("Column1\tColumn2\tColumn3\n");
    return 0;
}
Output:
This is line 1.
This is line 2.
Column1    Column2    Column3


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Pratibha Suralkar

    25-09-2016

    what is the output of following :

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

    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer