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
}
Related
This question already has answers here:
Why cast an unused function parameter value to void?
(2 answers)
Closed 2 years ago.
I saw the piece of the code. But do not know what the purpose is.
void a()
{
static const char *string = "STRING";
...
(void)string; // <---- What the purpose of the line is?
...
}
(void) before a variable like that creates an empty expression and is used to silence warnings about a variable not being used by the program.
In this specific case it would be better to simply comment out the variable declaration.
This question already has answers here:
C function syntax, parameter types declared after parameter list
(7 answers)
Closed 3 years ago.
My friend asked me could I add "frozen top line" feature to the standard less *NIX program. I started to view the source and was very surprised in function definition the filename.c:
public char *
last_component(name)
char *name;
{
....
}
I wrote some code at the C and usually I met:
return_type function_name arguments_list; if it is function prototype
return_type function_name arguments_list code-block if it is function definition.
But here I found something new: return_type function_name arguments list something_mysterious; code block.
What does this syntax means?
This is old C syntax (also known as K&R style), where a function definition could be written as where the argument are declared on the subsequent line:
int func(a,b)
int a;
char b;
{
This question already has answers here:
static keyword inside array [] brackets [duplicate]
(1 answer)
What is the purpose of static keyword in array parameter of function like "char s[static 10]"?
(1 answer)
Closed 5 years ago.
I saw a function whose prototype was this:
int foo(int arr[static], size_t len);
I have learnt that it's possible to put const and volatile there, which should be adjusted to pointers pointing to objects with corresponding cv-qualifiers. But this time it doesn't seem quite the same as
int foo(static int *arr, size_t len);
as static shouldn't appear here at all.
So what is it at last?
This question already has answers here:
Alternative (K&R) C syntax for function declaration versus prototypes
(5 answers)
Closed 6 years ago.
Why is the variable "char *s" declared in between the function name and curly braces? What is the significance of it?
main(m1,s) char *s; {
/*
some code here
*/
}
This is old K&R C syntax (pre-dates ANSI/ISO C). Nowadays, you should not use it anymore (as you have already noticed its major disadvantage: the compiler won't check the types of arguments for you).
main(m1,s) char *s; {
/*
some code here
*/
}
in this code compiler did not check data type of m1 and s.
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