Topic: Q: why don't var decl. default to int?


Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Thu, 26 Jan 1995 10:49:53 GMT
Raw View
In article <3g0ib1$pj3@news.panix.com> Viktor Yurkovsky <n4mation@panix.com> writes:
>A simple question:
>
>Given that I can declare a function as:
>
> int myfun(void);
>OR
> myfun(void); // defaults to int
>

 Deprecated. And the case

 x; // declares the int x

is NOT permitted in ISO C and allowing this in C++ was an oversight
which has now been corrected.

 DO NOT WRITE CODE WITH IMPLICIT INT. Your code
may break on later compilers which have a switch to disallow
deprecated behaviour. The C++ committee may ban this behaviour
completely in the _next_ version of the ISO C++ Standard.

--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,
        81A Glebe Point Rd, GLEBE   Mem: SA IT/9/22,SC22/WG21
        NSW 2037, AUSTRALIA     Phone: 61-2-566-2189




Author: Viktor Yurkovsky <n4mation@panix.com>
Date: 23 Jan 1995 15:34:57 GMT
Raw View
A simple question:

Given that I can declare a function as:

 int myfun(void);
OR
 myfun(void); // defaults to int

Why is it that I can't do this:

 myint;  // defaults to int

or better yet,

 myint = 99; // create int var and initialize

-------------------------------
n4mation@panix.com             |
Data In Formation Inc.         |
                               |
Victor Yurkovsky               |
                               |
Compiler maker                 |
                               |
Special discounts for          |
weddings and funerals.         |
_______________________________|





Author: b91926@fsgm01.fnal.gov (David Sachs)
Date: 23 Jan 1995 12:38:37 -0600
Raw View
Viktor Yurkovsky <n4mation@panix.com> writes:

>A simple question:

>Given that I can declare a function as:

> int myfun(void);
>OR
> myfun(void); // defaults to int

>Why is it that I can't do this:

> myint;  // defaults to int

>or better yet,

> myint = 99; // create int var and initialize

I think the reason is that current compiler technology is unable
the disambiguate such situations. The general rule is that if a
statement is a syntactically valid declaration, then it is a
declaration and NOT a valid executable statment. Both the statements
you mention are valid non-declartion statement.




Author: kodis@access.digex.net (John Kodis)
Date: 24 Jan 1995 14:13:34 -0500
Raw View
David Sachs <b91926@fsgm01.fnal.gov> wrote:
>Viktor Yurkovsky <n4mation@panix.com> writes:
>>A simple question:
>>Given that I can declare a function as:
>> int myfun(void);
>>OR
>> myfun(void); // defaults to int
>>Why is it that I can't do this:
>> myint;  // defaults to int
>>or better yet,
>> myint = 99; // create int var and initialize
>
>I think the reason is that current compiler technology is unable
>the disambiguate such situations. The general rule is that if a
>statement is a syntactically valid declaration, then it is a
>declaration and NOT a valid executable statment. Both the statements
>you mention are valid non-declartion statement.

I don't think that it's a compiler limitation.  After all, in pre-ANSI
C this construct was allowed.  It wasn't ambiguous because it was
onlly allowed outside of a function body (i.e., at file scope).

More likely, it was disallowed to eliminate mistyping a global variable.
For example, given the following file-scope declaration:

    double
 alpha,
 beta;
 gamma,
 epsilon;

What is the type of gamma?  (Hint... it's not a double!).  I've actually
been bitten by this in the past, so I know that it could happen, and that
it results in some ``interesting'' program behavior.

-- John.




Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 24 Jan 1995 21:19:28 GMT
Raw View
In article bfl@fsgm01.fnal.gov, b91926@fsgm01.fnal.gov (David Sachs) writes:
>Viktor Yurkovsky <n4mation@panix.com> writes:
>
>>A simple question:
>
>>Given that I can declare a function as:
>
>> int myfun(void);
>>OR
>> myfun(void); // defaults to int
>
>>Why is it that I can't do this:
>
>> myint;  // defaults to int
>
>>or better yet,
>
>> myint = 99; // create int var and initialize
>
>I think the reason is that current compiler technology is unable
>the disambiguate such situations.

No, that is not why. The two examples
 myint;
 myint = 99;
already have a meaning, as Paul Lucas has pointed out. The first is an
expression whose value is unused, and the second is an assignment.

Suppose myint had not previously been declared. Could we not take it
as an implicit declaration of int type? We could. FORTRAN worked this way,
and we learned in the 1950's that this is a terrible idea. The Vanguard
rocket program had early failures (exploding rockets) due to uncaught
typographical errors in the Fortran source code.

Example:
 extern int safety; // default init to 0
 cout << "Enter safety factor, 0=min, 99=max: ";
 cin >> saftey;  // note typo, creates new variable
 initialize_nuclear_reactor(safety); // Is this what happened at Chernobyl?
---
Steve Clamage, stephen.clamage@eng.sun.com