The greatest mistake you can make in life is to be continually fearing you will make one

Tuesday, 7 September 2010

Data Input and Output-III

Little More about printf

Flags in printf function:
  • We can place flags in the printf statement with in the control string which will affect the appearance of the output.
  • The flag must be placed immediately after the percent sign(%).
  • Two or flags can be combined for a single output.
  • Table shows the commonly used flags


    FlagMeaning
    -Data item is left justified(ie)Blanks added after the data item
    +A sign(+(or)-) will precede with each signed numerical data item
    0Place leading zeros to appear instead of leading blanks
    #(with 0- & x- type conversion)Causes octal and hexadecimal data items to be processed by 0 and 0x respectively
    #(with e-,f- & g- type conversion)Causes a decimal point to be present in all floating point numbers even if the data item is a whole number.Also prevents the truncation of trailing zeros in g-type conversion
  • Example:



     
     
    #include<stdio.h>
    int main()
    {
        int x=1234;
        float y=15.5,z=-5.5;
        printf("%d %f %e\n",x,y,z);
        printf("%6d %7.0f %10.1e\n",x,y,z);
        printf("%-6d %-7.0f %-10.1e\n",x,y,z);
        printf("%+6d %+7.0f %+10.1e\n",x,y,z);
        printf("%-+6d %-+7.0f %-+10.1e\n",x,y,z);
        printf("%7.0f %#7.0f %7g %#7g\n",y,y,z,z);
        return 0;
    }

    OutPut:


    1234 15.500000 -5.500000e+00
      1234      16   -5.5e+00
    1234   16      -5.5e+00 
     +1234     +16   -5.5e+00
    +1234  +16     -5.5e+00 
         16     16.    -5.5 -5.50000
Prefixes in printf function: 
  • Prefixes within the control string to indicate the length of the corresponding argument.
  • Example: i indicates a signed or unsigned integer argument or a double precision argument. h indicates signed or unsigned short integer.

    #include<stdio.h>
    int main()
    {
        short x,y;
        long a,b;
        printf("%5hd %6hx %8lo %lu\n",x,y,a,b);
        return(0);
    }
    hd->short decimal integer
    hx->short hexadecimal integer
    lo->Long octal integer
    lu->Long unsigned integer
  • It is also possible to write the conversion characters x,e,g in uppercase as X,E,G. These uppercase conversion characters cause any letters with in the output data to be displayed in uppercase
    Example:

    #include<stdio.h>
    int main()
    {
        int x=0x10ab,y=0777;
        float z=0.3e-12;
        printf("|%8x %10.2e|\n",x,z);
        printf("|%8X %10.2E|\n",x,z);
        printf("|%8x %8o|\n",x,y);
        printf("|%#8x %#8o|\n",x,y);
        return(0);
    }
    Output:

    | 10ab 3.00e-13|
    | 10AB 3.00E-13|
    | 10ab 777|
    | 0x10ab 0777|
gets() and puts() Function:
  • gets and puts function used to read and write string(character array terminate with a newline character)
  • The string may include whitespace characters
  • Example:

    #include<stdio.h>
    int main()
    {
        char text[100];
        gets(text);
        puts(text);
        return(0);
    }

    Output:
    ./a.out
    helo welcome
    helo welcome

No comments:

Post a Comment