본문 바로가기
C 언어

[c] printf

by SpeeDr00t 2016. 8. 3.
반응형

[c] printf

1.소스

#include <stdio.h>

int test1()
{
    printf("hello world!! \n" );
    printf("\n");
}

int test2()
{
    int i = 20;
    printf("int i = %d , %%d = %d, %%o = %o, %%x =  %x\n", i, i, i, i );
    printf("int i = %d , %%4d = [%4d]\n",i ,  i  );
  
    printf("\n");
}

int test3()
{
    unsigned long int i = 100;
    printf("unsigned long int i = %lu , %%4lu = [%4lu] \n", i , i   );

    printf("\n");
}



int test4()
{
    short int i = 100;
    printf("short int i = %hd , %%4hd = [%4hd] \n", i , i   );
    
    printf("\n");
}

int test5()
{
    long int i = 100L;
    printf("long int i = %ldL , %%4ld = [%4ld] \n", i , i   );
    
    printf("\n");
}


int test6()
{
    unsigned i = 100u;
    printf("unsigned i = %uu , %%4u = [%4u] \n", i , i   );
    
    printf("\n");
}


int test7()
{
    float i = 100.123f;
    printf("float i = %ff , %%20f = [%20f] \n", i , i   );
    printf("float i = %ff , %%20e = [%20e] \n", i , i   );


    float j = 0.123123123123f;
    printf("float j = %ff , %%.20f = [%.20f] \n", j , j );

    printf("\n");
}


int test8()
{
    double i = 1.123123;
    printf("double i = %lf , %%20lf = [%20lf] \n", i , i   );
    printf("double i = %lf , %%20le = [%20le] \n", i , i   );

    double j = 0.123123123123;
    printf("double j = %lf , %%.20lf = [%.20lf] \n", j , j   );
    
    printf("\n");
}


int test9()
{
    long double i = 13.123l;
    printf("long double i = %Lfl , %%20Lf = [%20Lf] \n", i , i );
    printf("long double i = %Lfl , %%20Le = [%20Le] \n", i , i );
    
    long double j = 0.123123123123l;
    printf("long double j = %Lfl , %%.20Lf = [%.20Lf] \n", j , j );

    printf("\n");
}

int test10()
{
    char c1 = 'A';

    printf("char c1 = %c , %%c = %c , %%d =  %d\n", c1 , c1 , c1 );
    printf("char c1 = %c , A+1 = %c \n", c1 , c1 + 1  );
    

    char c2 = 65;
    printf("char c2 = %d , %%c = %c, c2+1[%%c] = %c, c2+1[%%d] = %d  \n", c2 , c2, c2+1, c2+1 );
    
    printf("\n");
}


int test11()
{
    unsigned char c1 = 'A';

    printf("unsigned char c1 = %c , %%c = %c , %%d =  %d\n", c1 , c1 , c1 );
    printf("unsigned char c1 = %c , A+1 = %c \n", c1 , c1 + 1  );
  
    unsigned char c2 = 65;
    printf("unsigned char c2 = %d , %%c = %c, c2+1[%%c] = %c, c2+1[%%d] = %d  \n", c2 , c2, c2+1, c2+1 );

 
    printf("\n");
}



int 
main( int argc , char ** argv ) 
{
    test1();
    test2();
    test3();
    test4();
    test5();
    test6();
    test7();
    test8();
    test9();
    test10();
    test11();
}

결과

hacker@ubuntu:~/c$ 
hacker@ubuntu:~/c$ 
hacker@ubuntu:~/c$ gcc -o printf1 printf1.c 
hacker@ubuntu:~/c$ 
hacker@ubuntu:~/c$ 
hacker@ubuntu:~/c$ 
hacker@ubuntu:~/c$ ./printf1 
hello world!! 

int i = 20 , %d = 20, %o = 24, %x =  14
int i = 20 , %4d = [  20]

unsigned long int i = 100 , %4lu = [ 100] 

short int i = 100 , %4hd = [ 100] 

long int i = 100L , %4ld = [ 100] 

unsigned i = 100u , %4u = [ 100] 

float i = 100.123001f , %20f = [          100.123001] 
float i = 100.123001f , %20e = [        1.001230e+02] 
float j = 0.123123f , %.20f = [0.12312312424182891846] 

double i = 1.123123 , %20lf = [            1.123123] 
double i = 1.123123 , %20le = [        1.123123e+00] 
double j = 0.123123 , %.20lf = [0.12312312312300000006] 

long double i = 13.123000l , %20Lf = [           13.123000] 
long double i = 13.123000l , %20Le = [        1.312300e+01] 
long double j = 0.123123l , %.20Lf = [0.12312312312300000000] 

char c1 = A , %c = A , %d =  65
char c1 = A , A+1 = B 
char c2 = 65 , %c = A, c2+1[%c] = B, c2+1[%d] = 66  

unsigned char c1 = A , %c = A , %d =  65
unsigned char c1 = A , A+1 = B 
unsigned char c2 = 65 , %c = A, c2+1[%c] = B, c2+1[%d] = 66  

반응형

'C 언어' 카테고리의 다른 글

bi-directional sweep sort  (0) 2016.08.03
time  (0) 2016.08.03
PHP_STRLCPY  (0) 2016.07.27
매크로 사용하기(php소스 쪼개서 사용하기)  (0) 2016.07.21
strdup  (0) 2016.07.20