C Can anyone explain this syntax? [duplicate] - c

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.

Related

what does register const char *const *name; mean and why is this variable outside of the function? [duplicate]

This question already has answers here:
C function syntax, parameter types declared after parameter list
(7 answers)
Should I place the parameter storage class specifier in the function definition or in both the declaration and definition?
(5 answers)
"register" keyword in C?
(19 answers)
Closed 2 years ago.
void
usage (cpp)
register const char *const *cpp;
{
(void) fprintf (stderr, *cpp++, program_name, cvs_cmd_name);
for (; *cpp; cpp++)
(void) fprintf (stderr, *cpp);
error_exit ();
}
I don't get why register variable is not inside the curly brackets and what is this (void) in front of fprintf? Also register const char *const *cpp, i've never seen anything like this before
The reason you've "never seen this before" is because this is using the syntax of the original, historical, K&R C version. That must be some real, old C code you're looking at. I just looked up, and ANSI C (C90) came out in 1990, so this code must be at least 30 years, or so, old.
I suppose this is something that needs to be known if one needs to deal with historic C code, but otherwise you can simply forget it.
The (void) cast means the same thing it means today.
The register keyword is a hint to the compiler that you'd like that value to be kept in a dedicated register on the processor. This can speed up reads and writes. With modern compilers, however, this sort of optimization is not only unnecessary but often counterproductive.
The reason it is between the function declaration and the block is that in old c (pre C90) you wouldn't declare parameter type next to the parameter but between the declaration of the function and the block.
For example:
int main(argc, argv)
char ** argv;
{
...
}
Notice I didn't do anything for argc because if you don't explicitly define a type it defaults to int.
You'll see this more often than you'd think. I ran into this a bunch when I did work on FFMPEG.
The (void) cast thing prevents unused parameter warnings/errors. I've run into this when working on PortAudio with a low-level callback function.

Call a char pointer in the function [duplicate]

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.

Uncommon function definition in the 'less' program source [duplicate]

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;
{

Why declaring function arguments this way: void func(x) int x; {} [duplicate]

This question already has answers here:
Alternative (K&R) C syntax for function declaration versus prototypes
(5 answers)
Closed 3 years ago.
I stumbled upon these lines when looking into putchar.c
I'm wondering about why the arguments ptr and c are declared outside the arguments body ?
Is this some kind of "good old way" or does it have some actual use ?
int
_putchar_r (ptr, c)
struct _reent *ptr;
int c;
{
return __sputc (c, _stdout_r (ptr));
}
Indeed it is the "olden" way of declaring a function's parameters.
I kind of like it, because it serves as a constant reminder that all a function's parameters are local variables that exist only in the scope of the function and that any argument passed into any function ALWAYS is a value copy.
Its a K&R C style introduced in classic C Programming Book
It is a function definition with an identifier list. Each identifier in the identifier list is declared before the compound statement of the function.
So a function can be defined either with a parameter type list or using the old style with an identifier list.

What does this weird C++ definition mean? [duplicate]

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
}

Resources