0

I've been trying to follow along with all the examples in the walkthrough and there's something that I can't get to work and I can't for the life of me figure out WHY I keep getting this error message. It's for week 1 in the "sizeof" example.

When I do it exactly as he does it, I get this: enter image description here

I triple checked the syntax I don't think I've made any mistakes there... So I tried a little something different, and it half worked: enter image description here

and by half I mean that for some reason it compiles "char" and "int" just fine, but not "double" and "float" enter image description here

I'm not entirely sure I understand the error message at the bottom there, but I don't see why it wouldn't do the same thing for all four when they're written exactly the same....What on earth am I doing wrong?

0

Great approach, trying other things to evaluate differences. While it is true that you "do it exactly as he does it, ". It is the computer that is doing something different. Notice the difference between the clang in the course video (below) and the clang in your terminal window

clip from sizeof walkthrough

There have been many changes to the class environment since the video was made, some very subtle like this one.

The compiler itself is giving you guidance. The operator sizeof returns a value of type size_t. size_t is defined in some header file as an unsigned long. You need to use the (suggested) format string %lu, which means long unsigned. It is basically preventing an overflow error, because sizeof() can return a value much bigger than "%i" can handle.

In the second and third examples, "%i" is a valid format string for char or integer, so the compiler does not complain about the format string. But it is not valid for d. Obviously, char c = sizeof(c) has the same potential to overflow c as the printf. I don't know the compiler internals or diagnostics well enough to explain why it is doing stricter type matching in the case of printf.

| improve this answer | |
0

When using a printf call like the ones you are doing, the format code has to match the value being printed. Otherwise, compiling will return errors like you are getting. %i is the format type to use for an integer variable. %c will print a char, but %i will print the ASCII value of a char instead. For a float, it should be %f, etc.

In the code where you were trying to use sizeof(), the function call to sizeof will return an unsigned long, so you need to use %lu.

Spend a little time and google "printf format specifiers in c" and read up on it.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .