views:

163

answers:

5

In a C lab this uber simple code appears:

#include <stdio.h>

int suma (int a, int b)
{
        return a+b;
}

int mult (int a, int b)
{
        return a*b;
}

int main(void)
{
        int a,b;
        printf ("Operando 1: ");
        scanf("%d",&a);
        printf("Operando 2: ");
        scanf("%d",&b);
        printf("%d+%d=%d\n",a,b,suma(a,b));
        printf("%d*%d=%d\n",a,b,mult(a,b));
        return 0; 
}

by looking at the code I'm supposed to determine to which C standard it is compliant (ANSI, ISO or de facto K&R). After reading this and this I'm inclined to say that it's compliant to the three standards. Would that be correct?

+4  A: 

If you happen to use gcc, with the -std=..., -Wall and -pedantic options you may change the standard that it expects and get warning / errors if the code doesn't comply.

Jens Gustedt
*gasp* But that would be cheating! ;)
Tim Yates
@Tim, but I'd respect the question more if it cited the results of doing just that and asked about the ensuing confusion.... It would also be faster than asking here at all...
RBerteig
+2  A: 

K&R C does not include arguments in function declarations.

See, for instance, the Wikipedia text on C's history.

unwind
+6  A: 

It's not K&R. K&R function declarations define arguments outside the parentheses like this:

int mult (a, b)
    int a;
    int b;

and void was ntroduced with the first ANSI standard.

To me it looks likeit is compliant with both ANSI C89 and C99.

JeremyP
+3  A: 

I say C89 (ANSI C89 or ISO C90).

It isn't pre-standard (K&R) C: function definition has type information for parameters inside the declaration.

It follows all rules of the C89 standard and all rules of the C99 standard (*). However, the C99 standard says that main returns 0 if it reaches the closing } without a return statement. So, the guy who wrote this had to add the return 0; statement ... or the question doesn't make much sense.

(*) the first 2 printf shoud really have a '\n' (or call fflush(stdout); afterwards) to flush the output

pmg
Surely the question is, "to what standards does this conform?" not "for what standards is this the typical way to write the code?". I don't think the question makes much sense regardless - (1) who even cares whether code is or isn't K-)
Steve Jessop
A: 
#if __STDC_VERSION__ >= 199901L
puts("C99");
#else
puts("C89 or pre C89");
#endif

will work for ANSI C compilers.

Get ready for the future: `puts("C99 or post C99");` :-)
pmg