Note that there are some explanatory texts on larger screens.

plurals
  1. POPrinting int type with %lu - C+XINU
    primarykey
    data
    text
    <p>I have a given code, in my opinion there is something wrong with that code: I compile under XINU.</p> <p>The next variables are relevant :</p> <pre class="lang-c prettyprint-override"><code>unsigned long ularray[]; int num; char str[100]; </code></pre> <p>There is a function returns int:</p> <pre class="lang-c prettyprint-override"><code>int func(int i) { return ularray[i]; } </code></pre> <p>now the code is like this:</p> <pre class="lang-c prettyprint-override"><code>num = func(i); sprintf(str, "number = %lu\n", num); printf(str); </code></pre> <p>The problem is I get big numbers while printing with %lu, which is not correct.</p> <p>If i change the <strong>%lu</strong> to <strong>%d</strong>, i get the correct number. For example: with %lu i get 27654342, while with %d i get 26, the latter is correct;</p> <p>The variables are given, the declaration of the function is given, i write the body but it must return int;</p> <p>My questions are:</p> <ol> <li><p>I'm not familiar with 'sprintf' maybe the problem is there?</p></li> <li><p>I assigned unsigned long to int and then print the int with %lu, is That correct?</p></li> <li><p>How can i fix the problem?</p></li> </ol> <p>Thanks in advance.</p> <p>Thanks everyone for answering. I just want to mention I'm working under XINU, well i changed the order of the compilation of the files and what you know... its working and showing same numbers on %lu and %d.</p> <p>I'm well aware that assigning 'unsigned long' to int and then printing with %lu is incorrect coding and may result loss of data. But as i said, the code is given, i couldn't change the variables and the printing command.</p> <p>I had no errors or warnings btw. I have no idea why changing the compilation order fixed the problem, if someone have an idea you are more then welcome to share.</p> <p>I want to thank all of you who tried to help me.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload