-3

I'm doing a college assignment in C. We have to write a simple programme that just creates different data types (ints, longs, doubles) and prints them out and their size using the sizeof() function. This is the code I have written for the function that creates an int:

void createInt(){
int i = 3;
printf(int i);
printf(sizeof(int i));
}

It's giving the following errors: For "printf(int i);" it's giving: warning C4047: 'function': 'const char *const ' differs in levels of indirection from 'int' For "printf(sizeof(int i));" it's giving: warning C4024: 'printf': different types for formal and actual parameter 1

Any help would be greatly appreciated. I'm completely new to C. Have never used it before. Thanks!

JavaChick2570
  • 15
  • 1
  • 2

2 Answers2

1

What the error means is that you're passing the wrong type of parameter to the function printf. It was declared to take a const char * as its first parameter. You're passing it an int then a size_t.

When printf takes only one parameter, that parameter must be a string. But if you want to print the content of the integer i use printf("%d", i) or the value of sizeof(i) which is of the type size_t use it like this : printf("%zu%, sizeof(i))

Mouradif
  • 2,666
  • 1
  • 20
  • 37
-2

printf needs a string as its first parameter. What you mean to do is

printf("%i\n", i);
printf("%zu\n", sizeof i);
Leo Aso
  • 11,898
  • 3
  • 25
  • 46
  • 1
    did you mean `%d` ? – Mouradif Sep 28 '17 at 14:56
  • 2
    Use `%zu` instead of `%i` for result of `sizeof`. – BLUEPIXY Sep 28 '17 at 14:57
  • nit: `sizeof i` is probably better style, as it indicates that sizeof is not a runtime function. But this is purely a stylistic point. – William Pursell Sep 28 '17 at 14:58
  • @KiJéy using either %d or %i is a bad idea. [`size_t` are supposed to be printed by `%zu`](https://stackoverflow.com/q/940087/995714). Using the [wrong format specifier invokes undefined behavior](https://stackoverflow.com/q/16864552/995714) – phuclv Sep 28 '17 at 15:05
  • Yes I meant that for the int `i`, I didn't know the existence of `%i` and I still don't know what's the difference between `%i` and `%d` – Mouradif Sep 28 '17 at 15:35