[c언어] 출력할 데이터의 자릿수 맞추기

|



형식 지정자에 대해 전체 자릿수 또는 소수 이하의 자릿수를 지정하는 방법 외에 기호 +, - 또는 숫자 0을 함께 사용하여 출력방법을 다르게 조절해 보는 프로그램

# 정수형 숫자에 대해 자릿수를 맞추어 출력

#include <stdio.h>
void main()
{
    int a=-5, b=23, c=5376;
    long d=325678;
    printf("정수형 숫자 format\n");
    printf("\n1. a=%d", a);
    printf("\n2. a=%6d", a);
    printf("\n3. a=%06d", a);
    printf("\n4. b=%6d", b);
    printf("\n5. b=%+6d", b);
    printf("\n6. c=%6d", c);
    printf("\n7. c=%-6d", c);
    printf("\n8. d=%6d\n", d);
}

# 실행 결과




# 실수형 숫자에 대해 자릿수를 맞추어 출력

#include <stdio.h>
void main()
{
    float a=-437.46, b=1278.9;
    double c=5.567;
    printf("실수형 숫자 format\n");
    printf("\n1. a=%8.3f", a);
    printf("\n2. b=%8.3f", b);
    printf("\n3. c=%8.3f", c);
    printf("\n4. c=%8.f", c);
    printf("\n5. c=8.1f", c);
    printf("\n6. c=%.2f", c);
    printf("\n7. a=%+.2f", a);
    printf("\n8. b=%+.2f", b);
}

# 실행 결과

  ::