473,326 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

K&R2 "hello world" exercise

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
4 6259
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Martin Maurer | last post by:
Hello, i have a problem with NameValueCollection.Add or better with converting to a QueryString: NameValueCollection myQueryStringCollection = new NameValueCollection();...
3
by: RobertoPerez | last post by:
We have a default "Hello World" web services that we can call from VC++ creating an unmanaged DLL. The DLL is used in old languages that calls the DLL-function and that is calling the web sevice....
6
by: Rich | last post by:
Hello, I have to create a table in an Access mdb (remotely) on the fly. Create Table tbl1(fld1 Integer, fld2 varchar(10), fld3...) Then I have to insert data: Insert Into tbl1 Values(" &...
3
by: Anthony Irwin | last post by:
Hi All, I have done the horizontal version of exercise 1-13 and just wanted to make sure that I had done it right as I was not fully sure what a histogram was. I also wanted to check to see...
88
by: santosh | last post by:
Hello all, In K&R2 one exercise asks the reader to compute and print the limits for the basic integer types. This is trivial for unsigned types. But is it possible for signed types without...
82
by: arnuld | last post by:
PURPOSE :: see statement in comments GOT: Segmentation Fault I guess the segfault is sourced in the compile-time warning but I am giving a char* to the function already.
2
by: Bill H | last post by:
Everyone probably started with the infamous print "hello world" as their 1st program. I am looking for a site online that basically does the same for php & database communication. A site that walks...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.