Variable declaration - Is this valid in standard C?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kannan

    #1

    Variable declaration - Is this valid in standard C?

    Its been a while I have done pure C programming (I was coding in C++).

    Is the following function valid according to standard C?

    int main(int argc, char *argv[])
    {
    int x;
    x = 9;
    printf("Value of x is: %d\n", x);

    int i; /* <==== Is this declaration valid?*/

    for(i=0; i < 10; i++)
    printf("Value of i is: %d\n", i);

    }

    Its compiling in gcc (ver 3.4.0) and cc (ver 6). For gcc I tried the to
    compile with "-x c" and "-ansi" option (gcc -x c -ansi <sourcefile.c >)
    the result is same. What I remember from when I was doing C programs
    actively was this will give you a compile error.

    Please tell me what is the standard about this?


    I have one more question

    If I declare the variable "i" in "for" loop like this
    ............... ........
    for(int i=0; i < 10; i++)
    ............... ............... .

    Then
    1. When I compile with gcc (Command line $gcc <sourcefile.c >) gives me
    an error "10: error: 'for' loop initial declaration used outside C99
    mode".

    2. When I compile with cc (command line $cc <sourcefile.c >) its
    compiling and producing the binary executable.

    I am bit confused here !?!?
    Any comments appreciated.

    Thank you for your time.
    -Kannan

  • Walter Roberson

    #2
    Re: Variable declaration - Is this valid in standard C?

    In article <1154365881.203 751.238470@i3g2 000cwc.googlegr oups.com>,
    Kannan <riderchap@gmai l.comwrote:
    >Is the following function valid according to standard C?
    >int main(int argc, char *argv[])
    >{
    int x;
    x = 9;
    printf("Value of x is: %d\n", x);
    int i; /* <==== Is this declaration valid?*/
    Not in C89, but Yes in C99.
    for(i=0; i < 10; i++)
    printf("Value of i is: %d\n", i);
    >}
    >Its compiling in gcc (ver 3.4.0) and cc (ver 6). For gcc I tried the to
    >compile with "-x c" and "-ansi" option (gcc -x c -ansi <sourcefile.c >)
    >the result is same. What I remember from when I was doing C programs
    >actively was this will give you a compile error.
    specific gcc options are off-topic, but try -std=c89

    >I have one more question
    >If I declare the variable "i" in "for" loop like this
    >.............. ........
    >for(int i=0; i < 10; i++)
    >.............. ............... .
    >Then
    >1. When I compile with gcc (Command line $gcc <sourcefile.c >) gives me
    >an error "10: error: 'for' loop initial declaration used outside C99
    >mode".
    A correct diagnostic.

    >2. When I compile with cc (command line $cc <sourcefile.c >) its
    >compiling and producing the binary executable.
    >I am bit confused here !?!?
    cc on your [unnamed] platform might default to c99, or might
    default to supporting those kinds of declarations as a nonstandard
    extension.
    --
    "No one has the right to destroy another person's belief by
    demanding empirical evidence." -- Ann Landers

    Comment

    • Richard Heathfield

      #3
      Re: Variable declaration - Is this valid in standard C?

      Kannan said:
      Its been a while I have done pure C programming (I was coding in C++).
      >
      Is the following function valid according to standard C?
      >
      int main(int argc, char *argv[])
      {
      int x;
      x = 9;
      printf("Value of x is: %d\n", x);
      >
      int i; /* <==== Is this declaration valid?*/
      Yes and no!

      In C as standardised in 1989 by ANSI and adopted by ISO in 1990, the answer
      is "no". But the language was revised in 1999, and this was made legal. On
      the other hand, not all compilers - in fact, hardly any - fully conform to
      the new standard, and so the most portable thing to do is to avoid such a
      usage, at least for the time being.
      I have one more question
      >
      If I declare the variable "i" in "for" loop like this
      ............... .......
      for(int i=0; i < 10; i++)
      Same applies - see above.

      --
      Richard Heathfield
      "Usenet is a strange place" - dmr 29/7/1999

      email: rjh at above domain (but drop the www, obviously)

      Comment

      • jacob navia

        #4
        Re: Variable declaration - Is this valid in standard C?

        Richard Heathfield wrote:
        >
        In C as standardised in 1989 by ANSI and adopted by ISO in 1990, the answer
        is "no". But the language was revised in 1999, and this was made legal. On
        the other hand, not all compilers - in fact, hardly any - fully conform to
        the new standard, and so the most portable thing to do is to avoid such a
        usage, at least for the time being.
        >
        To the original poster:

        Please diregard this nonsense. Many compilers support c99,
        gcc supports it with the command line option
        -std=c99

        Note that standard C accepts this. Notable exception is
        microsoft, that refuses the current standard. But you haven't
        this problem since gcc supports it.

        jacob

        Comment

        • Walter Roberson

          #5
          Re: Variable declaration - Is this valid in standard C?

          In article <44ce4eae$0$990 $ba4acef3@news. orange.fr>,
          jacob navia <jacob@jacob.re mcomp.frwrote:
          >Richard Heathfield wrote:
          >In C as standardised in 1989 by ANSI and adopted by ISO in 1990, the answer
          >is "no". But the language was revised in 1999, and this was made legal. On
          >the other hand, not all compilers - in fact, hardly any - fully conform to
          >the new standard, and so the most portable thing to do is to avoid such a
          >usage, at least for the time being.
          >To the original poster:
          >Please diregard this nonsense. Many compilers support c99,
          >gcc supports it with the command line option
          >-std=c99
          Richard said "fully conform". gcc's c99 does not fully conform.

          >Note that standard C accepts this. Notable exception is
          >microsoft, that refuses the current standard. But you haven't
          >this problem since gcc supports it.
          In that last sentance, "it" can only refer to "the current standard"
          and that in turn must refer back to "standard C". The C99 standard does
          accept the construct, but it is not the case that gcc fully supports the
          C99 standard. Richard is warning that it is not presently a good idea to
          rely on C99 extensions, because most compilers [including gcc] that
          -claim- C99 support are broken in various respects.
          --
          Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson

          Comment

          • Ancient_Hacker

            #6
            Re: Variable declaration - Is this valid in standard C?

            int main(int argc, char *argv[])
            {
            int x;
            x = 9;
            printf("Value of x is: %d\n", x);
            >
            int i; /* <==== Is this declaration valid?*/

            I *think* it's okay in C *if* it's at the beginning of a block. So if
            you write:

            int main(int argc, char *argv[])
            {
            int x;
            x = 9;
            printf("Value of x is: %d\n", x);
            >
            { int i; blah blah blah statements using "i" }
            then that would be okay. Note that the {}'s don't have to be "useful",
            as in delimiting a compound statement. They're threre just to delimit
            the scope of the declarations.

            Comment

            • Stephen Sprunk

              #7
              Re: Variable declaration - Is this valid in standard C?

              "Kannan" <riderchap@gmai l.comwrote in message
              news:1154365881 .203751.238470@ i3g2000cwc.goog legroups.com...
              Its been a while I have done pure C programming (I was coding in
              C++).
              >
              Is the following function valid according to standard C?
              >
              int main(int argc, char *argv[])
              {
              int x;
              x = 9;
              printf("Value of x is: %d\n", x);
              >
              int i; /* <==== Is this declaration valid?*/
              >
              for(i=0; i < 10; i++)
              printf("Value of i is: %d\n", i);
              >
              }
              >
              Its compiling in gcc (ver 3.4.0) and cc (ver 6). For gcc I tried the
              to
              compile with "-x c" and "-ansi" option (gcc -x c -ansi
              <sourcefile.c >)
              the result is same. What I remember from when I was doing C programs
              actively was this will give you a compile error.
              >
              Please tell me what is the standard about this?
              It's legal in C99, but not in C89.

              <OT>However, by default GCC accepts it even when run with "-ansi" or
              "-std=c89"; you need "-pedantic" to turn off many GNU extensions such
              as this one.</OT>
              I have one more question
              >
              If I declare the variable "i" in "for" loop like this
              ............... .......
              for(int i=0; i < 10; i++)
              ............... ...............
              >
              Then
              1. When I compile with gcc (Command line $gcc <sourcefile.c >) gives
              me an error "10: error: 'for' loop initial declaration used outside
              C99
              mode".
              That is correct; that is not legal C89.

              <OT>GCC needs -std=c99 to accept this code. As to why GCC accepts the
              former by default but not the latter, you'll have to ask over in
              gnu.gcc.help.</OT>
              2. When I compile with cc (command line $cc <sourcefile.c >) its
              compiling and producing the binary executable.
              >
              I am bit confused here !?!?
              It appears your "cc" either defaults to C99 mode or has an extension
              which allows that which is on by default. Figure out how to run it in
              C89 mode and it should complain too.

              In general, if you're sure your code will only be used on platforms
              that have a reasonably-conforming C99 compiler, feel free to use the
              new features (such as this one). However, if you're going to be
              distributing your code widely, it's best to stick to C89.

              S

              --
              Stephen Sprunk "God does not play dice." --Albert Einstein
              CCIE #3723 "God is an inveterate gambler, and He throws the
              K5SSS dice at every possible opportunity." --Stephen Hawking



              --
              Posted via a free Usenet account from http://www.teranews.com

              Comment

              • Keith Thompson

                #8
                Re: Variable declaration - Is this valid in standard C?

                jacob navia <jacob@jacob.re mcomp.frwrites:
                Richard Heathfield wrote:
                >In C as standardised in 1989 by ANSI and adopted by ISO in 1990, the
                >answer is "no". But the language was revised in 1999, and this was
                >made legal. On the other hand, not all compilers - in fact, hardly
                >any - fully conform to the new standard, and so the most portable
                >thing to do is to avoid such a usage, at least for the time being.
                >
                To the original poster:
                >
                Please diregard this nonsense. Many compilers support c99,
                gcc supports it with the command line option
                -std=c99
                To the original poster:

                Richard is entirely correct; jacob is wrong.

                Many compilers support *some* C99 features. gcc with its "-std=c99"
                option supports some, but not all, of C99; see
                <http://gcc.gnu.org/c99status.htmlf or details. (The server seems to
                be down at the moment.)

                The ability to declare a variable in a for loop happens to be one of
                the C99 features that gcc does support, and it's fairly likely that
                any compiler that supports *some* C99 features will support that one,
                along with "//" comments and mixed declarations and statements.

                But if you want your code to be maximally portable, it needs to be
                portable to compilers that support only C90. In this case, it's very
                easy to do; just move the variable declaration outside the for loop
                (and before any other statements).

                Being able to localize the control variable of a for loop is a nice
                feature, but it's not universally supported.

                If the convenience of being able to use a few C99-specific features is
                more important to you than the ability to compile your code with
                compilers that don't support those features, that's fine; use them in
                good health. You should just be aware of the tradeoffs.

                (I honestly don't know why jacob has such trouble understanding this,
                or at least cknowledging it.)

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                We must do something. This is something. Therefore, we must do this.

                Comment

                • Keith Thompson

                  #9
                  Re: Variable declaration - Is this valid in standard C?

                  "Ancient_Hacker " <grg2@comcast.n etwrites:
                  >int main(int argc, char *argv[])
                  >{
                  > int x;
                  > x = 9;
                  > printf("Value of x is: %d\n", x);
                  >>
                  > int i; /* <==== Is this declaration valid?*/
                  >
                  >
                  I *think* it's okay in C *if* it's at the beginning of a block.
                  In C90, a block (the thing delimited by the '{' and '}' characters)
                  contains zero or more declarations followed by zero or more
                  statements; a declaration following a statement is illegal. In C99,
                  you can freely mix declarations and statements within a block. C99 is
                  not yet universally supported.

                  <OT>C++ also allows declarations and statements to be mixed. Some C
                  programmers make the mistake of compiling their code with a C++
                  compiler, which can mask some errors.</OT>

                  (Ancient_Hacker : There's no need to guess; plenty of people here
                  actually know this stuff.)

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                  We must do something. This is something. Therefore, we must do this.

                  Comment

                  • jacob navia

                    #10
                    Re: Variable declaration - Is this valid in standard C?

                    Please:
                    The question was

                    Is this valid in standard C?

                    And I answer

                    YES it is valid in standard C!!!!

                    And if your compiler is BROKEN and doesn't support standard C
                    use another one!

                    The original poster did not mention portability to old broken
                    stuff, and it did not mention C370, or C90 or C80 or C70!!!

                    And yes, C89 is not portable to old PDP 11 simulators.

                    This group is always trying to destroy C and to deny
                    all progress done since more than 10 years of standard
                    C development.

                    The differences between gnu c99 implementation and
                    the standard are absolutely MINOR and only known
                    to specialists in that matter. To use that to say
                    the gcc compiler supports some features of c99 only
                    is a gross misrepresentati on of facts.

                    If we use the same criteria gcc doesn't compile
                    C++. It still has a lot to go to implement 100%
                    of that standard.







                    Comment

                    • Richard Heathfield

                      #11
                      Re: Variable declaration - Is this valid in standard C?

                      Keith Thompson said:

                      <snip>
                      >
                      If the convenience of being able to use a few C99-specific features is
                      more important to you than the ability to compile your code with
                      compilers that don't support those features, that's fine; use them in
                      good health. You should just be aware of the tradeoffs.
                      >
                      (I honestly don't know why jacob has such trouble understanding this,
                      or at least cknowledging it.)
                      I covered that a day or two ago.

                      --
                      Richard Heathfield
                      "Usenet is a strange place" - dmr 29/7/1999

                      email: rjh at above domain (but drop the www, obviously)

                      Comment

                      • Richard Heathfield

                        #12
                        Re: Variable declaration - Is this valid in standard C?

                        jacob navia said:
                        Please:
                        The question was
                        >
                        Is this valid in standard C?
                        >
                        And I answer
                        >
                        YES it is valid in standard C!!!!
                        Indeed it is valid in standard C, but not everybody has a C compiler that
                        fully conforms to the current standard, so it makes sense to point out the
                        fact that the feature in question is not fully portable, and so should be
                        avoided if portability is an issue.
                        And if your compiler is BROKEN and doesn't support standard C
                        use another one!
                        If it's broken, by all means use another one, but it isn't necessarily
                        broken just because it isn't fully conforming to C99. After all, gcc isn't,
                        Visual C isn't, and that's enough to be going on with. Are you seriously
                        suggesting that these two compilers are "BROKEN"?
                        The original poster did not mention portability to old broken
                        stuff, and it did not mention C370, or C90 or C80 or C70!!!
                        The original poster was concerned about whether the code was standard C; he
                        would not have asked if he did not have some reason to need to observe the
                        Standard - whether it be for contractual reasons, or for portability
                        reasons, or whatever - but he didn't mention the reason. It is likely,
                        although not guaranteed, that his concern was in fact portability.
                        And yes, C89 is not portable to old PDP 11 simulators.
                        I'd be surprised if that were true, but it's irrelevant.
                        This group is always trying to destroy C and to deny
                        all progress done since more than 10 years of standard
                        C development.
                        Nonsense. As soon as C99 becomes fully as portable as C89, the issue of
                        portability will fall by the wayside, and we can stop mentioning it. And
                        nothing would delight us more! But that time is not yet here.
                        The differences between gnu c99 implementation and
                        the standard are absolutely MINOR and only known
                        to specialists in that matter. To use that to say
                        the gcc compiler supports some features of c99 only
                        is a gross misrepresentati on of facts.
                        No, it's a correct representation of the situation.
                        If we use the same criteria gcc doesn't compile
                        C++. It still has a lot to go to implement 100%
                        of that standard.
                        Indeed, and that's why you will probably avoid C++ if portability is very
                        important to you.

                        --
                        Richard Heathfield
                        "Usenet is a strange place" - dmr 29/7/1999

                        email: rjh at above domain (but drop the www, obviously)

                        Comment

                        • Keith Thompson

                          #13
                          Re: Variable declaration - Is this valid in standard C?

                          jacob navia <jacob@jacob.re mcomp.frwrites:
                          Please:
                          The question was
                          >
                          Is this valid in standard C?
                          >
                          And I answer
                          >
                          YES it is valid in standard C!!!!
                          Correct; officially, the current C standard is the one issued by ISO
                          in 1999. That standard is not yet as widely implemented as the
                          earlier standard issued by ISO in 1990, as you should know very well.
                          And if your compiler is BROKEN and doesn't support standard C
                          use another one!
                          That is not always possible, as you should know very well.
                          The original poster did not mention portability to old broken
                          stuff, and it did not mention C370, or C90 or C80 or C70!!!
                          >
                          And yes, C89 is not portable to old PDP 11 simulators.
                          The OP didn't mention PDP 11 simulators, or C370. I don't know what
                          you mean by C80 or C70.

                          The C90 standard is supported by almost all current C compilers. I
                          don't know of a significant platform which has a C compiler but
                          doesn't have a conforming C90 compiler (ignoring minor bugs, which
                          exist in virtually all software).

                          Some systems with pre-ANSI compilers are probably still in use.
                          Anyone who needs advice about programming in C on such a system can
                          always ask here. I don't often see such questions these days, but
                          I'll be glad to do my best to answer them. (My first piece of advice
                          would be to obtain a conforming C89/C90 compiler if possible; if
                          that's not possible, there are alternatives I can suggest.)

                          If you write code that restricts itself to the C90 standard, while
                          avoiding any incompatible changes in C99 (for example, that avoids
                          using "inline" and "restrict" as identifiers), that code will be
                          significantly more portable than code that depends on features that
                          are in C99 but not in C90.

                          Of course, such code is valid C99 as well.
                          This group is always trying to destroy C and to deny
                          all progress done since more than 10 years of standard
                          C development.
                          That is complete and utter nonsense, as you should know very well.

                          On a personal note, jacob, this kind of deliberate misrepresentati on
                          of other people's statements is probably the most offensive and
                          annoying thing you do here. Knock it off.

                          I would be delighted if C99 enjoyed the same level of support as C90.
                          There are some (but not all) features of C99 that I'd like to be able
                          to use, and would use without hesitation if they were sufficiently
                          widely available. But the fact is that I cannot do that.
                          The differences between gnu c99 implementation and
                          the standard are absolutely MINOR and only known
                          to specialists in that matter. To use that to say
                          the gcc compiler supports some features of c99 only
                          is a gross misrepresentati on of facts.
                          That's what the gcc project's own web site says, as you should know
                          very well.

                          There are features of C99 that gcc does not support. You deliberately
                          omitted that fact in your response. Why are you trying to hide the
                          truth, when the gcc project itself explicilty acknowledges it?
                          If we use the same criteria gcc doesn't compile
                          C++. It still has a lot to go to implement 100%
                          of that standard.
                          I have no idea whether that's true or not; in any case, it's
                          completely off-topic here, as you should know very well.

                          I provided information that could be useful to the OP, information
                          that you deliberately withheld. You deliberately implied that gcc is
                          a conforming C99 compiler; it clearly is not. It may be close enough
                          for some particular user's purposes, and if so, that's find. But such
                          a user needs to have the opportunity to understand what it supports
                          and what it doesn't, and make that decision for himself.

                          And even if the latest version of gcc completely and flawlessly
                          supported the C99 standard, there are plenty of systems that use older
                          versions of gcc, and a typical user very likely doesn't have the
                          ability to upgrade the compiler. Furthermore, gcc does not include
                          the runtime library; rather, programs generated by gcc use whatever
                          runtime library is provided on the system. In many cases, that
                          runtime library does not support certain required features of C99.

                          For example, consider this program:

                          #include <stdio.h>
                          #include <math.h>
                          int main(void)
                          {
                          double x = sqrt(2.0);
                          printf("x = %a\n", x);
                          return 0;
                          }

                          On one system with gcc 3.4.5, it correctly prints:

                          x = 0x1.6a09e667f3b cdp+0

                          On another, with gcc 4.0.2, it prints

                          x = a

                          because the runtime library (which I'm not able to replace) doesn't
                          support printf's "%a" format.

                          But you insist, without qualification, that gcc supports C99, when in
                          fact it only partially supports it, as you should know very well.

                          I don't expect to convince you of anything. I've tried and failed
                          before; I don't expect to succeed this time. I post this as a warning
                          to others that jacob navia, for whatever reason, apparently wishes to
                          conceal these facts from programmers who may need to know them.

                          --
                          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                          San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                          We must do something. This is something. Therefore, we must do this.

                          Comment

                          • Kannan

                            #14
                            Re: Variable declaration - Is this valid in standard C?

                            Thank you folks for your help and comments. I decided to stick to C89
                            standards for the current project.

                            About the cc compiler; I found the compiler options to compile in 6 or
                            7 different language levels (don't know the fine difference between
                            some of them).

                            Thank you once again.
                            -Kannan

                            Keith Thompson wrote:
                            jacob navia <jacob@jacob.re mcomp.frwrites:
                            Please:
                            The question was

                            Is this valid in standard C?

                            And I answer

                            YES it is valid in standard C!!!!
                            >
                            Correct; officially, the current C standard is the one issued by ISO
                            in 1999. That standard is not yet as widely implemented as the
                            earlier standard issued by ISO in 1990, as you should know very well.
                            >

                            Comment

                            • Clever Monkey

                              #15
                              Re: Variable declaration - Is this valid in standard C?

                              jacob navia wrote:
                              Please:
                              The question was
                              >
                              Is this valid in standard C?
                              >
                              And I answer
                              >
                              YES it is valid in standard C!!!!
                              >
                              Say it brother! AMEN!
                              And if your compiler is BROKEN and doesn't support standard C
                              use another one!
                              >
                              YOW! All these CAPS and EXCLAMATION points are making me DIZZY!
                              The original poster did not mention portability to old broken
                              stuff, and it did not mention C370, or C90 or C80 or C70!!!
                              >
                              And yes, C89 is not portable to old PDP 11 simulators.
                              >
                              That word "portable". You keep using it. I do not think it means what
                              you think it means. Try using "inconceivable" , instead.
                              This group is always trying to destroy C and to deny
                              all progress done since more than 10 years of standard
                              C development.
                              >
                              Thus, I believe if you look at the c.l.c charter, you will see that the
                              first statement indicates comp.lang.c is charged with "denying all
                              progress of the language with the eventual aim of destroying it".

                              The first step is to ensure that as many people will a poor
                              understanding of "portabilit y", "standardizatio n" and "developmen t
                              practices" post as often as possible in order to share these
                              misunderstandin gs.
                              The differences between gnu c99 implementation and
                              the standard are absolutely MINOR and only known
                              to specialists in that matter. To use that to say
                              the gcc compiler supports some features of c99 only
                              is a gross misrepresentati on of facts.
                              >
                              For now, every smart coder knows that C89 conventions are best, unless
                              you enjoy looking forward to tears and recriminations. Hence, Richard's
                              caveats regarding why some conventions are not common. C is a living
                              language, and the standards are important, but it is not the only thing.

                              Case in point: many new coders come to my company expecting their C code
                              to compile when they declare variables anywhere they want. Guess which
                              new coders end up knocking on my door to ask why their new code won't
                              compile? Looks like the compiler toolchain they use at school is
                              different than the one we use. Since we have no reason to move to C99,
                              and few compilers reliably conform to C99, we have not moved to C99.

                              Standards are not equivalent to best practices and coding conventions.
                              Each can inform the other, but they inhabit different conceptual spaces.
                              If we use the same criteria gcc doesn't compile
                              C++. It still has a lot to go to implement 100%
                              of that standard.
                              >
                              Straw-man argument. Why mention a completely different standard for a
                              completely different language? The gcc toolchain includes a variety of
                              other compilers (hint: "gcc" is _not_ an acronym for "Gnu C Compiler").
                              Why stop there? How conforming is the Fortran compiler? The
                              COBOL-to-C translator? How does any of it impact the C89, C99
                              standards, or the specific Gnu implementation of those standards?

                              Comment

                              Working...