Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function definition C [duplicate]

Tags:

c

function

Possible Duplicate:
C function syntax, parameter types declared after parameter list

I saw the following syntax for function definition in "Expert C Programming"

int compare(s1, s2)
    char * s1, *s2;
{
    while (*s1++ == *s2) {
        if (*s2++ == 0) return (0);
    }
    return (*--s1 - *s2);
}

How is the above definition valid? It compiles and runs perfectly without any errors.

I am more comfortable with the following syntax for function definition

int compare(char * s1,char *s2)
{
    while (*s1++ == *s2) {
        if (*s2++ == 0) return (0);
    }
    return (*--s1 - *s2);
}

and no where I've seen the one given in the book(While studying C in my college or elsewhere), can anyone please throw some light on the one given in the book.

like image 398
Kartik Anand Avatar asked May 26 '12 14:05

Kartik Anand


1 Answers

This topic has been discussed here before, it's the "Kernighan and Ritchie style" of function definition.

Nowadays you should prefer the second syntax, the first one is still accepted by some compilers for backwards compatibility reasons but it should be considered deprecated for all practical purposes.

like image 89
Óscar López Avatar answered Sep 20 '22 19:09

Óscar López