printf format tag - KimTaebin-ai/study_posts GitHub Wiki

flag ํ•„๋“œ์˜ -, 0, ., *

width ๋ฐ precision ํ•„๋“œ,

type ํ•„๋“œ์˜ c, s, p, d, i, u, x, X, %

Bonus: %[parameter][flags][width][.precision][length]type

[parameter]: ์ธ์ž์˜ ์ˆœ์„œ ์ง€์ •

printf("%2$d %1$d", 10, 20); //์ถœ๋ ฅ: 20 10

[flags]: ์ถœ๋ ฅ ํ˜•์‹ ์กฐ์ •

  • '-': ์™ผ์ชฝ ์ •๋ ฌ
  • '+': ์–‘์ˆ˜์—๋„ ๋ถ€ํ˜ธ ํ‘œ์‹œ
  • ' ': ์–‘์ˆ˜ ์•ž์— ๊ณต๋ฐฑ ์ถ”๊ฐ€
  • '0': ๋‚จ๋Š” ๊ณต๊ฐ„์„ 0์œผ๋กœ ์ฑ„์›€
printf("%+d", 42);    // ์ถœ๋ ฅ: +42
printf("%-10s", "Hello"); // ์ถœ๋ ฅ: "Hello     "

[width]: ์ถœ๋ ฅํ•  ์ตœ์†Œ ๋„ˆ๋น„ ์ง€์ •

printf("%5d", 42);    // ์ถœ๋ ฅ: "   42"

[.precision]: ์†Œ์ˆ˜์  ์ดํ•˜ ์ž๋ฆฟ์ˆ˜๋‚˜ ๋ฌธ์ž์—ด ๊ธธ์ด ์ง€์ •

printf("%.2f", 3.14159);  // ์ถœ๋ ฅ: 3.14
printf("%.3s", "Hello");  // ์ถœ๋ ฅ: Hel

[length]: ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •

  • h: short int
  • l: long int
  • ll: long long int
printf("%ld", 1234567890L);

type: ๋ฐ์ดํ„ฐ ํƒ€์ž… ์ง€์ •

  • d, i: ์ •์ˆ˜
  • f: ๋ถ€๋™์†Œ์ˆ˜์ 
  • s: ๋ฌธ์ž์—ด
  • c: ๋ฌธ์ž
  • x, X: 16์ง„์ˆ˜
  • o: 8์ง„์ˆ˜
  • p: ํฌ์ธํ„ฐ ์ฃผ์†Œ
#include <stdio.h>

int main() {
    int num = 42;
    float pi = 3.14159;
    char* str = "Hello";

    printf("%2$+10.2f %1$-5d %3$*.3s\n", num, pi, str, 6);
    //    +3.14    42  Hel
    return 0;
}
โš ๏ธ **GitHub.com Fallback** โš ๏ธ