473,394 Members | 1,841 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,394 software developers and data experts.

hello world programme

i compiled the "hello world" programme from K&R2:

#include<stdio.h>

int main() {

printf("hello world\n");

}
now i compiled it using: /gcc hello.c/

eveything went fine and programme ran.

when i compiled using:

--------------------------------------------------
[arnuld@arch programming]$ gcc -ansi -pedantic -Wall -Wextra hello.c
hello.c: In function 'main':
hello.c:6: warning: control reaches end of non-void function
[arnuld@arch programming]$
-------------------------------------------------

this time programme runs too but what does that "warning mean"?

Feb 12 '07 #1
18 2118
In article <11**********************@h3g2000cwc.googlegroups. com>,
arnuld <ge*********@gmail.comwrote:
>i compiled the "hello world" programme from K&R2:
>#include<stdio.h>
int main() {
printf("hello world\n");
}
>when i compiled using:
>--------------------------------------------------
[arnuld@arch programming]$ gcc -ansi -pedantic -Wall -Wextra hello.c
hello.c: In function 'main':
hello.c:6: warning: control reaches end of non-void function
[arnuld@arch programming]$
>this time programme runs too but what does that "warning mean"?
You declared main() as returning an int, but you failed to use
return to return a value from main.

--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Feb 12 '07 #2
"arnuld" <ge*********@gmail.comwrites:
hello.c:6: warning: control reaches end of non-void function

this time programme runs too but what does that "warning mean"?
The main function should return a value, but you didn't provide a
return statement.
--
"I should killfile you where you stand, worthless human." --Kaz
Feb 12 '07 #3
--------------------------------------------------
[arnuld@arch programming]$ gcc -ansi -pedantic -Wall -Wextra hello.c
hello.c: In function 'main':
hello.c:6: warning: control reaches end of non-void function
[arnuld@arch programming]$
this time programme runs too but what does that "warning mean"?

You declared main() as returning an int, but you failed to use
return to return a value from main.
so ANSI C requires it.

If you lie to the compiler, it will get its revenge. -- Henry Spencer
Ha....Ha.... that's means my program

Feb 12 '07 #4
In article <11*********************@m58g2000cwm.googlegroups. com>,
arnuld <ge*********@gmail.comwrote:
>--------------------------------------------------
[arnuld@arch programming]$ gcc -ansi -pedantic -Wall -Wextra hello.c
hello.c: In function 'main':
hello.c:6: warning: control reaches end of non-void function
>You declared main() as returning an int, but you failed to use
return to return a value from main.
>so ANSI C requires it.
No, main is special. In C89, failure to return a value results in
undefined behaviour -- the operating system might pick up any random
return status, and the operating system might do strange things
with some of the odd statuses.

In C99, failure to return a value from main is the same as returning 0.
However, in C99, you cannot define functions with the empty parameter
list; you would need int main(void) I believe.

Notice that what you got was a warning, not an error: a warning
is a note from the compiler to the effect of "Usually when I see
this pattern of code, a mistake has been made or something has been
overlooked, but you are the boss so I've gone ahead and compiled it
anyhow. I don't promise that what I interpreted it as is the
same as what you were expecting it to do, so you should probably
check this bit of code over carefully!"
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Feb 12 '07 #5
arnuld wrote:
i compiled the "hello world" programme from K&R2:

#include<stdio.h>

int main() {

printf("hello world\n");

}
now i compiled it using: /gcc hello.c/

eveything went fine and programme ran.

when i compiled using:

--------------------------------------------------
[arnuld@arch programming]$ gcc -ansi -pedantic -Wall -Wextra hello.c
hello.c: In function 'main':
hello.c:6: warning: control reaches end of non-void function
[arnuld@arch programming]$
-------------------------------------------------

this time programme runs too but what does that "warning mean"?

It's telling you that you didn't return a value from a function that is
expected to do so. In C89, I believe this was legal for any function to
hit the end brace, which was the equivalent of a return with no
expression. That was legal under that standard, although undefined
behavior to use the return value.

In C99, they tightened that up and it's not legal to do any of that for
most functions, however there is an explicit exception for main() so
that it is equivalent to returning 0.

At any rate, the implementation is allowed to issue any diagnostics it
likes. As not having the return there may be an indication that the
programmer forgot, it's useful. For C89 implementations, that
indeterminant return value to the host system could be a problem.

Easy and desirable fix, add in at the end:

return 0;


Brian
Feb 12 '07 #6
Walter Roberson said:

<snip>
However, in C99, you cannot define functions with the empty
parameter list;
Chapter and verse, please. I think you're getting mixed up with implicit
int being dropped.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 12 '07 #7
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <11*********************@m58g2000cwm.googlegroups. com>,
arnuld <ge*********@gmail.comwrote:
>>--------------------------------------------------
[arnuld@arch programming]$ gcc -ansi -pedantic -Wall -Wextra hello.c
hello.c: In function 'main':
hello.c:6: warning: control reaches end of non-void function
>>You declared main() as returning an int, but you failed to use
return to return a value from main.
>>so ANSI C requires it.

No, main is special. In C89, failure to return a value results in
undefined behaviour -- the operating system might pick up any random
return status, and the operating system might do strange things
with some of the odd statuses.
No, it merely causes an unspecified status to be returned to the
calling environment.

[snip]

--
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 #8
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
>No, main is special. In C89, failure to return a value results in
undefined behaviour -- the operating system might pick up any random
return status, and the operating system might do strange things
with some of the odd statuses.
>No, it merely causes an unspecified status to be returned to the
calling environment.
I just croschecked C89, and the wording is that the status value
returned is "undefined". So we are into the nitty gritty of exactly
what "undefined behaviour" is.

If it had used the wording you used, that the value was "unspecified",
the implication would be that there was -some- value that would be
returned -- possibly not a constant value, but that it'll get
-something-.

But as the wording says "undefined", I would take that as license for
the compiler to do something like attempt to grab a value from where it
-expects- a return value would be, and if the grabbing happens
upon a trapping value, then a trap could happen.

Hypothesize an implementation in which the ABI was such that
parameters were returned by setting a register to a pointer
to a return area (useful for passing back large structs, for example.)
It could be, even, that the calling routine was responsible for
allocating space for the return parameters and passing in that
pointer to the called routine, and that the called routine was
expected to save and return that pointer. Then the C 'return'
statement could be translated [in this scheme] into retrieving that
saved pointer, saving the return value there, and returning the pointer.
But in this scheme, if there was no 'return' then the register
used to return the pointer might get left in a random state,
possibly holding a value that, if interpreted as an address, would
result in a page fault. It seems to me that the "undefined"
wording of C89 would allow the fault -- i.e., "undefined behaviour",
whereas if the value is merely "unspecified" then although garbage
might get returned, a fault at that point would not be valid.
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
Feb 12 '07 #9
On Feb 13, 5:09 am, Richard Heathfield <r...@see.sig.invalidwrote:
Walter Roberson said:

<snip>
However, in C99, you cannot define functions with the empty
parameter list;

Chapter and verse, please. I think you're getting mixed up with
implicit int being dropped.
Or possibly 6.11.6p1...

The use of function declarators with empty parentheses (not
prototype-format parameter type declarators) is an obsolescent
feature.

This was deprecated in C90 and remains so in C99. Despite the
warnings, it's unlikely to ever be removed from C. What is slightly
more likely is that C will ultimately follow C++'s rule that an
empty parameter list will be a prototype for a function taking
no parameters. When I say slightly more likely, I still think the
removal is highly unlikely since there are quite a few C programs
that actually rely on the current behaviour, whether the construct
is deprecated or otherwise.

--
Peter

Feb 12 '07 #10
Walter Roberson wrote:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
No, main is special. In C89, failure to return a value results in
undefined behaviour -- the operating system might pick up any random
return status, and the operating system might do strange things
with some of the odd statuses.
No, it merely causes an unspecified status to be returned to the
calling environment.

I just croschecked C89, and the wording is that the status value
returned is "undefined". So we are into the nitty gritty of exactly
what "undefined behaviour" is.
Is the return status undefined, or is the behaviour undefined? Only if
the latter are all bets off.

Feb 12 '07 #11
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>>ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
>>No, main is special. In C89, failure to return a value results in
undefined behaviour -- the operating system might pick up any random
return status, and the operating system might do strange things
with some of the odd statuses.
>>No, it merely causes an unspecified status to be returned to the
calling environment.

I just croschecked C89, and the wording is that the status value
returned is "undefined". So we are into the nitty gritty of exactly
what "undefined behaviour" is.
Yes, that's a bit tricky. The standard defines the phrase "undefined
behavior", but not the word "undefined". If this hadn't been
superseded by C99, I'd argue that the wording needs to be cleaned up.
If it had used the wording you used, that the value was "unspecified",
the implication would be that there was -some- value that would be
returned -- possibly not a constant value, but that it'll get
-something-.

But as the wording says "undefined", I would take that as license for
the compiler to do something like attempt to grab a value from where it
-expects- a return value would be, and if the grabbing happens
upon a trapping value, then a trap could happen.
There's no such thing as a "trapping value". A "trap representation",
by definition, does not represent a value at all. My interpretation
is that the "undefined value" returned by "int main(void){}" cannot be
a trap representation.

This means, I suppose, that a conforming C90 implementation that
returns the contents of some arbitrary location would have to take
care that that location doesn't contain a trap representation -- but
this restriction has no effect on the vast majority of implementations
on which type int has no trap representations.

[...]

--
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 #12
Harald van Dijk wrote:
Walter Roberson wrote:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
No, it merely causes an unspecified status to be returned to the
calling environment.
I just croschecked C89, and the wording is that the status value
returned is "undefined". So we are into the nitty gritty of exactly
what "undefined behaviour" is.

Is the return status undefined, or is the behaviour undefined? Only if
the latter are all bets off.
I think the behavior of the host system is always undefined. The only
thing the standard discusses is the return value itself.


Brian
Feb 12 '07 #13
Harald van D?k said:
Walter Roberson wrote:
>In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
>No, main is special. In C89, failure to return a value results in
undefined behaviour -- the operating system might pick up any
random return status, and the operating system might do strange
things with some of the odd statuses.
>No, it merely causes an unspecified status to be returned to the
calling environment.

I just croschecked C89, and the wording is that the status value
returned is "undefined". So we are into the nitty gritty of exactly
what "undefined behaviour" is.

Is the return status undefined, or is the behaviour undefined? Only if
the latter are all bets off.
The former, so all bets are back on. We did this discussion a few years
ago. (I seem to recall that I lost.)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 13 '07 #14
Peter Nilsson said:

<snip>
[...] 6.11.6p1...

The use of function declarators with empty parentheses (not
prototype-format parameter type declarators) is an obsolescent
feature.

This was deprecated in C90 and remains so in C99. Despite the
warnings, it's unlikely to ever be removed from C.
I'm delighted to hear you say so, because if it did happen it would
effectively outlaw generic function pointers. Bad plan, ISO! No
biscuit!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 13 '07 #15
At about the time of 2/12/2007 8:40 AM, arnuld stated the following:
i compiled the "hello world" programme from K&R2:

#include<stdio.h>

int main() {

printf("hello world\n");

}
now i compiled it using: /gcc hello.c/

eveything went fine and programme ran.

when i compiled using:

--------------------------------------------------
[arnuld@arch programming]$ gcc -ansi -pedantic -Wall -Wextra hello.c
hello.c: In function 'main':
hello.c:6: warning: control reaches end of non-void function
[arnuld@arch programming]$
-------------------------------------------------

this time programme runs too but what does that "warning mean"?
The warning is a diagnostic message from the compiler that indicates
that a function that was defined as returning a value isn't.

You need to be *VERY* careful with this. Some operating systems will do
strange things depending on what it gets back. One case in point was
returning some arbitrary negative value that caused the ELF loader to
die with a segmentation fault, which then forced the kernel into a panic
situation.

This demonstrated a bug in the underlying operating system, but it also
demonstrates that if you define a function as returning a value, then
return a value, even if it's 0 (or NULL in the case of a pointer).

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Feb 13 '07 #16
On Tue, 13 Feb 2007 06:44:24 +0000, in comp.lang.c , Richard
Heathfield <rj*@see.sig.invalidwrote:
>Peter Nilsson said:

<snip>
>[...] 6.11.6p1...

The use of function declarators with empty parentheses (not
prototype-format parameter type declarators) is an obsolescent
feature.

This was deprecated in C90 and remains so in C99. Despite the
warnings, it's unlikely to ever be removed from C.

I'm delighted to hear you say so, because if it did happen it would
effectively outlaw generic function pointers.
Would it? Isn't there a difference between a function declarator and a
pointer to a function?
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Feb 13 '07 #17
Mark McIntyre said:
On Tue, 13 Feb 2007 06:44:24 +0000, in comp.lang.c , Richard
Heathfield <rj*@see.sig.invalidwrote:
>>Peter Nilsson said:

<snip>
>>[...] 6.11.6p1...

The use of function declarators with empty parentheses (not
prototype-format parameter type declarators) is an obsolescent
feature.

This was deprecated in C90 and remains so in C99. Despite the
warnings, it's unlikely to ever be removed from C.

I'm delighted to hear you say so, because if it did happen it would
effectively outlaw generic function pointers.

Would it? Isn't there a difference between a function declarator and a
pointer to a function?
Function pointers are created using function declarators. See 3.5.4.3 or
its C99 equivalent.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 14 '07 #18
On 12 Feb 2007 17:59:04 GMT, "Default User" <de***********@yahoo.com>
wrote:
arnuld wrote:
<snip>
hello.c:6: warning: control reaches end of non-void function
It's telling you that you didn't return a value from a function that is
expected to do so. In C89, I believe this was legal for any function to
hit the end brace, which was the equivalent of a return with no
expression. That was legal under that standard, although undefined
behavior to use the return value.
.... in the C code; since the initial call of main is (somehow) from
the environment it instead gives undefined termination status. (This
is still a bad thing, it's just a different bad thing from UB.)
In C99, they tightened that up and it's not legal to do any of that for
most functions, however there is an explicit exception for main() so
that it is equivalent to returning 0.
It is still legal but undesirable to hit the end brace of a non-void
function, which returns indeterminate value and using it is UB. What
changed is that an actual return statement in a non-void function can
no longer omit the expression (of or convertible to the return type).

And as you say, as a special case, end brace of main returns 0.
Actually this is only required _for the initial call_, but recursive
calls to main are almost always a bad idea anyway.

<snip rest>

- formerly david.thompson1 || achar(64) || worldnet.att.net
Feb 26 '07 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: NDAKI MBOULET | last post by:
J'ai un problème pour écrire un programme. Voici mon sujet: Ecrire en c++ un programme qui reçoit en entrée une suite d'instruction encadrées par les mots clés BIBODLE et LISUK dans un langage...
6
by: RC | last post by:
Hello World, I am try do call a JavaScript function from XSLT, but I got function not avaible error. See "????" below. Would someone out there tell me how? Thank Q! <xsl:stylesheet...
31
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
8
by: vijay | last post by:
Hello, As the subject suggests, I need to print the string in the reverse order. I made the following program: # include<stdio.h> struct llnode { char *info;
4
by: arnuld | last post by:
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...
1
by: James T. Dennis | last post by:
You'd think that using things like gettext would be easy. Superficially it seems well documented in the Library Reference(*). However, it can be surprisingly difficult to get the external details...
6
by: =?ISO-8859-1?Q?FERHAT_A=C7ICI?= | last post by:
hi everyone...I want to run any programme or file with my programme.example any file or programme, like this "xxx.ncn".I want If users double click this xxx.ncn this file can run by my...
11
by: cj | last post by:
Perhaps someone knows how to do this. When I open a new ASP.NET web service application in VS 2008, it opens with a simple Hello World web service already written. I want to see this work. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.