Why use void in C? [duplicate] - c

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

Related

Hi, im trying to start my first C language in VS code, but i can’t run it [duplicate]

This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 1 year ago.
#include <stdio.h>
void main(void)
{
printf("hello");
}
I can’t seem to get the code to run. I tried using some extensions/terminal but the problem does not solve.
If you use int as the type of main, it would be a good idea to include a return statement. The following works in VS Code
#include <stdio.h>
int main()
{
printf("Hello");
return 0;
}
You need int main (void), not void main(void).
See the Microsoft documentation.

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);
}

Is zero size array invalid in C struct? [duplicate]

This question already has answers here:
Array of size 0 at the end of struct [duplicate]
(5 answers)
What is the purpose of a zero length array in a struct? [duplicate]
(2 answers)
Closed 3 years ago.
I have compiled followin gprogram using GCC compiler on Ubuntu platform.
I am wondering why the following program is working fine in C?
Is it undefined behaviour?
#include <stdio.h>
struct str
{
char arr[0];
};
int main()
{
struct str s;
s.arr[0]=1; // I think it is invalid in C
printf("%d\n", s.arr[0]);
return 0;
}
Output:
1
and when I print printf("%zu\n", sizeof(s));. it is print 0.

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

Resources