Can we specify variable field width in a scanf() format string? If possible how?

5 replies [Last post]
dhanunjaya
Offline
Last seen: 10 years 4 weeks ago
Joined: 2009-04-26
bOt Points: 153

Can we specify variable field width in a scanf() format string? If possible how?

codeguru
Offline
Last seen: 10 years 44 weeks ago
Joined: 2009-04-08
bOt Points: 114
again i stress that you

again i stress that you seriously need to put up your query in a more elaborate manner

 

 

 

nyways what i assume is that you  are trying to input a string using scanf() and want  that the string length that you use be a variable.

 

it would be a good thing if you posted the piece of code that you working upon or is it that  its just a random query ??

j.sushanthkumar
Offline
Last seen: 10 years 31 weeks ago
Joined: 2009-08-21
bOt Points: 66
scaning the prefered number of charecters

scanf("%3d",&a);

the field width is 3 here

this code reads 3 charecters of the variable 

 

Note:- This post has been edited by j.sushanthkumar at Fri, 2009-08-21 18:45.

ramo44
Offline
Last seen: 10 years 31 weeks ago
Joined: 2009-08-24
bOt Points: 66
its not possible...

in scanf () ...we can't use variable width specifier...  we can only use like 

 

scanf ("%3d",&a);

 

if u enter 343434343 , the scanf simply scan the first 3 digits (ie) 343 only..

 

 

But we can use variable width specifiers in printf ()....

 

 

printf("%*d",3,a);

 

3 is the width of the integer a... 

 

 

and sorry for my english.. this is my first post .. please comment positively..

j.sushanthkumar
Offline
Last seen: 10 years 31 weeks ago
Joined: 2009-08-21
bOt Points: 66
@ramo44

int a,b;

printf("enter the same values for the a and b");

Scanf("%d%3d",&a,&b);

printf("%3d %d",a,b);

 

 

input: 2525 2525

the out put is

252 252

 

%d -- is an action specifier not the width specifier

 

 

the actual value of a and b is 2525 252

 

 

while printing u r specifing the field widht using action specifiers here there will be no change of value at the address a and b("it doesnot reflects at address"). It specifies only how to be printed

 

 

But in scanf it specifies the Width("it reflects in address").

 

 

 "NO WORRIES WITH UR ENGLISH" "KEEP POSTING COMMENTS "

Note:- This post has been edited by j.sushanthkumar at Mon, 2009-08-31 12:10.

ramo44
Offline
Last seen: 10 years 31 weeks ago
Joined: 2009-08-24
bOt Points: 66
thanks...

Thanks for your comment.. Mr.j.sushanthkumar...