Why c functions can be defined this way? [duplicate] - c

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
What is useful about this C syntax?
What is this strange function definition syntax in C?
static void
add_shopt_to_alist (opt, on_or_off)
char *opt;
int on_or_off;
{
...
}
What's the syntax for :
(opt, on_or_off)
char *opt;
int on_or_off;
?

Related

Why use void in C? [duplicate]

This question already has answers here:
What are the valid signatures for C's main() function?
(5 answers)
func() vs func(void) in C99
(4 answers)
Closed 5 months ago.
I was looking at this simple line of code on github and noticed it ran the exact same after I took out void. Why would void even be used in this situation?
#include <stdio.h>
/*
* #author jelathro
* #date 2/18/13
*
* Print "Hello, World!" to the console
*/
int main(void){
printf("Hello, World!\n");
return 0;
}

Calling Functions from Main [duplicate]

This question already has answers here:
Why it seems that the function I defined can't run [duplicate]
(2 answers)
Call function without parameter and parenthesis
(3 answers)
Closed 2 years ago.
#include <stdio.h>
int function()
{
printf("Hello World");
}
int main()
{
function;
return 0;
}
new to coding. This code compiles but doesnt work as intended. Any thoughts?

variadic function pointer points to a function with preused arguments [duplicate]

This question already has answers here:
Currying/binding with ISO C99
(5 answers)
Functional Programming (Currying) in C / Issue with Types
(4 answers)
Is there a way to do currying in C?
(6 answers)
Partially applying a function in C
(3 answers)
Closed 3 years ago.
let's assume this variadic function is printing nice logs (without knowing its implementation because we don't care)
int printLog(int bla, int foo, const char *format, ...);
in the following code how do I affect the arguments?
void myFunction(int param)
{
typedef int (*myPrint) (const char *, ...);
if(param > 0)
{
myPrint = printLog; //how do I pass half of the argument here?
}
else
{
myPrint = printLog(1,2,...); //this is what I would like
}
//then use it
myPrint("Hello World");
unsigned int blah=1;
myPrint("blah is %d",blah);
}

How C allows argument definition after Close baracket( ")" )? [duplicate]

This question already has answers here:
strange function definition in Scilab<->C interface
(4 answers)
Alternative (K&R) C syntax for function declaration versus prototypes
(5 answers)
Closed 3 years ago.
How Does C Allows declaring type of argument after Closed Bracket like below Code?. The below Code is actually compiling and Executing without any error.
How does this generally Work?
#include <stdio.h>
void print_int(num)
int num;
{
printf("\n\n\nNumber : %d\n\n",num);
}
int main(argc,argv)
int argc;
char** argv;
{
print_int(2);
return 0;
}

Pass pointer to a literal direct to a function [duplicate]

This question already has answers here:
Pointer to literal value
(12 answers)
Closed 4 years ago.
Given the following piece of C code:
void calc(int *value)
{
// do something with value
}
int main(void)
{
int i;
i = 10;
calc(&i);
}
Is it possible to get rid of setting up i and pass directly 10 to function calc? If yes, how can this be done?
Example of what I have in mind (which doesn't work):
calc( (int *) 10);
Nope, it's impossible to have a pointer to a constant in C.
UPD: however it seems that there is a trick using a struct compound literals syntax (thanks to #antti-haapala for the correction). Try this:
calc(&(int){10});

Resources