K&R2 "hello world" exercise

A

arnuld

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

Walter Roberson

arnuld said:
#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.
 
G

Guest

arnuld said:
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.
 
K

Keith Thompson

arnuld said:
#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.
 
R

Rusty D. Wyr

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top