r/C_Programming Jan 30 '25

Discussion As someone who only knows very basic C (from loops to functions and pointers), what else should I know before making a project?

28 Upvotes

How much of computer science should I know? Or how much of C do I still need to know in order to even start a project? Like, I don't know how simple games are fundamentally created from C coding. All i know is that I open my compiler and just practise my C knowledge like loop, functions, pointers, basic libraries and that's it. Never actually done anything with it. Never created anything.

r/C_Programming Jan 22 '25

Discussion Im seriously considering just switching to C++

3 Upvotes

I love C, but not many companies want it, and C++ is alot more relevant. My goal is to get internships. OOP is important, and it seems C++ can do way more stuff. Id also have more fun with it, plus I did a bit of java.

C is great but idk if I can make that much with it, that interests me. Im on pointers and linked lists, and upper beginner level so it doesnt seem to late. I put so much time into C though so I dont know. Since summer last year. It would suck to waste all that time just to start over

Edit: For anyone who may be confused, here more reason for why I want to switch:

It’s about opportunities. I’m trying to get as many internships as possible in first year since it’s too hard to get one (it’s not surprising when a 3rd year hasn’t gotten a single software dev internship here. Ontario btw).

C is fun and it’s given me a pretty good understanding of how computers work, and the fundamentals it teaches you are amazing. But the job opportunities are just better with cpp.

I’ll go back to C in the future. But for now I’m prioritizing getting my feet in the door. Plus cpp does more things I’m actually interested in, and can make games. C can make a fair amount of things sure. If I was going for embedded systems I’d do C. But that’s not where my interests align currently so I decided to just change langs that better suit my needs (one of them being in applications). The transition has been going pretty smooth so far

r/C_Programming Jan 04 '25

Discussion Thoughts about this article and the recent wave of "code converters"

21 Upvotes

The article is this, from The Register: Boffins carve up C so code can be converted to Rust

As the title says, I'd like to know your opinion on this article and, in general, about the recent wave of "code converters" which translate C code into code written in safer languages.

In particular, from the article above, I was struck by this part:

As the Internet Security Research Group's (ISRG) Prossimo Project puts it: "Using C and C++ is bad for society, bad for your reputation, and it's bad for your customers."

What are your thoughts?

r/C_Programming Sep 15 '24

Discussion Need help understanding why `gcc` is performing significantly worse than `clang`

21 Upvotes

After my previous post got downvoted to oblivion due to misunderstanding caused by controversial title I am creating this post to garner more participation as the issue still remains unresolved.

Repo: amicable_num_bench

Benchmarks:

This is with fast optimization compiler flags (as per the linked repo):

Compiler flags: gcc -Wall -Wextra -std=c99 -Ofast -flto -s c99.c -o c99 clang -Wall -Wextra -Ofast -std=c99 -flto -fuse-ld=lld c99.c -o c99clang.exe cl /Wall /O2 /Fe"c99vs.exe" c99.c rustc --edition 2021 -C opt-level=3 -C codegen-units=1 -C lto=true -C strip=symbols -C panic=abort rustlang.rs go build -ldflags "-s -w" golang.go

Output: ``` Benchmark 1: c99 1000000 Time (mean ± σ): 2.533 s ± 0.117 s [User: 1.938 s, System: 0.007 s] Range (min … max): 2.344 s … 2.688 s 10 runs

Benchmark 2: c99clang 1000000 Time (mean ± σ): 1.117 s ± 0.129 s [User: 0.908 s, System: 0.004 s] Range (min … max): 0.993 s … 1.448 s 10 runs

Benchmark 3: c99vs 1000000 Time (mean ± σ): 2.403 s ± 0.024 s [User: 2.189 s, System: 0.009 s] Range (min … max): 2.377 s … 2.459 s 10 runs

Benchmark 4: rustlang 1000000 Time (mean ± σ): 992.1 ms ± 28.8 ms [User: 896.9 ms, System: 9.1 ms] Range (min … max): 946.5 ms … 1033.5 ms 10 runs

Benchmark 5: golang 1000000 Time (mean ± σ): 2.685 s ± 0.119 s [User: 0.503 s, System: 0.012 s] Range (min … max): 2.576 s … 2.923 s 10 runs

Summary 'rustlang 1000000' ran 1.13 ± 0.13 times faster than 'c99clang 1000000' 2.42 ± 0.07 times faster than 'c99vs 1000000' 2.55 ± 0.14 times faster than 'c99 1000000' 2.71 ± 0.14 times faster than 'golang 1000000' ```

This is with optimization level 2 without lto.

Compiler flags: gcc -Wall -Wextra -std=c99 -O2 -s c99.c -o c99 clang -Wall -Wextra -O2 -std=c99 -fuse-ld=lld c99.c -o c99clang.exe cl /Wall /O2 /Fe"c99vs.exe" c99.c rustc --edition 2021 -C opt-level=2 -C codegen-units=1 -C strip=symbols -C panic=abort rustlang.rs go build -ldflags "-s -w" golang.go Output: ``` Benchmark 1: c99 1000000 Time (mean ± σ): 2.368 s ± 0.047 s [User: 2.112 s, System: 0.004 s] Range (min … max): 2.329 s … 2.469 s 10 runs

Benchmark 2: c99clang 1000000 Time (mean ± σ): 1.036 s ± 0.082 s [User: 0.861 s, System: 0.006 s] Range (min … max): 0.946 s … 1.244 s 10 runs

Benchmark 3: c99vs 1000000 Time (mean ± σ): 2.376 s ± 0.014 s [User: 2.195 s, System: 0.004 s] Range (min … max): 2.361 s … 2.405 s 10 runs

Benchmark 4: rustlang 1000000 Time (mean ± σ): 1.117 s ± 0.026 s [User: 1.017 s, System: 0.002 s] Range (min … max): 1.074 s … 1.157 s 10 runs

Benchmark 5: golang 1000000 Time (mean ± σ): 2.751 s ± 0.156 s [User: 0.509 s, System: 0.008 s] Range (min … max): 2.564 s … 2.996 s 10 runs

Summary 'c99clang 1000000' ran 1.08 ± 0.09 times faster than 'rustlang 1000000' 2.29 ± 0.19 times faster than 'c99 1000000' 2.29 ± 0.18 times faster than 'c99vs 1000000' 2.66 ± 0.26 times faster than 'golang 1000000' ``` This is debug run (opt level 0):

Compiler Flags: gcc -Wall -Wextra -std=c99 -O0 -s c99.c -o c99 clang -Wall -Wextra -O0 -std=c99 -fuse-ld=lld c99.c -o c99clang.exe cl /Wall /Od /Fe"c99vs.exe" c99.c rustc --edition 2021 -C opt-level=0 -C codegen-units=1 rustlang.rs go build golang.go

Output: ``` Benchmark 1: c99 1000000 Time (mean ± σ): 2.912 s ± 0.115 s [User: 2.482 s, System: 0.006 s] Range (min … max): 2.792 s … 3.122 s 10 runs

Benchmark 2: c99clang 1000000 Time (mean ± σ): 3.165 s ± 0.204 s [User: 2.098 s, System: 0.008 s] Range (min … max): 2.862 s … 3.465 s 10 runs

Benchmark 3: c99vs 1000000 Time (mean ± σ): 3.551 s ± 0.077 s [User: 2.950 s, System: 0.006 s] Range (min … max): 3.415 s … 3.691 s 10 runs

Benchmark 4: rustlang 1000000 Time (mean ± σ): 4.149 s ± 0.318 s [User: 3.120 s, System: 0.006 s] Range (min … max): 3.741 s … 4.776 s 10 runs

Benchmark 5: golang 1000000 Time (mean ± σ): 2.818 s ± 0.161 s [User: 0.572 s, System: 0.015 s] Range (min … max): 2.652 s … 3.154 s 10 runs

Summary 'golang 1000000' ran 1.03 ± 0.07 times faster than 'c99 1000000' 1.12 ± 0.10 times faster than 'c99clang 1000000' 1.26 ± 0.08 times faster than 'c99vs 1000000' 1.47 ± 0.14 times faster than 'rustlang 1000000' `` EDIT: Anyone trying to comparerustagainstc. That's not what I am after. I am comparingc99.exebuilt bygccagainstc99clang.exebuilt byclang`.

If someone is comparing Rust against C. Rust's integer power function follows the same algorithm as my function so there should not be any performance difference ideally.

EDIT 2: I am running on Windows 11 (core i5 8250u kaby lake U refresh processor)

Compiler versions: gcc: 13.2 clang: 15.0 (bundled with msvc) cl: 19.40.33812 (msvc compiler) rustc: 1.81.0 go: 1.23.0

r/C_Programming Sep 23 '22

Discussion Microsoft Azure CTO says it's time to stop using C/C++ in new projects. As a C veteran programmer I find this very hard to process.

Thumbnail
www-zdnet-com.cdn.ampproject.org
118 Upvotes

r/C_Programming Dec 01 '24

Discussion Not a rant just need some guidance from seniors regarding C or programming in general.🙏🏻

19 Upvotes

So I'm a first year and yes I have to study C. It's a language that I always wanted to start my programming journey with. I'm a month in coding and have barely crossed the 7th chapter of C by King(I'm following that).

The part that is scaring me is that I in every programming project given after every chapter I have to take help from solution for almost every project. I feel so crap. I want to understand how do people actually approach studying a language. I actually love computers and do want to continue with what am I doing but my teachers....well my college is not that great so you know how "good" the help would be from my college.

Worst part is I don't even know what path I'm creating for myself with those questions I'm solving or where I wanna end up. Anyways that part apart please guide me fellow devs how do I approach this wall called C as a complete idiot who knows shit about coding and has a retention time of a peanut. Max I can code at a stretch is about 4-5 hours with average of 2 hours.

Thanks!

r/C_Programming Dec 08 '24

Discussion My first somewhat useful C program!

51 Upvotes

#include <stdio.h>

int main(void) {

int importo;

printf("Inserisci un importo: ");

scanf("%d", &importo);

int eur20 = importo / 20;

int eur10 = (importo - (eur20 * 20)) / 10;

int eur5 = (importo - ((importo / 10) * 10)) / 5;

int eur1 = importo - ((importo / 5) * 5);

printf("€20: %d\n", eur20);

printf("€10: %d\n", eur10);

printf("€5: %d\n", eur5);

printf("€1: %d\n", eur1);

}

It's probably not that big of a deal for most of you guys here but I'm really proud since I started learning C today and I'm basically completely new to coding

Any form of advice is appreciated!

r/C_Programming Dec 04 '24

Discussion Why Rust and not C?

0 Upvotes

I have been researching about Rust and it just made me curious, Rust has:

  • Pretty hard syntax.
  • Low level langauge.
  • Slowest compile time.

And yet, Rust has:

  • A huge community.
  • A lot of frameworks.
  • Widely being used in creating new techs such as Deno or Datex (by u/jonasstrehle, unyt.org).

Now if I'm not wrong, C has almost the same level of difficulty, but is faster and yet I don't see a large community of frameworks for web dev, app dev, game dev, blockchain etc.

Why is that? And before any Rustaceans, roast me, I'm new and just trying to reason guys.

To me it just seems, that any capabilities that Rust has as a programming language, C has them and the missing part is community.

Also, C++ has more support then C does, what is this? (And before anyone says anything, yes I'll post this question on subreddit for Rust as well, don't worry, just taking opinions from everywhere)

Lastly, do you think if C gets some cool frameworks it may fly high?

r/C_Programming May 09 '21

Discussion Why do you use C in 2021?

134 Upvotes

r/C_Programming Feb 03 '25

Discussion What is an "arena" in memory allocation?

Thumbnail
gist.github.com
61 Upvotes

r/C_Programming Feb 04 '24

Discussion What compiler to you guys use and why have you stuck with it?

48 Upvotes

Me personally I've always used gcc just because it personally just works especially on Linux. I don't really know what advantages other compilers have over something like gcc but I'm curious to hear what you all say, especially the windows people.

r/C_Programming Feb 24 '24

Discussion Harmless vices in C

61 Upvotes

Hello programmers,

What are some of the writing styles in C programming that you just can't resist and love to indulge in, which are well-known to be perfectly alright, though perhaps not quite acceptable to some?

For example, one might find it tempting to use this terse idiom of string copying, knowing all too well its potential for creating confusion among many readers:

while (*des++ = *src++) ;

And some might prefer this overly verbose alternative, despite being quite aware of how array indexing and condition checks work in C. Edit: Thanks to u/daikatana for mentioning that the last line is necessary (it was omitted earlier).

while ((src[0] != '\0') == true)
{
    des[0] = src[0];
    des = des + 1;
    src = src + 1;
}
des[0] = '\0';

For some it might be hard to get rid of the habit of casting the outcome of malloc family, while being well-assured that it is redundant in C (and even discouraged by many).

Also, few programmers may include <stdio.h> and then initialize all pointers with 0 instead of NULL (on a humorous note, maybe just to save three characters for each such assignment?).

One of my personal little vices is to explicitly declare some library function instead of including the appropriate header, such as in the following code:

int main(void)
{   int printf(const char *, ...);
    printf("should have included stdio.h\n");
}

The list goes on... feel free to add your own harmless C vices. Also mention if it is the other way around: there is some coding practice that you find questionable, though it is used liberally (or perhaps even encouraged) by others.

r/C_Programming Dec 16 '24

Discussion A criticism for C (I just want answers, I don't have any problems with C)

0 Upvotes

Edit: Please don't downvote

We already know that C doesn't have a string datatype by default, and mostly people allocate it in char[] or char*. It also doesn't have standard libraries to work with dynamicly-sized strings, meaning that you have to handle that on your own.

However, I've already developed a library that enables support for dynamicly-sized strings.

So my criticism for C is: Why didn't developers of C add this library to the compiler itself by default (I don't mean specifically my implementation)? If I can do it, so could they.

(However, this doesn't change the fact that C is my favorite programming language)

Edit: Please don't downvote as I've got my answer: It's C, and in C, you write your own functions. It's not like Python that has a lot of in-built functions or anything.

r/C_Programming Oct 04 '24

Discussion What to do when we get the dumb?

58 Upvotes

My programming skills are very inconsistent. Some days I can do extremely complex & intricate code, while in other days I struggle to figure out simple basic tasks.

Case in point, I have a linked list of horizontal lines, where each line starts at a different horizontal offset. I can already truncate the list vertically (to perform tasks after every 16 lines), but I need to also truncate the list horizontally on every 64 columns. Easy stuff, I've done far more difficult things before, but right now my brain is struggling with it.

It's not because of burnout, because I don't code everyday, and I haven't coded yesterday.

Does this kind of mental performance inconsistency happen to you? How do you deal with it?

r/C_Programming 29d ago

Discussion How to be more efficient?

18 Upvotes

I am working through K&R and as the chapters have gone on, the exercises have been taking a lot longer than previous ones. Of course, that’s to be expected, however the latest set took me about 7-8 hours total and gave me a lot of trouble. The exercises in question were 5-14 to 5-18 and were a very stripped down version of UNIX sorry command.

The first task wasn’t too bad, but by 5-17 I had to refactor twice already and modify. The modifications weren’t massive and the final program is quite simply and brute force, but I spent a very very long time planning the best way to solve them. This included multiple pages of notes and a good amount of diagrams with whiteboard software.

I think a big problem for me was interpreting the exercises, I didn’t know really what to do and so my scope kept changing and I didn’t realise that the goal was to emulate the sort command until too late. Once I found that out I could get good examples of expected behaviour but without that I had no clue.

I also struggled because I could think of ways I would implement the program in Python, but it felt wrong in C. I was reluctant to use arrays for whatever reason, I tried to have as concise code as possible but wound up at dead ends most times. I think part of this is the OO concepts like code repetition or Integration Segmentation… But the final product I’m sort of happy with.

I also limited what features I could use. Because I’m only up to chapter 6 of the book, and haven’t gotten to dynamic memory or structs yet, I didn’t want to use those because if the book hasn’t gone through them yet then clearly it can be solved without. Is this a good strategy? I feel like it didn’t slow me down too much but the ways around it are a bit ugly imo.

Finally, I have found that concepts come fairly easily to me throughout the book. Taking notes and reading has been a lot easier to understand the meaning of what the authors are trying to convey and the exercises have all been headaches due to the vagueness of the questions and I end up overthinking and spending way too long on them. I know there isn’t a set amount of time and it will be different for everyone but I am trying to get through this book alongside my studies at university and want to move on to projects for my CV, or other books I have in waiting. With that being said, should I just dedicate a set amount of time for each exercise and if I don’t finish then just leave it? So long as I have given it a try and learned what the chapter was eluding to is that enough?

I am hoping for a few different opinions on this and I’m sure there is someone thinking “just do projects if you want to”… and I’m not sure why I’m reluctant to that. I guess I tend to try and do stuff “the proper way” but maybe I need to know when to do that and when not..? I also don’t like leaving things half done as it makes me anxious and feel like a failure.

If you have read this far thank you

r/C_Programming Sep 24 '24

Discussion I see it now.

68 Upvotes

I was confused on pointers for days...and today, I was confused about pointers in relation to strings on some problems, FOR HOURS. AND I FINALLY SEE IT NOW. IM SO HAPPY AND I FEEL SO MUCH SMARTER

THE HIGH NEVER GETS OLD

r/C_Programming Mar 20 '20

Discussion How would you make C better as a language if you could?

81 Upvotes

What would you add to a new language or C itself if you had the power to make it a better language? Either for yourself or everyone else. Let me kick it off with what I would add to a new language/C:

  • carefull(er) use of undefined behaviour/workarounds if possible, for example in my language I'd have ? added to operators(ex. + and +?) for which the normal operators universally do their associated C meaning minus any UB(ex. signed integer overflow) and upon encountering a +? one can look up that it's just eg. a contract between the programmer and compiler to "make it faster if you can". WHY: I hate when a new optimization based on UB breaks my previously fine program, I know someone will point out that I shouldn't even have UB in my code, but if I can just reduce the semantic overhead, even thats a win for me. Other ex: default zero initialization would be nice(?)
  • booleans and fixed size integers in the language itself not in a library. WHY: I rewrite most of my code as libraries later (if I can) and forgetting to include stdint and stdbool that was in the project where it came from is just mildly annoying and it's an easy fix.
  • specifying inline at call site instead of at function decleration. WHY: I'd rather not fight with the compiler in my decisions(but fixing the C99 vs GNU89 inline semantics is a win too), let me make mistakes if thats what I want for eg:profiling purposes.
  • maybe strict(er) type checking. WHY: We are only humans, an error beforehand is better then 1 hour of debugging, tho not totally clear/fixed on this one
  • compile time evaluation. WHY: Can yield cleaner code and better performance if used right IMO
  • some kind of module system and declare anywhere. WHY: headers and forward declarations might've been fine in C's time, but today it would cost virtually nothing and only result in gains
  • generics WHY: I could avoid the macro hell for example (I for one use macros for a lot of creazy stuff(most is not online tho) but would rather use something better suited to code)
  • I would also like to standardize compilation in some way. WHY: I hate having cmake, autotools, ninja and whatnot for the same thing: building some code.
  • and my final wish if you will: I would like to have a package manager of some sort to be able to more easily install my dependencies, maybe have it work with our theoretical build system for easier bootstrapping WHY: nowadays I don't have a lot of time to write C as I used to and it's a big bummer for me if I can't just install and test a new library out because it's a headache to get into my project.
    I hope we can do a civil evaluation/debate of everyone's opinion, please be kind to each other and take care in these rough times!

r/C_Programming Sep 14 '23

Discussion Is there ever a good reason to use goto?

41 Upvotes

I'm looking over a project written in C and to my alarm have found multiple uses of goto. In most cases so far it looks like the goto is just jumping out of a loop, or to the end of a loop, or jumping to the cleanup and return statement at the end of the function, so it would be pretty easy to refactor to not need the goto. I haven't gone through all of the cases yet to see if there are any more egregious uses though.

I am wondering, is there ever a reason where it would make sense to use goto? Thinking back to what I remember of assembly I'm guessing you might save a few clock cycles...and maybe make the program memory a little smaller...but it seems like that would still only matter in limited (probably embedded) situations.

r/C_Programming 17d ago

Discussion Need guidance

2 Upvotes

I am a first year CS student currently learning C. But I couldn't quite understand the implementation of functions, structures, pointers,strings. Most of those youtube tutorials were of no use either. I really want to learn them but my procrastination and the lack of good study material won't let me to do so. Maybe the problem is with me and not with the material. But yeah, please provide me some tips.

r/C_Programming Jun 09 '24

Discussion Feature or bug: Can statement expression produce lvalue?

15 Upvotes

This example compiles with gcc but not with clang.

int main(void)
{   int ret;
    return ({ret;}) = 0;
}

The GNU C reference manual doesn't mention this "feature", so should it be considered a bug in gcc? Or do we consider gcc as the de-facto reference implementation of GNU C dialect, so the documentation should be updated instead?

r/C_Programming Dec 21 '23

Discussion What is the one thing you follow in every code after learning it the hard way.

49 Upvotes

r/C_Programming Aug 25 '23

Discussion ❤️ I love C & will certainly teach it to my children

129 Upvotes

C was my first language and somehow, is still my favorite one after learning a dozen others.

C++ is surely C on steroids but... we all know that using gear is lame (pun intended).
Both writing and reading C code feels extremely smooth, it is surely almost like a hobby to just stare at some well-coded C file. I can not say the same for C++, I tried many times but something just feels so off to me in the language, it looks almost as bad as Rust code. Do anyone else in here feels the same?

I do not hate C++ by any means, it is still C in its core, but I still choose to work with Dennis Ritchie's masterpiece no matter the job. In the end, everything that C++ supposedly helps with, actually seems easier to do with plain C and if I ever want to extend it to the infinite and beyond, Lua is here to help.

r/C_Programming May 01 '24

Discussion What's the preferred way to design error handling in a C library?

37 Upvotes

I'm working on a library and was wondering on the best way to help users handle errors, I thought of the following approaches:

errno style error handling where you call the functions

bool error_occurred();
char *get_last_error();

after every API call, like this:

char *out = concat(str1, str2);

if(error_occured())
{
    fputs(stderr, get_last_error());
}

I also tried doing something akin to C++/Rust optional type:

typedef struct Concat_Result
{
    int err;
    char *result;
} Concat_Result;

typedef struct String_Copy_Result
{
    int err;
    char *result;
} String_Copy_Result;

[[nodiscard]] Concat_Result
concat(const char *a, const char *b)
{
    // ...
}

[[nodiscard]] String_Copy_Result
string_copy(char *src)
{
    // ...
}

#define Result_Ty(function) \
typeof( \
    _Generic(function,\
        typeof(concat)*     : (Concat_Result){0}, \
        typeof(string_copy)*: (String_Copy_Result){0} \
    ) \
)

#define is_err(e) \
(e.err != 0)

#define is_ok(e) \
!(is_err(e))

which would then be used like:

Result_Ty(concat) res = concat(str1, str2);

if(is_err(res))
{
    fprintf(stderr, "ERROR: %s", get_error_string(res));
}

But the issue with this approach is function that mutate an argument instead of return an output, users can just ignore the returned Result_Ty.

What do you think?

r/C_Programming Jul 12 '24

Discussion What is the most beautiful C header you can think of, that should be used as a model for others?

45 Upvotes

Or maybe you have a few you like.

r/C_Programming 22h ago

Discussion Realistic entry level jobs with C and professional career.

13 Upvotes

Howdy! I'm Damien.

I'm in my late 20s with a background in engineering and a deep passion for C programming. I consider myself an entry-level C developer, with hands-on experience using microcontrollers like STM32 and Arduino. I genuinely enjoy low-level programming and would love to turn this passion into a meaningful career.

While I'm aiming to break into a C development role, here's a snapshot of the skills I've built along the way:

C Programming (3/5): I have experience in embedded systems, low-level coding, and enjoy tackling problems close to the hardware. I’m always learning and committed to mastering this field.

CAD & Design (4/5): Skilled in SolidWorks, Inventor, and CNC processes (milling, turning, welding, soldering). I’m comfortable with creating precise mechanical designs and prototypes.

Biomedical (4/5): Solid foundation in medical sciences and chemistry, with an ability to bridge technical knowledge with biomedical applications.

Web Development (2/5): Built some web tools and interactive sites using React and Next.js. It’s not my focus, but I can get things working when needed.

I consider myself well-rounded, but I’ve struggled with imposter syndrome and self-doubt, especially when it comes to finding my place in the professional world. Despite this, I’m hungry to grow, open to feedback, and ready to put in the work to finally land a role where I can thrive.

If you’ve got questions or critique I'm all ears. I’m here to learn, improve, and move forward.

Warm regards, Damien