Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Code Reviews

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Posts tagged c

15 child tags

Use this tag for questions concerning use of the C programming language and for questions containing code in the C language. Avoid using it together with the C++ tag unless your question is explicitly about code compatibility or differences between C and C++.

Unless the question explicitly mentions which version of the C standard that is uses, the current active version of ISO 9899 is assumed (C17/C18).

When applicable, always include information about which compiler and target system you are using.
Tag Wiki

The C language is a general-purpose programming language developed by Dennis Ritche at Bell Labs in the early 1970s. The original purpose was kernel- and application programming for the UNIX operative system, which is why many of the standard functions and libraries in C originate from UNIX.

In the 70s and 80s, there was no formal standardization of C. The closest thing to a canonical source was the book The C Programming Language, written by Brian Kernighan and Dennis Ritchie. The book is referred to as "K&R" (Kernighan & Ritchie) and everything before standardization is therefore usually called "K&R C".

The first standardization of the language was carried out in USA, by ANSI in the year 1989. This first version is therefore widely known as "ANSI C". One year later in 1990, the ANSI standard was adopted internationally by ISO, why this version of the language is also often called "C90". Technically, "ANSI C", "C89" and "C90" are identical.

The language standard has since then been revised several times:

  • a minor revision in 1995 ("C95")
  • a major revision in 1999 ("C99")
  • a major revision in 2011 ("C11")
  • a minor revision in 2017 ("C17" or "C18", since it was released by the committee in 2017 but published by ISO in 2018).

These revisions are typically backwards compatible, but modern C code will not run on older compilers. C17 is the current standard. The C language is technically owned by ISO and it is maintained by the ISO committee JTC1/SC22/WG14.

The formal name for the standard is ISO/IEC 9899 - Information technology - Programming languages - C. Draft versions of the C standard are usually available free of charge, such as draft N1548 of C11.

60%
+1 −0
Code Reviews A simple implementation of a mutable String in C

(Brought over from SE.) The following code implements a simple interface to operate on mutable* Strings in C. It is composed of two files: one for the structure definition and the available oper...

2 answers  ·  posted 10h ago by aura-lsprog-86‭  ·  last activity 2h ago by Lundin‭

Question c string
80%
+6 −0
Q&A 2D-array pointer as a struct member

I have an array of struct: static struct basket baskets[MAX_ITEMLEN + 1]; struct basket { char *items; // malloc(itemlen * itemcount) int itemcount; }; char *items does all I nee...

2 answers  ·  posted 1mo ago by wasIzy‭  ·  last activity 1mo ago by Alexei‭

Question c array
77%
+5 −0
Q&A How does the strict aliasing rule enable or prevent compiler optimizations?

Inspired by https://meta.stackoverflow.com/questions/432242, and the relevant main-space questions. I have heard that C and C++ something called the "strict aliasing rule", which means for exa...

2 answers  ·  posted 3mo ago by Karl Knechtel‭  ·  last activity 1mo ago by Lundin‭

66%
+2 −0
Q&A How do I get a hexadecimal string representation of a GdkRGBA struct in C?

I have a program that reads a hexadecimal string representation from a configuration file, turns it into a GdkRGBA struct from Gdk, uses gtk_color_dialog_button_set_rgba () to set it as the default...

1 answer  ·  posted 1mo ago by Newbyte‭  ·  last activity 1mo ago by Newbyte‭

Question c
72%
+6 −1
Q&A Why is atoi dangerous and what should be used instead?

According to Which functions in the C standard library must always be avoided?, the atoi family of functions is dangerous and should never be used for any purpose. The rationale given in the answer...

1 answer  ·  posted 2mo ago by Lundin‭  ·  last activity 2mo ago by Alexei‭

Question c atoi strtol
84%
+14 −1
Q&A What is the difference between operator precedence and order of evaluation?

When doing something simple such as this int a=1; int b=2; int c=3; printf("%d\n", a + b * c); then I was told that operator precedence guarantees that the code is equivalent to a + (b * c)...

2 answers  ·  posted 4y ago by Lundin‭  ·  last activity 3mo ago by Alexis Wilke‭

66%
+2 −0
Q&A typeof_unqual behaves differently in gcc and clang

C23 6.7.3.6 contains this (informative) example demonstrating the use of typeof_unqual: const char* const animals[] = { "aardvark", "bluejay", "catte", }; typeof_unqual(animals) anima...

1 answer  ·  posted 3mo ago by Lundin‭  ·  edited 3mo ago by hkotsubo‭

Question c gcc clang c23 typeof
81%
+7 −0
Q&A Common string handling pitfalls in C programming

This is a self-answered Q&A meant as a C string handling FAQ. It will ask several questions at once which isn't ideal, but they are all closely related and I'd rather not fragment the post in...

2 answers  ·  posted 3y ago by Lundin‭  ·  last activity 3mo ago by Karl Knechtel‭

91%
+19 −0
Q&A What is C23 and why should I care?

The C language has gone through many iterations and the latest one not yet released is informally called "C23", supposedly because they hoped to release it in 2023. The latest draft version N3096 i...

1 answer  ·  posted 1y ago by Lundin‭  ·  edited 3mo ago by Lundin‭

Question c c23
60%
+4 −2
Q&A Which platforms return a non-null pointer on malloc(0)

What is the portability of malloc(0);? Which platforms return NULL without setting errno? Which platforms return a non-null pointer? Do any platforms have some other behavior?

2 answers  ·  posted 5mo ago by alx‭  ·  last activity 4mo ago by Alexei‭

Question c malloc
77%
+5 −0
Q&A How do you implement polymorphism in C?

The topic of how to implement polymorphism in C tends to pop up now and then. Many programmers are used to OO design from higher level languages and supposedly OO is a "language-agnostic" way of pr...

1 answer  ·  posted 4mo ago by Lundin‭  ·  last activity 4mo ago by Lundin‭

77%
+5 −0
Q&A make: How to compile all files in a directory.

I am learning how to write makefile to compile a c program. I have a directory structure like this: . ├── include │   └── library.h ├── lib │   └── library.c ├── makefile └── obj My makef...

1 answer  ·  posted 4mo ago by Vanity Slug ❤️‭  ·  last activity 4mo ago by Vanity Slug ❤️‭

Question c make
71%
+3 −0
Q&A How can I get the same "not all control paths return a value" behaviour across Clang and MSVC?

I've recently discovered that it's not actually an error to have control reach the end of a non-void function without returning anything, it's merely undefined behaviour. I want to promote the rele...

2 answers  ·  posted 5mo ago by Hyperlynx‭  ·  last activity 4mo ago by celtschk‭

Question c++ clang msvc
71%
+3 −0
Q&A multi-arch code fails on mingw 64bits

I have a C/++ codebase that compiles in Windows x86_64 targeting x86 using the mingw32 32-bit shell of the MSYS64 project. (It also compiles fine on linux x86/x86_64 and android's 4 archs.) Howev...

0 answers  ·  posted 6mo ago by JohnRando‭  ·  edited 5mo ago by JohnRando‭

Question c c++ mingw
81%
+7 −0
Q&A Why is the new auto keyword from C++11 or C23 dangerous?

In older C and C++ standards, the auto keyword simply meant automatic storage duration. As in the compiler automatically handles where the variable is stored, typically on the stack or in a registe...

2 answers  ·  posted 7mo ago by Lundin‭  ·  last activity 6mo ago by celtschk‭

66%
+4 −1
Code Reviews New elementsof() operator

Original problem: https://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c/57537491#57537491 Arrays in C are problematic. They are transformed into pointers too easi...

1 answer  ·  posted 6mo ago by alx‭  ·  edited 6mo ago by alx‭

Question c array operators
75%
+4 −0
Q&A Managing a dependency for a C application

One of my applications has a dependency on a stb-library. Now I am publishing this application to Github. Should I: Include the header that was used during development with the code? Have an in...

2 answers  ·  posted 8mo ago by Melkor-1‭  ·  edited 7mo ago by matthewsnyder‭

Question c package
71%
+3 −0
Q&A Are these two function pointer declarations equivalent?

Say I have two functions: FILE* get_input(const char fname[static 1]); FILE* get_output(const char fname[static 1]); And I wish to declare a function pointer and assign it the result of some p...

1 answer  ·  posted 8mo ago by Melkor-1‭  ·  edited 8mo ago by Melkor-1‭

Question c c23 typeof
72%
+6 −1
Q&A What's the fastest way to copy, preserving file attributes, on each platform?

I am looking copy files platform-dependently on Linux, Oracle Solaris, MacOS, BSDs, and Windows. I have found some system calls for each platform, namely sendfile(), copy_file_range(), fcopyfile()...

0 answers  ·  posted 8mo ago by Melkor-1‭  ·  last activity 8mo ago by Melkor-1‭

81%
+7 −0
Q&A Where to place digit separators in C23?

C23 introduces the digit separator ' which can be placed anywhere inside an integer constant for the purpose of clarity and self-documenting code. These are otherwise ignored by the compiler when d...

2 answers  ·  posted 8mo ago by Lundin‭  ·  last activity 8mo ago by John C‭

77%
+5 −0
Q&A Testing an opaque type's internals

First: What is an opaque pointer in C? Now when it comes to testing such a type, I know of 3 ways: Include the source file (the one containing the definition of the type and the functions tha...

2 answers  ·  posted 9mo ago by Melkor-1‭  ·  last activity 9mo ago by Alexei‭

89%
+15 −0
Q&A What does the static keyword do in C?

What exactly does the static keyword do in C? I have seen it used in several diverse contexts: 1) As a variable outside a function: #include <stdio.h> static int x = 5; int main (void) ...

2 answers  ·  posted 3y ago by Lundin‭  ·  last activity 9mo ago by aghast‭

66%
+2 −0
Q&A How can I manage multiple consecutive strings in a buffer (and add more later)?

This question is inspired by If I have a char array containing strings with a null byte (\0) terminating each string, how would I add another string onto the end? on Stack Overflow. Suppose I have...

2 answers  ·  posted 11mo ago by Karl Knechtel‭  ·  last activity 10mo ago by Lundin‭

Question c string
80%
+6 −0
Q&A Can I access an array element from a pointer to an object contiguous with but outside the array?

C prohibits accessing an array out of bounds even if measures were taken to ensure that what should lie outside those bounds were known: struct MyStruct { int x[2]; int y, z; }; static...

1 answer  ·  posted 11mo ago by CPlus‭  ·  last activity 11mo ago by Lundin‭

66%
+2 −0
Code Reviews Pattern / architecture for interfacing with components in C

I'm working on the architecture, where the assumption is to easily extend the options in the system and also to provide some kind of encapsulation (from the main we could only access the type1 / ty...

1 answer  ·  posted 11mo ago by patriko8520‭  ·  last activity 11mo ago by Lundin‭

Question c architecture
89%
+15 −0
Q&A Is strcpy dangerous and what should be used instead?

I heard rumours that the strcpy function is dangerous and shouldn't be used. Supposedly it can be exploited to create buffer overflows somehow. And indeed when I compile my C code in the admittedl...

2 answers  ·  posted 4y ago by Lundin‭  ·  last activity 11mo ago by Lundin‭

66%
+2 −0
Code Reviews A small header-only input output library

The library (inspired by stb libraries) attempts to provide some commonly used functions (reading a file into memory, determining the size of a file) that are missing from the C standard library po...

0 answers  ·  posted 12mo ago by Melkor-1‭  ·  edited 11mo ago by Melkor-1‭

Question c string input output
77%
+5 −0
Q&A Are there technical reasons to pick one struct coding style over the other?

C offers two different styles when it comes to structs (and union/enum too). Either declare them using a struct tag only ("struct tag style"): struct my_type { ... }; struct my_type x; Or ...

1 answer  ·  posted 12mo ago by Lundin‭  ·  last activity 12mo ago by Lundin‭

Question c code-style struct
66%
+2 −0
Code Reviews Trie Implementation, Graph Visualization and Auto-Completion in C

Given a list of strings (say a text file containing some C symbols: c-symbols.txt), the program can: Generate a graph of the underlying trie (-p/--prefix can be specified to inspect a specific ...

1 answer  ·  posted 12mo ago by Melkor-1‭  ·  last activity 12mo ago by Lundin‭

77%
+5 −0
Q&A What is the purpose of having underscored names and then defining a non-underscored alias to it?

In a C implementation in <stdio.h> on Linux I saw something like: extern FILE *__stdinp; extern FILE *__stdoutp; extern FILE *__stderrp; And then: #define stdin __stdinp #define stdou...

1 answer  ·  posted 1y ago by 90 98‭  ·  last activity 1y ago by aghast‭

78%
+9 −1
Q&A What is do { } while(0) in macros and should we use it?

Background I can see the need to use { } when implementing a function-like macro such as this one: #define HCF(code) fprintf(stderr, "halt and catch fire"); exit(code); Because if we use the f...

2 answers  ·  posted 4y ago by Lundin‭  ·  last activity 1y ago by Karl Knechtel‭

Question c code-style macros
77%
+5 −0
Q&A When should I parenthesize macro arguments in C?

I've seen macros use parentheses to enclose its arguments. Most of these make sense, as in #define sum(a, b) ((a) + (b)) The outer prevents the following: #define sum_bad(a, b) (a) + (b) ...

2 answers  ·  posted 1y ago by alx‭  ·  last activity 1y ago by Lundin‭

Question c macros
80%
+6 −0
Q&A What is the meaning of "short circuit" operators?

When reading about various operators used by programming languages, the term "short circuit behavior" is often used. For example in this C code: int a = 0; a && b++ Someone explained t...

2 answers  ·  posted 1y ago by Lundin‭  ·  last activity 1y ago by matthewsnyder‭

83%
+8 −0
Q&A How to declare variable-length arrays correctly?

This is meant as a FAQ Q&A regarding variable-length arrays (VLA). The two bugs described below are surprisingly common. I'm trying to use the variable-length arrays feature of C99 (and newe...

1 answer  ·  posted 3y ago by Lundin‭  ·  last activity 1y ago by Lundin‭

77%
+5 −0
Q&A Why does calloc accept 2 arguments, and with what arguments should one call it?

According to the standard (C17 draft, 7.22.3.2) The function calloc void *calloc(size_t nmemb, size_t size); "allocates space for an array of nmemb objects, each of whose size is size [and] i...

1 answer  ·  posted 1y ago by Lover of Structure‭  ·  last activity 1y ago by Lundin‭

85%
+10 −0
Q&A How to properly use malloc?

I have seen this construct quite a lot: int *p = (int*) malloc(n * sizeof(int)); Is this how it should be? What is the proper way to use malloc? I have also seen this: int *p = malloc(n * size...

3 answers  ·  posted 3y ago by klutt‭  ·  last activity 1y ago by Lundin‭

83%
+8 −0
Q&A What are disadvantages of static functions (ie functions with internal linkage) in C?

Functions in C have external linkage by default. In other words, the storage class specifier extern is applied to functions by default, with the effect that they are visible to all translation unit...

3 answers  ·  posted 1y ago by Lover of Structure‭  ·  last activity 1y ago by Dirk Herrmann‭

Question c linkage
40%
+2 −4
Q&A constructor in C

#include<stdio.h> struct Book { char title[20]; char author[20]; int pages; }; void init_Book_types(struct Book* aTitle,struct Book* aAuthor,struct Book* aPages){ aTitle->ti...

2 answers  ·  posted 3y ago by deleted user  ·  last activity 1y ago by __blackjack__‭

Question c constructor
84%
+9 −0
Q&A Are there references in C?

When reading posts at programming sites such as this one, I frequently encounter people saying things like: "There is no pass-by-reference in C, everything is passed by value." People claiming su...

3 answers  ·  posted 3y ago by Lundin‭  ·  last activity 1y ago by Alexei‭

77%
+5 −0
Q&A Storing more bytes than a union member has, but less than the union size, with memcpy(3)

Let's say we have an object, we store it in a union (into some other narrower type, but with memcpy(3), so it's allowed --I guess--), and then read it from the union via it's original type (so no a...

2 answers  ·  posted 1y ago by alx‭  ·  last activity 1y ago by Lundin‭

77%
+5 −0
Q&A Can freed pointers undergo lvalue conversion?

char *p, *q; p = malloc(1); free(p); q = p; // lvalue conversion Is the last lvalue conversion (= p;) Undefined Behavior or not? We didn't take the address of the local p. C11::6.3.2.1...

2 answers  ·  posted 2y ago by alx‭  ·  last activity 2y ago by Dirk Herrmann‭

84%
+9 −0
Q&A memcmp(3) memory containing invalid values

What does it mean that we can use memcmp(3) on invalid values? ISO C allows comparing an invalid value through memcmp(3), because it doesn't read the value, but rather its representation, and "rea...

2 answers  ·  posted 2y ago by alx‭  ·  last activity 2y ago by Lundin‭

83%
+8 −0
Q&A Is partial allocation of an object Undefined Behavior?

Is it valid to partly allocate an object, as long as you only use the allocated part of it? #include <stdio.h> #include <stdlib.h> struct s { int i[100]; }; int main(void) {...

1 answer  ·  posted 2y ago by alx‭  ·  last activity 2y ago by Lundin‭

71%
+3 −0
Q&A Strict aliasing rules and function boundaries

Let's analyze this code, assuming an architecture where the alignment of int64_t is the same as that of double: void bar(double *f, int64_t *j) { *(int64_t *)f = *j; } void foo(void) ...

1 answer  ·  posted 2y ago by alx‭  ·  edited 2y ago by Lundin‭

70%
+5 −1
Code Reviews stpecpy(): Design a better string copy function that truncates

I was directed a few days ago to a post about a string copy function, which IMO improves the commonly known string copy functions, including strlcpy(3BSD), strlcat(3BSD), and strscpy(9). It define...

2 answers  ·  posted 3y ago by alx‭  ·  last activity 2y ago by alx‭

71%
+3 −0
Q&A How to write a macro that discards the const qualifier, for any type?

How to write a macro that discards the const qualifier, for any type? I hope some combination of typeof and a cast will do, but haven't found the combination. I tried this, without luck: #define...

2 answers  ·  posted 2y ago by alx‭  ·  last activity 2y ago by alx‭

70%
+5 −1
Q&A C naming convention, module trigrams?

For my company, I'm writing naming conventions for embedded code in C language. Function names must be named in lowerCamelCase() and start with a verb. Global variables are in Maj_started_lowe...

3 answers  ·  posted 2y ago by AdriZ‭  ·  last activity 2y ago by Dirk Herrmann‭

71%
+3 −0
Q&A Cast uninitialized variable to (void)

Is it undefined behaviour to cast an uninitialized variable to (void)? Example: int main() { int x; (void)x; return 0; }

4 answers  ·  posted 2y ago by Estela‭  ·  last activity 2y ago by Ethan‭

Question c casting
86%
+11 −0
Q&A How to do private encapsulation in C?

I'm using an object-oriented design for my C project and trying to implement classes with private encapsulation. How do I do this? Some things I've tried that are problematic: Using a struct f...

1 answer  ·  posted 3y ago by Lundin‭  ·  last activity 2y ago by Lundin‭

23%
+1 −8
Q&A How to efficiently remove every occurrence of one string inside another

I have two strings and want to remove every occurrence of the second string inside the first one starting from the beginning of the first string and as if each occurrence was removed immediately. ...

1 answer  ·  posted 3y ago by dumplings‭  ·  edited 2y ago by __blackjack__‭