are these lines acceptable ANSI C ? 
Author Message
 are these lines acceptable ANSI C ?

are these (admitedly lousy) lines acceptable in strict ANSI C ?

int ;
int x, ;
int y[1] = {1,};

Most compilers accept the first, some with warning.
Some compilers accept the second, others not.
Most compilers seem to accept the third without warning.

Francois Grieu
--



Mon, 01 Apr 2002 03:00:00 GMT  
 are these lines acceptable ANSI C ?


Quote:

> are these (admitedly lousy) lines acceptable in strict ANSI C ?

> int ;
> int x, ;
> int y[1] = {1,};

> Most compilers accept the first, some with warning.
> Some compilers accept the second, others not.
> Most compilers seem to accept the third without warning.

In C89 the first and third are acceptable, the second is not.
I'm surprized that the first is allowed - does anyone know why?

Sent via Deja.com http://www.deja.com/
Before you buy.
--



Tue, 02 Apr 2002 03:00:00 GMT  
 are these lines acceptable ANSI C ?


Quote:
> are these (admitedly lousy) lines acceptable in strict ANSI C ?

> int ;
> int x, ;
> int y[1] = {1,};

> Most compilers accept the first, some with warning.
> Some compilers accept the second, others not.
> Most compilers seem to accept the third without warning.

Well, when running the magic gcc -ansi -pedantic test, it gives a warning
on "int ;" and barfs on "int x,;". The third line is correct; in most
circumstances (and off-hand I can't think of a case when this isn't true)
a trailing comma on a list inside braces is allowable (this makes it a lot
easier to put lists together from macros).

--
+- David Given ---------------McQ-+ "There does not now, nor will there ever,


+- http://wired.st-and.ac.uk/~dg -+ --- Flon's Axiom                            
--



Tue, 02 Apr 2002 03:00:00 GMT  
 are these lines acceptable ANSI C ?
...
# One situation where such a comma is disallowed is inside an enum
# definition.
#       enum {a, b, c, };
# is not allowed.

C99 is going to allow it, IIRC, for symmetry reasons. BTW, Peter van der
Linden (in Expert C Programming) gives an argument why "extra comma" is
actually not so cool as many people might think. Basically, it's quite
easy to skip some operation the first time through a loop. If "some
operation" is "output comma" before a list item the way to go is obvious.

Regards,

        Jens
--
Jens Schweikhardt  http://www.schweikhardt.net/
SIGSIG -- signature too long (core dumped)
--



Fri, 05 Apr 2002 03:00:00 GMT  
 are these lines acceptable ANSI C ?

Quote:



>> are these (admitedly lousy) lines acceptable in strict ANSI C ?

>> int ;
>> int x, ;
>> int y[1] = {1,};

>> Most compilers accept the first, some with warning.
>> Some compilers accept the second, others not.
>> Most compilers seem to accept the third without warning.

>In C89 the first and third are acceptable, the second is not.
>I'm surprized that the first is allowed - does anyone know why?

I do not see any difference between the first and the second, though cant
understand why my vc++ compiler gives a warning for first and error on
second and nothing on third.

The third shouldnt bother the compiler much, as there is no array bound
check in C.

It doesnt look like the compiler doesnt follow the same logic for comma
operator in the declarations.

--



Sat, 06 Apr 2002 03:00:00 GMT  
 are these lines acceptable ANSI C ?


Quote:

>...
># One situation where such a comma is disallowed is inside an enum
># definition.
>#   enum {a, b, c, };
># is not allowed.

>C99 is going to allow it, IIRC, for symmetry reasons. BTW, Peter van der
>Linden (in Expert C Programming) gives an argument why "extra comma" is
>actually not so cool as many people might think. Basically, it's quite
>easy to skip some operation the first time through a loop. If "some
>operation" is "output comma" before a list item the way to go is obvious.

I think a "generic" loop should be like this:

loop {
        statements;
        if (condition) break;
        statements;

Quote:
}

We can probably make the syntax look better but this is the idea.

C loop constructs can all be represented this way:

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

for (s1; cond; s2) {            s1;
        s3;                     loop {

Quote:
}                                       ;

                                        if (!cond) break;
                                        s2;
                                }

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

while (cond) {                  loop {
        s1;                             ;

Quote:
}                                       if (!cond) break;

                                        s1;
                                }

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

do {                            loop {
        s1;                             s1;

Quote:
} while (cond);                         if (!cond) break;

                                        ;
                                }

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

For example, a program to output the above enum statement would be
like (using "while (1)" instead to make it C):

#include <stdio.h>

int main()
{
    int ch;

    printf("enum { ");
    ch = 'a';
    while (1) {
        printf("%c", ch);
        if (ch >= 'c') break;
        printf(", ");
        ch++;
    }
    printf(" };\n");

    return 0;

Quote:
}

Stephen
--



Sat, 06 Apr 2002 03:00:00 GMT  
 are these lines acceptable ANSI C ?


Quote:

>C loop constructs can all be represented this way:

>=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

>for (s1; cond; s2) {                s1;
>    s3;                     loop {
>}                                   ;
>                                    if (!cond) break;

Obviously, I forgot to put s3 here...

Quote:
>                                    s2;
>                            }

Stephen
--



Tue, 09 Apr 2002 03:00:00 GMT  
 are these lines acceptable ANSI C ?

Quote:

> I think a "generic" loop should be like this:

> loop {
>    statements;
>    if (condition) break;
>    statements;
> }

I prefer my generalized do-while loop:

        do statement while ( expr ) statement-opt ;

-Larry Jones

My dreams are getting way too literal. -- Calvin
--



Tue, 09 Apr 2002 03:00:00 GMT  
 are these lines acceptable ANSI C ?


Quote:




> >> are these (admitedly lousy) lines acceptable in strict ANSI C ?

> >> int ;
> >> int x, ;
> >> int y[1] = {1,};

> >> Most compilers accept the first, some with warning.
> >> Some compilers accept the second, others not.
> >> Most compilers seem to accept the third without warning.

> >In C89 the first and third are acceptable, the second is not.
> >I'm surprized that the first is allowed - does anyone know why?

In a conversation at the end of last week (which Deja has
currently lost) someone pointed out that I'd forgotten to
consider the Constraints in the Standard. Only the third
of these lines is valid C.

Quote:
> I do not see any difference between the first and the second, though
> cant understand why my vc++ compiler gives a warning for first and
> error on second and nothing on third.

The first breaks a Constraint, the second is a syntax error,
and the third is valid.

Quote:
> The third shouldnt bother the compiler much, as there is no
> array bound check in C.

I don't understand why bounds checks are relevant.

Quote:
> It doesnt look like the compiler doesnt follow the same logic
> for comma operator in the declarations.

I don't know what you mean by this - there are no comma operators
involved in this.

Sent via Deja.com http://www.deja.com/
Before you buy.
--



Tue, 09 Apr 2002 03:00:00 GMT  
 are these lines acceptable ANSI C ?

Quote:


>> I think a "generic" loop should be like this:

>> loop {
>>        statements;
>>        if (condition) break;
>>        statements;
>> }

>I prefer my generalized do-while loop:

>    do statement while ( expr ) statement-opt ;

>-Larry Jones

>My dreams are getting way too literal. -- Calvin

Can you elaborate?

Stephen

--



Fri, 12 Apr 2002 03:00:00 GMT  
 are these lines acceptable ANSI C ?

Quote:

> I prefer my generalized do-while loop:

>    do statement while ( expr ) statement-opt ;

Ahem.  Shouldn't that just be

  do statement while ( expr ) statement

?  Otherwise you end up with ugliness like

  do x++; while (y--) a = *b++;; /* Second semicolon required */

-- [mdw]
--



Tue, 16 Apr 2002 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. are these lines acceptable ANSI C ?

2. Newbie: separate big .cs file into small .cs files

3. Am I working on the right lines... (long)

4. C keywords, delimiters and acceptable characters

5. New Line Problem in ANSI-C program

6. The limits of "acceptable" code

7. How: Create acceptable argv[] for execv() from string?

8. Teaching Worldwide More Acceptable Language

9. I am new to programming and am lost

10. ANSI C on-line?

11. ANSI rationale has possibly garbled line

12. Acceptable Solution?

 

 
Powered by phpBB® Forum Software