Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

C stdio printf() Function

❮ C stdio Library


Example

Output a string:

printf("Hello World!");
Try it Yourself »

Definition and Usage

The printf() function writes a formatted string to the console.

The printf() function is defined in the <stdio.h> header file.

Note: More accurately, it writes to the location specified by stdout which is usually the console but it may be configured to point to a file or other location.

Format specifiers

The format string can contain format specifiers which describe where and how to represent additional arguments that are passed into the function.

The format specifiers have the form %[flags][width][.precision][length]specifier. The components in [square brackets] are optional.

An explanation of each of the components:

  • flags - Optional. A sequence of any of the following characters:
    • - - Makes the output left-justified by adding any padding spaces to the right instead of to the left.
    • # - Shows an alternate representation of the formatted data depending on the conversion.
    • + - Causes positive numbers to always be prefixed with "+".
    • - (A space character) This prefixes a space to positive numbers, primarily so that the digits can be lined up with the digits of negative numbers.
    • 0 - Pads numbers with zeroes on the left.
  • width - Optional. A whole number specifying the minimum number of characters that the output should occupy. If necessary, spaces are added to the right to reach this number, or to the left if the - flag is used. If an *asterisk is used then the width is given by the argument preceding the one being represented.
  • .precision - Optional. A . followed by a whole number indicating how many decimal digits to show in the formatted data.
  • length - Optional. A sequence of characters which changes the expected data type of the argument. It can be one of the following:
    • hh - Expect char type for whole numbers.
    • h - Expect short int type for whole numbers.
    • l - Expect long int type for whole numbers.
             Expect wint_t type for characters.
             Expect wchar_t* type for strings.
    • ll - Expect long long int type for whole numbers.
    • j - Expect intmax_tor uintmax_t type for whole numbers.
    • z - Expect size_t type for whole numbers.
    • t - Expect ptrdiff_t type for whole numbers.
    • L - Expect long double type for floating point numbers.
  • specifier - Required. A character which indicates how an argument's data should be represented. The list of possible characters is shown in the table below.

List of specifiers

Character Specifier Description
d or i Decimal integer Represents a whole number as a decimal integer.
u Unsigned decimal integer Represents a whole number as an unsigned decimal integer.
o Octal integer Represents a whole number as an octal integer. The "#" flag will prefix the number with "0".
x or X Hexadecimal integer Represents a whole number as a hexadecimal integer. The "#" flag will prefix the number with "0x". If "X" is used then digits A to F and the letter X are shown in uppercase.
f or F Floating point number Represents a floating point number. If "F" is used then letters (from values like "nan") will be represented in uppercase. The "#" flag will force a decimal point even if there are no decimal digits.
e or E Scientific notation Represents a floating point number in scientific notation. If "E" is used then letters will be represented in uppercase. The "#" flag will force a decimal point even if there are no decimal digits.
g or G General number Uses the shortest representation between f and e for a floating point number. If "G" is used then it chooses between F and E instead.
a or A Hexadecimal floating point number Displays a floating point number's internal representation with hexadecimal digits. If "A" is used then the digits are represented in uppercase.
c Character Represents a character. If the argument is an integer then it represents the character for the ASCII value specified by the integer.
s String Represents a string.
p Pointer Represents the memory address of a pointer, usually with hexadecimal digits.
n No output The number of characters that have been printed up to this point is written into the argument. The argument must be a pointer to an integer.
% Percent symbol Represents a literal "%" character.

Syntax

printf(const char * format, arg1, arg2...);

Parameter Values

Parameter Description
format Required. A string representing the format of the data to be written to the file.
arg1, arg2... Optional. Any number of additional arguments, their values can be formatted and printed to the console using the specifiers in the format argument.

Technical Details

Returns: An int value representing the number of characters that were printed. If an error occurred then it returns a negative number.

More Examples

Example

Represent integers of various types:

char var1 = 102;
short int var2 = 2024;
int var3 = 95;
long int var4 = 212;
long long int var5 = 1200L;
printf("%hhd %hd %d %ld %lld", var1, var2, var3, var4, var5);
Try it Yourself »

Example

Represent an integer in a variety of ways:

int value = 1024;
printf( "%d \n", value); // Integer
printf( "%o \n", value); // Octal
printf( "%x \n", value); // Hexadecimal
printf("%#o \n", value); // Octal with "0" prefix
printf("%#X \n", value); // Uppercase hexadecimal with "0X" prefix
Try it Yourself »

Example

Represent a floating point number in a variety of ways:

float myFloat = 19.99;
printf("%f \n", myFloat); // Floating point
printf("%e \n", myFloat); // Scientific
printf("%g \n", myFloat); // Optimal
printf("%.2f \n", myFloat); // 2 decimal digits
printf("%8.2f \n", myFloat); // 8 characters wide and 2 decimal digits
printf("%08.2f \n", myFloat); // zero-padded 8 characters wide and 2 decimal digits
Try it Yourself »

Example

Use the %n specifier to measure the length of a string:

// Reserve 20 bytes of memory for a string
char myString[20] = "Hello World!\0";
int memorySize = sizeof(myString);
int stringSize;

// Print the string and use %n to measure it
printf("%s%n\n", myString, &stringSize);

// Print the size of the string
printf("Memory size: %d\n", memorySize);
printf("String size: %d\n", stringSize);
Try it Yourself »

❮ C stdio Library

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.