Tag: C11

Can you compile this?

Can you compile this?

No return
Image by PublicDomainPictures from Pixabay

It’s just a little bit of C. I compiled it with these three compilers. MS VC (16.7.7 on Windows) gcc, and clang on Ubuntu 20.04 LTS. MS VC picked up the _Noreturn and complained, gcc 9.33 compiled it with not a whisper while clang 10.0.0 warned about void in the main function but compiled it anyway.

Both the clang and gcc compiled files ran and as you’d expect sat in an infinite loop until I control-c’d it.

However soon MS VC will compile it. According to this blog entry, MSVC from 16.8 preview 3 supports C11 and the _Noreturn feature (which tells the compiler that the function never returns) will be ok. Both gcc and clang support C11 so no problems.

#include <stdio.h>

_Noreturn void nrloop() {
	while (1) {
		;
	}
}

void main() {
	nrloop();
}

 

When would I use such a function, you might ask. IO can’t see me using it much unless I wnt to write a background thread function that just runs forever. I have indeed written such a function recently but it was in Delphi not C. Another use is a function that exits perhaps vi a jmp. It also lets the compiler know so it can optimize return code away.

As I said, I don’t think I’ll be using it much if at all.

C11 and C17 support in MSVC

C11 and C17 support in MSVC

 

The letter C
Image by Peggy und Marco Lachmann-Anke from Pixabay

Microsoft have announced that they will be supporting both C11 and C17 in Visual Studio 2019 version 16.8 Preview 3. All the required features but not optional features and not VLAs (Variable Length Arrays) which is considered unsafe.

One of the complaints has been that Microsoft always prioritised C++ over C for many years. C was supported inasmuch as it was needed for C++. Until fairly recently C++ was a superset of C and you could compile C program as C++. Just change the extension to .cpp.

That said, I will probably continue to write C code as C99 for now and take a look at the C11/C17 features such as restrict, stdnoreturn and so on.  Note C17 is considered a bit of a bug fix for C11.

I’m considering switching to C11

I’m considering switching to C11

Programming image
Image by Gerd Altmann from Pixabay

All C code I write in the books is currently to the C99 standard. All the compilers involved (Visual C++ on Windows and Clang on Ubuntu) support C99 but C11 support seems restricted to GCC and Clang.

Microsoft has traditionally supported C++ but their C support seems a bit grudging; realistically they don’t prioritise it which I can understand.

Given though that I’m not going to republish my first e-book for a while (I’d like to add a WebAssembly chapter or two first), I’m going to investigate whether it’s worth switching to C11 for the 2nd book. From what I’ve read all it needs is a flag to tll it to compile to C11 standards. This is for Clang.

-std=c11

But the other question is what will I gain by doing this and I can’t actually see there’s that much benefit.. I don’t need Unicode, I don’t think alignment will really make much difference. You can read about the C11 changes on WikiChip.

So I’ve made the decision. I’ll stick with C99 for now. But for an alternative view, I recommend Danny Kalev’s 2012 article on C11.