#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Formatted input-output in c .


    Formatted input and output in c
    i am very confuse to format for integer output.
    %wd(here 'w' is the integer number specifying the minimum field width of the output data.and 'd' is the conversion specification character for integer value.
    ********my problem ************
    1) when length of variable is more than the width specifier,then also the output is printed correctly....why and how?
    printf("a=%3d,b=%4d",a,b);
    value of variable-
    a=2691(taken 5 digit)
    b=19412(taken 5 digit)
    output:-
    a=2691 (there is no effect on output)
    b=19412
    explain more
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a=26357;
    int b=19412;

    clrscr();

    printf("a:%3d\nb:%4d",a,b);
    getch();
    }
    *******if value of variable exceed to two value then effect on output like this.....
    if i taken to
    a=269178(taken extra two digit 78 digit)
    b=194126(taken extra one digit 6 digit)
    ********
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a=269178;
    int b=194126;

    clrscr();

    printf("a:%3d\nb:%4d",a,b);
    getch();
    }
    output:
    a:7034
    b:-2482(minus 2482).

    i am very confuse to solve that problem.if i given to %3d and %4d in printf statement to above statement...then length of variable is more than the width specifier,then also the output is printed correctly...maximum five digit there is no effect on output.
    but if i want to exceed one digit extra,one or both side.then the output will not correctly.a width specifier is fail to this situation.
    what is the reason explain to that topic.
    plz....solve this problem
    respectfully sir.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Lemme guess. You're using a 16-bit compiler like turbo-C. Guess what happens when you assign a value to your variable, which is bigger than the largest value that the variable type can take (for your compiler, max. integer value is 32767).

    As an aside, if you really want to learn C, stop using such an ancient and obsolete compiler.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Read The Manual (RTFM!).

    From one of literally thousands of man pages for printf that are on-line (this from one at http://linux.die.net/man/3/printf) -- underlining added by me:
    Originally Posted by man page
    The field width

    An optional decimal digit string (with nonzero first digit) specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been given). Instead of a decimal digit string one may write "*" or "*m$" (for some decimal integer m) to specify that the field width is given in the next argument, or in the m-th argument, respectively, which must be of type int. A negative field width is taken as a '-' flag followed by a positive field width. In no case does a nonexistent or small field width cause truncation of a field; if the result of a conversion is wider than the field width, the field is expanded to contain the conversion result.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    In case you didn't understand Scorpions4ever's answer, consider the numbers you gave (also note the use of code tags to preserve indenting):
    Code:
        int a=269178;
        int b=194126;
    269178 would be stored as (in hex) 0x00041B7A, but since you're using a 16-bit compiler it will instead only store the lower-order 16 bits, which would be 0x1B7A which is 7034 decimal.

    Similarly, 269178 would be stored as (in hex) 0x0002F64E, but since you're using a 16-bit compiler it will instead only store the lower-order 16 bits, which would be 0xF64E. Now here we have the additional fact that this is a signed int and the sign bit is set, making this a negative value (in 0x1B7A the sign bit was reset, making that value positive). Applying 16's Complement (you can use 2's Comp if your school isn't that advanced yet), that would be a negative 0x09B2 which is -2482 decimal.

    Upon inspection, we find that 7034 and -2482 are precisely the numbers that you displayed, because those are the actual values stored in a and b. C does not report the programmer's overflow errors; C assumes that you know what you are doing.

    Read your limits.h header file to find out what the maximum and minimum values are that you can store in each data type. long int would have handled the numbers you were working with. If you still want to use int, then get a 32-bit compiler.

    If you're using a 16-bit compiler, then you are undoubtedly also using a 16-bit IDE, most likely Turbo C. What kind of system are you running it on? My 64-bit Win7 refuses to run 16-bit applications. If those schools continue to require using Turbo C, how are the students supposed to comply if they cannot run it on their computers?

IMN logo majestic logo threadwatch logo seochat tools logo