Questions tagged [c11]
C11 is the informal name of an older standard version (ISO/IEC 9899:2011) of the C programming language.
879
questions
-4
votes
0
answers
54
views
Why the code doesn't work when I add "\n" at the end of first printf statement [closed]
\n problem at the end of first printf. please provide a suitable answer as I am a beginner in the coding world. i was trying to write a code of asking an integer p such that 3<p<20 and printing ...
3
votes
2
answers
203
views
How to do file I/O with bool in C?
Should I treat a bool variable like an int when doing file I/O?
I mean is it correct to use the following code snippet?
bool b = true;
FILE *fp1, *fp2;
...
fprintf(fp1, "%d", b);
fscanf(fp2, ...
0
votes
1
answer
47
views
How can I combine a comparison and an update in one atomic operation?
I have two threads, which will execute:
_Atomic volatile uint64_t foo;
// returns 1 if it updated foo, 0 if fool already had the new value
int try_to_update_foo(uint64_t new) {
if (foo < new) ...
3
votes
1
answer
41
views
MSVC compiled code hits debug assert when using _Atomic float
I have the following code:
static _Atomic float testf;
void func() {
testf=1.0f;
float f=testf;
printf("%f\n", f);
}
Running it causes the program to hit a Debug Assert, Invalid ...
0
votes
0
answers
40
views
Memory ordering and RMW operations
Suppose I make two relaxed modifications to two atomic objects in thread0, one per each object, and then make thread1 observe the modification that came second in thread0.
Now without memory fences, ...
2
votes
1
answer
114
views
How to get C11 threads (and other) man pages on linux system
I can't find manual pages for any of the functions declared in threads.h added in C11.
There is good documentation for them in https://en.cppreference.com/w/c/11
and in the glibc docs, however I would ...
2
votes
2
answers
52
views
C11 nested generics
I am writing a math library and want to have a call like "add" which is generic, takes two parameters v1 and v2, and calls the right function. If v1 is vec2 and v2 is vec2 then it will call ...
7
votes
1
answer
289
views
Missing the great reason for memset_s (C11)
I'm sure there are a lot of buffer overflow risks in coding, many of which are addressed by standard library's "_s" safe functions. Nonetheless, I find myself confused, from time to time, on ...
0
votes
2
answers
60
views
What to do to make '_Generic('a', char : 1, int : 2) == 1' true
Is there any way the compiler can set the type 'a' to char, instead of int.
This makes the values of these expressions true:
sizeof('a') == 1
_Generic('a', char : true, default : false)
In gcc ...
0
votes
0
answers
41
views
C11: Use volatile for simple communication between threads
I'm using C (more exactly: C11 with gcc) for developing some low-latency software for x64 (more exactly: Intel CPUs only). I don't care about portability or any other architecture in general.
I know ...
1
vote
1
answer
97
views
Cmake + vs2022 (cl.exe) + ninja + Vulkan2D (C11 Atomics...?)
Context: I'm trying to build the samples in the Vulkan2D repo using the latest version of Visual Studio. Opening the V2D repo in VS2022 and configuring the cmake project works fine, but trying to kick ...
0
votes
1
answer
51
views
C: member to drive up structure size up to a value [duplicate]
I have a structure that looks like this:
struct myStructure
{
int index1;
int index2;
void *buffer;
char fillData[];
};
I want to make the fillData member as big as it needs to make ...
2
votes
1
answer
128
views
C11 memory fence and atomic operation
I'm studying about memory barriers.
I have some questions about following code.
//version 1
Thread A:
*val = 1;
atomic_thread_fence(memory_order_release);
atomic_store_explicit(published, ...
3
votes
1
answer
427
views
How to properly mix stdatomic between C and C++
Consider a library that uses stdatomic.h header. This one will not compile in C++ project, especially if it is required to use structure with atomic components.
How to properly implement library with ...
4
votes
3
answers
215
views
Can C11 _Generic be used with no arguments?
I would like to use _Generic to overload functions, one of them having no arguments, something like:
#include <stdio.h>
#include <string.h>
void f1(void)
{
printf("F1\n");
}
...
1
vote
0
answers
67
views
No output for MSVC 2015! What to do? Should I change the C Standard on IDE?
Here's the c code:
#include <stdio.h>
typedef char charray5[5];
charray5 carr1 = { 'a', 'b', 'c', 'd', '\0' };
charray5 carr2 = { 'q', 'w', 'e', 'r', '\0' };
charray5 carr3 = { 'x', 'y', 'z', '...
39
votes
6
answers
3k
views
Can volatile variables be read multiple times between sequence points?
I'm making my own C compiler to try to learn as much details as possible about C. I'm now trying to understand exactly how volatile objects work.
What is confusing is that, every read access in the ...
0
votes
3
answers
77
views
Is it okay to type-pun allocated structs varying only in const-ness?
In C (let's say C11 if we need to specific), is the following program well-defined? Will it always print a=3 b=4 or could compiler optimizations affect the output?
(The real-world motivation is to ...
1
vote
1
answer
169
views
Apparently erroneous implicit declaration of function warning
I am getting a warning for lstat being implicitly declared, but it should be explicitly declared in the included header.
Compiling the following:
// Standard library
#include <stdio.h>
// ...
0
votes
0
answers
161
views
Is there an glibc equivalent for MinGW's _aligned_offset_malloc?
I'm using a 64 bits Linux machine, with C11 and _GNUSOURCE flag (and C++17 if necessary, but I'd like to keep it to C).
I need to dynamically allocate some data (variable size X from a few KBs to MBs, ...
0
votes
0
answers
58
views
Returning the 2D array from function in C11 [duplicate]
I am trying to pass the input array to the main() function, but the array is still empty after executing the enter_array() function.
I get the message "Process finished with exit code -1073741819 ...
2
votes
1
answer
384
views
Atomic array in C11
Based on the reference, the _Atomic type specifier can not be used on arrays. Is there a standard way/workaround to be able to issue atomic_exchange calls for an object that is stored within the array?...
4
votes
0
answers
166
views
How can I use c11 headers in openbsd?
I want to use specific c11 macros in my C code, but they aren't available. I installed gcc 8.4 via pkg_add on a fresh OpenBSD 7.1 install. /usr/include/float.h is missing any c11 definitions.
gcc-8.4:
...
3
votes
1
answer
325
views
How to use C11 keyword _Generic with struct?
Related
Syntax and Sample Usage of _Generic in C11
_Generic works with typedef structs or only with primitive types?
How do you use _Generic with structs that are typedef-ed in C?
Good day, everyone!...
0
votes
2
answers
96
views
Check if Compiler supports optional C feature in the source
I have a simple program (a VERY simplistic malloc/free implementation, originally written for a university assignment), which has to check the "validity" of the pointers in the free, for ...
0
votes
2
answers
477
views
How to align but with offset
Say I need to allocate struct A but b needs to be 4 aligned.
struct A
{
char a;
char b[42];
};
I guess I can manually pad a pointer returned by malloc(). Any cleaner way? Like gcc's ...
1
vote
2
answers
6k
views
Error - 'for' loop initial declarations are only allowed in C99 or C11 mode
[Error] 'for' loop initial declarations are only allowed in C99 or C11 mode
[Note] use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code
I'm giving the above problem's solution,...
-1
votes
1
answer
1k
views
Initializing struct in function call parameter [duplicate]
I have code like this:
typedef enum {
API_SIG_SERVER_CONNECTED = 1,
API_SIG_DATA_RECEIVED,
} api_signals_t;
typedef struct {
api_signals_t signal;
void * params;
} api_msg_t;
static ...
-4
votes
1
answer
133
views
Why isn't an IEC 60559 conformant implementation required to define __STDC_IEC_559__ (to 1)?
The C (C99+) standard requires (though implicitly) a conforming implementation to define __STDC__ to 1.
However, the C standard does not require an IEC 60559 conformant implementation to define ...
1
vote
1
answer
569
views
GCC undefined reference to thrd_create() in C11 mode after #include <threads.h> in Debian
I'm trying to compile a program that I've been able to compile on several other Debian environments with no issues using the C11 <threads.h> library on a relatively fresh install of Debian ...
0
votes
0
answers
133
views
Issues implementing symbol resolution for crash handling on an executable compiled with Mingw-w64
I'm developing a C (NOT C++) application for Windows using the Mingw-w64 toolchains. As part of the functionality I'm in need of a crash handler and as such, have been searching for solutions. I ...
0
votes
1
answer
82
views
C11 variadic macro : put elements into brackets [duplicate]
I'm looking at a macro, or more likely a combination of macros, that would achieve the following effect :
BRACKET(a) => { a }
BRACKET(a, b) => { a }, { b }
BRACKET(a, b, c) => { a }, { b ...
0
votes
2
answers
270
views
Is _Thread_local independent from __STDC_NO_THREADS__?
It seems that currently _Thread_local is independent from __STDC_NO_THREADS__.
Consequence: even if an implementation defines __STDC_NO_THREADS__ to 1, then it still needs to support (at least to ...
1
vote
1
answer
62
views
Is a string literal converted to bool valid as part of a constant initializer expression?
enum {
compile_time_assertion = 1 / ("description" && 1);
};
Does the above snippet conform to C11? Having a string literal as part of the expression may violate one of the many ...
0
votes
1
answer
165
views
How to create a vector of objects efficiently in C++?
vector<vector<int>> v(100, vector<int>(100)); // 101 vector<int> are created
I need to created a vector of vectors (or objects) like above. But then there will be 101 vector&...
6
votes
3
answers
251
views
_Static_assert in unused generic selection
It looks like the typeof operator is likely to be accepted into the next C standard, and I was looking to see if there was a way to leverage this to create a macro using portable ISO-C that can get ...
1
vote
1
answer
86
views
Aliasing array of unions
This is a follow-up question to this question.
The following code is on Compiler Explorer.
#include <stddef.h>
#include <stdint.h>
union my_type
{
uint8_t m8[8];
uint16_t m16[4];
...
4
votes
3
answers
319
views
C union type punning arrays
Given the following code, I have some questions related to type punning. I do not see any way that this isn't violating strict aliasing rules, but I cannot point to the specific violation. My best ...
1
vote
1
answer
229
views
Are overlapping C for loop variable definitions allowed? [duplicate]
Recently, I accidentally wrote C code that looks like this:
for (size_t i = 0; i < SOME_VALUE; ++i)
{
for (size_t i = 0; i < ANOTHER_VALUE; ++i)
{
// do work with 'i' from inner ...
2
votes
3
answers
507
views
Are static variables automatically thread local?
Are local static variables automatically thread local, or are they shared between threads?
void f() {
static int x; // <-- need explicit _Thread_local ?
}
-2
votes
1
answer
77
views
Why I can't assign struct returned by a function to struct?
I wrote "lightweight" time library and I have struct and typedef like this:
struct tmt {
uint16_t year;
uint8_t month;
uint8_t day;
uint8_t hour;
uint8_t minute;
...
1
vote
1
answer
395
views
Can't resolve C warning related to pointer array in STM32CubeIDE
Facing a a warning which we are not able to get rid of. I am using stm32 MCU and STM32CubeIDE with a standard C11 compiler.
I think I can understand why the compiler is throwing the warning but the ...
1
vote
0
answers
191
views
Can I use thread.h from C11 in C++?
I don't want to use C++11 std::thread because of exceptions. Is it possible to use C11's threads from C++ (for some version)?
0
votes
1
answer
552
views
Function as condition in for loop - does anyone write like this in C?
I'm re-discovering C after years of using high level languages like C#, Java and I'm messing around with generic-like functions, I'm trying to write something like functors etc.
I wrote something like ...
1
vote
2
answers
166
views
Generic function typedef in C - how to get rid of initialization warning?
I'm experimenting with generic-like code and I have a function like this (a lot of not relevant code removed):
typedef uint8_t (*struct_converter_t)(void *, char *);
void convert_struct(
...
0
votes
0
answers
17
views
What `(void)thread_id;` means/does in c? [duplicate]
I have found something like
(void)thread_id;
in CMSIS-RTOS API.
Here is whole function:
/**
* @brief Create and Initialize a Message Queue
* @param queue_def queue definition referenced with \ref ...
6
votes
4
answers
1k
views
Is conversion of a function pointer to a uintptr_t / intptr_t invalid?
Microsoft extensions to C and C++:
To perform the same cast and also maintain ANSI compatibility, you can cast the function pointer
to a uintptr_t before you cast it to a data pointer:
int ( * pfunc )...
1
vote
0
answers
70
views
Is it considered normal that under FE_DOWNWARD or FE_TOWARDZERO expression FLT_MAX * FLT_MAX evaluates to FLT_MAX?
Sample code:
#include <float.h>
#include <fenv.h>
#pragma STDC FENV_ACCESS ON
int main(void)
{
if (fesetround(RM) != 0) return 2;
return ((FLT_MAX * FLT_MAX) == FLT_MAX) ? 0 : 1;
...
2
votes
2
answers
74
views
A pointer-to-a-VLA-typed controlling expression in a _Generic expression
Why doesn't this static assertion trigger on major compilers?
void test(int x){
_Static_assert(_Generic( (char(*)[x])0,
char (*)[1]: 1, default: 0),"");
}
https://gcc.godbolt.org/z/...
1
vote
0
answers
218
views
How to do closest guess on C99, C11 or C17?
I have a source code of C-project that has more than 600 c-files in it and it compiles for ARM mcu using GCC toolchain. I wonder how can I make a closest guess about which of the C standard this c-...