Note that there are some explanatory texts on larger screens.

plurals
  1. POBehaviour of printf when printing a %d without supplying variable name
    text
    copied!<p>I've just encountered a weird problem, I'm trying to printf an integer variable, but I forgot to specify the variable name, i.e.</p> <pre><code>printf("%d"); </code></pre> <p>instead of</p> <pre><code>printf("%d", integerName); </code></pre> <p>Surprisingly the program compiles, there is output and it is not random. In fact, it happens to be the very integer I wanted to print in the first place, which happens to be m-1.</p> <p>The errorneous <code>printf</code> statement will consistently output m-1 for as long as the program keeps running... In other words, it's behaving exactly as if the statement reads</p> <pre><code>printf("%d", m-1); </code></pre> <p>Anybody knows the reason behind this behaviour? I'm using g++ without any command line options.</p> <pre><code>#include &lt;iostream&gt; #define maxN 100 #define ON 1 #define OFF 0 using namespace std; void clearArray(int* array, int n); int fillArray(int* array, int m, int n); int main() { int n = -1, i, m; int array[maxN]; int found; scanf("%d", &amp;n); while(n!=0) { found=0; m = 1; while(found!=1) { if(m != 2 &amp;&amp; m != 3 &amp;&amp; m != 4 &amp;&amp; m != 6 &amp;&amp; m != 12) { clearArray(array, n); if(fillArray(array, m, n) == 0) { found = 1; } } m++; } printf("%d\n"); scanf("%d", &amp;n); } return 0; } void clearArray(int* array, int n) { for(int i = 1; i &lt;= n; i++) array[i] = ON; } int fillArray(int* array, int m, int n) { int i = 1, j, offCounter = 0, incrementCounter; while(offCounter != n) { if(*(array+i)==ON) { *(array+i) = OFF; offCounter++; } else { j = 0; while((*array+i+j)==OFF) { j++; } *(array+i+j) = OFF; offCounter++; } if(*(array+13) == OFF &amp;&amp; offCounter != n) return 1; if(offCounter ==n) break; incrementCounter = 0; while(incrementCounter != m) { i++; if(i &gt; n) i = 1; if(*(array+i) == ON) incrementCounter++; } } return 0; } </code></pre>
 

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