Confusing C programmers

Consider this code:

int i;

i = 42;

someFunction( i );

printf( "The value of i is %d\n", i );

What value will it print?

Comment away…

4 thoughts on “Confusing C programmers

  1. wb8nbs

    42, Because it is the answer to Life, the Universe, Everything. And IIRC functions operate on a copy of a passed argument and don’t change the value. If you want I to change you have to pass a pointer.

    1. Allen Huffman Post author

      Ah, yes. That’s what I thought . . . And that is true in old C. There is apparently a way in C++ to specify that the function can modify it! I was stumped by code like that, wondering how it cold possible do anything, and when I stepped through it in the debugger the variable was indeed modified. It’s a crappy C++ thing (which I don’t know) that makes it impossible to look at something simple like that and truly know what it does! Hmph.

    1. Allen Huffman Post author

      It is. Just an arbitrary function. In C, it only would get a copy of the variable, and if it changed it inside the function, when it is done, the original value would still be there. But in C++ they can change that behavior…


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.