Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Possible C shorthand -- please help! (http://www.velocityreviews.com/forums/t665841-possible-c-shorthand-please-help.html)

almurph@altavista.com 01-16-2009 12:35 PM

Possible C shorthand -- please help!
 
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.

nick_keighley_nospam@hotmail.com 01-16-2009 12:41 PM

Re: Possible C shorthand -- please help!
 
On 16 Jan, 12:35, "almu...@altavista.com" <almu...@altavista.com>
wrote:
> 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

August Karlstrom 01-16-2009 12:43 PM

Re: Possible C shorthand -- please help!
 
almurph@altavista.com wrote:
> 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)

nick_keighley_nospam@hotmail.com 01-16-2009 12:57 PM

Re: Possible C shorthand -- please help!
 
On 16 Jan, 12:43, August Karlstrom <fusionf...@gmail.com> wrote:
> almu...@altavista.com wrote:
> > 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)


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

--
Nick Keighley

almurph@altavista.com 01-16-2009 01:43 PM

Re: Possible C shorthand -- please help!
 
On Jan 16, 12:57*pm, nick_keighley_nos...@hotmail.com wrote:
> On 16 Jan, 12:43, August Karlstrom <fusionf...@gmail.com> wrote:
>
>
>
>
>
> > almu...@altavista.com wrote:
> > > 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)

>
> there's nothing to stop you calling things methods though.
> He might be writing a compiler for something that does have
> methods.
>
> --
> Nick Keighley- Hide quoted text -
>
> - Show quoted text -


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.

Ben Bacarisse 01-16-2009 02:05 PM

Re: Possible C shorthand -- please help!
 
"almurph@altavista.com" <almurph@altavista.com> writes:
<snip>
> 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.

--
Ben.

Chris Dollin 01-16-2009 02:11 PM

Re: Possible C shorthand -- please help!
 
almurph@altavista.com wrote:

> On Jan 16, 12:57*pm, nick_keighley_nos...@hotmail.com wrote:
>> On 16 Jan, 12:43, August Karlstrom <fusionf...@gmail.com> wrote:
>> > almu...@altavista.com wrote:
>> > > 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();

>
> 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.]

--
"- born in the lab under strict supervision -", /Genetesis/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN


Flash Gordon 01-16-2009 08:52 PM

Re: Possible C shorthand -- please help!
 
blargg wrote:
> August Karlstrom wrote:
>> almurph@altavista.com wrote:
>>> 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();

>
> 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.
--
Flash Gordon

Richard 01-16-2009 09:28 PM

Re: Possible C shorthand -- please help!
 
Flash Gordon <smap@spam.causeway.com> writes:

> blargg wrote:
>> August Karlstrom wrote:
>>> almurph@altavista.com wrote:
>>>> 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();

>>
>> 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.


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

>
> extern short somevar;
> int main(void)
> {
> return somevar;
> }
>
> However, if you get rid of the extern it is correct and will work.


--
I'm not a person who particularly had heros when growing up.
- What Dennis Ritchie could potentially say when asked about the hero worship coming from c.l.c.

Kaz Kylheku 01-16-2009 10:04 PM

Re: Possible C shorthand -- please help!
 
On 2009-01-16, almurph@altavista.com <almurph@altavista.com> wrote:
> 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.


All times are GMT. The time now is 07:32 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.