473,666 Members | 2,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need clarification on a function declaration

what does the following mean?
int func2();

According to C++, it surely means func2 is a function that takes no
argument and returns an integer. But what about in C? Does it have the
same meaning or does it mean that func2 is a function that takes any
number of arguments and returns an integer?

Thanks for any clarification.

Feb 11 '07 #1
9 1657
"wizwx" <wi****@gmail.c omwrites:
int func2();

According to C++, it surely means func2 is a function that takes no
argument and returns an integer. But what about in C? Does it have the
same meaning or does it mean that func2 is a function that takes any
number of arguments and returns an integer?
The latter is roughly true, with some limitations. For example,
the function cannot take a variable number of arguments and it
cannot have parameters of type "short" or "float" or other narrow
types.
--
int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Feb 11 '07 #2
On Feb 11, 6:00 pm, Ben Pfaff <b...@cs.stanfo rd.eduwrote:
"wizwx" <wiz...@gmail.c omwrites:
int func2();
According to C++, it surely means func2 is a function that takes no
argument and returns an integer. But what about in C? Does it have the
same meaning or does it mean that func2 is a function that takes any
number of arguments and returns an integer?

The latter is roughly true, with some limitations. For example,
the function cannot take a variable number of arguments and it
cannot have parameters of type "short" or "float" or other narrow
types.
--
int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Will you please clarify what is a narrow type and what is not? Is long
a narrow type?

Feb 11 '07 #3
wizwx wrote:
On Feb 11, 6:00 pm, Ben Pfaff <b...@cs.stanfo rd.eduwrote:
"wizwx" <wiz...@gmail.c omwrites:
int func2();
According to C++, it surely means func2 is a function that takes no
argument and returns an integer. But what about in C? Does it have the
same meaning or does it mean that func2 is a function that takes any
number of arguments and returns an integer?
The latter is roughly true, with some limitations. For example,
the function cannot take a variable number of arguments and it
cannot have parameters of type "short" or "float" or other narrow
types.

Will you please clarify what is a narrow type and what is not? Is long
a narrow type?
The integer types smaller than (signed or unsigned) int, and float,
count as narrow types here. Integer types exactly as large as (signed
or unsigned) int may or may not, depending on the implementation. And
all other types don't.

Feb 12 '07 #4

"wizwx" <wi****@gmail.c omwrote in message
what does the following mean?
int func2();

According to C++, it surely means func2 is a function that takes no
argument and returns an integer. But what about in C? Does it have the
same meaning or does it mean that func2 is a function that takes any
number of arguments and returns an integer?

Thanks for any clarification.
New C code should always have function prototypes. The return values of the
function and the arguments it takes should be specified expicitly.

However to avoid breaking old code, it is permitted to evade the system by
declaring a function with an empty, as opposed to void, parameter list. In
C++ this is not allowed.
If you've got to deal with code that uses empty parameter lists you are very
unfortunate. Almost certainly you can just ignore the whole issue.
Occasionally a sloppy programmer will write

func2();

when he means

int func2(void);

The difference is that if you call the first version with parameters, the
compiler won't flag an error.

Feb 12 '07 #5
"Harald van Dijk" <tr*****@gmail. comwrites:
wizwx wrote:
[...]
>Will you please clarify what is a narrow type and what is not? Is long
a narrow type?

The integer types smaller than (signed or unsigned) int, and float,
count as narrow types here. Integer types exactly as large as (signed
or unsigned) int may or may not, depending on the implementation. And
all other types don't.
Specifically, char, signed char, unsigned char, short, and unsigned
short, and float are all narrow types. It's possible, for example,
for short and int to be the same width, but short is still a narrow
type even if so.

I don't think the standard uses the term "narrow type"; what's being
referred to is types that are affected by argument promotions.

--
Keith Thompson (The_Other_Keit h) 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 #6
Keith Thompson wrote:
"Harald van Dijk" <tr*****@gmail. comwrites:
wizwx wrote:
[...]
Will you please clarify what is a narrow type and what is not? Is long
a narrow type?
The integer types smaller than (signed or unsigned) int, and float,
count as narrow types here. Integer types exactly as large as (signed
or unsigned) int may or may not, depending on the implementation. And
all other types don't.

Specifically, char, signed char, unsigned char, short, and unsigned
short, and float are all narrow types.
As well as bool, and possibly extended integer types, which may be
made available via standard typedefs. For example, UINT_MAX ==
0x7FFFFFFF, and SIZE_MAX is 0xFFFF, then size_t must be a narrow type,
even if it is not a typedef for unsigned short (or char or unsigned
char). If UINT_MAX == 0xFFFF, and SIZE_MAX is too, then size_t may or
may not be a narrow type.
It's possible, for example,
for short and int to be the same width, but short is still a narrow
type even if so.
Thanks, I should've mentioned that.
I don't think the standard uses the term "narrow type"; what's being
referred to is types that are affected by argument promotions.
It indeed doesn't use that term. It talks about integer types with a
lower conversion rank than int, and adds float as a special case.

Feb 12 '07 #7
"wizwx" <wi****@gmail.c omwrote in message
news:11******** *************@v 45g2000cwv.goog legroups.com...
>
Will you please clarify what is a narrow type and what is not? Is long
a narrow type?
In the old days, when men were men individual responsibility was a cherished
value, a good programmer could manage function parameters himself and
wouldn't rely on the compiler to spot errors.

int func();

meant that anything could be passed so long as one understood that the
compiler was going to promote all "narrow" types (char becomes int, float
becomes double, etc.). By "narrow" type, the original poster meant "a type
that is promoted to something else when put in the argument list".

However, as programmer skill declined and new standards emerged, it was
decided that:

int func(void);

would mean no parameters.

I don't believe in function prototypes or compiler warnings. They slow down
the compiler. I've calculated that a programmer who uses function
prototypes and compiler warnings may lose, over a lifetime of programming,
as much as 5 seconds due to the compiler processing the prototypes and
issuing the warnings.

However, if you want to do the stylish thing and use them, you should never
leave an argument list empty ...

--
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
Feb 12 '07 #8
"David T. Ashley" wrote:
>
.... snip ...
>
I don't believe in function prototypes or compiler warnings. They
slow down the compiler. I've calculated that a programmer who
uses function prototypes and compiler warnings may lose, over a
lifetime of programming, as much as 5 seconds due to the compiler
processing the prototypes and issuing the warnings.
I was about to contradict you. Then I read the 3rd sentence.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

Feb 12 '07 #9
"CBFalconer " <cb********@yah oo.comwrote in message
news:45******** *******@yahoo.c om...
"David T. Ashley" wrote:
>>
... snip ...
>>
I don't believe in function prototypes or compiler warnings. They
slow down the compiler. I've calculated that a programmer who
uses function prototypes and compiler warnings may lose, over a
lifetime of programming, as much as 5 seconds due to the compiler
processing the prototypes and issuing the warnings.

I was about to contradict you. Then I read the 3rd sentence.
Well, the losses cited above are just the "first-tier" losses. There is
also:

a)Lost time typing the function prototypes into a text editor.

b)Lost time printing the function prototypes (as well as toner and paper).

c)Extra disk space consumed in version control systems.

d)Etc.

Function prototypes are really a dangerous practice.

I'm actually writing a new book about needless caution and worthless safety
devices. Other things I don't believe in:

a)Condoms.

b)Automobile safety belts and airbags.

c)Swimming pool lifeguards.

d)Safety rails on balconies and so forth.

e)Insurance.

It really is a slippery slope. Once you start believing that you might make
a mistake or that something unforeseen might occur, you just spend lots of
unnecessary money. The economic losses from this type of defective thinking
are staggering. A typical consumer is probably paying $2,000 more per car
for this kind of silliness.

--
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
Feb 13 '07 #10

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

Similar topics

3
3178
by: Anoop | last post by:
Hi guys i have a piece of code main() { switch(...) { case 1: {
2
1836
by: Karthik Kumar | last post by:
Hi, I was writing this application that used namespaces. The structure of the file was as follows. //Header file #ifndef MYNS_MYHEADER_H #define MYNS_MYHEADER_H
5
2562
by: anand | last post by:
Hi void f(); means that any type of exception may be thrown from the function. If you say void f() throw(); it means that no exceptions are thrown from a function So suppose i write a code like this
11
1782
by: Achintya | last post by:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& #include<iostream> using namespace std; class A { int i; virtual void f(){ cout<<endl<<"i= "<<i; } public: A(int j) { i = j; }
70
2783
by: rahul8143 | last post by:
hello, 1) First how following program get executed i mean how output is printed and also why following program gives different output in Turbo C++ compiler and Visual c++ 6 compiler? void main() { int val=5; printf("%d %d %d %d",val,--val,++val,val--); } under turbo compiler its giving
10
2261
by: mark | last post by:
I have this class: class Selections { OSStatus Init(); protected: CFMutableSetRef selectionObjects; static void CFASelectionsApplier(const void* value, void* ctx); OSType ready; public: Selections();
12
3872
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
3
1582
by: a | last post by:
Hi, I need clarification for virtual method and pure virtual method. e.g Class Base{ virtual void func(){ ---- } } Class Child : public Base{ void func()
13
1211
by: mdh | last post by:
FAQ 1.7 shows that, amongst other things, int i = 0; is a definition. Page 128 of K&R say: "A struct declaration defines a type. The right brace that terminates the list of members may be followed by a list of variables, just as
0
8454
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8878
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8560
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8644
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5671
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4200
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1778
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.