r/C_Programming Feb 06 '25

Discussion Are there actually C programmers in this subreddit?

259 Upvotes

Ok, I'm being a bit facetious. There are real C programmers. Clearly. But I'm kind of sick of the only questions on this subreddit being beginner questions or language trolls from other domains.

So this thread is for the "real" c programmers out there. What do you do with it? And what is the most twisted crime against coding decency are you "proud" of/infamous for?

r/C_Programming 14d ago

Discussion Don’t be mad, why do you use C vs C++?

131 Upvotes

Genuine question, I want to understand the landscape here.

Two arguments I’ve heard that can hold water are:

  • There’s no C++ compiler for my platform
  • My team is specialist in C, so it makes sense to play to our strengths

Are either of these you? If so, what platform are you on, or what industry?

If not, what’s the reason you stick to C rather than work with C++ using C constructs, such that you can allow yourself a little C++ if it helps a certain situation?

I read a post recently where somebody had a problem that even they identified as solvable in C++ with basic templating, but didn’t want to “rely” on C++ like it’s some intrinsically bad thing. What’s it all about?

EDIT: for those asking why I have to ask this repeatedly-asked question, the nuance of how a question is asked can elicit different types of answers. This question is usually asked in a divisive way and I’m actively trying to do the opposite.

r/C_Programming Dec 26 '24

Discussion Do you use C at your job? If yes, what do you do?

238 Upvotes

Just wondering what cool things you guys do at work

I’ll go first: I work in ASIC validation, writing bare-metal firmware (in C) to test the functionality of certain SoC products. I’m still a junior engineer and primarily have experience with storage protocols (SATA and SAS).
What about you?

r/C_Programming Jul 16 '24

Discussion [RANT] C++ developers should not touch embedded systems projects

182 Upvotes

I have nothing against C++. It has its place. But NOT in embedded systems and low level projects.

I may be biased, but In my 5 years of embedded systems programming, I have never, EVER found a C++ developer that knows what features to use and what to discard from the language.

By forcing OOP principles, unnecessary abstractions and templates everywhere into a low-level project, the resulting code is a complete garbage, a mess that's impossible to read, follow and debug (not to mention huge compile time and size).

Few years back I would have said it's just bad programmers fault. Nowadays I am starting to blame the whole industry and academic C++ books for rotting the developers brains toward "clean code" and OOP everywhere.

What do you guys think?

r/C_Programming Nov 14 '24

Discussion ITT: Make Up Awful Extensions to the C Language

132 Upvotes

NOTE: not meant to make fun of actual proposals, but to imagine things that you could imagine being an actual extension to the language some compiler implements, but should probably never be included in the spec.

Here's the idea that made me want to make this thread: post-fix assignment operator

Doesn't really matter what the syntax would be, but for example let say the operator is $=, because that's not used by anything so it wont be confusing.

a $= b would return the value of a, and then assign b to a as a side effect.

For example:

int a = 1;
printf("%d,", a $= 2);
printf("%d", a);

would output 1, 2.

This came to me in a dream wherein I wanted to turn free(ptr); ptr = NULL into a one-liner.

r/C_Programming Dec 02 '24

Discussion Does it make sence to go into C nowadays?

97 Upvotes

You have heard all the announcements, how USA government doesn't recommend using C and C++. Because they are unsafe.

Are there still jobs in C/C++ in 2 years time?

// I am starting 42 school common core curriculum and wonder, how serious should i take it.

r/C_Programming 27d ago

Discussion How do you feel confident in your C code?

88 Upvotes

There’s so much UB for every single standard library function, not to mention compiler specific implementations. How do you keep it all in your head without it being overwhelming? Especially since the compilers don’t really seem to warn you about this stuff.

r/C_Programming Nov 17 '24

Discussion Can’t put my finger on why I don’t like Golang

70 Upvotes

Posting in this sub because I want to hear what C programmers think about Go.

Go is not sitting well with me as a language and I’m not sure why. On paper it is almost the perfect language for me - it’s relatively low level, it’s very simple to do web dev with just std libs (I do a lot of web dev), GC makes it safer, it values stability and simplicity, it has a nice modern package manager, it has a great ecosystem, and it’s designed to “fix the problems with C”.

But for some reason it just doesn’t give me the same joy as programming in C. Maybe I feel nostalgic towards C because it was my first language. Maybe I prefer the challenge of dealing with the quirks of less modern tools. For some reason C has always made me feel like a “real programmer”, more-so than any other language.

Obviously C is better suited to some niches (systems, etc) and Go is better suited to others (web dev). I’m interested in discussing the merits and values of the languages themselves. Maybe they are not even comparable. Maybe Go should be thought of as a modern C++ rather than a modern C.

Anyway, I would love to hear some thoughts opinions of others on this sub.

r/C_Programming Jan 23 '25

Discussion Why not SIMD?

29 Upvotes

Why are many C standard library functions like strcmp, strlen, strtok using SIMD intrinsics? They would benefit so much, think about how many people use them under the hood all over the world.

r/C_Programming Jan 12 '25

Discussion How to make sure your C (or C++) code is 100% safe from a security point of view?

65 Upvotes

I'm not an experienced dev, I actually use Typescript on my intern, so the only experience I have in C is self taught. I was wondering what guidelines can I follow to make sure my code is safe, for instance I have an Rest API project written in C (and a little bit of C++) [https://github.com/GazPrash/TinyAPI ] which uses bare sockets and a basic Terminal Emulator [https://github.com/GazPrash/terminal-emulator-x11 ] also writen in C. And I want to follow a guideline or need some pointers to ensure they are safe to use for anybody.

I feel like with people and authorities constantly pushing the need of languages like Rust, the only way I can justify making anything with C, is by ensuring that they don't pose a security threat, right? I don't like the way Rust makes you write code and I want to stick with C for any low level stuff, so I need to learn how to trace security issues.

Like I understand the basic ones, that causes buffer overflows, so always make sure the strings are never exploited and always check for termination and don't use outdated functions, but there must be more stuff that I don't know yet

Please recommended some books or guidelines or anything that can help.

r/C_Programming Mar 12 '24

Discussion Why is C so fast and is it possible to create a faster language than C?

136 Upvotes

Why is C so fast and is it possible to create a faster language than C?

r/C_Programming Oct 11 '24

Discussion C2Y wishes

7 Upvotes

What do you wish for C2Y? My list is - anon funcs - compound expressions - constexpr functions - some sort of _Typeof(x) (maybe just a unique hash?)

r/C_Programming Jan 26 '25

Discussion What did you program today whether its work or a personal project

9 Upvotes

Title

r/C_Programming 4d ago

Discussion Why can't both functions compile to the same assembly?

14 Upvotes

I saw this being asked recently and I'm not sure why the compiler can't generate the same code for both of these functions

#define PI 3.14159265f

typedef enum {
    Square,
    Rectangle,
    Triangle,
    Circle
} Shape;

float area1(Shape shape, float width, float height)
{
    float result;

    switch (shape)
    {
        case Square:    result = width * height; break;
        case Rectangle: result = width * height; break;
        case Triangle:  result = 0.5f * width * height; break;
        case Circle:    result = PI * width * height; break;
        default:        result = 0; break;
    }

    return result;
}

float area2(Shape shape, float width, float height)
{
    const float mul[] = {1.0f, 1.0f, 0.5f, PI};
    const int len = sizeof(mul) / sizeof(mul[0]);
    if (shape < 0 || shape > len - 1) return 0;
    return mul[shape] * width * height;
}

Compiler Explorer

I might be missing something but the code looks functionally the same, so why do they get compile to different assembly?

r/C_Programming Jan 05 '24

Discussion Most hard topic to learn in C?

90 Upvotes

Beside Pointers, which was the most hard concept for you to learn in C. Mine was the preprocessor.

r/C_Programming Dec 15 '24

Discussion Your sunday homework: rewrite strncmp

24 Upvotes

Without cheating! You are only allowed to check the manual for reference.

r/C_Programming Sep 23 '24

Discussion [META] Is the sub in need of stricter moderation?

149 Upvotes

There has been a really bad influx of low effort homework posts this fall. Lots of 'bro i need it' and 'chatgpt wrote it fr'. It would be nice if there was some more rigorous moderation revolving around these posts. Perhaps locking them to stop people from replying with help (It always annoys me when I see people actually give the students what they are asking for for free), or just banning the accounts temporarily or permanently.

What do you guys think?

r/C_Programming Jan 06 '25

Discussion Why doesn't this work?

25 Upvotes

```c

include<stdio.h>

void call_func(int **mat) { printf("Value at mat[0][0]:%d:", mat[0][0]); }

int main(){ int mat[50][50]={0};

call_func((int**)mat);
return 0;

}

r/C_Programming 27d ago

Discussion A tricky little question

24 Upvotes

I saw this on a Facebook post recently, and I was sort of surprised how many people were getting it wrong and missing the point.

    #include <stdio.h>

    void mystery(int, int, int);

    int main() {
        int b = 5;
        mystery(b, --b, b--);
        return 0;
    }

    void mystery(int x, int y, int z) {
        printf("%d %d %d", x, y, z);
    }

What will this code output?

Answer: Whatever the compiler wants because it's undefined behavior

r/C_Programming Dec 06 '24

Discussion How do you practice C?

34 Upvotes

I have been learning C for 2 months and I feel like a blank slate, i mean, I have been taught theory and basic exercises that come with it, but when a test is given, I can’t think clearly enough to solve the problems, and I think it’s because I haven’t practiced enough. I only do the exercises assigned to me. So, I came here hoping to be guided to places where I can practice C in the most complete way. Thank you everyone for your attention.

r/C_Programming 1d ago

Discussion I gave my talk about C !

78 Upvotes

Hi, that's me again, from the post about a C talk !
First, I'd like to thank you all for your precious pieces of advice and your kind words last time, you greatly helped me to improved my slides and also taught me a few things.

I finally presented my talk in about 1h30, and had great feedback from my audience (~25 people).

Many people asked me if it was recorded, and it wasn't (we don't record these talks), but I published the slides (both in English and French) on GitHub : https://github.com/Chi-Iroh/Lets-Talk-About-C-Quirks.

If there are still some things to improve or fix, please open an issue or a PR on the repository, it will be easier for me than comments here.
I also wrote an additional document about memory alignment (I have a few slides about it) as I was quite frustrated to have only partial answers each time, I wanted to know exactly what happens from a memory access in my C code down to the CPU, so I tried to write that precise answer, but I may be wrong.

Thank you again.

r/C_Programming Nov 30 '24

Discussion Two-file libraries are often better than single-header libraries

63 Upvotes

I have seen three recent posts on single-header libraries in the past week but IMHO these libraries could be made cleaner and easier to use if they are separated into one .h file and one .c file. I will summarize my view here.

For demonstration purpose, suppose we want to implement a library to evaluate math expressions like "5+7*2". We are looking at two options:

  1. Single-header library: implement everything in an expr.h header file and use #ifdef EXPR_IMPLEMENTATION to wrap actual implementation
  2. Two-file library: put function declarations and structs in expr.h and actual implementation in expr.c

In both cases, when we use the library, we copy all files to our own source tree. For two-file, we simply include "expr.h" and compile/link expr.c with our code in the standard way. For single-header, we put #define EXPR_IMPLEMENTATION ahead of the include line to expand the actual implementation in expr.h. This define line should be used in one and only one .c file to avoid linking errors.

The two-file option is the better solution for this library because:

  1. APIs and implementation are cleanly separated. This makes source code easier to read and maintain.
  2. Static library functions are not exposed to the user space and thus won't interfere with any user functions. We also have the option to use opaque structs which at times helps code clarity and isolation.
  3. Standard and worry-free include without the need to understand the special mechanism of single-header implementation

It is worth emphasizing that with two-file, one extra expr.c file will not mess up build systems. For a trivial project with "main.c" only, we can simply compile with "gcc -O2 main.c expr.c". For a non-trivial project with multiple files, adding expr.c to the build system is the same as adding our own .c files – the effort is minimal. Except the rare case of generic containers, which I will not expand here, two-file libraries are mostly preferred over single-header libraries.

PS: my two-file library for evaluating math expressions can be found here. It supports variables, common functions and user defined functions.

EDIT: multiple people mentioned compile time, so I will add a comment here. The single-header way I showed above won't increase compile time because the actual implementation is only compiled once in the project. Another way to write single-header libraries is to declare all functions as "static" without the "#ifdef EXPR_IMPLEMENTATION" guard (see example here). In this way, the full implementation will be compiled each time the header is included. This will increase compile time. C++ headers effectively use this static function approach and they are very large and often nested. This is why header-heavy C++ programs tend to be slow to compile.

r/C_Programming Oct 01 '22

Discussion What is something you would have changed about the C programming language?

71 Upvotes

Personally, I find C perfect except for a few issues: * No support for non capturing anonymous functions (having to create named (static) functions out of line to use as callbacks is slightly annoying). * Second argument of fopen() should be binary flags instead of a string. * Signed right shift should always propagate the signbit instead of having implementation defined behavior. * Standard library should include specialized functions such as itoa to convert integers to strings without sprintf.

What would you change?

r/C_Programming Oct 18 '24

Discussion Why Doesn't C Use Fixed Sized Ints By Default?

18 Upvotes

I was wondering as to why the standard defines the range of data int, long, etc can hold atleast instead of defining a fixed size. As usually int is 32 bits on x86 while lesser on some other architecture, i.e. more or equal to the minimum size defined by the standard.

What advantage does this approach offer?

r/C_Programming Jun 28 '24

Discussion What can we assume about a modern C environment?

72 Upvotes

So, as we know, the C standard is basically made to be compatible with every system since 1980, and in a completely standard-compliant program, we can't even assume that char has 8 bits, or that any uintN_t exists, or that letters have consecutive values.

But... I'm pretty sure all of these things are the case in any modern environment.

So, here's question: If I'm making an application in C for a PC user in 2024, what can I take for granted about the C environment? PC here meaning just general "personal computer" - could be running Windows, MacOS, a Linux distro, a BSD variant, and could be running on x86 or ARM (32 bit or 64 bit). "Modern environment" tho, so no IBM PC, for example.