Or Is Not Valid C++:Why Does This Code Compile

or is not valid C++ : why does this code compile?

According to Wikipedia:

C++ defines keywords to act as aliases
for a number of symbols that function
as operators: and (&&), bitand (&),
and_eq (&=), or (||), bitor (|), or_eq
(|=), xor (^), xor_eq (^=), not (!),
not_eq (!=), compl (~).

As MadKeithV points out, these replacements came from C's iso646.h, and were included in ISO C++ as operator keywords. The Wikipedia article for iso646.h says that the reason for these keywords was indeed for international and other non-QWERTY keyboards that might not have had easy access to the symbols.

Why does an invalid use of C function compile fine without any warnings?

The compiler should issue a message for this program

int foo() {
return 3;
}

int main() {
int x;
foo(&x);
return x;
}

because the function foo called with an argument though its identifier list is empty. So the program has undefined behavior.

According to the C Standard *6.7.6.3 Function declarators (including prototypes)
)

14 An identifier list declares only the identifiers of the parameters
of the function. An empty list in a function declarator that is part
of a definition of that function specifies that the function has no
parameters.
The empty list in a function declarator that is not part
of a definition of that function specifies that no information about
the number or types of the parameters is supplied.

So the program is invalid.

You could make it a valid program the following way

int foo();

int main() {
int x;
foo(&x);
return x;
}

int foo( int *p ) {
return 3;
}

Though the compiler can issue a warning that the parameter p is not used.

In this case the function declaration that is not its definition means that there is no information about the number and types of parameters.

Opposite to C in C++ this declaration

int foo();

is equivalent to

int foo( void );

This is a valid C code but not a valid C++ code?

Yes. K&R, pre-standard C. Avoid using it.

Why does this compile with the Dev-C++ compiler and not Visual Studio's one?

This:

int arr[n];

is invalid because n is not a constant expression. You need to allocate variable sized arrays on the heap using malloc (and then free them when you are done with free).

If you are trying to compile this with a .cpp extension, main must have a return type of int. If you are trying to compile this with a .c extension, then you need to use c-style local variable declaration and declare all of your local variables at the top of the function.

Why does this invalid-looking code compile successfully on g++ 6.0?

This looks to be an bug/feature/issue with g++ in all of the versions I can test it on. Running

int main()
{
int(*){} Is it C++14 or any other language?
}

On godbolt.org for all versions of g++ with no compilation flags give the following assembly ouput.

main:
pushq %rbp
movq %rsp, %rbp
movl $0, %eax
leave
ret

The only diagnosis I get is on godbolt.org and that is

!!warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x

Clang, ICC and MSVS all fail to compile this.

EDIT:

From the comments zwol filed a bug with gcc on this. The bug report can be found here.

is there any circumstance under which valid C code will not compile properly using g++

C is not a subset of C++.

Try:

foo.c

int main() {
int class = 0;
return 0;
}

Anyway have fun here: Where is C not a subset of C++?

Code::Blocks Can't compile application

You either have the wrong compiler selected in your project's build options OR you don't have executables set in your compiler settings.

To select your compiler in your project:

First, go to the Project menu and select Build options:

Project menu

Then, in the Build options dialog, select the compiler you have set up as described in your question:

Build Options dialog

To change your compiler's executables:

Go to Settings and then Compiler:

Settings->Compiler

Then select Toolchain Executables:

Toolchain Executables

and fill in your executable names:

Sample Image

Why does the C Standard allow this code to compile without errors?

#include <stdio.h>
#include <string.h>

int main() {
if (strcmp(1, 2))
printf(3);
}

The calls to strcmp and printf, since they pass arguments of incorrect types (and there is no implicit conversion available), are constraint violations.

The C standard requires at least one diagnostic message for any program that violates a constraint or syntax rule. A warning is a perfectly valid diagnostic message as far as the standard is concerned.

Here's what the standard says (quoting the N1570 draft of the C11 standard, section 5.1.1.3):

A conforming implementation shall produce at least one diagnostic
message (identified in an implementation-defined manner) if a
preprocessing translation unit or translation unit contains a
violation of any syntax rule or constraint, even if the behavior is
also explicitly specified as undefined or implementation-defined.
Diagnostic messages need not be produced in other circumstances.

The only case where the standard actually requires a source file to be rejected is when it contains a #error directive. Section 4, paragraph 4:

The implementation shall not successfully translate a preprocessing
translation unit containing a #error preprocessing directive unless it
is part of a group skipped by conditional inclusion.

I personally would prefer gcc and clang to reject programs that violate syntax rules or constraints, but as I said non-fatal warnings are permitted by the standard. If you want such programs to be rejected, there are various options you can use, such as -std=c11 -pedantic-errors. Note that gcc and clang do not fully conform to the C standard by default, but these non-fatal warnings are not an example of that non-conformance.

gcc not recognize *.C source code as valid c program

GCC recognizes .C (capital letter) extension as C++ file. You need to change extension of your file to .c (small letter). Also, you rightly mentioned and referenced that C++ requires a cast in case of malloc where as in c there is an implicit conversion from any object pointer type to void *.

See below explanation about file extensions (.C and .c) from GCC documentation. Please refer below GCC link for detail explanation for various file extensions.

file.C

C++ source code which must be preprocessed. Note that in .cxx, the last two letters must both be literally x. Likewise, .C refers to a literal capital C.

file.c

C source code which must be preprocessed.

https://gcc.gnu.org/onlinedocs/gcc-3.3/gcc/Overall-Options.html

Additionally, you can give the flag -x c to force GCC to treat the file as C, not C++.



Related Topics



Leave a reply



Submit