July 16th, 2013, 07:27 AM
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?
value of variable-
a=2691(taken 5 digit)
b=19412(taken 5 digit)
a=2691 (there is no effect on output)
*******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)
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
July 16th, 2013, 09:28 AM
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
July 16th, 2013, 10:15 AM
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
July 16th, 2013, 03:11 PM
In case you didn't understand Scorpions4ever's answer, consider the numbers you gave (also note the use of code tags to preserve indenting):
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?