Hacker News new | ask | show | jobs
by whatshisface 1871 days ago
Quick, I need to parse a simple string; should I use scanf, fscanf, sscanf, scanf_s, fscanf_s, or sscanf_s?
5 comments

You write your own parser.

Seriously, I don't use scanf that much in C. Most of the times, it doesn't do exactly what I want, or there is a more specialized alternative (ex: strtol). Also, generally, %s is bad, even the "safe" variant. For sscanf, it makes a copy, which is inefficient (if you are writing C, you want efficient code, right?) and you have to know the size in advance, and usually, you don't.

Most of my string manipulation in C involves loops, pointers and a lot of in-place manipulation. It is tedious compared to other languages but it is very efficient, the reason I picked C in the first place.

So in the context of the larger debate, this answer falls in to the "C is simple, until you try to use it, at which point you start writing your own programming language in C to write your application in," category. In this perspective C is simple mainly because it's missing the already-written code that you have to benefit you in other languages.
I don't think any of this is close to fair. "C is simple" and "it takes some work to do stuff in C sometimes" aren't mutually exclusive, in fact the former necessarily implies the latter. C is simple, which means it doesn't have a pre-written obscure built-in or convoluted syntax to handle every possible thing you might want to do in the language. You're going to have to write some of the code in your project yourself. That doesn't mean C isn't simple. I don't think I have to get into why writing a string-parsing function isn't quite the same thing as writing your own programming language.

And C has libraries too, to be fair. So if you prefer the "choose between 25 competing libraries that all provide the desired functionality but half of them are deprecated or don't work" workflow a lot of people are used to from C++ or whatever you can do that. Lots of people use libraries in C. In fact, as you might be aware, your OS's package manager doubles as the C language's third-party package manager. There has been plenty of "already-written C code," because it's a language that has been ubiquitous in computing for fifty years.

I'm not sure where these same tired critiques bubble up from but I see them constantly and they don't make sense. There are a number of entirely valid and damning critiques to be made of the C ecosystem but "there aren't any libraries written in C" and "oh, so you say C is simple, huh? Have you tried using it?!" aren't among them.

You're not going to be a 20 year veteran C developer and then suddenly just today learn about some obscure built-in, and that's what is meant by "simple" here, and that's a good thing.

>You're not going to be a 20 year veteran C developer and then suddenly just today learn about some obscure built-in, and that's what is meant by "simple" here, and that's a good thing.

But you could suddenly learn about a new library feature that performs the same function as another language's builtin, which is for all practical purposes an equivalent experience. In both cases you see an unfamiliar string of characters in someone else's code, and then look up what it does.

someone else on the internet writing a function does not make the c programming language more complicated. c's (lack of) library namespacing, which is responsible for this confusion, is, frankly, one of those valid critiques, but this is gibberish.
I don't understand C programmers are their seemingly utter inability to understand that C is actually not perfect and has tons of flaws, including a lot of complexity. Just because you're used to something doesn't make that thing not complex. Try teaching C to first year undergrads.
I don't understand non-C programmers and their seeming utter inability to understand that C programmers are very aware of C imperfections and how to effectively deal with it, while being equally blind to the flaws and complexity in their own environments.

I learned C as a first year undergrad.

I'm a final year undergrad and all my courses have used C except for the funcprog and OOP courses which used haskell and Java respectively.

I've even been a teaching assistant for the very first programming course students take, so I have taught C to first year undergrads!

I feel like undergrads learn C in their first year? Or at least I did (well I already knew it, but had a class that was in C).
I don't think C is the standard anymore. I'm graduating class of 2022 and my college taught Java to freshmen. My friends at different colleges mostly had the same experience, with a few using Python or C++
This definitely varies by university. I had haskell and C on my first year, only had Java on the second.
On my university we learned C++, already with proper strings, vectors, in-house collection library.

About 30 years ago.

C the language is really neat and exciting. C the ecosystem, including standard libraries, is a lot less compelling.
I do (well to apprentices who are the same age and same level) I teach about 2 hours a week on c++ and they are fine. They pass the cpp institute exams (which are super hard!)

So unless you actually DO teach then don't assume :)

Have taught first year undergrads. Highly recommended experience: 5 stars.

C definitely has many flaws, but I wouldn't say complexity is one of them. I wulld rather say it's too simple and one has to add the complexity all by themselves :D

Learned C in a class as a first year undergrad, it was great!
C was my first language as a first year undergrad, in mechanical engineering to boot. I think I came out of the experience just fine.
How often do you need to parse a string in an RTS game?

If you need to do lots of string processing, C is simply not the right language.

Thankfully one isn't forced to only learn a single programming language in life ;)

>How often do you need to parse a string in an RTS game?

Asset files, config files, shaders, obj files, CSV, XML, JSON, etc... it's not uncommon. Read the asset loading code in the posted game.

For a language that doesn't even know what a "string" is, C applications tend to involve a lot of string parsing.

I type "man scanf" and check which one I need, I wish other languages had such accessible documentation.
Is this supposed to be a trick question? All do virtually the same thing at their core, differing in where they take their input from and whether they are the C11 bounds checking variants. You can add all the v prefix variants too, doesn't really make things more difficult.

You could have thrown in a bunch of functions from string(3), but even then... C string handling isn't great by any means, but it isn't vast or complicated either.

You can learn the C stdlib+system headers, but at that point you're doing something more or less equivalent to learning a high level language's features, and you're past the simplicity of K&R.
I'm not sure what you mean. C is a high level language. And yes it and standard library has obviously grown more complex than it was 50 years ago.

It's small and easy to grasp the entire thing though, unlike most other more modern high level languages.

That said, it's not entirely clear that matters too much. Almost any non-toy C project is going to be bringing in libraries outside libc, so you still need to go off and learn those if you want to work on a project. Doesn't really matter whether they're in the base language or not.

I think the argument is converging on, "by the time you do anything useful with it, C is as complex as any other language, except for C++ which almost nothing is as complex as." That's more or less what everyone seems to agree on.
Ready for a random question out of the 200+ UB cases on the ISO C, or about a random feature from any C compiler?
I'm not sure if you replied to the wrong comment or not because it doesn't address what I wrote.
I surely replied to the right comment,

> It's small and easy to grasp the entire thing though, unlike most other more modern high level languages.

So ready for the question?