0

The program instructed in the video is about usage of sizeof(). But terminal gives 4 errors which are similar;

*the argument has type 'unsigned long'

Any ideas on why this is happening? The program should return bytes of char, double, float and int.

2

The walkthrough was filmed on the CS50 Appliance (which is a 32-bit machine). I assume you are trying to run the program using this year's CS50 IDE, which is a 64-bit machine. As such, the sizeof() function will be returning unsigned long values rather than int values. You will need to change your print format from %i or %d to %lu. I think the compile error actually says that as well.

edit


Note that if you store the size values into int variables before printing, then you can print using %i because you have implicitly cast the values to integers.

But, as the walkthrough code works, printf("%lu", sizeof(long));, for example, requires the unsigned long print format.

5
  • Made me curious. Ran the following: int main(void) { int i = sizeof(long); printf("test = %i\n",i); } and it compiled and ran fine on the web based IDE. Is it possible that there's something that might be in the setup of one env that would make your scenario happen, Kiwi? – Cliff B Jan 18 '16 at 21:29
  • You stored it in an int. The walkthrough prints sizeof(long) directly. – curiouskiwi Jan 18 '16 at 21:37
  • cliffb1:~/workspace/test $ make tst clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wshadow tst.c -lcs50 -lm -o tst ---> what's different? – Cliff B Jan 18 '16 at 21:38
  • (sorry, I edited my comment... see new comment) – curiouskiwi Jan 18 '16 at 21:39
  • And we have a winner! By directly calculating the value in the printf statement, the compiler wants the marker in the format string to exactly match the type delivered by sizeof(). Kudos to Kiwi yet again. – Cliff B Jan 18 '16 at 21:41
0

This usually means a mismatch in the types of what should be somewhere vs. what is actually there. It could be an argument given to a function or it could be an incompatible type mismatch between the result from the function and the variable where it is to be placed. For example, a function might expect an int, but is being given a float, or a function returns a long but the variable it needs to put it in is an int. It would help if you could post the code that is generating the error along with the exact error message.

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

You must log in to answer this question.

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