Quote:
is there any way to specify variable field width in scanf().....................???????????
|
The format string can be a variable just like any other string.
A quick example (using printf instead, but scanf works the same way):
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char format[6];
int length = 10;
int number = 123456;
if(argc > 1)
{
length = atoi(argv[1]);
if(length < 1) { length = 1; }
if(length > 99) { length = 99; }
}
// Construct the format
// "%%" becomes "%", "%d" is replaced with length, last "d" remains unchanged
sprintf(format, "%%%dd\n", length);
printf(format, number);
return(0);
}
Quote:
I ve got one more doubt........Are these auto variables stored in registers.?
|
They can be in registers or in the stack, depending where the compiler decides to put them. You can test this yourself. Write some simple functions, compile them with "gcc -S example.c example.s" and examine the output assembler file to see where gcc puts your variables.
Quote:
Is there any diff between auto and register?
|
In gcc they are the same thing except that a register variable cannot have an address, so code like this:
Code:
register int foo;
do_something(&foo);
causes a warning. Other compilers, especially old ones, may actually force register variables into registers.