Questions tagged [c]

For questions about the design of, or languages or features which are closely related to, C. Do keep in mind that C is related to but distinct from C++ (and there is no language called 'C/C++') and the appropriate tag should be used.

Filter by
Sorted by
Tagged with
16 votes
11 answers
5k views

What prevents languages from having arbitrary sized return data on the stack?

While programming, I often wonder what prevents languages from allowing arbitrary sized stack returns, like this: ...
user avatar
5 votes
9 answers
1k views

Correctness of mixed signed/unsigned arithmetic

I'm implementing signed and unsigned integers in my language. They are represented in C as signed long and unsigned long ...
Matheus Moreira's user avatar
4 votes
6 answers
522 views

How can memory addresses be compile-time constants?

In C the memory address of a statically allocated object or a function is considered compile-time constant. For example this is valid code: ...
user16217248's user avatar
  • 7,535
10 votes
1 answer
238 views

What was the rationale for making realloc(ptr, 0) have UB in C23

This is the first breaking change that C made, which was making realloc(ptr, 0) have UB instead of being roughly equivalent to ...
user1345541's user avatar
35 votes
2 answers
19k views

Why do common Rust packages depend on C code?

Chapter 1 of "The Rust Programming Language" (Klabnik and Nichols) says: [S]ome common Rust packages depend on C code and will need a C compiler. Why do Rust packages have any dependency ...
StoneThrow's user avatar
8 votes
4 answers
2k views

Why are mixed declarations more challenging to implement than forcing all declarations to be at the top of a scope?

C89 had a requirement that all declarations must appear at the top of the scope before any statements: ...
user16217248's user avatar
  • 7,535
11 votes
3 answers
495 views

When was our current definition of "undefined behavior" first used?

In my answer to another question, What was the purpose of Tartan's "disable" statement?, I asserted that it's anachronistic to use our modern C-derived definition of "undefined ...
Glenn Willen's user avatar
17 votes
3 answers
2k views

What optimizations does the strict aliasing rule facilitate?

This question is tangentially related to: Why is type reinterpretation considered highly problematic in many programming languages? Regardless how 'problematic' type reinterpretation is, why do some ...
user16217248's user avatar
  • 7,535
3 votes
2 answers
382 views

Why does the C library include fgetpos()/fsetpos() if the same functionality can be achieved with ftell()/fseek()?

C includes 2 methods for saving the position of a stream and setting it later. Using fseek() and ftell(): ...
user16217248's user avatar
  • 7,535
19 votes
10 answers
4k views

Why would accessing uninitialized memory necessarily be undefined behavior?

In C, accessing any indeterminate/uninitialized memory is undefined behavior, period. Even in the case that the type in question is guaranteed to have no trap representations, such as ...
user16217248's user avatar
  • 7,535
9 votes
8 answers
3k views

Invalid signed integer in C and similar languages

In C a signed integer is just a binary number and all binary numbers are valid integers. There is no NaN value. C uses 2s complement for signed integers meaning ...
John Rennie's user avatar
15 votes
8 answers
5k views

Why would a language need to have trap representations?

In C there is a concept of trap representations, or non-value representations. If such a value is produced or used, immediate undefined behavior is invoked. This is one of the dangers of using ...
user16217248's user avatar
  • 7,535
7 votes
6 answers
822 views

Do variadic functions need to have a different calling convention to regular functions?

In C, calling a variadic functions are 'special' and not to be treated as regular functions or vice versa. Calling a variadic function through a regular function pointer, or vice-versa invokes ...
user16217248's user avatar
  • 7,535
19 votes
4 answers
5k views

What obstacles prevented C and C++ from standardizing π?

C does not even have M_PI standardized. C++ only added std::numbers::pi very recently. Yes, the fact that this took so long does ...
user16217248's user avatar
  • 7,535
4 votes
3 answers
240 views

C source compatibility versus C linker compatibility

Due to the large amount of existing code written in C, it's common for languages to provide some sort of mechanism for interoperating with C. One approach, taken by C++ and Objective C, is to make the ...
dan04's user avatar
  • 1,809
31 votes
16 answers
5k views

How could a language make the loop-and-a-half less error-prone?

Anyone who's used C-family languages enough will have seen or used a "loop-and-a-half", which can be written in one of two ways: ...
Bbrk24's user avatar
  • 8,857
17 votes
4 answers
2k views

Why can C not be lexed without resolving identifiers?

I’ve seen it said before that C and C++ cannot be lexed without resolving identifiers to an extent. A common example is (a)*b, which could be either multiplication <...
Bbrk24's user avatar
  • 8,857
8 votes
1 answer
2k views

Why doesn't C's standard library have functions for searching memory like strings? [closed]

I can't help but to notice that not all str* functions have mem* counterparts, even when having them would make logical sense. ...
user1345541's user avatar
8 votes
9 answers
620 views

Would a structure ever require padding beyond what is required to align the members?

In C a structure can have an arbitrary amount of padding. In theory this implementation conforms to the C standard: ...
user16217248's user avatar
  • 7,535
16 votes
3 answers
965 views

What are the implications of implementing variable-length arrays?

At first C did not have variable-length arrays; all array sizes must be compile times constants. Then, in C99, variable-length arrays, a controversial addition, were added, permitting runtime-sized ...
user16217248's user avatar
  • 7,535
6 votes
2 answers
324 views

If size could be determined at compile time then why could size not be determined at preprocess time?

In C the sizeof() operator cannot be resolved in preprocessor conditions. If other operators such as + or ...
user16217248's user avatar
  • 7,535
7 votes
1 answer
167 views

Disjoint language extensions (C++ and Objective-C)

Objective-C and C++ are both extensions of C (or at least started out to be). So for the purpose of this question I allow myself the slightly inaccurate assumption that both languages are pure ...
chrysante's user avatar
  • 748
4 votes
2 answers
820 views

Why was C implemented with a register preservation convention that seems to be far less efficient than its predecessor's?

In original implementations of the B language, the caller was responsible for saving and restoring any registers it cares about before calling a function. But in the C language, the called function is ...
Ray Butterworth's user avatar
31 votes
8 answers
3k views

Why would a language have a concept of undefined behavior instead of raising an error?

Certain constructs or conditions in programming just are not allowed. Languages such as Java or Swift handle these by raising an error when encountered. C and C++ on the other hand say 'Anything could ...
user16217248's user avatar
  • 7,535
7 votes
2 answers
262 views

Are there any technical limitations against adding anonymous functions in C?

An anonymous function behaves the same as a regular function with no identifier attached. One could be passed as a callback or stored in a function pointer variable. In C++ they can be written as ...
user16217248's user avatar
  • 7,535
6 votes
3 answers
168 views

How could C implement first class arrays?

C does not have first class arrays that can be assigned (copied) or returned from functions directly. In such a low level language where arrays are essentially pointers to the first value. This way, ...
user16217248's user avatar
  • 7,535
7 votes
6 answers
474 views

Advantages/disavantages of including the comma operator?

In many C style languages the , binary operator takes 2 expressions and simply returns the second one: ...
mousetail's user avatar
  • 8,111
13 votes
10 answers
958 views

What are the advantages of strings and character arrays being different?

In many languages such as Java, strings and character arrays are distinct types. In Java one must use toCharArray() in order to use array semantics on strings. In C,...
user16217248's user avatar
  • 7,535
12 votes
3 answers
902 views

Is it possible to extend C to have the Rust concept of ownership for memory safety?

Rust has a number of memory safety features. Is it possible to extend or enhance C or C++ to also provide similar memory safety features instead of using workarounds such as the Valgrind tool suite?
James Risner's user avatar