Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

What does this line do?

Posted on 2006-11-08
8
196 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: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 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: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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 84

Expert Comment

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

Expert Comment

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
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 and use pointers in the C programming language.
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.

839 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