melatonin15
melatonin15

Reputation: 2269

'printf' makes pointer from integer without a cast

I am really new C and I am trying to run the following piece of code in C:

#include <stdio.h>
int main()
{
  unsigned long i = 1UL << 2;
  int j = (i==4);
  printf('%d', j);
  return 0;
}

But it's giving the error:

prog.c: In function 'main':
prog.c:6:10: warning: multi-character character constant [-Wmultichar]
   printf('%d', j);
          ^
prog.c:6:10: warning: passing argument 1 of 'printf' makes pointer from integer without a cast [-Wint-conversion]
In file included from prog.c:1:0:
/usr/include/stdio.h:362:12: note: expected 'const char * restrict' but argument is of type 'int'
 extern int printf (const char *__restrict __format, ...);

I am not sure what's wrong here. Any help?

Upvotes: 2

Views: 10280

Answers (2)

Bathsheba
Bathsheba

Reputation: 234715

'%d' is a multi-character literal, as you've enclosed more than one character in single quotation characters. Its value is implementation defined, but the C standard insists on it being an int type. (Hence the compiler diagnostic "pointer from an integer").

You want "%d" instead, that is use double quotation characters.

printf takes a const char* pointer as the first argument. Formally "%d" is a const char[3] type, but via a mechanism called pointer decay it becomes a suitable value for that first argument.

Upvotes: 3

Michael
Michael

Reputation: 3239

You can't use single quotes for a printf statement. Try this:

printf("%d", j);

Upvotes: 7

Related Questions