• Beginner level: %g,%e,%E,%G in C


    GUYS IF YOU REALLY LIKE THE POSTS OF THIS BLOG THEN PLEASE JOIN OUR BLOG.
    AND HELP US TO PUT MORE POSTS LIKE THIS.
    PLEASE HIT THE LIKE BUTTON ON RIGHT HAND SIDE.


    printf()


    Syntax:
    #include <stdio.h>
    int printf( const char *format, ... );


    Description:
    The printf() function prints output to STDOUT, according to format and other arguments passed to printf(). The string format consists of two types of items - characters that will be printed to the screen, and format commands that define how the other arguments to printf() are displayed. Basically, you specify a format string that has text in it, as well as "special" characters that map to the other arguments of printf().

    The %s means, "insert the first argument, a string, right here." The %d indicates that the second argument (an integer) should be placed there. There are different %-codes for different variable types, as well as options to limit the length of the variables and whatnot.

    Code  Format
    
    ----    ------
    
    %c  character
    
    %d  signed integers
    
    %i  signed integers
    
    %e  scientific notation, with a lowercase "e"
    
    %E  scientific notation, with a uppercase "E"
    
    %f  floating point
    
    %g  use %e or %f, whichever is shorter
    
    %G  use %E or %f, whichever is shorter
    
    %o  octal
    
    %s  a string of characters
    
    %u  unsigned integer
    
    %x  unsigned hexadecimal, with lowercase letters
    
    %X  unsigned hexadecimal, with uppercase letters
    
    %p  a pointer
    
    %n  the argument shall be a pointer to an integer into which is placed the number of characters written so far
    
    %%  a '%' sign


    An integer placed between a % sign and the format command acts as a minimum field width specifier, and pads the output with spaces or zeros to make it long enough. If you want to pad with zeros, place a zero before the minimum field width specifier. You can use a precision modifier, which has different meanings depending on the format code being used.


    • With %e, %E, and %f, the precision modifier lets you specify the number of decimal places desired. For example,

      %12.6f


      will display a floating number at least 12 digits wide, with six decimal places.

    • With %g and %G, the precision modifier determines the maximum number of significant digits displayed.

    • With %s, the precision modifer simply acts as a maximumfield length, to complement the minimum field length that precedes the period.

    All of printf()'s output is right-justified, unless you place a minus sign right after the % sign. For example,

        %-12.4f


    will display a floating point number with a minimum of 12 characters, 4 decimal places, and left justified. You may modify the %d, %i, %o, %u, and %x type specifiers with the letter l and the letter h to specify long and short data types (e.g. %hd means a short integer). The %e, %f, and %g type specifiers can have the letter l before them to indicate that a double follows. The %g, %f, and %e type specifiers can be preceded with the character '#' to ensure that the decimal point will be present, even if there are no decimal digits. The use of the '#' character with the %x type specifier indicates that the hexidecimal number should be printed with the '0x' prefix. The use of the '#' character with the %o type specifier indicates that the octal value should be displayed with a 0 prefix.

    You can also include constant escape sequences in the output string.

    The return value of printf() is the number of characters printed, or a negative number if an error occurred. 

    Example:
    char name[20] = "Bob";
    int age = 21;
    printf( "Hello %s, you are %d years old\n", name, age );
    
    OUTPUT: Hello Bob, you are 21 years old
     

9 comments:

  1. tapas said...

    nice.... learned many concepts..Thanks

  2. Unknown said...

    thanks buddy!
    Please contribute to us. If you like it.
    Promote it where ever feasible to you

  3. Ankita said...

    what will be the output for this code:

    void main()
    {
    int i=3,j;
    j=++i*++i*++i;
    printf("\n %d",j);
    }

  4. Unknown said...

    216..
    and you might be thinking

    j= 6 * 5 * 4;
    and here you are totally wrong.
    because in postfix expressions,

    first operator is postfix and den further computations are done.

    first ++x = 4
    next ++x, x=5
    last ++x, x=6
    now j= 6 * 6 *6;

    Hope you got the answer now :)

  5. Ankita said...

    Thank u..!!

  6. Ankita said...

    what will be the output of the following code:
    void main()
    {
    int i=10;
    char ch='A';
    float a=3.14;
    printf("%d %c %f\n",i,ch,a);
    printf("%d %f %f\n",i,ch,a);
    printf("%f %c %f\n",i,ch,a);
    }

  7. Unknown said...
    This comment has been removed by the author.
  8. Unknown said...

    10 A 3.140000
    10 Unexpected 3.14
    unexpected unexpected 3.14

  9. Unknown said...

    The unexpected output is so produced due to the internal type conversion of the C compiler.

Post a Comment