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

Tuesday, 31 August 2010

Data Input and Output-II

4.Printf function

  • Printf function prints data from the computers memory to the standard output device.
  • Syntax:
    printf("%converion character",Argument list);

    Argument->It can be constants,single variable, array names (or) complex expressions
  • Example:

    #include<stdio.h>
    int main()
    {
        int i=10,j=20;
        char text[20];
        printf("%d %d %s\n",i,j,text);
        return 0;
    }
  • printf allow us to include labels and message with the output data. The extra labels in the double quotes in the printf will be displayed in the screen.
    Example:
    #include &lt; stdio.h&gt;
    int main()
    {
    int avg=80;
    printf("Average=%d%\n",avg);
    return 0;
    }

    Output
    Average=80%
  • The f-type conversion and the e-type conversions are both used to output floating point values.  In e-type conversion an exponent to be included in the output.
    Example


    #include&lt; stdio.h &gt;
    int main()
    {
        float x=100.0,y=25.25;
        printf("%f %f \n",x,y);
        printf("%e %e\n",x,y);
        return 0;
    }

    Output
    100.000000 25.2500001.000000e+02 2.525000e+01
  •  In the printf, s-type conversion is used to output a string that is terminatedd by the null character(\0). Whitespace characters may be included with in the string.
    Example: Reading and writing a line of text.
    #include&lt; stdio.h &gt;
    int main()
    {
        char text[100];
        scanf("%[^\n]",text);
        printf("%s",text);
        return 0;
    }
Minimum field width specification
  • We can restrict the number of characters or numbers displayed in the screen by specifying a fieldwidth. A minimum field width can be specified by preceding the conversion character by an unsigned integer.
  • Syntax:
    %fieldwidth conversioncharacter
  • If the number of characters in the corresponding data item is less than the specified field width then the data item will be preceded by enough leading blanks to fill the specified field.
  • If the number of characters in the data item exceeds the specified field width then additional space will be allocated to the data item so that the entire data item will be displayed.
    Rule1: Number of digits > field width=Add additional space
    Rule2: Number of digits < field width=Add leading blankspace
  • Example:

    #include<stdio.h>
    int main()
    {
        int x=12345;
        float y=123.456;
        printf("%3d %5d %7d\n",x,x,x);
        printf("%3f %10f %13f\n",y,y,y);
        printf("%3e %10e %13e\n",y,y,y);
        return 0;
    }
    Output
    12345 12345   12345
    123.456000 123.456000    123.456000
    1.234560e+02 1.234560e+02  1.234560e+02
    x has 5 digits
    %3d->no of digits(5) < fieldwidth(3) so additional space added and all  the digits are displayed.
    %7d-> no of digits(5) > fieldwidth(7)=>12345
     
  • g-type conversion:
       In g-type conversion no extra trailing zeros are added in the floating point number.
    Example:
    #include<stdio.h>
    int main()
    {
        float x=123.456;
        printf("%3g %10g %13g\n",x,x,x);
        printf("%3g %13g %16g\n",x,x,x);
        return 0;
    }

    Output
    123.456    123.456       123.456
    123.456       123.456          123.456
Floating point precision specification
  • It is also possible to specify the maximum number of decimal places for a floating point value or the maximum number of characters displayed for a string. This specification is known as precision.
  • The precision is an unsigned integer. Precision is always preceded by a decimal point.
  • A floating point number will be rounded if the number of digits exceeds the precision.
  • Example:
    #include<stdio.h>
    int main()
    {
        float x=123.456,y=1.5,z=4575.255;
        printf("%5f %7f %7.1f %7.2f %7.3f\n",x,x,x,x,x);
        printf("%5f %7f %7.1f %7.2f %7.3f\n",y,y,y,y,y);
        printf("%5f %7f %7.1f %7.2f %7.3f\n",z,z,z,z,z);
        printf("%5g %7g %7.1g %7.2g %7.3g\n",x,x,x,x,x);
        printf("12e %12.3e %12.5e\n",x,x,x);
        //only precision no field width
        printf("%f %0.1f %0.3f\n",x,x,x);
        printf("%e %0.3e %0.5e\n",x,x,x);
        return 0;
    }

    Output
    123.456000 123.456000   123.5  123.46 123.456
    1.500000 1.500000     1.5    1.50   1.500
    4575.254883 4575.254883  4575.3 4575.25 4575.255
    123.456 123.456   1e+02 1.2e+02     123
    12e    1.235e+02  1.23456e+02
    123.456000 123.5 123.456
    1.234560e+02 1.235e+02 1.23456e+02
Minimum field width and precision specification for character data
  • The specification for a string is same as numerical data. (ie) leading blanks will be added if the string is shorter than the specified field width and additional space will be allocatedd if the string is longer than the specified field width.
  1. string characters < field width=add leading blanks
  2. string characters > field width=add additional space
  • The precision specification will determine the maximum number of characters that can be displayed.

    Precision < string characters=excess right most characters will not be displayed
  • This rule is applied even if the minimum field width is larger than the entire string but additional blanks will be added to the truncated string.
  • Example:

    #include<stdio.h>
    int main()
    {
        char text[20]="Welcome";
        printf("%5s\n %7s\n %10s\n %15s\n %0.5s\n %15.5s\n",text,text,text,text,text,text);
        return 0;
    }

    Output:
    Welcome
     Welcome
        Welcome
             Welcome
     Welco
               Welcome
    %0.5s=Welco [no field width but the maximum precision is 5. so only 5 characters displayed.
    %15.5s= < 10 blankspace > Welco [maximum precision is 5 so the string trimmed to Welco but the field width is 15 so 10 leading blanks added.]

Friday, 13 August 2010

Data Input and Output-I

  • 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.
1.Single Character Input-getchar() function:
  •  The getchar() function reads a single character from the input device(keyboard)
  • Syntax:


      char variable=getchar();
  • Example:

        char c;
        c=getchar();

    The character entered on the keyboard is read by the getchar function and assigned to the character variable c.
  • If an end of file condition is found when reading a character using getchar, the value of symbolic constant(-1) will be returned.
2.Single Character Output-putchar() function:
  • The putchar() function display a single c character on the standard output device.
  • Syntax:


      putchar(variable);
  • Example:


        char c;
        putchar(c);

    The character stored in the variable c is displayed in the monitor.
Scanf function
  • scanf function used to read any numerical values, single characters or strings.
  • The function returns the number of data items that have been entered successfully.
  • Syntax:


        scanf("control string",argument list);
           control string=%conversion character.
  • Table shows the conversion character and the data item meaning



    Conversion characterMeaning
    cSingle character
    ddecimal integer
    e,f,gFloating point value
    hShort integer
    idecimal, hexa decimal or octal integer
    ooctal integer
    uunsigned decimal integer
    xHexadecimal integer
    sString
    [...]String include whitespace characters
  • The arguments may be variables or array.
  • Each variable name must be preceded by an ampersand(&). Array name should not begin with &.
  • Example

    #include<stdio.h>
    int main()
    {
        int no;
        float f;
        char text[50];
        scanf("%d %f %s",&no,&f,text);
    }
  • If 2 or more data items are entered they must be separeted by whitespace characters[blank space (or) new line (or) tab].
  • While reading a string that include whitespace characters use []. specify the whitespace characters inside the []
  • Example: Read a string which contains only capital letters and blank space

    #include<stdio.h>
    int main()
    {
        char text[50];
        scanf("%[ ABCDEFGHIJKLMNOPQRSTUVWXYZ]",text);}
    here if the input is given as WELCOME TO C then text contain WELCOME TO C. If the input is given as Welcome To C then only the single letter W would be assigned to text
  • If the characters with in the brackets are given after circumflex(^) operator then the scanf will read all the characters except the characters in the []

        scanf("%[^\n]",text);
    The program will read any characters(maximum 49 characters) until the newline is pressed.
Little More about scanf function:
  • It is possible to limit the number of characters read by specifying a maximum field width for that data item. To do that an unsigned integer integer indicating the field width is placed with in the control string between the percent sign(%) and the conversion character.
  • Syntax:
    %fieldwidth conversioncharacter
  • Example:
        %4d -->The decimal number can contain maximum of 4 digits
       %5f -->The float number can contain 5 digits including the dot(20.20,200.1)
  • The data item may contain fewer characters than the specified field width but cannot exceed the specified field width. If we give more characters than the specified field width the leftover characters are incorrectly interpreted as a next data item
  • Example1:
    {

        int a,b,c;

        scanf("%2d %2d %2d",a,b,c);
    }

    1. If the input is given as 12 34 56 then a=12 b=34 c=56.
    2. If the input is given as 123456789 then a=12 b=34 c=56
    3. If the input is 123 4 5678  then a=12 b=3 c=4
  • Example 2:
        float f;
        scanf("%5f",&f);

    1. if the input is 555.55 then f=555.500000
    2. If the input is 456789.555 then f=45678.000000
    3. if the input is 55555.555 then f=55555.00000
  • If there is no whitespace present between the conversion characters in the scanf function then the whitespace characters with in the input data will be interpreted as a data item.
  • Example:
        char a,b,c;
        scanf("%c%c%c",&a,&b,&c);
    If x y z is given as input then a=x b= (blankspace) c=y
  • To avoid reading the blankspace use %1s instead of %c
        char a,b,c;
        scanf("%c%1s%1s",a,b,c);
    If x y z is given as input then a=x b=y c=z or we can write the scanf as scanf("%c %c %c",&a,&b,&c);
  • Unrecognized characters within the control string are expected to be matched by the same characters in the input data. The unrecognized characters are read but not assigned to any variable but if the characters are not present then the scanf function will terminate.
  • Example:
        char a,b;
        scanf("%c * %c",&a,&b);
    If the input is given as a *  b then only a=a b=b. if the input is given as a b then the function is stop executing as * is not given as input. so a is assigned to a . b is Null