Possible C shorthand -- please help!

A

almurph

Hi

Hope you can help me with this one. Is the following "C" code:

short somevar, MethodName();

shorthand for:

short somevar = MethodName();


where the MethodName() returns a short?


Would appreciate any comments/suggestions/explanations that you may
have.

Thanking you,
Al.
 
G

Guest

Hi
        Hope you can help me with this one. Is the following "C" code:

short somevar, MethodName();

        shorthand for:

short somevar = MethodName();

        where the MethodName() returns a short?

no. it means

short somevar;
short MethodName();

ie. the second bit is a function declaration, though not
a complete prototype. It is not used to initialise somevar
in the code given. somevar may be uninitialised, depending
if it has file scope or not.

<snip>

--
Nick Keighley

"Een schip op het strand is een baken in zee.
[A ship on the beach is a lighthouse to the sea.]"
- Dutch Proverb
 
A

August Karlstrom

Hi

Hope you can help me with this one. Is the following "C" code:

short somevar, MethodName();

shorthand for:

short somevar = MethodName();


where the MethodName() returns a short?

No, it's a shorthand for

short somevar;
short MethodName();


August


(Note that C do not have "methods", only functions)
 
G

Guest

No, it's a shorthand for

short somevar;
short MethodName();

August

(Note that C do not have "methods", only functions)

there's nothing to stop you calling things methods though.
He might be writing a compiler for something that does have
methods.
 
A

almurph

there's nothing to stop you calling things methods though.
He might be writing a compiler for something that does have
methods.

Hi,

Just to let you know - i'm converting this C code into the
equivalent C#.NET code. That's why I wanted the clarification as
MethodName() returns a short so i think I have to do somethign like
this in C#.NET:

short somevar;
short someVar = MethodName();

Thanks,
Al.
 
B

Ben Bacarisse

Just to let you know - i'm converting this C code into the
equivalent C#.NET code.

The best translations (I am talking about human language) here is
almost always by people who translate into their native tongue. A
similar rule applies to computer languages. You'll end up writing
better C# by asking C# experts how a particular C idiom should be
written in C#.
That's why I wanted the clarification as
MethodName() returns a short so i think I have to do somethign like
this in C#.NET:

short somevar;
short someVar = MethodName();

That is almost certainly wrong. If the capital V is a typo the other
variable in not needed; if not you declare a variable twice.
 
C

Chris Dollin

Just to let you know - i'm converting this C code into the
equivalent C#.NET code. That's why I wanted the clarification as
MethodName() returns a short so i think I have to do somethign like
this in C#.NET:

short somevar;
short someVar = MethodName();

If your C source /really is/

short somevar, MethodName(); // A

then you /must not/ translate it into

short somevar = MethodName(); // B

because that would not have the correct effect. The effect of
(A) is to declare that `somevar` is a short variable and that
`MethodName` is a function taking an unspecified argument list
and returning a short; it /does not/ call `MethodName()`. The
C# form (B) is, offtopically and IIRC, a declaration of `shortvar`
and a /call/ to `MethodName()`, assigning the result of the
latter to the newly-declared variable.

[This is assuming that (A) isn't from C code so old that any
kind of literal translation, into C89, C99, C++, C#, or C-side,
would be more boneheaded than a shipful of Minbari.]
 
F

Flash Gordon

blargg said:
That's still shorthand.

extern short int somevar;
extern short int MethodName( ... );

is fully longhand (drop the extern for somevar if this at function scope).

Drop the extern for somevar under ALL conditions. I.e. the following
requires a diagnostic and on many (most) implementations it will be a
fatal diagnostic.

extern short somevar;
int main(void)
{
return somevar;
}

However, if you get rid of the extern it is correct and will work.
 
R

Richard

Flash Gordon said:
Drop the extern for somevar under ALL conditions. I.e. the following
requires a diagnostic and on many (most) implementations it will be a
fatal diagnostic.

Not under all conditions at all. If somevar is in another unit then
extern is fine.
 
K

Kaz Kylheku

Hi

Hope you can help me with this one. Is the following "C" code:

short somevar, MethodName();

shorthand for:

short somevar = MethodName();

You really should be using a language reference manual for this kind ``what is
the meaning of this basic syntax'' question.

That a declaration can have multiple comma-separate declarators which
share the same ``stem'' of specifiers and qualifiers, is knowledge
so basic that one has to conclude that you hardly know C at all.

If you hardly know C, learning it through a series of reference-manual-like
queries to a Usenet newsgroup isn't the most efficient way to get started.

I would suggest goint through a tutorial book, like _The C Programming
Language_ (second edition) by Kernighan and Ritchie.
Would appreciate any comments/suggestions/explanations that you may
have.

Just the above.
 
F

Flash Gordon

Richard said:
Not under all conditions at all. If somevar is in another unit then
extern is fine.

I didn't say that it was impossible to use extern on a variable at file
scope. I was correcting the claim that that there are any conditions in
which "short int somevar" is shorthand for (i.e. means the same as)
"extern short int somevar". The code below is merely and requirement for
a diagnostic (where it was implicit that it was the entire code for the
program) was merely an example to make the point that at file scope the
"extern" changed the meaning.
 
C

CBFalconer

Hope you can help me with this one. Is the following "C" code:

short somevar, MethodName();

shorthand for:

short somevar = MethodName();

where the MethodName() returns a short?

No. I suggest you get a good book on C fundamentals, such as
K&RII, and read it.
 
R

Richard

CBFalconer said:
No. I suggest you get a good book on C fundamentals, such as
K&RII, and read it.

Q: how can you not have seen the other replies and not know that was
already recommended? You use motzarella and it is instant near enough.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top