472,780 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 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 6211
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.