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
Q&A

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.

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 6mo ago by alx‭  ·  last activity 26d 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 2mo ago by AdriZ‭  ·  last activity 2mo 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 3mo ago by Estela‭  ·  last activity 2mo ago by Ethan‭

Question c casting
85%
+10 −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 1y ago by Lundin‭  ·  last activity 3mo 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 12mo ago by dumplings‭  ·  edited 5mo ago by __blackjack__‭

71%
+3 −0
Q&A 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 5mo ago by alx‭  ·  last activity 5mo ago by alx‭

33%
+2 −6
Q&A 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 12mo ago by dumplings‭  ·  edited 5mo ago by Alexei‭

33%
+1 −4
Q&A 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 12mo ago by dumplings‭  ·  edited 5mo ago by Alexei‭

Question c file-handling
40%
+2 −4
Q&A 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 12mo ago by dumplings‭  ·  edited 5mo ago by Alexei‭

55%
+3 −2
Q&A 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 12mo ago by dumplings‭  ·  edited 5mo ago by dumplings‭

80%
+6 −0
Q&A 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 6mo ago by alx‭  ·  edited 6mo ago by alx‭

40%
+2 −4
Q&A 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 7mo ago by Luca_Impellizzeri‭  ·  last activity 6mo ago by Alexei‭

80%
+6 −0
Q&A 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 1y ago by Lundin‭  ·  last activity 7mo ago by Lundin‭

71%
+3 −0
Q&A 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 7mo ago by alx‭  ·  last activity 7mo ago by InfiniteDissent‭

86%
+11 −0
Q&A 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 8mo ago by Lundin‭  ·  last activity 8mo ago by Dirk Herrmann‭

77%
+5 −0
Q&A 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 8mo ago by Lundin‭  ·  last activity 8mo ago by Lundin‭

70%
+5 −1
Q&A 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 12mo ago by dumplings‭  ·  last activity 8mo ago by Martin Bonner‭

Question c pointers
77%
+5 −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...

2 answers  ·  posted 10mo ago by klutt‭  ·  last activity 9mo ago by klutt‭

77%
+5 −0
Q&A 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 9mo ago by alx‭  ·  edited 9mo ago by alx‭

75%
+4 −0
Q&A stpecpy(): Design a better string copy function

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

1 answer  ·  posted 10mo ago by alx‭  ·  edited 9mo ago by alx‭

80%
+6 −0
Q&A 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 10mo ago by hkotsubo‭  ·  last activity 9mo ago by Lundin‭

Question c pointers scanf
80%
+6 −0
Q&A 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 10mo ago by klutt‭  ·  edited 10mo ago by hkotsubo‭

Question c pointers malloc
80%
+6 −0
Q&A 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 10mo ago by klutt‭  ·  last activity 10mo ago by Lundin‭

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

4 answers  ·  posted 10mo ago by Lundin‭  ·  last activity 10mo ago by klutt‭

77%
+5 −0
Q&A 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 11mo ago by Josh Hyatt‭  ·  edited 11mo ago by Josh Hyatt‭

Question c standard
60%
+4 −2
Q&A 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 11mo ago by hamburgersarecool‭  ·  edited 11mo ago by hamburgersarecool‭

86%
+11 −0
Q&A 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 11mo ago by ajv‭  ·  last activity 11mo ago by bta‭

84%
+9 −0
Q&A 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 12mo ago by Josh Hyatt‭  ·  last activity 11mo ago by Alexei‭

37%
+1 −3
Q&A 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 12mo ago by hamburgersarecool‭  ·  edited 11mo ago by Alexei‭

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

How to clear the contents of a file using C?

1 answer  ·  posted 12mo ago by dumplings‭  ·  edited 12mo ago by hkotsubo‭

Question c file-handling
28%
+0 −3
Q&A 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 12mo ago by hamburgersarecool‭  ·  last activity 12mo ago by Alexei‭

37%
+1 −3
Q&A 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 12mo ago by hamburgersarecool‭  ·  edited 12mo ago by hamburgersarecool‭

50%
+2 −2
Q&A 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 12mo ago by hamburgersarecool‭  ·  edited 12mo ago by Alexei‭

Question c sockets port winsock2
25%
+0 −4
Q&A 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 12mo ago by dumplings‭  ·  last activity 12mo ago by Lundin‭

66%
+4 −1
Q&A 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 12mo ago by dumplings‭  ·  last activity 12mo ago by celtschk‭

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

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

77%
+5 −0
Q&A 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
Q&A 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 2y ago by Lundin‭  ·  edited 1y ago by ghost-in-the-zsh‭

87%
+12 −0
Q&A 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 1y ago by Lundin‭  ·  edited 1y ago by Lundin‭

42%
+1 −2
Q&A 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
Q&A 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 1y ago by klutt‭

55%
+3 −2
Q&A 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 1y ago by deleted user  ·  last activity 1y ago by Lorenzo Donati‭

Question c constructor
37%
+1 −3
Q&A 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 1y ago by M3dc0d‭  ·  last activity 1y ago by Lundin‭

62%
+3 −1
Q&A 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 1y ago by southernisles‭  ·  edited 1y ago by Alexei‭

66%
+4 −1
Q&A 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 1y ago by General Sebast1an‭  ·  last activity 1y ago by Lundin‭

71%
+3 −0
Q&A Why use an asterisk after a type?

#include&lt;stdio.h&gt; struct Node{ int data; struct Node* next; }; Here I used an asterisk after Node. What is it used for? What if I don't put any asterisk after Node (both Node...

2 answers  ·  posted 1y ago by deleted user  ·  edited 1y ago by hkotsubo‭

Question c pointers
50%
+1 −1
Q&A Input taking only first character of a string

I wrote a program named Kernel.c 2 months into my life in programming and created 4 functions at the time, and now there are 7 functions. Recently, I updated the program completely onto an online ...

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

Question c string input
87%
+12 −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...

1 answer  ·  posted 1y ago by Lundin‭  ·  last activity 1y ago by J-hen‭

77%
+5 −0
Q&A Warn of implicit cast in a function's arguments with GCC?

In the C program below, I make a mistake and call the function with (ld, ld) instead of (d, ld). #include &lt;stdio.h&gt; #include &lt;limits.h&gt; void print_int_long(int n, long l){ pri...

2 answers  ·  posted 1y ago by Quasímodo‭  ·  last activity 1y ago by Lundin‭

83%
+8 −0
Q&A Is `uint8_t` always an alias for a character type if it exists?

Is uint8_t guaranteed to be a character type if it exists? Will using a uint8_t* to examine bytes of an object cause violation of the strict aliasing rule? Is the following legal code: #include &l...

1 answer  ·  posted 1y ago by Ayxan Haqverdili‭  ·  edited 1y ago by Lundin‭

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!

Like what we're doing? Support us! Donate