In C programming language, printf() function is used to print the "character, string, float, integer, octal and hexadecimal value" onto the output screen(Console).

We use printf() function with %d format specifier to display the value of an integer variable.

Similarly %c is used to display character, %f for float variable, %s for string variable, %lf for double and %x for hexadecimal variable.

To generate a newline,we use "\n" in C printf() statement.

Note : C language is case sensitive. For example, printf() and scanf() are different from Printf() and Scanf(). All characters in printf() and scanf() functions must be in lower case

  • Example of using printf() function in c

    Example)
    drag_handle
     
    #include <stdio.h>
    int main()
    {
       char ch = 'B';
       char str[20] = "onlyjavatech.com";
       float flt = 10.234;
       int no = 150;
       double dbl = 20.123456;
       printf("Character is %c \n", ch);
       printf("String is %s \n" , str);
       printf("Float value is %f \n", flt);
       printf("Integer value is %d\n" , no);
       printf("Double value is %lf \n", dbl);
       printf("Octal value is %o \n", no);
       printf("Hexadecimal value is %x \n", no);
       return 0;
    }
    
    Output:
    Character is B
    String is onlyjavatech.com
    Float value is 10.234000
    Integer value is 150
    Double value is 20.123456
    Octal value is 226
    Hexadecimal value is 96

    %d got replaced by value of an integer variable (no),

    %c got replaced by value of a character variable (ch),

    %f got replaced by value of a float variable (flt),

    %lf got replaced by value of a double variable (dbl),

    %s got replaced by value of a string variable (str),

    etc

To change the line print "\n" .

for example : printf("\n");

What next?

The next topic is scanf() function C

Share this page