EmbeddedRelated.com

(So far, 48 people got it right out of 422 for a success rate of 11%)

(Thank you to Jason Sachs for submitting this question)


What does kapow() return in this C program?

#include <stdint.h>
uint32_t howjadoo(uint32_t prevsum, uint16_t command, uint16_t response)
{
    return prevsum + (command - response);
}
uint32_t kapow()
{
    return howjadoo(100000, 10001, 10002);
}
Pick one:
99999
165535
it depends on sizeof(int)
this is implementation-defined behavior
this is undefined behavior


[ - ]
Comment by QLNovember 2, 2022

Interesting quiz! One quick correction to the explanation section, which says "On machines where an int is 8 or 16 bits,...". The int type in C cannot be only 8-bit wide even on 8-bit CPU. This is because the C Standard says that "Basic signed integer type. Capable of containing at least the [−32,767, +32,767] range."

[ - ]
Comment by jms_nhNovember 2, 2022
ah -- thanks, I missed that detail!
[ - ]
Comment by rbonvinoNovember 7, 2022

Good point I didn't know about

[ - ]
Comment by TurbonattaNovember 2, 2022

Well, I could understand the result if the quiz was written using "int", "unsigned int", "long" and "unsigned long" which are processor dipendent but every I looked it says that uint16_t is 2 bytes and uint32_t is 4 bytes regardless the processor. I am confused.

To post reply to a comment, click on the 'reply' button attached to each comment. To post a new comment (not a reply to a comment) check out the 'Write a Comment' tab at the top of the comments.

Please login (on the right) if you already have an account on this platform.

Otherwise, please use this form to register (free) an join one of the largest online community for Electrical/Embedded/DSP/FPGA/ML engineers: