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
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

Posts tagged c

11 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.

85%
+10 −0
Code Reviews 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 &lt;stdio.h&gt; static int x = 5; int main (void) ...

1 answer  ·  posted 1y ago by Lundin‭  ·  last activity 4d ago by Lundin‭

75%
+4 −0
Code Reviews 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...

0 answers  ·  posted 7d ago by alx‭  ·  edited 6d ago by alx‭

77%
+5 −0
Code Reviews 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 2mo ago by alx‭  ·  last activity 1mo ago by Dirk Herrmann‭

81%
+7 −0
Code Reviews 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 1y ago by klutt‭  ·  last activity 2mo ago by alx‭

84%
+9 −0
Code Reviews 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 3mo ago by alx‭  ·  last activity 3mo ago by Lundin‭

83%
+8 −0
Code Reviews 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 &lt;stdio.h&gt; #include &lt;stdlib.h&gt; struct s { int i[100]; }; int main(void) {...

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

71%
+3 −0
Code Reviews 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 4mo ago by alx‭  ·  edited 4mo ago by Lundin‭

77%
+5 −0
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 1y ago by alx‭  ·  last activity 6mo ago by alx‭

71%
+3 −0
Code Reviews 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 12mo ago by alx‭  ·  last activity 7mo ago by alx‭

70%
+5 −1
Code Reviews 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 8mo ago by AdriZ‭  ·  last activity 8mo ago by Dirk Herrmann‭

71%
+3 −0
Code Reviews 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 9mo ago by Estela‭  ·  last activity 8mo ago by Ethan‭

Question c casting
85%
+10 −0
Code Reviews 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 1y ago by Lundin‭  ·  last activity 9mo ago by Lundin‭

23%
+1 −8
Code Reviews 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 1y ago by dumplings‭  ·  edited 11mo ago by __blackjack__‭

71%
+3 −0
Code Reviews Assert that some code is not present in the final binary, at compile or link time.

I'd like to assert that some code can be optimized out, and is not present in the final binary object. #define CONSTANT 0 #if (!CONSTANT) [[landmine_A]] #endif static int foo(void); void...

1 answer  ·  posted 11mo ago by alx‭  ·  last activity 11mo ago by alx‭

33%
+2 −6
Code Reviews Regarding the implementation of data structures.

I'm attempting a question to do with data structures, file streams &amp; linked lists. The code isn't complete yet as I am still halfway working on it. I am required to use data structures in the d...

1 answer  ·  posted 1y ago by dumplings‭  ·  edited 11mo ago by Alexei‭

33%
+1 −4
Code Reviews How can I modify the code above to accept string as user input and use strcmp to compare with the contents of the text file & then delete that line?

I want to enter a string to compare with the text file, and if that word matches, then I want to delete that line containing that string. How can I modify the code below, since the code below take...

1 answer  ·  posted 1y ago by dumplings‭  ·  edited 11mo ago by Alexei‭

Question c file-handling
40%
+2 −4
Code Reviews Problems with data structures and filestreams.

So I just started learning how to use file-streams in C &amp; decided to attempt a question which is to do with library management in C, however I am currently encountering some problems and feel l...

2 answers  ·  posted 1y ago by dumplings‭  ·  edited 11mo ago by Alexei‭

55%
+3 −2
Code Reviews Why does fopen return NULL?

#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; int main (void){ FILE *cfPtr; char name[15]; int ID; if ((cfPtr = fopen("draft.txt","w")) == NULL){ printf("File could n...

2 answers  ·  posted 1y ago by dumplings‭  ·  edited 11mo ago by dumplings‭

80%
+6 −0
Code Reviews array of arrays vs array of pointers to store array of string literals

Let's consider the following code: const char a[][4] = {"aa", "aaa"}; const char *b[] = {"bb", "bbb"}; const char *const c[] = {"cc", "ccc"}; For shared libraries, both b and c arrays require...

0 answers  ·  posted 12mo ago by alx‭  ·  edited 12mo ago by alx‭

40%
+2 −4
Code Reviews Child process works only once after the parent's two calls to scanf

This program creates a child process and shares two integers (base and height) through the shared memory. The parent process asks four times to insert two integers and wait for the child process t...

1 answer  ·  posted 1y ago by Luca_Impellizzeri‭  ·  last activity 1y ago by Alexei‭

80%
+6 −0
Code Reviews How to declare variable-length arrays correctly?

This is meant as a FAQ Q&amp;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 2y ago by Lundin‭  ·  last activity 1y ago by Lundin‭

71%
+3 −0
Code Reviews Is `-isystem` a POSIX cc option?

Is -isystem/path/to/sys/includes a standard compiler option, or is it a compiler extension implemented by gcc, clang, and maybe other compilers? Can I rely on its availability? I couldn't find th...

1 answer  ·  posted 1y ago by alx‭  ·  last activity 1y ago by InfiniteDissent‭

86%
+11 −0
Code Reviews Should I check if pointer parameters are null pointers?

When writing any form of custom function such as this: void func (int* a, int* b) Should I add code to check if a and b are null pointers or not? if(a == NULL) /* error handling */ When po...

4 answers  ·  posted 1y ago by Lundin‭  ·  last activity 1y ago by Dirk Herrmann‭

80%
+6 −0
Code Reviews Which functions in the C standard library must always be avoided?

It would seem that the C standard library is a collection of diverse functions that pretty much just ended up in the standard by tradition or "accident", rather than through some careful plan or ra...

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

70%
+5 −1
Code Reviews What does a variable followed by parentheses ("ptr()") mean?

What does ptr() mean in this code? #include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; void PrintHello() { printf("Hello\n"); } int Add(int a, int b) { return a+b; } int main...

2 answers  ·  posted 1y ago by dumplings‭  ·  last activity 1y ago by Martin Bonner‭

Question c pointers
77%
+5 −0
Code Reviews noreturn function with non-void return type

Is it legal ISO C to declare a function as noreturn with a non-void return type (but of course not actually returning)? As far as I can read from the standard, it seems legal. Example: noreturn ...

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

80%
+6 −0
Code Reviews Is it OK to use scanf with a void pointer?

I've created a function that calls scanf passing a void pointer as argument: void read(const char *format, void *p) { scanf(format, p); } And tested it with different types: int n; read...

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

Question c pointers scanf
80%
+6 −0
Code Reviews When does it not work to dereference the pointer for sizeof during malloc?

Background This is kind of a subquestion to How to properly use malloc? When allocating, there are basically two common ways of using the sizeof operator: int *p; p = malloc(n * sizeof *p); /...

1 answer  ·  posted 1y ago by klutt‭  ·  edited 1y ago by hkotsubo‭

Question c pointers malloc
80%
+6 −0
Code Reviews Are static pointers implicitly initialized to NULL?

Consider this code: #include &lt;stdio.h&gt; int main(void) { static void *ptr; if(ptr == NULL) puts("It's NULL!"); } I wonder if this is guaranteed to print "It's NULL!" I know ...

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

83%
+8 −0
Code Reviews 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...

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

77%
+5 −0
Code Reviews C Language Standard Linking Specifications

What (if anything) does the C standard have to say about linking objects? My guess is that, because C only defines language-&gt;behavior rules, it completely ignores any potential intermediate form...

1 answer  ·  posted 1y ago by Josh Hyatt‭  ·  edited 1y ago by Josh Hyatt‭

Question c standard
60%
+4 −2
Code Reviews Question regarding an error message in my compiler to do with my code on linked list.

Can anyone help me, I'm currently learning pointers, this is the code I wrote to try and insert a node at the beginning of the list. However at the part where I included the comment of "error at th...

1 answer  ·  posted 1y ago by hamburgersarecool‭  ·  edited 1y ago by hamburgersarecool‭

86%
+11 −0
Code Reviews What is malloc's standard-defined behavior with respect to the amount of memory it allocates?

I recently told a friend that malloc(n) allocates and returns a pointer to a block of at least N bytes of memory, as opposed to exactly N; that it is allowed to allocate 'extra' memory to meet e.g....

2 answers  ·  posted 1y ago by ajv‭  ·  last activity 1y ago by bta‭

84%
+9 −0
Code Reviews Behavior of Pointer Arithmetic on the Stack

Consider the following code: #include &lt;stdio.h&gt; int main() { int a = 5; int b; ++*(&amp;b + 1); printf("%d\n", a); return 0; } The output is as expected: 6 ...

4 answers  ·  posted 1y ago by Josh Hyatt‭  ·  last activity 1y ago by Alexei‭

37%
+1 −3
Code Reviews Regarding the heap sort algorithm.

I get the concept of the heap sort algorithm and its like first you have a heap(ordered binary tree) then we have the Max heap which has the highest element value in the array at the top of the tre...

1 answer  ·  posted 1y ago by hamburgersarecool‭  ·  edited 1y ago by Alexei‭

Question c heap-memory sorting
75%
+4 −0
Code Reviews How to clear the contents of a file?

How to clear the contents of a file using C?

1 answer  ·  posted 1y ago by dumplings‭  ·  edited 1y ago by hkotsubo‭

Question c file-handling
28%
+0 −3
Code Reviews Data structure implementation with Linked lists.

Could someone explain this part of the coding for data structures &amp; linked list? I actually got this code from a textbook but no matter how I read the textbook, I still don't get the concept &a...

1 answer  ·  posted 1y ago by hamburgersarecool‭  ·  last activity 1y ago by Alexei‭

37%
+1 −3
Code Reviews How can I delete a book from the collection by a particular title from my library file?

I would like to delete a book from the collection by a particular title from my library if the user enters 2 as input for my switch statement. How can I do that with my current code? #include&lt;...

0 answers  ·  posted 1y ago by hamburgersarecool‭  ·  edited 1y ago by hamburgersarecool‭

50%
+2 −2
Code Reviews How can you modify your server code to make it accept any port numbers instead of just one which is the one you predefined?

Is there any way where I can modify the original server code so that it accepts any port number instead of the predefined one (i.e., port number 8989)? Port number will be entered in the command li...

0 answers  ·  posted 1y ago by hamburgersarecool‭  ·  edited 1y ago by Alexei‭

Question c sockets port winsock2
25%
+0 −4
Code Reviews What does the greater than 0 indicate in the case of this statement 'if (compare(A[j], A[j+1]) > 0)' ? Thank you.

Question: There is a list of integers in an array (3,2,1,5,6,4), sort this list in both ASCENDING &amp; DESCENDING order. Create a sort function. #include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; ...

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

66%
+4 −1
Code Reviews Invalid memory access when trying to dereference a pointer obtained through a function call

Hey, regarding a question on returning pointers as functions, why does my code produce this error &amp; won't print out the sum = 6? #include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; int *A...

1 answer  ·  posted 1y ago by dumplings‭  ·  last activity 1y ago by celtschk‭

77%
+5 −0
Code Reviews Common string handling pitfalls in C programming

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

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

90%
+18 −0
Code Reviews What must a C compiler do when it finds an error?

What exactly must a C compiler do when it finds a compile-time error? The most obvious kind of errors are language syntax errors, but the C standard also speaks of constraints, which are rules tha...

1 answer  ·  posted 3y ago by Lundin‭  ·  edited 1y ago by ghost-in-the-zsh‭

87%
+12 −0
Code Reviews What compiler options are recommended for beginners learning C?

When reading questions about C programming from beginners, I very often see them describing peculiar run-time errors and crashes, segmentation faults and similar. They have spent a lot of time chas...

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

42%
+1 −2
Code Reviews How do I filter an array in C?

No, I'm not trying to get the full program written completely in C by you guys. I only need some way to implement the functionalities of each function I found confusing. In this challenge in Code ...

2 answers  ·  posted 1y ago by General Sebast1an‭  ·  last activity 1y ago by Derek Elkins‭

Question c javascript array
75%
+4 −0
Code Reviews Counting number of assignments that a `fscanf` format strings implies

I'm writing a function that counts the number of assignments for a fscanf format string. I studied the documentation in C standard 7.21.6.2 It looks like it works. It passes all test cases I have w...

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

55%
+3 −2
Code Reviews constructor in C

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

2 answers  ·  posted 2y ago by deleted user  ·  last activity 2y ago by Lorenzo Donati‭

Question c constructor
37%
+1 −3
Code Reviews Why does this code that uses a pointer-to-pointer-to-int segfault?

Hello folks, can someone resolve this seg fault with me please, i can't find the error where it occur. Thank you. #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; int ft_ultimate_range(int *...

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

62%
+3 −1
Code Reviews Reading contents of XML node

I'm writing some functions in C that parses a part of a XML file (using libxml), but instead of extracting the content of the XML node that has a specific name, it outputs a string that's not in UT...

0 answers  ·  posted 2y ago by southernisles‭  ·  edited 2y ago by Alexei‭

66%
+4 −1
Code Reviews BMPL: The language I'll bring to life

Last year, I started working on a language I named SuperCode but then decided BMPL (Builder's Multi-Purpose Language) as the final name. The language would be written using C and up until now, the ...

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