?
Solved

What does this line do?

Posted on 2006-11-08
8
Medium Priority
?
203 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 46

Expert Comment

by:Kent Olsen
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 85

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 46

Expert Comment

by:Kent Olsen
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 85

Expert Comment

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

Expert Comment

by:Kent Olsen
ID: 17901556


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

0
 
LVL 85

Expert Comment

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

Accepted Solution

by:
Kent Olsen earned 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
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.
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 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