Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C printf()

Formatted Output and the printf function

One of the common task in every program is the printing of output. We use the output to request input from a user and later display the status/result, computations etc. In C programming there are several functions for printing formatted output. Here we discuss the printf() function, which writes output to the computer monitor. To use the printf() function we must include the stdio library in the source code. To do this just place the following code at the beginning of your program.

#include <stdio.h>

To print a simple message in computer screen you might call printf() function as follows:

#include <stdio.h>
main()
{
printf ("You are learning printf() function");
}

Output:

You are learning printf() function

In the above examples, the cursor will remain at the end of the printed output. If you repeat the above code in the following way the second message would appear immediately after the first one.

#include <stdio.h>
main()
{
printf("You are learning printf() function");
printf("You are learning printf() function");
}

Output:

You are learning printf() function You are learning printf() function

If we want to print the second output in a new line we must need a different way, which has discussed in the following section.

Escape sequences in C

An escape sequence is a series of characters that represents a special character. It begins with a backslash character (\), which indicates that the character(s) that follow the backslash character should be treated in a special way. C uses escape sequences within a format string to print certain special characters. For example \n moves the output position to the beginning of the next line. The following is a list of escape sequences.

Escape sequence Action
\n prints a new line
\b backs up one character
\t moves the output position to the next tab stop
\\ prints a backslash
\" prints a double quote
\' prints a single quote

You will get an idea of using the above escape sequences from the following example.

#include<stdio.h>
main()
{
printf("Create a new line\n");
printf("Print a double quotes (\") within a string\n");
printf("Print a single quotes (\') within a string\n");
printf("Print a Backslash\\ within a string\n");
printf("Using Backspace\b within a string\n");
printf("Using\tTab within a string\n");
}

Output:

Create a new line
  Print a double quotes (") within a string
  Print a single quotes (') within a string
  Print a Backslash\ within a string
  Using Backspace within a string
  Using     Tab within a string

Using printf() to print values

In the above section, we have discussed how to print a new line, single quote, double quote etc. In this section, we will discuss how to print the value of a variable. There are various format available to print different type values. Let start with these codes :

int x;
x = 150;
printf("Number of students in Class V is %d",x);

In the printf() function the format parameter '%d' is replaced with the value of the parameter x. Therefore the output looks like : Number of students in Class V is 150. In addition to %d, there are quite a number of format specifiers, each having a different meaning. Here are the basic ones (several others exist) :

Parameter Meaning
%d Print an integer number printed in decimal (preceded by a minus sign if the number is negative).
%f Print a floating point number ( in the form dddd.dddddd).
%E Print a floating point number ( in scientific notation: d.dddEddd).
%g Print a floating point number (either as f or E, depending on value and precision).
%x Print an integer number in hexadecimal with lower case letters.
%X Print an integer number printed in hexadecimal with upper case letters.
%c Print a character.
%s Print a string.

The following code prints two numbers one is positive another is negative.

#include<stdio.h>
main()
{
int x,y;
x = 5;
y= -5;
printf("The value of x is %d and value of y is %d",x,y);
}

Output:

The value of x is 5 and value of y is -5

You can do some calculation within printf. See the following example :

#include<stdio.h>
main()
{
int x;
x = 5;
printf ("%d + %d = %d\n",x,y,x+y); 
printf ("%d - %d = %d\n",x,y,x-y); 
printf ("%d x %d = %d\n",x,y,x*y);  
printf ("%d / %d = %d\n",x,y,x/y);  
}

Output:

5 + 5 = 10
  
  5 - 5 = 0
  
  5 x 5 = 25
  
  5 / 5 = 1

The following code uses the other parameters of printf mentioned in the parameter list :

#include<stdio.h>
main()
{
char a;
float x,y;
a = 'G';
x = 5.23;
y = 76000000.00;
printf("%c %f %g %E %s\n", a, x, y, y,"String"); 
printf("Hexadecimal(lower case) of 12 is %x\n",12);
printf("Hexadecimal(upper case) of 12 is %X\n",12);
}

Output:

G 5.230000 7.6e+007 7.600000E+007 String
  Hexadecimal(lower case) of 12 is c
  Hexadecimal(upper case) of 12 is C

Using format specifier options in printf

You can specify the width and precision of numbers and strings as they are inserted. See the syntax :

%[flags][width][.precision]Parameter

For example, %4c means the entire field (three blanks and one non-blank character) occupies 4 columns, %7.2f means to print a float or double in a field at least 7 spaces wide, with two places to the right of the decimal.

Previous: C Operators
Next: C if else



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP