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;

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.

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.

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.

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?