1,942 questions
4
votes
3
answers
54
views
GCC C99 Disable compilation of main() without return
How to force gcc compilator throw error when int main() have no return statement. This code compiles without any errors
#include<stdio.h>
int main(){
printf("Hi");
}
I am using
...
2
votes
1
answer
119
views
Math error conditions (C99, C11, etc.) in GCC
With ISO C99 onward we have a few macros (or constants) that help understanding how some math errors (see <math.h> related) are signaled. However, to me it looks like they haven't been ...
6
votes
1
answer
120
views
Recent MSVC versions don't treat NAN as constant, workaround?
Recent MSVC versions don't seem to treat NAN as a constant anymore. The new definition appears to be (__ucrt_int_to_float(0x7FC00000)). The old one was (-(float)(((float)(1e+300 * 1e+300)) * 0.0F)).
...
0
votes
2
answers
86
views
Is it legal to have a function prototype slightly different from its definition?
//my_struct.h
typedef struct my_struct_t *my_handle;
void f1(my_handle handle);
void f2(my_handle handle);
//my_struct.c
#include "my_struct.h"
typedef struct
{
int a;
int b;
} ...
2
votes
1
answer
116
views
Is it safe and defined behaviour to cast a pointer to another level of indirection?
I am working with nested sparse arrays of pointers and want to generalise allocation and initialisation of the pointers at each level.
I decided to use a for loop to iterate over each level, however, ...
2
votes
2
answers
97
views
Translation limits of an identifier in C
I have started to write my own C compiler for the purpose of education. My goal is to be mostly C99 standard conform. Now I have a question about the maximum length of an identifier.
This is an ...
0
votes
1
answer
55
views
CMake / QNX 6.5 does not understand gnu99 standard
I have already been made aware of this question, in which the OP seeks to use C++11 standards.
And I understand that - regrettably - C++11 is just too new for QNX 6.5.
But my question relates to the ...
1
vote
1
answer
70
views
Using clang and trying to generate a visual studio solution with premake doesn't compile with "llvm-lib.exe exited with code 1"
I have 2 premake files 1 for my renderer, and one for my engine but when I generate a vs2022 solution via the command premake5 vs2022 trying to build said solution hits me with a
I would like to know ...
2
votes
1
answer
129
views
How to prevent reordering of calls to a function that prints log messages in C?
typedef enum {
A = 0,
B = 1,
C = 2
} my_enum;
/**
* @warning If random is NULL or blabla is invalid the behavior of this function is undefined.
*/
int foo(int *random, my_enum blabla)
{
...
1
vote
2
answers
174
views
How to perform accurate FixedPoint Number Calculation in C?
How to perform accurate FixedPoint Number Calculation in C ??
I use the struct in C to store the integer part and the fraction part.
I guess the function is incorrect, maybe didn't handle the overflow ...
0
votes
3
answers
192
views
Can I cast an unsigned long int into uintptr_t without losing bits?
uintptr_t uint_to_uintptr(unsigned int n) {
return (uintptr_t)n;
}
uintptr_t ulint_to_uintptr(unsigned long int n) {
return (uintptr_t)n;
}
uintptr_t ullint_to_uintptr(unsigned long long int ...
0
votes
0
answers
142
views
I don't know what i'm doing wrong(BEECROWD 2035)
I need help with the following question:
Argentina's rugby is currently in one of its best moments of all time.
Recently the under-18 and under-21 national teams qualified for their
corresponding ...
0
votes
0
answers
43
views
Volatile struct member assignment: side effects / undefined behavior
I am considering the following code:
#include <stdint.h>
typedef struct {
uint32_t a;
} foo_t;
volatile foo_t foo;
volatile uint32_t b;
int main(void)
{
foo.a = b;
return 0;
}
In ...
1
vote
1
answer
70
views
Typing of Array in C structure is "overflowing" into subsequent fields?
I have the following types:
typedef float vec4[4]; // from cglm
typedef struct Node Node;
struct Node {
float expand;
vec4 color;
Node *children;
}
For some reason, the color field's ...
0
votes
0
answers
381
views
ScreenCaptureKit example in Go/C
I'm working on improving open source library to capture screenshots: https://github.com/kbinani/screenshot - it is somewhat popular and it uses legacy approach, and MacOS 15 (beta as of time of ...
1
vote
1
answer
71
views
format string and vararg - can I tell compiler to check them?
I have the following function for Logging purposes:
void LogE(const char* scope, const char* s, ...) {
fprintf(stderr, "%s : ", scope);
va_list list;
va_start(list, s);
...
2
votes
1
answer
76
views
Using previously set fields when initializing structure with compound literals
What is the expected behavior of referring to previously set fields, when declaring initial values for a struct using compound literal: Specifically is it ok to: struct foo v = { .v1 = ..., .v2 = .v1+...
0
votes
2
answers
128
views
GCC compiler handles "char * str" and "char str[]" with the & operator differently [duplicate]
The compiler treats the types char * var and char var[] differently when it comes to taking the address of a variable via the & operator. Here's a little snippet of code to demonstrate:
#include &...
0
votes
3
answers
138
views
When inlining a C function, can an optimizing compiler dereference a pointer more times than explicitly written in the source code?
Consider the following code:
int y = 1;
int* p = &y;
inline int f(int x) {
return x + x;
}
int g(void) {
return f(*p);
}
In this code, there is one explicit dereference.
Is a C compiler ...
2
votes
1
answer
119
views
IEEE floating-point rounding in C
I am having trouble to understand a specific IEEE double computation. Take the following C99 program, that runs on a host with IEEE double (8 bytes, 11 bits biased exponent, 52 bits encoded mantissa):...
3
votes
0
answers
98
views
C99's Non-Null Array Syntax Pointers for Parameters for MSVC
The following code:
int swap(int a[static 1], int b[static 1])
{
...
}
works with GCC, Clang, and ICX, but MSVC chokes on static in array parameters (or so I have been apprised by @Davilsor).
GNU ...
3
votes
0
answers
106
views
Did gcc and clang accidentally drop C99 sequence point conformance?
Background:
C99 6.5 §2 said:
Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value ...
3
votes
3
answers
189
views
Is recursively calling main from its own parameters (abusing sizeof with VLAs) standard C99?
Disclaimer: my question isn't practical at all, it's more a question about 2 codes which allegedly abuse the rules and somewhat compile (with more or less warnings / errors according to the compiler).
...
1
vote
0
answers
122
views
Decryption program only returns the first 40 characters
I was writing a simple encryption and decryption C program and encountered a bug where the program would not receive more than 40 character of input and would only return the first 40 character when ...
-1
votes
1
answer
60
views
Assign one struct variable to another of the same type which has bit fields in it, in C, UB?
I asked AI (Claude), and it told me it is UB. Is that real?
#include <stdio.h>
struct BitFieldStruct {
unsigned int a : 5;
unsigned int : 3; // 3位填充位
unsigned int b : 6;
...
1
vote
1
answer
122
views
Undefined behavior with pointer casts in C99 and MISRA C:2012
I'm working on a C99 project that requires us to follow the MISRA C:2012 standard.
Rule 11.3 essentially prevents from casting a pointer between different object types without declaring a justified ...
0
votes
2
answers
59
views
Why isn't indexing a 2D Array by pointer arithmetic the same as using [j] brackets, though the spec says it should be?
Learning C, I stumbled upon this video on which page 82 of the C99 specification appears (though it's dated 2007, maybe a revision?).
Therein it is stated that any expression that indexes arrays using ...
4
votes
2
answers
97
views
Are bitmasks mandatory for unsigned conversions?
I'm implementing a toy project to learn C and I have a seemingly simple question about unsigned type conversion rules.
In particular, I would like to know if the C standard expects unsigned types ...
1
vote
1
answer
103
views
Why "static" specifier generates an external definition for inline function?
In C99 standard for inline specifier(6.7.4 paragraph 6) it states:
"If all of the file scope declarations for a function in a translation
unit include the inline function specifier without ...
4
votes
2
answers
123
views
Function pointer compatibility between single pointer and empty parameter lists
I've been reading about function pointer compatibility, but have not found the following scenario documented as being acceptable (below).
With this code, it is allowed (without warnings) to call a ...
2
votes
2
answers
134
views
Is this use of va_copy undefined behaviour?
I made a function that prints pairs of keys and values, where the keys are trusted compile time literal strings which possibly can contain printf specifiers. Now my question is: is this function legal ...
3
votes
4
answers
88
views
cascaded calls to variadic functions in C
I need to make a wrapper of vprintf, so that multiple implementations of a printf like function can be done. This code demonstrates the issue:
#include <stdio.h>
#include <stdlib.h>
#...
2
votes
1
answer
97
views
Why does return of a compound literal (C99) generate more assembly code?
I have two functions that return a struct, one returns it via compund litearal and the other initializes a variable to then return it:
typedef struct {
int storage[256];
} data_t;
data_t ...
0
votes
1
answer
162
views
Forcing C Standard in CMake
I am trying to use C11 instead of C99 in my CMake project. I tried setting the standard like this:
set(CMAKE_C_STANDARD 11)
And like this as well:
set_property(TARGET vkvoxel PROPERTY C_STANDARD 11)
...
3
votes
1
answer
135
views
Do C compilers follow the "formal definition of `restrict`"?
Consider this code:
extern int A[2];
/* Just returns `p` back. */
extern int *identity(int *p);
int f(int *restrict p)
{
int *q = identity(p); /* `q` becomes "based on" `p` */
int ...
0
votes
1
answer
67
views
Weird macros for defining constants
I stumbled on the following stuff in someone's source code in C:
typedef struct {
u32 reg;
} reg_t;
#define _REG(r) ((const reg_t){.reg=(r)})
#define REG_A _REG(123)
#define REG_B _REG(456)
...
...
1
vote
2
answers
122
views
Is up-casting numeric types in C always reversible?
EDIT: Oh dear I feel foolish. Of course you're not going to be able to cast from longs to doubles and back - longs have more significant bits. Will casting to a long double improve this?
I'm ...
-1
votes
2
answers
201
views
Undefined behaviour in C in i=i++; but what about i=++i;?
I have studied sequence points (C99) and sequenced before /unsequenced (C17) and some other posts here in SO about this topic and its relation with undefined behaviour.
I think with C99 its ultra-...
0
votes
1
answer
72
views
Ambiguous behavior by GCC when it ought to decide scope for a function declaration
By reading the C99 standard specification draft (n1256) section 6.7.5.3 paragraph 17, from what I can take away the function declarations (including implicit) inside a block scope shall have a block ...
1
vote
0
answers
50
views
What is this example of modifiable lvalue found in error.h ISO C99?
I'm reading the ISO C99 Standard and this example of a modifiable lvalue object: *errno() blew me away.
Please could you tell me if this possible implementation of errno is ISO C99 compliant?
int * ...
1
vote
2
answers
208
views
Delete comments from a C file
Purpose of work: to learn how to work with files using the functions of the standard C library
Task:
There is a file with a C program. It is necessary to delete all comments from it and write the code ...
1
vote
0
answers
666
views
Implicit declaration of function is invalid in C99 - error when installing software on macOS
I am trying to install Elmer FEM software on my MacOS 12.6.
I use:
XCode Version 14.0.1 (14A400)
Apple clang version 14.0.0 (clang-1400.0.29.102)
GNU Fortran (Homebrew GCC 13.2.0) 13.2.0
I followed ...
2
votes
1
answer
95
views
Why is this allocation not well done?
I have point.h and polygon.h files, with their associated .c files. In point.h
// point.h
#ifndef POINT_H
#define POINT_H
typedef struct Point point;
point *point_alloc(void);
void *point_free(point ...
0
votes
0
answers
69
views
Undefined reference to 'adjacency_array' [duplicate]
When I try to compile my C program I get this error:
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: ./objects/linked_list.o:linked_list.c:(.rdata$....
2
votes
1
answer
90
views
Reasonable to use a compound literal array as a temporary string buffer?
I often wish I had a simple way to construct a string with sprintf/snprintf without the hassle of defining a local array, like so:
char str[256];
snprintf(str, sizeof(str), format, ...);
use_string(...
3
votes
1
answer
145
views
vsprintf() does not print warning when having more arguments then specified in format
I was trying the create an error check while creating logs, with a simple logging mechanism. I observed that it is possible use vfprintf(), that it does not print any warning at compile time, that too ...
3
votes
1
answer
215
views
Is GCC right about this being a VLA?
This question is quite similar to "Are conformant array parameters VLAs?", with the only difference being that here I'm using the static keyword within the [ and ] of the array declaration (...
3
votes
3
answers
503
views
Problems with CORDIC for Logarithm in C
In order to get started with CORDIC for log10, I implemented the algorithm derived in this PDF, pp6:
#include <stdio.h>
#include <math.h>
// https://www.mikrocontroller.net/attachment/...
4
votes
2
answers
160
views
Is `void foo(int a[static 0]);` valid?
Is the following function strictly compliant to C99?
void foo(int a[static 0]) {
(void)a;
}
Both GCC and Clang emits warning about using zero-sized array but I don't think this warning is justified....
3
votes
4
answers
518
views
%zu format specifier with C99 not working
I'm willing to print a size_t value using the %zu format specifier in my format string, however, I always get "zu" as an output, rather than the actual value in my size_t variable:
size_t ...