C Language printf format
printf function summary
Format :%i, %d, %c ,%f, %s,%ll , %ull, %x etc.
description-------------------------------------------------------------------------------------------
\n (newline)
\t (tab)
\v (vertical tab)
\f (new page)
\b (backspace)
\r (carriage return)
\n (newline)
%d (print as a decimal integer)
%3d (print as a decimal integer with a width of at least 3 wide)
%f (print as a floating point)
%5f (print as a floating point with a width of at least 5 wide)
%.4f (print as a floating point with a precision of four characters after the decimal point)
%6.2f (print as a floating point at least 6 wide and a precision of 2)
example-------------------------------------------------------------------------------------------
#include<stdio.h>
int main()
{
int a,b;
float c,d;
a = 17;
b = a / 2;
printf("%d\n",b);
printf("%3d\n",b);
printf("%03d\n",b);
c = 18.3;
d = c / 3;
printf("%3.2f\n",d);
}
result-------------------------------------------------------------------------------------------
8
8
007
6.10
example-------------------------------------------------------------------------------------------
#include<stdio.h>
int main()
{
printf("1.%s:\n", "Hello, abcde!");
printf("2.%15s:\n", "Hello, abcde!");
printf("3.%.10s:\n", "Hello, abcde!");
printf("4.%-10s:\n", "Hello, abcde!");
printf("5.%-15s:\n", "Hello, abcde!");
printf("6.%.15s:\n", "Hello, abcde!");
printf("7.%15.10s:\n", "Hello, abcde!");
printf("8.%-15.10s:\n", "Hello, abcde!");
}
result:-------------------------------------------------------------------------------------------
1.Hello, abcde!:
2. Hello, abcde!:
3.Hello, abc:
4.Hello, abcde!:
5.Hello, abcde! :
6.Hello, abcde!:
7. Hello, abc:
8.Hello, abc
description------------------------------------------------------------------------------------
1. statement prints the string (nothing special happens.)
2.statement prints the string, but print 15 characters. If the string is smaller the “empty” positions will be filled with “Space.”
3. statement prints the string, but print only 10 characters of the string.
4. statement prints the string, but prints at least 10 characters. If the string is smaller “space” is added at the end. (See next example.)
5. statement prints the string, but prints at least 15 characters. The string in this case is shorter than the defined 15 character, thus “space” is added at the end (defined by the minus sign.)
6. statement prints the string, but print only 15 characters of the string. In this case the string is shorter than 15, thus the whole string is printed.
7. statement prints the string, but print 15 characters.
If the string is smaller the “empty” positions will be filled with “space.” But it will only print a maximum of 10 characters, thus only part of new string (old string plus the space positions) is printed.
8. statement prints the string, but it does the exact same thing as the previous statement, accept the “space” is added at the end.
Format :%i, %d, %c ,%f, %s,%ll , %ull, %x etc.
unsigned int -> %u
d - decimal number
o - octal number
x -hexadecimal number
short -> %hd(decimal number)
short -> %ho(octal number)
short -> %hx(hexadecimal number)
long -> %ld(decimal number)
long -> %lo(octal number)
long -> %lx(hexadecimal number)
unsigned long -> %lu
long long -> %lld
unsigned long long -> %llu
\n (newline)
\t (tab)
\v (vertical tab)
\f (new page)
\b (backspace)
\r (carriage return)
\n (newline)
%d (print as a decimal integer)
%3d (print as a decimal integer with a width of at least 3 wide)
%f (print as a floating point)
%5f (print as a floating point with a width of at least 5 wide)
%.4f (print as a floating point with a precision of four characters after the decimal point)
%6.2f (print as a floating point at least 6 wide and a precision of 2)
example-------------------------------------------------------------------------------------------
#include<stdio.h>
int main()
{
int a,b;
float c,d;
a = 17;
b = a / 2;
printf("%d\n",b);
printf("%3d\n",b);
printf("%03d\n",b);
c = 18.3;
d = c / 3;
printf("%3.2f\n",d);
}
result-------------------------------------------------------------------------------------------
8
8
007
6.10
example-------------------------------------------------------------------------------------------
#include<stdio.h>
int main()
{
printf("1.%s:\n", "Hello, abcde!");
printf("2.%15s:\n", "Hello, abcde!");
printf("3.%.10s:\n", "Hello, abcde!");
printf("4.%-10s:\n", "Hello, abcde!");
printf("5.%-15s:\n", "Hello, abcde!");
printf("6.%.15s:\n", "Hello, abcde!");
printf("7.%15.10s:\n", "Hello, abcde!");
printf("8.%-15.10s:\n", "Hello, abcde!");
}
result:-------------------------------------------------------------------------------------------
1.Hello, abcde!:
2. Hello, abcde!:
3.Hello, abc:
4.Hello, abcde!:
5.Hello, abcde! :
6.Hello, abcde!:
7. Hello, abc:
8.Hello, abc
description------------------------------------------------------------------------------------
1. statement prints the string (nothing special happens.)
2.statement prints the string, but print 15 characters. If the string is smaller the “empty” positions will be filled with “Space.”
3. statement prints the string, but print only 10 characters of the string.
4. statement prints the string, but prints at least 10 characters. If the string is smaller “space” is added at the end. (See next example.)
5. statement prints the string, but prints at least 15 characters. The string in this case is shorter than the defined 15 character, thus “space” is added at the end (defined by the minus sign.)
6. statement prints the string, but print only 15 characters of the string. In this case the string is shorter than 15, thus the whole string is printed.
7. statement prints the string, but print 15 characters.
If the string is smaller the “empty” positions will be filled with “space.” But it will only print a maximum of 10 characters, thus only part of new string (old string plus the space positions) is printed.
8. statement prints the string, but it does the exact same thing as the previous statement, accept the “space” is added at the end.
Subscribe to:
Comments (Atom)
알뜰폰 삼성페이 교통카드 '한도 초과' 오류(등록 불가 오류) 해결방법
sk7mobile 알뜰폰 삼성페이 교통카드 등록 시, 한도 초과 문제 해결방법 skt usim 해킹 사건으로 인해 sk7mobile 알뜰폰을 사용하는 저도 usim을 바꾸고 나니 삼성페이 교통카드가 등록이 안되더라구요... 삼성페이 교통카드 기능 은...
-
Choosing Pet Insurance in New York (2024): Benefits and Cost Comparison 🐾 Owning a pet in New York City comes with countless joys, but al...
-
How to Start a Career in Renewable Energy: Best Jobs, Skills & Training for a Green Future 🌱⚡ Why the Renewable Energy Industry Is th...
-
ERROR : Please ensure that your setting of VIRTUAL-RUNTIME_init_manager (systemd) matches the entries enabled in DISTRO_FEATURES kiwon...