By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,649 Members | 2,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,649 IT Pros & Developers. It's quick & easy.

K&R2 "hello world" exercise

P: n/a
i am learning C and doing the exercise 1-1 of K&R2, where K&R ask to
remove some parts of programme and experiment with error, so here i
go:

#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")

Feb 12 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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)
Feb 12 '07 #2

P: n/a
arnuld wrote:
i am learning C and doing the exercise 1-1 of K&R2, where K&R ask to
remove some parts of programme and experiment with error, so here i
go:

#include <stdio.h>

int main () {

printf('hello world\n');
}

i get this error:

[arnuld@arch programming]$ gcc hello.c
If you compile programs using gcc without any options, it does not and
does not claim to compile standard C. At the minimum, you need to use
the -ansi and -pedantic options if you're interested in a standard C
compiler.
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")
They're both really the same error.

'a' (single quotes) is an integer constant, the value of which is the
character code of a. "a" (double quotes) is a string constant, which
normally evaluates to a pointer. In this program, you need to use
double quotes.

Feb 12 '07 #3

P: n/a
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
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.
[...]

Passing an int value as an argument to a function that expects a
pointer argument (except for the special case of a null pointer
constant, which doesn't apply here) is a constraint violation. The
language does not define any implicit conversion from integers to
pointers; the only way to do the conversion is with a cast operator,
which you haven't used here. So the compiler would have been within
its rights to reject your program with a fatal error message.

This kind of error is called a "constraint violation". The standard
requires a diagnostic for any constraint violation; it doesn't
actually require that diagnostic to be a fatal error. So gcc's
behavior here (issuing a warning and performing a conversion anyway)
is actually conforming.

The lesson here is that you need to pay as much attention to warnings
as to error messages; the standard doesn't make any distinction
between them.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 12 '07 #4

P: n/a
On 12 Feb 2007 08:51:14 -0800, "arnuld" <ge*********@gmail.comwrote:
>i am learning C and doing the exercise 1-1 of K&R2, where K&R ask to
remove some parts of programme and experiment with error, so here i
go:

#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")
Single quotes are not the same as double quotes. A single quote
contains a single character. printf expects a string as a first
argument - doesn't have to end in explicit '\n'.

Try the following:

printf ("Hello, world.");
printf ("Hello, world.\n");

Try this:
fputs ("Hello, world", stdout);
fputc ('\n', stdout);

See the difference?
Feb 13 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.