0

I have a line in my program that goes like: sprintf (line, "%s [%lu..%lu]", pp->help, min, max); where min, max; are unsigned long and line is char[SOME_SIZE]. Before the sprintf is invoked I call a function that can assign negatives to min and min gets the value of -5 which translates to 4294967291 because it's unsigned.

So I tried to do something like:

if((long)min < 0) // works fine because 4294967291 has the binary of 0x1...
    sprintf (line, "%s [%d..%d]", pp->help, (long)min, max);

but that does not work. The if is true, but I still get positive numbers (i.e. 4294967291) when I print line, how can I fix this?

NOTE: I'm not sure how the print is implemented in the end of the execution but right now the sprintf itself is not working as i want it to, as it adds unsigned to the string instead of signed.

this is a simplification of my code:

unsigned long min = foo() /* in this case -5 */, max = goo() /* in this case 5*/;
char line[100];

if((long)min < 0){
    sprintf (line, "%s [%d..%d]", pp->help, (long)min, max);
}// when it prints I get [4294967291..5] instead of [-5..5]
else{
    sprintf (line, "%s [%lu..%lu]", pp->help, min, max);
}

EDIT: I tried to get similar results on my gcc but failed. But I saw something very strange that could explain this. When line is declared, it is char[], but when the debugger reaches the sprintf, line is identified as unsigned char... but sadly I can't post the code here.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
  • 2
    Please edit your question and tell us what output you expect for what values. – Jabberwocky Feb 27 '17 at 13:58
  • 2
    1) Any reason you use an `unsigned long` although you want a `signed long` apparently? 2) Provide a [mcve]. 3) Sounds like an XY problem (see 1) 4) The conversion unsigned to signed is implementation defined. Which behaviour does your implementation specify? – too honest for this site Feb 27 '17 at 13:59
  • @Olaf 1) I'm just doing a workaround, I don't want to change the whole function (somewhat big function) 2)I edited my q 3) I edited my q 4) 2's complement is the method used here – CIsForCookies Feb 27 '17 at 14:03
  • 1
    Cannot reproduce, click here -> [this](http://ideone.com/uB9u9i) works for me. – Jabberwocky Feb 27 '17 at 14:06
  • 1
    Please edit-in exactly what you are getting (not "that does not work") and what you are expecting (_concrete values) as that can provide clues as to what's wrong. For example, depending on your compiler/platform, you _might_ need `%ld` for the signed output. Seeing what gets printed for `max` (and what it should be) may help in deciding that. – TripeHound Feb 27 '17 at 14:10
  • And [this](http://ideone.com/Aadf7h) works also for me. – Jabberwocky Feb 27 '17 at 14:11
  • 1
    '%l' is the right format string for a signed long, not '%d'. Maybe that causes the problem – Ingo Leonhardt Feb 27 '17 at 14:13
  • @IngoLeonhardt I'm checking this right now, but with "%ld". is there a difference? I couldn't tell from what I read in the internet – CIsForCookies Feb 27 '17 at 14:15
  • 1
    my fault, %ld is correct – Ingo Leonhardt Feb 27 '17 at 14:17
  • Possible duplicate of [What is the argument for printf that formats a long?](http://stackoverflow.com/questions/38561/what-is-the-argument-for-printf-that-formats-a-long) – Paul Hankin Feb 27 '17 at 14:17
  • Not wanting to refactor a function is a very bad reason to use a cast! If this is production code and you have a good project supervisor, you will run into trouble with that excuse. And 2's complement is an encoding for negative values, not a conversion method! – too honest for this site Feb 27 '17 at 14:37
  • @Olaf if this would be my last hope, then of course I'll change the existing code, but I want to do as little as possible in order to not mess up with legacy code. I now think of converting it first to a string, adjusting it and then adding to original string – CIsForCookies Feb 27 '17 at 14:49
  • And what would that change about the conversion issue? With all due respect, but there seem to be some basics missing. – too honest for this site Feb 27 '17 at 14:58

0 Answers0