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

Tuesday 21 September 2010

Questions on Data Input and Output

  1. What are the commonly used input and output functions in c? How are they accessed?Solution


    * C has 6 popular data input and output library functions. They are getchar, putchar, printf, scanf, gets and puts.
    * These functions handle data transfer between computer and the input/output device (keyboard,monitor)
    * The functions getchar and putchar allow single characters to be transferred into and out of the computer.
    * The functions printf and scanf allow the transfer of single characters, numerical values and strings
    * The functions gets and puts handle input and output of strings.
    * To use these functions in a program the standard input output header file stdio.h must be included in the program.
  2. What is the header file needed to include the input and output functions?
    Solution



    To include the standard c input and output functions getchar, putchar, printf, scanf, gets and puts the header file #include< stdio.h > must be included in the program
  3. What happens when an end of file condition is encountered when reading characters using getchar() function?Solution



    If an end of file condition is encountered when reading a character using getchar() function , the value of the symbolic constant EOF will  automatically returned . The value of EOF is -1
  4. When entering data via the scanf function must octal data be preceded by 0 and must hexadecimal data be preceded by 0x?Solution



    Octal values need not be preceded by a 0 and hexadecimal values need not be preceded by 0x or 0X in scanf function
  5. How are unrecognized characters within the control string of a scanf function interpreted?Solution


    Un recognized characters within the control string are expected to be matched by the same characters in the input data. The unrecognized characters will be read into the computer but not assigned to an identifier. Execution of the scanf function will terminate if a match is not found
    Example:     scanf("%d * %d",&a,&b);
    the accepted  input is 5 * 4. If you give a and b as 5 and 4 then the program will stop executing as the expected character * is not found. so a is assigned to 5 and bis assigned to 0
  6. What is the difference between f-type conversion,e-tye conversion and g-type conversion when outputting floating point data with a printf function?
  7. What is the conversion character i represent in scanf and printf function?Solution



    "i" represent the dataitem is a decimal,hexadecimal or octal integer


  8. What is the minimum field width specifies and Where is it located?
    Solution


    It specifies the smallest field in which a value is to be printed. It is used in a conversion specification, and if present, it is placed immediately following the % sign.
  9. When the conversion specification is %, what is its minimum secification?
    Solution


    The size of the number being printed (including a minus sign, if the number is negative.)
  10. What is the meaning of 'f' in printf statement?
    Solution



    "f" stand for either formated or function

  11. In printf() function what is the output of printf("%d")?
    Solution
    When we write printf("%d",a); the compiler will print the value of a. But here nothing after "%d% so compiler will print garbage value

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