printf function-formats and prints a string
You can use the printf function to format and output the string. It can be used by including stdio.h header.
#include <stdio.h> int printf(const char * restrict format, ...);
This is a sample of the printf function. The first argument is a string that contains format specifiers such as "%s" which means a string and "%d" which means an integer. After the second argument, it is a variable length argument, and the value corresponding to the format specifier is passed.
#include <stdio.h> #include <stdint.h> int main (void) { const char * name = "Yuki Kimoto"; int32_t age = 40; printf("My name is%s. Age is%d. \ N", name, age); }
This is the output result.
My name is Yuki Kimoto. Age is 40.
Integer format
int32_t (signed 32-bit integer type) format
Use "%d" to format int32_t type (32bit signed integer type).
%d
"%D" is, to be exact, the format of int type.
Therefore, if the int type definition is a 16-bit implementation, the value of int32_t will not be output correctly in the format "%d". Hmm. However, assuming that all implementations in the world express int in 32bit or more, the code to format an int32_t type integer with "%d" is correct.
As a trial, let's pass the minimum value of int32_t type to "%d". How is it in your environment?
#include <stdio.h> #include <stdint.h> int main (void) { int32_t num = INT32_MIN; printf("%d\n", num); }
This is the output result.
-2147483648
We'll show you how to format a size-guaranteed integer type perfectly correctly later. It is more accurate to use this, but it has the drawback that it cannot be remembered in memory.
int16_t (signed 16bit integer type) format
Use "%d" to format int16_t (16bit signed integer type).
%d
int8_t (signed 8-bit integer type) format
Use "%d" to format int8_t (8bit signed integer type).
%d
int64_t (signed 64-bit integer type) format
To format int64_t (64bit signed integer type), use "PRId64" which will be explained later.
printf("%" PRId64 "\ n", num);
Since "%ld" and "%lld" may give a portability warning, it is safe to use PRId64 when outputting int64_t.
uint32_t (unsigned 32bit integer type) format
Use "%u" for the format of uint32_t (unsigned 32-bit integer type). The reason is the same as explained in the int32_t type format.
uint8_t (unsigned 8-bit integer type) format
Use "%u" for the format of uint8_t (unsigned 8-bit integer type).
uint16_t (unsigned 16bit integer type) format
Use "%u" for the format of uint16_t (unsigned 16bit integer type).
uint64_t (unsigned 64-bit integer type) format
For the format of uint64_t (unsigned 64-bit integer type), use "PRIu64" described later.
printf("%" PRIu64 "\ n", num);
Since "%lu" and "%llu" may give a portability warning, it is safe to use PRIu64 when outputting int64_t.
Integer format with guaranteed size
There is a way to correctly output an integer format with a guaranteed size. Let's format the value of int32_t type correctly. Use the PRId32 macro defined in the inttypes.h header.
#include <stdio.h> #include <stdint.h> #include <inttypes.h> int main (void) { int32_t num = INT32_MIN; printf("%" PRId32 "\ n", num); }
I feel like "What is this way of writing ...". In C, string literals are combined at compile time by arranging them like "" Foo "" Bar "". PRId32 is a macro. It will be replaced with the appropriate format, depending on the implementation definition. For example, it will be replaced with ""%"" d ""\n "".
Here's a list of the most commonly used integer formats with guaranteed sizes.
PRId8 | int8_t format |
---|---|
PRId16 | int16_t format |
PRId32 | int32_t format |
PRId64 | int64_t format |
PRIu8 | uint8_t format |
PRIu16 | uint16_t format |
PRIu32 | uint32_t format |
PRIu64 | uint64_t format |
Specifying the display width of an integer format
Let's specify the display width in integer format.
Right justify by specifying the display width
Specify "Display width" after "%" like "%5d". By default, integers are right-justified. The missing digits are filled with blanks.
# Specify the display width of an integer %2d
This is a sample that specifies the display width to 5 digits in the integer format of fprintf. It is right-justified by 5 digits.
#include <stdio.h> #include <stdint.h> int main (void) { int32_t num = 123; // Specify display width to 5 digits Right justified printf("foo,%5d, bar\n", num); }
This is the output result. The integer is output with 5 digits right justified.
foo, 123, bar
Specify the display width and left justify
To specify the display width and left justify, add a minus in front of the display width, such as "%-5d".
// Specify the display width and left justify %-5d
This is a sample that specifies the display width and displays it left-justified.
#include <stdio.h> #include <stdint.h> int main (void) { int32_t num = 123; // Specify the number of digits as 5 digits Left justified printf("foo,%-5d, bar\n", num); }
This is the output result.
foo,123, bar
Specify the display width and fill it with 0
To specify the display width and left justify, add 0 in front of the display width, such as "%05d".
// Specify the display width and left justify %05d
This is a sample that specifies the display width and displays it left-justified.
#include <stdio.h> #include <stdint.h> int main (void) { int32_t num = 123; // Specify the number of digits as 5 digits --- padded with 0 printf("foo,%05d, bar\n", num); }
This is the output result.
foo,00123, bar
Display integer type in hexadecimal
Use "%x" or "%X" to display integer types in hexadecimal. "%X" is output using "a to z", and "%X" is output using "A to Z".
#include <stdio.h> #include <stdint.h> int main (void) { int32_t num = 0x34AB; printf("%x\n", num); printf("%X\n", num); }
Floating point format
A description of the floating point format for the printf function.
Floating point format
The floating point format of the printf function is "%f". Both float type and double type are okay with "%f".
%f
This is a sample that outputs a floating point number with the printf function.
#include <stdio.h> #include <stdint.h> int main (void) { double num = 123.56; printf("%f\n", num); }
This is the output result.
123.560000
Specify the number of digits after the decimal point of the floating point number
To specify the number of digits after the decimal point of a floating point number, write as "%.2f".
This is a sample that outputs a floating point number with two decimal places by the printf function.
#include <stdio.h> #include <stdint.h> int main (void) { double num = 123.56; // Specify the number of digits after the decimal point as 2 digits printf("%.2f\n", num); }
This is the output result.
123.56
Display floating point numbers nicely
Use "%g" to get a nice floating point display. If there are many digits in exponential notation, it will be automatically converted to exponential notation.
#include <stdio.h> #include <stdint.h> int main (void) { double num1 = 123.56; double num2 = 2.56e40; // Display floating point nicely printf("%g\n", num1); printf("%g\n", num2); }
This is the output result.
123.56 2.56e + 40
Character format
Use "%c" to format the characters with the printf function.
%c
This is a sample to format characters with the printf function. Character literal The value described in "'a'" is char type. It is retained as an ASCII code. This is output as a character with "%c".
#include <stdio.h> int main (void) { char ch ='a'; // Output string printf("%c\n", ch); }
This is the output result.
a
String format
Use "%s" to format the string with the printf function.
%s
This is a sample to format a string with the printf function.
#include <stdio.h> int main (void) { const char * message = "Hello"; // Output string printf("%s\n", message); }
This is the output result.
Hello
Pointer format
Use "%p" to format the pointer with the printf function. You can get the value (address) of the pointer.
%p
This is a sample to format the pointer with the printf function.
#include <stdio.h> int main (void) { const char * message = "Hello"; // Output pointer printf("%p\n", message); }
This is the output result in my environment. The pointer value (address) is output.
0x4005c8