Solved

What does this line do?

Posted on 2006-11-08
8
193 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

743 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

12 Experts available now in Live!

Get 1:1 Help Now