몽상실현개발주의

[따배씨] 4.7 pritnf() 함수의 변환 지정자 본문

Language/C

[따배씨] 4.7 pritnf() 함수의 변환 지정자

migrationArc 2021. 5. 13. 18:53

[따배씨] 4.7 pritnf() 함수의 변환 지정자

따배씨 - 따라하며 배우는 C언어

4강 문자열과 형식 맞춘 입출력

4.7 pritnf() 함수의 변환 지정자 Conversion Specifiers

#include <stdio.h>
#include <limits.h>
#include <float.h>

int main()
{
    double d = 3.14159265358979323846264338327950288419716939937510;
    
    printf("%c\n", 'A');
    printf("%s", "I love you\n");
    printf("Even if there's a small chance, \
we owe this to everyone who's not in this room to try.\n");
    
    printf("\n");
    printf("%d %i %i %i\n", 1004, 1234, INT_MAX, UINT_MAX); // Note overflow
    printf("%u %u %u\n", 1024, -1, UINT_MAX);               // Note overflow
    
    printf("\n");
    printf("%f %f %lf\n", 3.141592f, d, d); // l in %f is ignored
    printf("%a %A\n", d, d);
    printf("%e %E\n", d, d);
    
    printf("\n");
    printf("%g %g\n", 123456.789, 1234567.89);
    printf("%G %G\n", 123456.789, 1234567.89);
    printf("%g %g\n", 0.00012345, 0.000012345);
    printf("%G %G\n", 0.00012345, 0.000012345);
    
    printf("\n");
    printf("%o\n", 9);
    printf("%o\n", 9);
    printf("%p\n", &d); // pointer-of perator
    
    printf("n");
    printf("%x %X\n", 11, 11);
    printf("%%\n", d);  // Note the warning. d is ignored.
    
    printf("\n");
    printf("%9d\n", 12345);
    printf("%09d\n", 12345);
    printf("%.2f\n", 3.141592);
    printf("%.20f %.20lf\n", d, d);
    
    printf("\n");
    int n_printed = printf("Counting!");
    printf("%u\n", n_printed);
    return 0;
}

 

printf("%f %f %lf\n", 3.141592f, d, d); // l in %f is ignored
  • double 을 출력할때 %f 가 float이 아닌 double을 출력하는 것으로 자동으로 바뀜

 

printf("%p\n", &d); // pointer-of perator
  • d 변수의 주소값 출력

 

printf("%9d\n", 12345);
//     12345
printf("%09d\n", 12345);
// 000012345 
printf("%.2f\n", 3.141592);
// 3.14 
printf("%.20f %.20lf\n", d, d);
// 3.14159265358979311600 3.14159265358979311600
  • 자리수 맞춰 출력
    • %숫자 - 오른쪽 정렬 자리수 맞추기, 모자란 자리수는 빈칸 처리
    • %0숫자 - 오른쪽 정렬 자리수 맞추기, 모자란 자리수는 0으로 처리
    • %.숫자f, %.숫자lf - 소숫점 이하자리 자리수 출력수 정하기

 

int n_printed = printf("Counting!");
printf("%u\n", n_printed);
// 9
  • printf 의 리턴 값이 존재, 출력하는 글자 수를 리턴

 

 

 

 


이 글의 모든 사진과 내용의 출처는 홍정모 교수님께 있음을 알려드립니다.

http://blog.naver.com/atelierjpro

 

실리콘 밸리의 프로그래머 : 네이버 블로그

안녕하세요! 홍정모 블로그에 오신 것을 환영합니다. 주로 프로그래밍 관련 메모 용도로 사용합니다. 강의 수강하시는 분들은 홍정모 연구소 카페로 오세요.

blog.naver.com

http://www.inflearn.com/course/following-c

 

홍정모의 따라하며 배우는 C언어 - 인프런 | 강의

'따배씨++'의 성원에 힘입어 새롭게 개발된 C 언어로 시작하는 프로그래밍 입문 강의입니다. '따배씨'와 함께 프로그래밍 인생을 업그레이드 해보세요., 따라하며 배우는 C언어 '따배씨++'의 성원

www.inflearn.com

 

Comments