#Why is C designed this way
59 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.
they are both correct by syntax
some compilers may not like empty parameters, but by default it is not marked as warning
unless you put the warning flag in the compilation process
in C ,I create a function C void foo(),it can pass in parameters and use them without error
yes that is correct by syntax
/* correct function declaration */
void foo();
/* This is also a correct function declaration */
void foo(void);
Why is C designed this way,Why isn't C omitted like C++
Why do I need to tell function that the parameter is void
you don't
see my examples. they are both correct
i do not understand what the fuss you are having
sure if your function is declared foo(int);
are you using g++ for both code?
#include <stdio.h>
void r()
{
printf("%d", 3);
}
int main()
{
r(3);
return 0;
}
compiled using gcc
<source>: In function 'int main()':
<source>:9:6: error: too many arguments to function 'void r()'
9 | r(3);
| ~^~~
<source>:3:6: note: declared here
3 | void r()
| ^
ASM generation compiler returned: 1
<source>: In function 'int main()':
<source>:9:6: error: too many arguments to function 'void r()'
9 | r(3);
| ~^~~
<source>:3:6: note: declared here
3 | void r()
| ^
Execution build compiler returned: 1
🤷♀️
if i use gcc,
Are there any serious consequences to this problem?
you use gcc for C source files, and C++ for C++ source files
Why does it allow by default
treat both languages as separate languages, or you just gonan confuse yourself
Here's how I do it. gcc does not report errors by default
the syntax is similar. C stuff may compile with g++, but not the other way around. I mentioned may compile, as this isn't the case 100% of time.
it use gcc
I did compile my example using gcc and the compiler flagged the error
what's the version of your g++ and gcc?
check with --version
not quite outdated, but there was no output was it?
I get an error when I compile c files with g++
expected
well, if you need to turn on the warnings, just add -Wall -Wextra
Why is it designed not to report errors by default, and what are the advantages of this design
g++ (Compiler-Explorer-Build-gcc--binutils-2.38) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
the one I'm using
This may seem counter-intuitive
I don't create the compiler, and it's GNU, maybe it is part of their extension. Compiling with ISO C would flag print(3) as an error.
yeah it's a gcc thing. I tried with clang with no flag. it flagged the extra parameter as warning
still can run
I found the same problem discussedhttps://stackoverflow.com/questions/693788/is-it-better-to-use-c-void-arguments-void-foovoid-or-not-void-foo
strange
that is why they are both correct by syntax foo(void) foo()
that's why gcc didn't flag foo(3); or r(3, 10, 100); as warning or error, unless you pass the warning flag
yes,and it seem like historical issues