Solved

What does this line do?

Posted on 2006-11-08
8
195 Views
Last Modified: 2010-04-15
Could someone explain what this line does?

sprintf(ERR_rec.err_flag,"%1d", unclaim->err_flag[i][j]);

I have programming experience, but I don't know c that well.
0
Comment
Question by:bbcac
  • 4
  • 3
8 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 17900814

sprintf() formats text and writes it to a string (character array) instead of a file.

In this case the bottom digit of unclaim->err_flag is being written to ERR_re.err_flag.  Since the format string is "%1d" only the bottom digit is stored.  Kind of like mod base 10.


Good Luck,
Kent
0
 
LVL 84

Expert Comment

by:ozo
ID: 17901125
%1d means that the mininimum field width is 1, so the number will be padded with spaces if there are fewer than 1 digits (which can never happen, since %d always prints at least one digit)
0
 
LVL 45

Expert Comment

by:Kdo
ID: 17901310

ozo's right.  "%d" is effectively the same as "%1d".  I suspect that the coder intended to show only the low-order digit, but that would require "%1.1d".


Man, what a week.......
Kent
0
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
LVL 84

Expert Comment

by:ozo
ID: 17901543
no, "%1.1d" is also the same as "%d"
0
 
LVL 45

Expert Comment

by:Kdo
ID: 17901556


Nope.  "%d" is a floating width.  "%1.1d" writes exactly 1 visible character.

0
 
LVL 84

Expert Comment

by:ozo
ID: 17901604
no, "%1.1d" writes at least 1 visible character.
0
 
LVL 45

Accepted Solution

by:
Kdo earned 50 total points
ID: 17901639

I can't tell you how many times I've corrected someone else for that same booboo.

%1.1 is floating width for integers
%1.1 is exact width for strings

As I said, Man, what a week....
Kent
0
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 17916580
> sprintf(ERR_rec.err_flag,"%1d", unclaim->err_flag[i][j]);

This is dangerous if the programmer is expecting the value written to the err_flag array to be only 2 bytes.  As was said earlier, %1d is only a minimum size, and on a 32-bit machine an int can take up to 12 characters ("-2147483648").

I had a bad buffer overflow once with something like this:

char buf[64];
sprintf("%20lf", doubleval);

64 is plenty big to hold a 20-digit number, right?  Well, on certain rare occasions the doubleval was close to MAXDOUBLE (about 1.797e+308), and printed with over 300 digits.

A safer way to do it would be:
sprintf(ERR_rec.err_flag,"%1d", unclaim->err_flag[i][j] != 0);

That constrains the value to be 0 or 1.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question