Solved

What does this line do?

Posted on 2006-11-08
8
194 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
 
LVL 84

Expert Comment

by:ozo
ID: 17901543
no, "%1.1d" is also the same as "%d"
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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 tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now