Entering function types of function C after a list of names and type for some parameters

I read some, for me, a kind of C-code, studying some examples in the book "Unix Software Environment" (1983).

As a curiosity, I wanted to know more about that, let me call it "style." The line below is interesting here.int main(argc, argv)

#include <stdio.h>

int main(argc, argv)
     char *argv[];
{
    printf("%s\n", argv[0]);
    return 0;
}

In my research, I found that compiling the above code with flags -Wall -pedantic -ansiworks without any warnings and replaces -ansiwith a more recent one -std=c99(or c11, with gccand cc) warns only about argcthe default int.

I looked at the old C89 standard, trying to find links to this particular way of writing, but didn’t find anything on my own, so I put aside more knowledge of the team.

, , , , , ( ?)

+4
4

, . int , , . , .

// In func.c

// Default to int return
func(x, y)
    int x;
    int y;
{
    return x + y;
}

// In main.c

main(argc, argv)
    int argc;
    char **argv;
{
    int result = func(2, 3);
}

, .

int result = func(2.0, 3,0); // Wrong, but no compiler error or warning

. .

// This is NOT a function prototype, it just declares the return type.
double sin();

double one = sin(3.14); // Correct
double zero = sin(0); // WRONG
double zero = sin(0.0); // Correct

- , . . -Wmissing-prototypes GCC .

int func(); // Old-style, can take any number of arguments.
int func(void); // New-style, takes no arguments, is a function prototype.
+4

K & R C.

, , , . , .

, .

int myFunc(const char *from, char *to, int len); // Ah that feels right, doesn't it

int myFunc(const char *from, char *to, int len){} // And here the definition

myKRFunc(); /* Okay, that a declaration */

myKRFunc(from, to, len) /* Yep, you just write the parameter names here */
    char *from, *to;    /* And you can write normal declarations, len defaults to an int */
{}

, , .

+2

K & R. - , ? . .

+1

"", . , , , .

0

Source: https://habr.com/ru/post/1546034/


All Articles