declaring a function with this specification [duplicate] - c

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

Related

What do parameter declarations between function declaration and bracket mean? [duplicate]

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

In C: Difference between main() and int main () [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
difference between main(void) and main() in c
I know this is super basic and some other threads already talked about similar questions. But I have a book (Absolute Beginners Guide to C) where all the code is written within the function main(). The int is always left out. How is that possible to run? Is that ok with maybe an older version of stdio.h?
Here is the sample code:
#include <stdio.h>
main() // not int main()
{
printf("This is it\n");
return 0;
}
I think the c89 standard will allow main() but c99 and above won't . You have to use int main() otherwise .
These kind of questions are highly standard-version dependent, so a general answer doesn't make much sense.
From a C89 draft (correct me if official C89 Standard is different, it's not freely avalaible):
The function called at program startup is named main.
The implementation declares no prototype for this function.
It can be defined with no parameters:
int main(void) { /*...*/ }
or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /*...*/ }
C99 and C11 standard say the same but they add something at the and:
[...]
or equivalent;[9] or in some other implementation-defined manner.
[9] Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on.
In general things that are not defined from the standard leads to undefined behavior, so that code is UB in C89/C90, and it could be valid in C99 and C11, but that's implementation-defined.
P. S.: as you can see, you should also add void in the parameters list, without it the behavior is defined as above.
main() works but is confusing, in C the main function always returns an int, to specify exit status, so the correct syntax is int main(), but if you do not bother to set the exit status then main() is enough, but the good C books will always have int main().

what is main(ac, av) int ac; char **av; {...}? [duplicate]

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.

What are the different valid prototypes of 'main' function? [duplicate]

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.

C variable declarations after function heading in definition [duplicate]

This question already has answers here:
What is this strange function definition syntax in C? [duplicate]
(6 answers)
Closed 8 years ago.
When reading some FreeBSD source code (See: radix.h lines 158-173), I found variable declarations that followed the "function heading" in the definition.
Is this valid in ISO C (C99)? when should this be done in production code instead of just declaring the variables within the "function heading?" Why is it being done here?
I refer to the function heading the string that looks like this: int someFunction(int i, int b) {
That looks like K&R (pre-ANSI) style. I don't think it's valid C99, but are they using C99? Joel
I think you are referring to the "old-fashioned" pre-ANSI way of declaring parameters in C. It looked something like this:
int foo(a, b)
int a,
int b
{
/* ... */
}
That may still be valid in C99, and will be accepted by compilers for backward-compatibility reasons, but it should be considered deprecated/archaic.
Er. Maybe I'm misunderstanding your question, but i and b in that snippet are parameters to the function. It's not some compact way of declaring variables within the function, like:
int someFunction() {
int i, b;
When you call someFunction, you pass it those arguments:
someFunction(1, 2); // `i` will be `1` and `b` `2` within `someFunction`

Resources