473,513 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

declaring variable and then also in function prototype -- seems weird

Here is my a portion of my program:
#include <stdio.h>
main()
{
int fahr, celsius;
int lower, upper, step;

int fc_conv(int fahr);
... snip ...
}
So I first add the 'int fahr', which is a " ... declaration [that]
announces the properties of the variables; it consists of a type name
and a list of variables..." And then I declare the "... parameter type
and names, and the type of result that the function returns." It seems
almost like I am defining fahr twice. Now I understand that "the called
function is given the values of its arguments in temporary variables
rather than the originals." So this seeming second declaration is
really not, but probably because of this localized copy. But couldn't
it be assumed to be the same type as the previous declaration, or can
you in fact change the local copy to be a float or something? Just
wondering.

All quotes taken from "The C Programming Language by K & R, 2ndEd).

Jan 11 '07 #1
2 2341
vlsidesign said:
Here is my a portion of my program:
#include <stdio.h>
main()
{
int fahr, celsius;
int lower, upper, step;

int fc_conv(int fahr);
... snip ...
}
So I first add the 'int fahr', which is a " ... declaration [that]
announces the properties of the variables; it consists of a type name
and a list of variables..." And then I declare the "... parameter type
and names, and the type of result that the function returns." It seems
almost like I am defining fahr twice.
No.

int fahr; defines an int object, reserving space for it, and associating the
name 'fahr' with it within the scope of the main() function.

int fc_conv(int fahr); declares (but does not define) a function that takes
an integer expression as its only formal parameter and returns an int. You
have chosen to take advantage of the option to add a name for that
parameter within the scope of the function prototype, but you could equally
have written int fc_conv(int); if you wanted.
Now I understand that "the called
function is given the values of its arguments in temporary variables
rather than the originals."
Lies-to-programmers - i.e. a convenient shorthand which doesn't really
reflect what's going on, but which is deemed a "good enough" explanation to
get the student programmer past this point, always trusting that, later on,
they'll get a clearer idea when their knowledge of the language is
broadened.

The "lie" here is "the originals", and it's a lie because there aren't any
originals!

The argument is an *expression*, and that expression is evaluated, and the
value is passed to the function. fc_conv(42) cannot be "given" 42. It must
be given an object with the value 42. 42 itself is not an object. It is an
expression with the value 42, so that value must be stored in an object if
fc_conv() is going to use it.
So this seeming second declaration is
really not, but probably because of this localized copy. But couldn't
it be assumed to be the same type as the previous declaration,
There isn't a previous declaration. The other declaration has nothing
whatsoever to do with this one. You could have written int fc_conv(int
walrus) or int fc_conv(int celsius) or int fc_conv(int lower), or even just
int fc_conv(int) - *without changing the meaning of the program*.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 11 '07 #2

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:o_******************************@bt.com...
vlsidesign said:
Here is my a portion of my program:
#include <stdio.h>
main()
{
int fahr, celsius;
int lower, upper, step;

int fc_conv(int fahr);
... snip ...
}
So I first add the 'int fahr', which is a " ... declaration [that]
announces the properties of the variables; it consists of a type name
and a list of variables..." And then I declare the "... parameter type
and names, and the type of result that the function returns." It seems
almost like I am defining fahr twice.

No.

int fahr; defines an int object, reserving space for it, and associating
the
name 'fahr' with it within the scope of the main() function.

int fc_conv(int fahr); declares (but does not define) a function that
takes
an integer expression as its only formal parameter and returns an int. You
have chosen to take advantage of the option to add a name for that
parameter within the scope of the function prototype, but you could
equally
have written int fc_conv(int); if you wanted.
Now I understand that "the called
function is given the values of its arguments in temporary variables
rather than the originals."

Lies-to-programmers - i.e. a convenient shorthand which doesn't really
reflect what's going on, but which is deemed a "good enough" explanation
to
get the student programmer past this point, always trusting that, later
on,
they'll get a clearer idea when their knowledge of the language is
broadened.

The "lie" here is "the originals", and it's a lie because there aren't any
originals!

The argument is an *expression*, and that expression is evaluated, and the
value is passed to the function. fc_conv(42) cannot be "given" 42. It must
be given an object with the value 42. 42 itself is not an object. It is an
expression with the value 42, so that value must be stored in an object if
fc_conv() is going to use it.
So this seeming second declaration is
really not, but probably because of this localized copy. But couldn't
it be assumed to be the same type as the previous declaration,

There isn't a previous declaration. The other declaration has nothing
whatsoever to do with this one. You could have written int fc_conv(int
walrus) or int fc_conv(int celsius) or int fc_conv(int lower), or even
just
int fc_conv(int) - *without changing the meaning of the program*.
For shame Mr. Heathfield. You might give windows programmers the
idea that when they read a prototype that says something like

int SomeTypeofCallbackFunc(
int somerediculouslyLongHungarianNotationForanINT)

they don't actually have to name their local variable
somerediculouslyLongHungarianNotationForanINT

or even worse that they don't have to name their callback function
SomeTypeofCallbackFunc

Apparently the latter case was common enough that MS added a
note to their documentation for some functions like:

SomeTypeofCallbackFunc is just a placeholder for your actual
function name.

:-)
Jan 11 '07 #3

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

Similar topics

14
1859
by: lutorm | last post by:
Hi, I'm having a problem with a return statement being parsed to return a function (I think). Check here: template <typename T> class A {}; template <typename T> class maker_of_A { public:...
5
1970
by: Greg Swindle | last post by:
Hello, I have a question about how prototyping relates to variables and their scope. Given the following code: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ var ParentObject = function() {...
16
25393
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
16
1967
by: G Patel | last post by:
Hi, If I want to call functions that don't return int without declaring them, will there be any harm? I only want to assign the function(return value) to the type that it returns, so I don't...
15
2937
by: CR | last post by:
I've noticed that the trend these days is to declare variables in the middle of code instead of at the top. What is the advantage of this? It seems like it makes it hard to reuse variables. Here...
6
2077
by: dndfan | last post by:
Hello, In the short time I have spent reading this newsgroup, I have seen this sort of declaration a few times: > int > func (string, number, structure) > char* string > int number >...
3
5432
by: pelleas | last post by:
Hello to all! 1a. I'm a newbie having trouble accessing a pointer from one .cpp file which is defined within a member function found in a separate .cpp file. I'm looking for a line or two of...
10
12771
by: John Passaniti | last post by:
(Note: This is not the same message I posted a week or so ago. The problem that prevented my previous attempt to work was a silly error in the template system I was using. This is a problem...
9
2088
by: Robbie Hatley | last post by:
Greetings, group. I just found a weird problem in a program where a variable declared in a {block} after a "case" keyword was being treated as having value 0 even though its actual value should...
0
7264
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
7166
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7386
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,...
0
7543
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
7534
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...
0
5689
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5094
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1601
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.