#Why is C designed this way

59 messages · Page 1 of 1 (latest)

native kayak
#

Why are functions that do not accept arguments represented as

void foo(void);

in C and don't like C++ as

void foo();

What is the use of such a design?

full grailBOT
#

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.

vapid sparrow
#

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

native kayak
#

in C ,I create a function C void foo(),it can pass in parameters and use them without error

vapid sparrow
#

yes that is correct by syntax

#
/* correct function declaration */
void foo(); 
#
/* This is also a correct function declaration */
void foo(void); 
native kayak
#

Why do I need to tell function that the parameter is void

vapid sparrow
#

you don't

#

see my examples. they are both correct

#

i do not understand what the fuss you are having

native kayak
#

but i can foo(4) in C and not error

vapid sparrow
#

sure if your function is declared foo(int);

native kayak
#

in C my function is declared void print()

#

but i C++ it's not allow

vapid sparrow
#

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
native kayak
#

It's the compiler

vapid sparrow
#

🤷‍♀️

native kayak
#

if i use gcc,
Are there any serious consequences to this problem?

vapid sparrow
#

you use gcc for C source files, and C++ for C++ source files

native kayak
#

Why does it allow by default

vapid sparrow
#

treat both languages as separate languages, or you just gonan confuse yourself

native kayak
vapid sparrow
native kayak
vapid sparrow
#

what's the version of your g++ and gcc?

#

check with --version

native kayak
vapid sparrow
#

not quite outdated, but there was no output was it?

native kayak
#

I get an error when I compile c files with g++

vapid sparrow
#

expected

native kayak
#

I didn't use "\n"

vapid sparrow
#

well, if you need to turn on the warnings, just add -Wall -Wextra

native kayak
#

Why is it designed not to report errors by default, and what are the advantages of this design

vapid sparrow
#
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

native kayak
#

This may seem counter-intuitive

vapid sparrow
#

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

native kayak
#

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

vapid sparrow
#

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

native kayak
#

yes,and it seem like historical issues

vapid sparrow
#

and that's why C and C++ are different languages, should be compiled using their respective compiler.

#

well with exception of msvc cl. it claims to be smart enough to tell the differences 😅

native kayak
#

Compiler Oriented Programming lol ,thank for you help

#

!solved