Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
Weird usage of printf/scanf? Why do they work?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Keith Thompson, post: 3936271"] It's astonishing that gcc with those options would not at least warn about passing an int as the first argument to printf. On my system, I get: prog-2-1.c: In function `main': prog-2-1.c:23: warning: passing argument 1 of `printf' makes pointer from integer without a cast And I'm frankly disappointed with gcc that it merely issues a warning. printf expects a char* as its first argument. Apparently gcc is generating an implicit conversion from int to char*. Which means that printf most likely would try to read a string starting at address 5 and interpret it as a format string. I got a run-time segmentation fault when I tried it on my system. I cannot think of any plausible explanation for this output that doesn't involve some form of deliberate cheating. (The program's behavior is undefined, so in theory any output is possible, but "5^3 = 125" is about as likely as demons flying out of your nose.) Both of these printf calls are perfectly legal, though I'd expect gcc to warn about them. Any excess arguments not consumed by the format string are quietly ignored (though of course the argument expressions are evaluated during the call). Here there's no room for oddities due to undefined behavior. If the first printf call produces any output other than "The area A of a circle with radius ", then there's something wrong with the implementation; similarly for the second call. (There's just the slightest wiggle room due to the lack of a new-line at the end of the output, but that's not going to have the observed effect on any system that supports gcc.) [third program with similar issues snipped] Just to be perfectly clear, did you compile and execute the student's programs on your own system and get the same results? Was this a system independent of the one the student is using? Can you try the code on yet another system, one to which you're certain the student has no access? (In effect, you've already done that by posting the code here, and you've seen the results.) There is no plausible legitimate way the posted programs could produce the posted output. Some form of cheating is the only explanation I can think of. Additionally, I suspect there's something relevant that you're not telling us. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Weird usage of printf/scanf? Why do they work?
Top