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

some questions on C++

dear friends,
i am a novice C++ user; all my previous experience is with fortran.
newly i started learnning C and C++. and there is few question in my
mind. I consider to post my queries here, so that experienced ppl may
help me.
1. in every function that C uses including main(), we use "return
0" (stroustroup prefers to use the variable directly in case of
functions). whta is the significance of this "0"? i checked with
return 1 and still its working fine. SO WHAT 0 MEANS IN 'RETURN 0'
STATEMENT?
2. As my foundation in fortran,(and i guess a valid understanding in C+
+ as well), function do some work of the the code; say when you need
to calculate something again and again or a seperate calculation whose
result is to be used by the main program. keeping this in mind, i am
not getting any logic of void function, which by efination, **does
not** return any value to the main(). So why on earth the VOID
FUNCTION NEEDED?

this may be very primitive question, but i ll be really greatful if
you ans them.
regards
Jun 27 '08 #1
11 1118
rudra wrote:
dear friends,
i am a novice C++ user; all my previous experience is with fortran.
newly i started learnning C and C++. and there is few question in my
mind. I consider to post my queries here, so that experienced ppl may
help me.
Welcome! Fortran -C and C++ is quite a change. Best of luck to you
in your efforts!
1. in every function that C uses including main(), we use "return
0" (stroustroup prefers to use the variable directly in case of
functions). whta is the significance of this "0"? i checked with
return 1 and still its working fine. SO WHAT 0 MEANS IN 'RETURN 0'
STATEMENT?
It depends on the environment that runs your program. The value zero
in many environments means "everything went OK" (you can think of it
as "the number of errors that has occurred"). Of course, just as many
environments _ignore_ the return value from 'main'.
2. As my foundation in fortran,(and i guess a valid understanding in
C+ + as well), function do some work of the the code; say when you
need to calculate something again and again or a seperate calculation
whose result is to be used by the main program. keeping this in mind,
i am not getting any logic of void function, which by efination,
**does not** return any value to the main(). So why on earth the VOID
FUNCTION NEEDED?
It's an abomination, I'll give you that. But think of a function
that returns 'void' (Not VOID, C++ is case-sensitive) as having the
same significance as "SUBROUTINE" in Fortran. C++ does not make any
distinction between "FUNCTION" and "SUBROUTINE", except by means of
the return value type.
>
this may be very primitive question, but i ll be really greatful if
you ans them.
regards
Hope my answers help. Keep asking.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
rudra wrote:
dear friends,
i am a novice C++ user; all my previous experience is with fortran.
newly i started learnning C and C++. and there is few question in my
mind. I consider to post my queries here, so that experienced ppl may
help me.
1. in every function that C uses including main(), we use "return
0" (stroustroup prefers to use the variable directly in case of
functions). whta is the significance of this "0"? i checked with
return 1 and still its working fine. SO WHAT 0 MEANS IN 'RETURN 0'
STATEMENT?
0 means no error returned from main, anything else is traditionally
interpreted as some kind of error condition. The only requirement is
'int' value returned from main.
2. As my foundation in fortran,(and i guess a valid understanding in C+
+ as well), function do some work of the the code; say when you need
to calculate something again and again or a seperate calculation whose
result is to be used by the main program. keeping this in mind, i am
not getting any logic of void function, which by efination, **does
not** return any value to the main(). So why on earth the VOID
FUNCTION NEEDED?
think of void function as 'subroutine' in Fortran.
>
this may be very primitive question, but i ll be really greatful if
you ans them.
regards
Fei
Jun 27 '08 #3
"Alf P. Steinbach" <al***@start.nowrites:
* rudra:
>dear friends,
i am a novice C++ user; all my previous experience is with fortran.
newly i started learnning C and C++.
<snip>
In C it's an ordinary function (although called automatically when the
program starts), whereas in C++ it's a special function that (1)
cannot be called by your code, and (2) doesn't have to explicitly
return a result value.
Tiny point: as of C99 an explicit return from main is no longer
required (though it is considered bad style to omit it).

--
Ben.
Jun 27 '08 #4
* Ben Bacarisse:
"Alf P. Steinbach" <al***@start.nowrites:
>* rudra:
>>dear friends,
i am a novice C++ user; all my previous experience is with fortran.
newly i started learnning C and C++.
<snip>
>In C it's an ordinary function (although called automatically when the
program starts), whereas in C++ it's a special function that (1)
cannot be called by your code, and (2) doesn't have to explicitly
return a result value.

Tiny point: as of C99 an explicit return from main is no longer
required (though it is considered bad style to omit it).
Thanks, I didn't know that.

But, who cares about C99?

Well, other than use of //-comments. :-)
Cheers,

- Alf
Jun 27 '08 #5
rudra wrote:
1. in every function that C uses including main(), we use "return
0" (stroustroup prefers to use the variable directly in case of
functions). whta is the significance of this "0"? i checked with
return 1 and still its working fine. SO WHAT 0 MEANS IN 'RETURN 0'
STATEMENT?
As people have already told you, not every function must return a value.

main() should return a value because the environment which launched
the program may use that value for something (by far the most usual use
for that return value is to indicate success or error).

One application where the return value of programs is completely
crucial is this: http://en.wikipedia.org/wiki/Make_(software)

(It's crucial because if a program launched by 'make' erroneously
returns an error code when it shouldn't, it will cause the build to
fail, erroneously. Also if the program fails but still returns the
success code, the building process will not notice the failure, continue
building, and all kinds of misbehavior is likely to happen.)

This is the reason why programmers not caring what their main()
returns is a really bad thing.
Jun 27 '08 #6
On Apr 14, 5:21 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
"Alf P. Steinbach" <al...@start.nowrites:
Tiny point: as of C99 an explicit return from main is no longer
required (though it is considered bad style to omit it).
It's also considered bad style to omit it in C++. If you return
from main. (Generally, in C++, it's considered bad style, and
can also cause problems, to call exit() directly, so well
written programs which terminate normally usually do return from
main.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #7
On Apr 14, 5:29 pm, "Alf P. Steinbach" <al...@start.nowrote:

[...]
But, who cares about C99?
The C++ standards committee. C compatiblity is still considered
de rigor for some aspects (preprocessor, basic types, etc.), and
that means standard C. In other words, C99. (Thus, we will be
getting extended integral types, vararg macros, etc., in the
next version of the standard.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #8
On Apr 14, 11:55 pm, Juha Nieminen <nos...@thanks.invalidwrote:

[...]
main() should return a value because the environment which launched
the program may use that value for something (by far the most usual use
for that return value is to indicate success or error).
One application where the return value of programs is completely
crucial is this:http://en.wikipedia.org/wiki/Make_(software)
Are there any applications where it isn't? Any program which
can be invoked from the command line will sooner or later end up
being invoked by something like:
prog < important temp && mv temp important
to make modifications in an existing file. And critical servers
are generally started by shell scripts, which restart them
automatically if they terminate abnormally.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #9
* James Kanze:
On Apr 14, 5:29 pm, "Alf P. Steinbach" <al...@start.nowrote:

[...]
>But, who cares about C99?

The C++ standards committee. C compatiblity is still considered
de rigor for some aspects (preprocessor, basic types, etc.), and
that means standard C. In other words, C99. (Thus, we will be
getting extended integral types, vararg macros, etc., in the
next version of the standard.)
Naturally, but in the (snipped) context of using a default 'main' result value?

Relying on C99 rules in ordinary C programming is (still) just a recipe for
disaster, except for things where C99 codified existing widespread practice.

At least for portable code.
Cheers,

- Alf

Jun 27 '08 #10
* James Kanze:
On Apr 14, 11:55 pm, Juha Nieminen <nos...@thanks.invalidwrote:

[...]
> main() should return a value because the environment which launched
the program may use that value for something (by far the most usual use
for that return value is to indicate success or error).
> One application where the return value of programs is completely
crucial is this:http://en.wikipedia.org/wiki/Make_(software)

Are there any applications where it isn't?
Yes, most of them. :-)

Any program which
can be invoked from the command line
I'm not aware of any application program that can't be invoked from the command
line.

will sooner or later end up
being invoked by something like:
prog < important temp && mv temp important
to make modifications in an existing file.
Uhm, that seems to be an environment specific point of view.

And critical servers
are generally started by shell scripts, which restart them
automatically if they terminate abnormally.
True.
Cheers, & hth.,

- Alf
Jun 27 '08 #11
On 15 avr, 12:06, "Alf P. Steinbach" <al...@start.nowrote:
* James Kanze:
On Apr 14, 5:29 pm, "Alf P. Steinbach" <al...@start.nowrote:
[...]
But, who cares about C99?
The C++ standards committee. C compatiblity is still considered
de rigor for some aspects (preprocessor, basic types, etc.), and
that means standard C. In other words, C99. (Thus, we will be
getting extended integral types, vararg macros, etc., in the
next version of the standard.)
Naturally, but in the (snipped) context of using a default
'main' result value?
Relying on C99 rules in ordinary C programming is (still) just
a recipe for disaster, except for things where C99 codified
existing widespread practice.
At least for portable code.
The same thing could be said about C++98. The "standard" way of
handling templates would involve export.

In practice, there are parts that have been widely adopted in
both cases (e.g. long long, or member templates), and others
which have been mainly ignored. Sometimes, it makes me wonder
why bother having a standard at all.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #12

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

Similar topics

2
by: Ross Micheals | last post by:
All I have some general .NET questions that I'm looking for some help with. Some of these questions (like the first) are ones that I've seen various conflicting information on, or questions that...
12
by: Vibhajha | last post by:
Hi friends, My sister is in great problem , she has this exam of C++ and she is stuck up with some questions, if friends like this group provides some help to her, she will be very grateful....
1
by: Tony Johansson | last post by:
Hello Experts! I have some questions about inheritance that I want to have an answer to. It says "Abstract superclasses define a behavioral pattern without specifying the implementation" I...
162
by: techievasant | last post by:
hello everyone, Iam vasant from India.. I have a test+interview on C /C++ in the coming month so plz help me by giving some resources of FAQS, interview questions, tracky questions, multiple...
50
by: Jatinder | last post by:
I 'm a professional looking for the job.In interview these questions were asked with some others which I answered.But some of them left unanswered.Plz help. Here are some questions on C/C++, OS...
24
by: Kevin | last post by:
Hey guys. I'm looking to get together some VB programmers on Yahoo messenger. I sit at a computer and program all day. I have about 3 or 4 people already, but it would be really cool to have a...
7
by: changs | last post by:
Hi, all I have a asm code, I suspect it sort of socket programming. Can anyone here give some instructions on how to determine the function or give the psudo-code in C? Thanks in advance! ...
3
by: iKiLL | last post by:
Hi all I am building an Windows Mobile 5 forms control in C#, for a Windows Mobile 5 application. I am using CF2.0 and SQL Mobile 2005. The control is a Questions and answer control.
4
by: Mr. X. | last post by:
Hello. I need some help, please. What can an employee ask (technical questions), if I am interviewed of Dot-net developer (also VB.NET and C#). (What are the most general questions, that I may...
30
by: GeorgeRXZ | last post by:
Hi Friends, I have some questions related to C Language. 1What is the difference between the standard C language and Non standard C language ? 2which is better C Lanugage, C under Linux/...
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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.