This question already has answers here:
Alternative (K&R) C syntax for function declaration versus prototypes
(5 answers)
Closed 8 years ago.
What's the difference between this declaration:
void
main(argc, argv)
int argc;
char **argv;
{
// ...
}
from this?
void
main(int argc, char **argv)
{
// ...
}
Thanks for answers.
Its a different notation style that was used in C at its beginning called the KR Notation (for Kernighan and Ritchie, C's designers). The well known C notation style that is used today follows the ANSI standard.
Here's link to a Wikipedia article describing the KR notation : http://en.wikipedia.org/wiki/K%26R_C#K.26R_C
Related
This question already has answers here:
Function declaration: K&R vs ANSI [duplicate]
(3 answers)
Closed 6 years ago.
i recently saw the following code in C which is said to be valid but I'm not sure.
int max(x,y)
int x,y;
{
return (x>y)?x:y;
}
Is this kind of function prototype valid? If so please give some reference to learn more about that.
This is the old-style "K&R" way of defining functions. The new way is better, so don't write code like this yourself.
This code is valid, it's just a pretty old standard.
Nowadays in function declaration the types of arguments are declared right before the names of these arguments:
int main(int argc, char **argv)
But years ago there was another standard where the syntax was different: you had to specify the types like this:
int main(argc, argv)
int argc; char **argv;
So, nothing weird here, different standards offer different syntax
This question already has answers here:
What is this strange function definition syntax in C? [duplicate]
(6 answers)
Closed 9 years ago.
I have this strange function definition in my homework code and I don't really know what it's supposed to mean.
char *
sh_single_quote (string)
char *string;
{...}
Especially the "char *string;" line, what with the semicolon at the end.
It is K&R style declaration of a function in C language.
In C, you usually write a function as:
size_t strlen(const char *str)
{
//code
}
In K&R style this will be written as:
size_t strlen(str) <--- here you write only the param name
const char *str; <--- here you write the type along with param name!
{
//code
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How many styles of writing functions are there in C?
I saw a program written in C with the following definition of main:
main(ac, av) int ac; char **av;
{
...
}
instead of
int main(int argc, char **argc)
{
...
}
I've never seen a C syntax as in the first code. What is it and where can I read about it?
The first one is an old-style, pre-ANSI, C function header before function prototypes became the common/standard way of writing code.
Frequently formatted like this:
main(ac, av)
int ac;
char **av;
{
...
}
What you see after the initial set of parenthesis are the type declarations for the parameters in main. Also note, no int return type is declared.
No one really writes code like that, stick to the 2nd form.
If you can dig up the original white book by Kernighan and Ritchie (1st ed, pre-ANSI) you'll see that form (and as #dirkgently correctly mentions this is sometimes referred to as K&R style C) as you would in other older C books.
Also check out this link if you are curious
Obsolete Forms of Function Declarations and Definitions. Searching on "old style C function declarations" in google will bring up a number of hits.
This question already has answers here:
C function syntax, parameter types declared after parameter list
(7 answers)
Closed 2 years ago.
A few times now, I've encountered code like this:
int
main(argc, argv)
int argc; char **argv;
{
...
}
Why are argc and argv declared in this way?
This was the syntax that was used for K&R* style parameter lists.
* K&R - Brian Kernighan and Dennis Ritchie, authors of the book, "The C Programming Language." Dennis Ritchie was also the creator of the C programming language and with Ken Thompson, the UNIX operating system.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What are the valid signatures for C's main() function?
What are the different valid prototypes of 'main' function?
Are there some non-standard prototypes also supported only by a few vendors?
The C standard (ยง 5.1.2.2.1) defines two entry point prototypes:
int main(void);
or
int main(int argc, char **argv);
Other than that, every OS has its own additional non-standard entry points. WinMain, etc.
The full prototype allowed by gcc is:
int main(int argc, char * argv[], char *envp[])
but envp is rarely used. Omitting argc and argv is also considered acceptable.