Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > problem with sizeof in while loop reading a file

Reply
Thread Tools

problem with sizeof in while loop reading a file

 
 
Joachim Schmitz
Guest
Posts: n/a
 
      09-15-2007
"Flash Gordon" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)-gordon.me.uk...
> Joachim Schmitz wrote, On 15/09/07 10:05:
>> <(E-Mail Removed)> schrieb im Newsbeitrag
>> news:(E-Mail Removed) ups.com...
>>> On Sep 14, 9:19 pm, CBFalconer <(E-Mail Removed)> wrote:

>> <snip>
>>>> You could use "return 0" here in place of exit.
>>> I was curious about that. How do you know when to use "return 0;" vs
>>> "exit(0);" and does it make a difference?

>> Inside "main" (and inside main only) "return" is equvalent to "exit".

>
> Unless main has been called recursively.

Is that legal?

Bye, Jojo


 
Reply With Quote
 
 
 
 
Charlie Gordon
Guest
Posts: n/a
 
      09-15-2007
"Joachim Schmitz" <(E-Mail Removed)> a écrit dans le message
de news: fcg79d$e5b$(E-Mail Removed)...
> <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed) ups.com...
>> On Sep 14, 9:19 pm, CBFalconer <(E-Mail Removed)> wrote:

> <snip>
>>> You could use "return 0" here in place of exit.

>>
>> I was curious about that. How do you know when to use "return 0;" vs
>> "exit(0);" and does it make a difference?

> Inside "main" (and inside main only) "return" is equvalent to "exit".
> Slight difference: "exit" is a function, so a propor prototy needs to be
> in scope and it needs the (), "return" is a keyword and the () are
> optional.


Even in main, exit and return are not equivalent:

- the return statement merely transfers control back to the caller of
function main with the value of the exit status. If main was called by the
system runtime, it usually calls exit with the return value of main as if
run-time code was exit(main(argc, argv)).

- exit on the other hand is a function that does not return to its caller
but terminates the program with an exit status (its argument). exit can be
called from any point in the program resulting in immediate program
termination, possibly preceded by various house-keeping tasks such as
flushing stream buffers, closing files, releasing memory...

- if the main function calls itself recursively, either directly or
indirectly, whether it uses return or exit to finish its task will produce a
very different outcome: return will allow the caller to continue, exit will
cause the program to stop.

--
Chqrlie.


 
Reply With Quote
 
 
 
 
Joachim Schmitz
Guest
Posts: n/a
 
      09-15-2007
"Charlie Gordon" <(E-Mail Removed)> schrieb im Newsbeitrag
news:46ebc8b0$0$26295$(E-Mail Removed)...
> "Joachim Schmitz" <(E-Mail Removed)> a écrit dans le message
> de news: fcg79d$e5b$(E-Mail Removed)...
>> <(E-Mail Removed)> schrieb im Newsbeitrag
>> news:(E-Mail Removed) ups.com...
>>> On Sep 14, 9:19 pm, CBFalconer <(E-Mail Removed)> wrote:

>> <snip>
>>>> You could use "return 0" here in place of exit.
>>>
>>> I was curious about that. How do you know when to use "return 0;" vs
>>> "exit(0);" and does it make a difference?

>> Inside "main" (and inside main only) "return" is equvalent to "exit".
>> Slight difference: "exit" is a function, so a propor prototy needs to be
>> in scope and it needs the (), "return" is a keyword and the () are
>> optional.

>
> Even in main, exit and return are not equivalent:


from n1256:

5.1.2.2.3 Program termination

1 If the return type of the main function is a type compatible with int, a
return from the initial call to the main function is equivalent to calling
the exit function with the value returned by the main function as its
argument



So the 'initial call to' part was missing in my post.

Bye, Jojo


 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      09-15-2007
Joachim Schmitz wrote:

> "Flash Gordon" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed)-gordon.me.uk...
>> Joachim Schmitz wrote, On 15/09/07 10:05:
>>> <(E-Mail Removed)> schrieb im Newsbeitrag
>>> news:(E-Mail Removed) ups.com...
>>>> On Sep 14, 9:19 pm, CBFalconer <(E-Mail Removed)> wrote:
>>> <snip>
>>>>> You could use "return 0" here in place of exit.
>>>> I was curious about that. How do you know when to use "return 0;" vs
>>>> "exit(0);" and does it make a difference?
>>> Inside "main" (and inside main only) "return" is equvalent to "exit".

>>
>> Unless main has been called recursively.

> Is that legal?


Why shouldn't it be?

 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      09-15-2007
"santosh" <(E-Mail Removed)> schrieb im Newsbeitrag
news:fcgtb6$ai6$(E-Mail Removed)...
> Joachim Schmitz wrote:
>
>> "Flash Gordon" <(E-Mail Removed)> schrieb im Newsbeitrag
>> news:(E-Mail Removed)-gordon.me.uk...
>>> Joachim Schmitz wrote, On 15/09/07 10:05:
>>>> <(E-Mail Removed)> schrieb im Newsbeitrag
>>>> news:(E-Mail Removed) ups.com...
>>>>> On Sep 14, 9:19 pm, CBFalconer <(E-Mail Removed)> wrote:
>>>> <snip>
>>>>>> You could use "return 0" here in place of exit.
>>>>> I was curious about that. How do you know when to use "return 0;" vs
>>>>> "exit(0);" and does it make a difference?
>>>> Inside "main" (and inside main only) "return" is equvalent to "exit".
>>>
>>> Unless main has been called recursively.

>> Is that legal?

>
> Why shouldn't it be?

I though it might been forbidden by the standard. Apparently it is not.

Bye, Jojo


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-15-2007
"(E-Mail Removed)" wrote:
> CBFalconer <(E-Mail Removed)> wrote:
>

.... snip ...
>
>> You could use "return 0" here in place of exit.

>
> I was curious about that. How do you know when to use "return 0;"
> vs "exit(0);" and does it make a difference?


Use return in main. exit exits the complete program, regardless of
where called. Both can only take the arguments 0, EXIT_SUCCESS,
EXIT_FAILURE. The macros are in stdlib.h.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-15-2007
Joachim Schmitz wrote:
> "Flash Gordon" <(E-Mail Removed)> schrieb:
>

.... snip ...
>
>> Unless main has been called recursively.

>
> Is that legal?


Yes

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      09-15-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> On Sep 14, 9:19 pm, CBFalconer <(E-Mail Removed)> wrote:
>>
>> You could use "return 0" here in place of exit.

>
> I was curious about that. How do you know when to use "return 0;" vs
> "exit(0);" and does it make a difference?


It will not really make a difference.
The start-up code that gets executed right before main() might have the
equivalent of
exit(main(argc, argv));
in it.

The convention is to use 'return <status code>;' when you want to end
the program from within the main() function, and 'exit(<status code>);'
when you want to end the program from within another function.
Some people will argue that program termination should only happen from
the context of the main() function. (therefor, they won't ever use
exit)

The status codes that are guaranteed to have a meaning are
- 0 for success
- EXIT_SUCCESS for success (#include <stdlib.h>
- EXIT_FAILURE for failure (#include <stdlib.h>

>
> Lisp 9000


Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-15-2007
"Joachim Schmitz" <(E-Mail Removed)> writes:
> "Charlie Gordon" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:46ebc8b0$0$26295$(E-Mail Removed)...
>> "Joachim Schmitz" <(E-Mail Removed)> a écrit dans le message
>> de news: fcg79d$e5b$(E-Mail Removed)...
>>> <(E-Mail Removed)> schrieb im Newsbeitrag
>>> news:(E-Mail Removed) ups.com...
>>>> On Sep 14, 9:19 pm, CBFalconer <(E-Mail Removed)> wrote:
>>> <snip>
>>>>> You could use "return 0" here in place of exit.
>>>>
>>>> I was curious about that. How do you know when to use "return 0;" vs
>>>> "exit(0);" and does it make a difference?
>>> Inside "main" (and inside main only) "return" is equvalent to "exit".
>>> Slight difference: "exit" is a function, so a propor prototy needs to be
>>> in scope and it needs the (), "return" is a keyword and the () are
>>> optional.

>>
>> Even in main, exit and return are not equivalent:

>
> from n1256:
>
> 5.1.2.2.3 Program termination
>
> 1 If the return type of the main function is a type compatible with int, a
> return from the initial call to the main function is equivalent to calling
> the exit function with the value returned by the main function as its
> argument
>
> So the 'initial call to' part was missing in my post.


That statement in the standard is very nearly correct. It's possible
for 'exit(0);' and 'return 0;', executed in the initial call to main,
to behave differently, but only in very obscure and perverse
conditions.

A function can be registered with 'atexit' so that it will be invoked
when the program terminates. If you call exit() from main, such
functions will be called before the main function terminates, so any
automatically allocated objects within main will still exist. If you
return from main, any registered functions will be called after the
main function has terminated, and its local variables will have gone
out of scope.

Such a registered function cannot see main's local variables directly,
but if the address of a variable that's local to main is saved in a
file-scope pointer, then the registered function can see the variable,
even though it can't refer to it by name.

No sane programmer would do such a thing (other than as a test).

Here's something even more obscure. If the implementation allows main
to be declared as 'void main(void)', then you can register the main
function itself with atexit(). I don't think this has any bizarre
consequences; it just means main() will be called again.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      09-15-2007
In article <(E-Mail Removed)>,
Keith Thompson <(E-Mail Removed)> wrote:

>A function can be registered with 'atexit' so that it will be invoked
>when the program terminates. If you call exit() from main, such
>functions will be called before the main function terminates,


Can you cite chapter and verse for this?

According to C99 7.20.4.2, the registered functions are called at
normal program termination. According to 7.20.4.3, exit() causes
normal termination to occur. I don't see anything that prevents
exit() from causing normal termination by longjmp()ing to main()'s
caller, which would then handle the atexit() functions.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Whats the difference between while loop in Windows message loop and while(1) Uday Bidkar C++ 4 12-12-2006 12:30 PM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments