Possible C shorthand -- please help!

Discussion in 'C Programming' started by almurph@altavista.com, Jan 16, 2009.

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


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

    Thanking you,
    Al.
     
    , Jan 16, 2009
    #1
    1. Advertising

  2. Guest

    On 16 Jan, 12:35, "" <>
    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
     
    , Jan 16, 2009
    #2
    1. Advertising

  3. 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)
     
    August Karlstrom, Jan 16, 2009
    #3
  4. Guest

    On 16 Jan, 12:43, August Karlstrom <> wrote:
    > 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
     
    , Jan 16, 2009
    #4
  5. Guest

    On Jan 16, 12:57 pm, wrote:
    > On 16 Jan, 12:43, August Karlstrom <> wrote:
    >
    >
    >
    >
    >
    > > 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.
     
    , Jan 16, 2009
    #5
  6. "" <> 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.
     
    Ben Bacarisse, Jan 16, 2009
    #6
  7. Chris Dollin Guest

    wrote:

    > On Jan 16, 12:57 pm, wrote:
    >> On 16 Jan, 12:43, August Karlstrom <> wrote:
    >> > 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
     
    Chris Dollin, Jan 16, 2009
    #7
  8. Flash Gordon Guest

    blargg wrote:
    > August Karlstrom wrote:
    >> 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
     
    Flash Gordon, Jan 16, 2009
    #8
  9. Richard Guest

    Flash Gordon <> writes:

    > blargg wrote:
    >> August Karlstrom wrote:
    >>> 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.
     
    Richard, Jan 16, 2009
    #9
  10. Kaz Kylheku Guest

    On 2009-01-16, <> 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.
     
    Kaz Kylheku, Jan 16, 2009
    #10
  11. Flash Gordon Guest

    Richard wrote:
    > Flash Gordon <> writes:
    >
    >> blargg wrote:
    >>> August Karlstrom wrote:
    >>>> 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.


    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.

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

    --
    Flash Gordon
     
    Flash Gordon, Jan 16, 2009
    #11
  12. CBFalconer Guest

    "" wrote:
    >
    > 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.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Jan 17, 2009
    #12
  13. Richard Guest

    CBFalconer <> writes:

    > "" wrote:
    >>
    >> 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.


    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.
     
    Richard, Jan 17, 2009
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. foreach shorthand

    , Mar 25, 2005, in forum: C++
    Replies:
    2
    Views:
    647
    =?iso-8859-1?Q?Ali_=C7ehreli?=
    Mar 25, 2005
  2. David W
    Replies:
    0
    Views:
    992
    David W
    Jan 17, 2007
  3. Foxpointe

    Shorthand for namespaces

    Foxpointe, Oct 31, 2006, in forum: XML
    Replies:
    4
    Views:
    455
    Foxpointe
    Oct 31, 2006
  4. Replies:
    7
    Views:
    427
  5. Replies:
    5
    Views:
    2,185
Loading...

Share This Page