[C] Printf

Integer Type Print

#include <stdio.h>

int main(void)
{
    printf(" %d, %d, %d \n", 30, 105, 8000);
    printf("%%d print decimal");

    return 0;

    /*
     30, 105, 8000 
    %d print decimal
    */
}

Floating Point Type Print

#include <stdio.h>

int main(void)
{
    printf("%f, %lf, %.2lf \n", 4.23456, 6.56789, 7.77777);
    printf("%%f is float (4byte) \n");
    printf("The default print of float point type is to the sixth decimal place \n");
    printf("%%lf is long float (8byte) \n");
    printf("%%.2lf print to the second decimal palce \n");
    
    return 0;

    /*
    4.234560, 6.567890, 7.78 
    %f is float (4byte) 
    The default print of float point type is to the sixth decimal place 
    %lf is long float (8byte) 
    %.2lf print to the second decimal palce 
    */
}

Character Print

#include <stdio.h>

int main(void)
{
    printf("%c, %c, %c \n", 'A', 'B', '$');
    printf("%%c is charracter");
    
    return 0;

    /*
    A, B, $ 
    %c is charracter 
    */
}

String print

#include <stdio.h>

int main(void)
{
    printf("printf do not incules New line \n");
    puts("puts incules New line");
    printf("-----------------------------------");
    
    return 0;

    /*
    printf do not incules New line 
    puts incules New line
    -----------------------------------
    */
}

escape sequences

#include <stdio.h>

int main(void)
{
    puts("--------------------");
    puts(" Escape sequences");
    puts("--------------------");
    
    puts("\\n is New line");
    puts("--------------------");
    
    puts("\\t is Tab");
    puts("--------------------");
    
    puts("\\b is Backspace");
    puts("--------------------");
    
    puts("\\a is Alarm or Beep");
    puts("--------------------");
    
    puts("\\0 is NULL");
    puts("--------------------");
    
    return 0;

    /*
    --------------------
     Escape sequences
    --------------------
    \n is New line
    --------------------
    \t is Tab
    --------------------
    \b is Backspace
    --------------------
    \a is Alarm or Beep
    --------------------
    \0 is NULL
    --------------------
    */
}

Others

#include <stdio.h>

int main(void)
{
    printf(" [\t%d] print after Tab(8 spaces)\n" ,777);
    puts("-------------------------");
    
    printf(" [%-10d] 10 spaces on the right\n", 777);
    puts("---------------");
    
    printf(" [%10d] 10 spaces on the left\n", 777);
    puts("---------------");
    
    printf(" [%-20s] 20 spaces on the right\n", "HAPPY");
    puts("---------------");
    printf(" [%20s] 20 paces on the left\n", "LOVE");
        
    return 0;

    /*
     [      777] print after Tab(8 spaces)
    -------------------------
     [777       ] 10 spaces on the right
    ---------------
     [       777] 10 spaces on the left
    ---------------
     [HAPPY               ] 20 spaces on the right
    ---------------
     [                LOVE] 20 paces on the left
    */
}

Reference

Tags:

Categories:

Updated: