In article <11*********************@m58g2000cwm.googlegroups. com>,
arnuld <ge*********@gmail.comwrote:
>#include <stdio.h>
int main () {
printf('hello world\n');
}
>i get this error:
[arnuld@arch programming]$ gcc hello.c
hello.c:5:10: warning: character constant too long for its type
hello.c: In function 'main':
hello.c:5: warning: passing argument 1 of 'printf' makes pointer from
integer without a cast
[arnuld@arch programming]$
>error 1, is pretty clear, gcc expects single character inside quotes
>what does error 2 mean?
(especially the mysterious "pointer from integer without a cast")
printf() always takes a string as its first argument.
A string is a pointer to a char array, with the char array needing
to be terminated with a binary 0 byte. A literal string such as
"Hi geek\n" is equivilent to using an (unnamed) pointer to a char array
that contains 'H', 'i', ' ', 'g', 'e', 'e', 'k', '\n', 0
Thus if you had coded printf("Hi geek\n") then the pointer
to that char array would have been the first argument to printf.
But you didn't code a string literal, and you didn't pass in a point
to a char: you coded a char literal instead. In C, a char literal
is the same type as an int is -- a char is a number in C.
So you passed an int in where a pointer to char was needed, and
instead of refusing to compile the program, the compiler has guessed
that you wanted to convert that int into a pointer, as if the int
happened to be the address of the char array. The compiler
warned you that what it was doing might not be what you were expecting.
The compiler guessed wrong in this case, but it isn't uncommon for
compilers to take the guess that allows them to continue compiling
instead of taking the guess that would require them to throw up
their metaphorical hands and give up on the program.
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)