← Home ← Back to /g/

Thread 106153346

112 posts 28 images /g/
Anonymous No.106153346 [Report] >>106153362 >>106153414 >>106154503 >>106157258 >>106157498 >>106157511 >>106157600 >>106157783 >>106158019 >>106158207 >>106160678 >>106161137 >>106162396 >>106163413 >>106164429 >>106165509 >>106165539 >>106165822 >>106166285 >>106167139 >>106167174 >>106167377 >>106168064 >>106169273 >>106173635
This is how you read a number in 100% memory-safe type-safe C. Take note, rustrannies!
Anonymous No.106153362 [Report] >>106157511
>>106153346 (OP)
>but saaaaaar using c makes me look badass since i call malloc saaaar please saar call me smart because i use malloc saaaaaaaaaar
Anonymous No.106153414 [Report] >>106153562
>>106153346 (OP)
Is this an llm thing?
Anonymous No.106153461 [Report]
i had a stroke reading that code
Anonymous No.106153562 [Report] >>106166285
>>106153414
No, it's from https://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html
Anonymous No.106154503 [Report] >>106161094 >>106162658
>>106153346 (OP)
This makes me miss my old job. Writing C programs really was a lot of fun.
I yearn for those days when I had written a couple thousand lines and was finally ready to start trying to compile, I would get a bunch of errors but slowly whittle them down until there's not so much as a warning and the program runs for the first time and I start to see the fruits of my labor. *chef's kiss*
Anonymous No.106154674 [Report] >>106159902 >>106172401
https://github.com/openbsd/src/blob/master/lib/libc/stdlib/strtonum.c
Anonymous No.106155030 [Report] >>106155241 >>106155571 >>106157952
(defun read-integer ()
(format t "Enter a number:~%")
(handler-case (values (parse-integer (read-line)))
(parse-error () (cerror "Try entering a number again." "That is not a number!~%") (read-integer))))
Anonymous No.106155241 [Report]
>>106155030
I like lisp's integers that default to being variable width. Makes a lot of prototyping algorithm really easy, instead of relying on mpfr/gmp calls
Anonymous No.106155571 [Report]
>>106155030
>no read error
Anonymous No.106157258 [Report]
>>106153346 (OP)
I wrote my own number conversion functions because I don't trust in the C library.
Anonymous No.106157498 [Report] >>106157580
>>106153346 (OP)
make your life a little easier
https://docs.gtk.org/glib/method.IOChannel.read_line.html
https://docs.gtk.org/glib/func.ascii_strtoll.html
Anonymous No.106157511 [Report] >>106161104
>>106153346 (OP)
>>106153362
Just use the standard library. Rust has more boilerplate than C and it has so much more of it that it's not even a contest.
Anonymous No.106157580 [Report]
>>106157498
>le global errno
Dude, just return a fucking error, at least they are not using the locale shit.
Anonymous No.106157600 [Report]
>>106153346 (OP)
Use case?
Anonymous No.106157733 [Report] >>106166527
In Beef it's just
static int Main()
{
Loop:repeat
{
Console.Write("Enter a number: ");
let buffer = Console.ReadLine(.. scope .());

switch (int64.Parse(buffer))
{
case .Ok(let val):
Console.WriteLine("You've entered: {}", val);
break Loop;

case .Err(.Overflow):
Console.WriteLine("Sorry, this number is too small or too large");

case .Err(let err):
Console.WriteLine("Error: {}", err);
}
} while (true);

return 0;
}
Anonymous No.106157783 [Report] >>106158072 >>106160433 >>106160795 >>106167284
>>106153346 (OP)
Anonymous No.106157952 [Report]
>>106155030
>lisp

End of discussion.
Anonymous No.106158019 [Report]
>>106153346 (OP)
#include <stdlib.h>

char line[256];
int n;
printf("Type an integer:\n");
getline(line, 256);
n = atoi(line);
Anonymous No.106158072 [Report] >>106158149 >>106160433
>>106157783
>blue checkmark
>greentexting on twitter
>filtered by lifetimes
Tells me all I need to know
Anonymous No.106158149 [Report] >>106158174 >>106158509 >>106158671 >>106166564 >>106169054
>>106158072
Anonymous No.106158174 [Report] >>106158671
>>106158149
Doesn't /g/ hate systemd? Wouldn't this be an endorsement of sorts?
Anonymous No.106158207 [Report] >>106163309
>>106153346 (OP)
jesus is this what AI code looks like? it looks like the kind of code i'd be embarrassed to see from a new hire.
Anonymous No.106158509 [Report] >>106158671
>>106158149
Is there a way to disable Rust kernel modules?
Anonymous No.106158671 [Report] >>106158745 >>106167650
>>106158149
>>106158174
>>106158509
fake screenshot
https://github.com/systemd/systemd/issues/18767#issuecomment-784570324
Anonymous No.106158745 [Report] >>106167650
>>106158671
it was edited later its real
Anonymous No.106159902 [Report]
>>106154674
I hate the single return boomerism
Anonymous No.106160143 [Report]
usecase for user entered numbers or strings?
Anonymous No.106160433 [Report] >>106165544
>>106157783
>>106158072
kai_fall is a retard h1b pajeet lmao
https://www.reddit.com/r/golang/comments/y9priy/go_vs_scala_vs_scss/?q=kai_fall&type=comments
Anonymous No.106160451 [Report] >>106161137
every day this fucking retard starts a thread whining about either go or c, yet he clearly doesn't know either
Anonymous No.106160678 [Report] >>106162578 >>106162675
>>106153346 (OP)
>100% memory-safe
what if they entered 1025 characters?
Anonymous No.106160795 [Report] >>106161137 >>106161443 >>106164675 >>106166013
>>106157783
Rust is fine if you use it like C. Unfortunately most people don't write Rust like they write C.
Anonymous No.106161077 [Report]
Ok so write a small header library for macros/inline functions to check these things. I'm sure something exists that does this for you if you're really that retarded.
Anonymous No.106161094 [Report]
>>106154503
Holy cringe.
Anonymous No.106161104 [Report]
>>106157511
Just write your own library, you cuck. It's not that hard. libc is mostly locale-dependant dogshit.
Anonymous No.106161137 [Report]
>>106153346 (OP)
>>106160451
>Memory safety is when you're pedantic about error checking instead of showing an error dialog when a function returns an unexpected value
Yeah OP clearly doesn't know what he's talking about.
>>106160795
They said that about C++ too. Unlike C++ Rust doesn't have ABI compatibiltiy.
Anonymous No.106161443 [Report]
>>106160795
>Rust is fine
It really isn't.
Anonymous No.106162396 [Report]
>>106153346 (OP)
Good, so I just write that once and then I won't have to worry about my small program ballooning to 20MB?
Anonymous No.106162578 [Report]
>>106160678
check that a terminator exists in the buffer before using cstdlib string functions.
or don't use cstdlib string functions because its 2025 and we've known about the problems for 30 years
Anonymous No.106162658 [Report]
>>106154503
>the program runs for the first time and I start to see the fruits of my labor: *segfault*
Anonymous No.106162675 [Report] >>106162712
>>106160678
fgets reads 1023 and sets the null terminator.
Anonymous No.106162712 [Report] >>106162805
>>106162675
what happens when you call fgets again? is what's left of the previous buffer still there? i recall bumping into that behavior at some point
Anonymous No.106162805 [Report] >>106162822
>>106162712
In that the rest would be in stdin's buffer. So in next iteration fgets reads those.
Anonymous No.106162822 [Report]
>>106162805
i thought either windows or linux threw it away, but maybe im high
Anonymous No.106163309 [Report] >>106163330
>>106158207
I doubt you've ever worked a job let alone hired anyone
Anonymous No.106163330 [Report]
>>106163309
what's the matter, neet can't imagine someone being productive and contributing to society in a meaningful way?
Anonymous No.106163391 [Report]
Your brain is broken, did you get chatgpt to write that reply?
Anonymous No.106163413 [Report] >>106165822
>>106153346 (OP)
int main(int argc, char* argv[])
no return statement
not storing and replacing the incoming errno
retarded comments

microsoft is cancer
Anonymous No.106164429 [Report] >>106164863
>>106153346 (OP)
>char buf[1024]
An unsigned integer (uint64_t) only has like 20 characters (in decimal).
Anonymous No.106164675 [Report]
>>106160795
if you think rust is fine you are probably okay with pacifisim
Anonymous No.106164863 [Report] >>106167592
>>106164429
can't you read the comment? "just to be sure"
degenerates like him belong on a cross and are why fucking notepad is burning 17% of my cpu and using 2.5 GB of ram to open a 1 MB text file
Anonymous No.106165509 [Report]
>>106153346 (OP)
Looks simple and readable to me. I don't get your problem.
Anonymous No.106165539 [Report]
>>106153346 (OP)
I'm nooting!
Anonymous No.106165544 [Report]
>>106160433
so just like most of /g/?
Anonymous No.106165810 [Report] >>106165933
User input is considered gay in computer science. Only gay men want to take input, and they write programs. If you insist on being gay, go full on bug chaser with raw scanf.
Anonymous No.106165822 [Report] >>106166080 >>106166136
>>106153346 (OP)
Keep up the studies with need more C men
>>106163413
Java is cancer, int argc, char* argv[] , should used unless the application is to use to it.

I guess you could only read comments, as single glance told me snipped was written by a man from the void, with a ugly neomodernistic style.
Anonymous No.106165933 [Report]
>>106165810
>they write programs.
*they write GAY programs
Anonymous No.106165980 [Report]
I know its bike shedding, but I always preferred to have ndone as a variable opposed to something like success. I never really liked having to negate my loop condition, with a ndone, you can set it to true for the task not being done and false once it completes.
Anonymous No.106166013 [Report] >>106166837
>>106160795
If only there was a language that lets you write C code out of the box.
Anonymous No.106166080 [Report]
>>106165822
msvc started the void main(void) crap.
that program is spitting loads of compiler warnings, whats two more about unused variables?
Anonymous No.106166136 [Report] >>106166363 >>106167091
>>106165822
>should used unless the application is to use to it.
Fuck no. It should be part of a library or something, like int getargscount(void) and const uint8_t* getargs(int) because that is how the OS works.
Anonymous No.106166285 [Report]
>>106153346 (OP)
>>106153562
>https://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html
i lost iq from reading this article

i will find you op, and do unspeakable things to you
Anonymous No.106166363 [Report] >>106166950
>>106166136
>that is how the OS works
what OS?
windows doesn't parse arguments
Anonymous No.106166527 [Report]
>>106157733
what is the :repeat after Loop label for? wtf is .. scope .() shit?
Anonymous No.106166564 [Report]
>>106158149
>systemd
>"basic building block for Linux systems"
kek
Anonymous No.106166837 [Report] >>106166846 >>106166936
>>106166013
I like C but using shell scripts to build my project fucking sucks.
>inb4 use x build system
They are all, without fail, worse than using shell scripts.
Anonymous No.106166846 [Report] >>106166936
>>106166837
embrace the suck that is cmake
Anonymous No.106166936 [Report] >>106167005 >>106167083
>>106166837
>>106166846
mmmmmmakefile
NAME := nouvo

CC := gcc
CFLAGS := -O3 -march=native -funroll-all-loops -flto -fgnu89-inline -g

SOURCE_DIR := src/
INCLUDE_DIR := inc/
CURRENt_cell_path := $(PWD)

SOURCES := $(shell find . -name '*.c')
INCLUDES_PATHS := $(shell find . -name '*.h')
INCLUDES := $(foreach path, $(sort $(dir $(shell find $(CURRENt_cell_path) -name *.h))), -I $(path))

all :
clear
$(CC) $(CFLAGS) $(SOURCES) -o $(NAME) $(INCLUDES)
Anonymous No.106166950 [Report]
>>106166363
Yes, that is what I mean.
Anonymous No.106167005 [Report] >>106167026
>>106166936
I'll give it a try. Ty white man.
Anonymous No.106167026 [Report]
>>106167005
you're welcome
Anonymous No.106167083 [Report] >>106167286
>>106166936
Fucking nigger-
Anonymous No.106167091 [Report]
>>106166136
Java sickness at OS level
Anonymous No.106167139 [Report] >>106167257 >>106167314
>>106153346 (OP)
scanf is not evil, it's simple and powerful
Anonymous No.106167174 [Report] >>106167251
>>106153346 (OP)
>Sorry, this number is too small or too large.
Are you fucking insane?
Anonymous No.106167251 [Report]
>>106167174
>https://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html

He just copy pasted a snippet
Anonymous No.106167257 [Report] >>106167265 >>106167369 >>106167533 >>106167556 >>106167602 >>106168019
>>106167139
Some graybeards think you need error checking for "hello world!". I'm not even fucking joking.
Anonymous No.106167265 [Report] >>106172247
>>106167257
My phone is a fag.
Anonymous No.106167284 [Report]
>>106157783
>somebody who chops off their own dick is a masochist
well color me surprised! who would have thunk?
Anonymous No.106167286 [Report]
>>106167083
im really not sure i wanna deal with this
Anonymous No.106167314 [Report] >>106167337 >>106167434
>>106167139
What if the read fails and the unititialized example is between 1 and 6000?
Anonymous No.106167337 [Report] >>106167342
>>106167314
Then you don't get to see a number. OH THE HUMANITY
Anonymous No.106167342 [Report]
>>106167337
Yes you do
Anonymous No.106167369 [Report]
>>106167257
Hahaha, the author is probably the idiot that submits to the new iso trash.
Anonymous No.106167377 [Report]
>>106153346 (OP)
We should do away with headers
Anonymous No.106167434 [Report]
>>106167314
Well you clearly failed to read
Anonymous No.106167533 [Report] >>106167577
>>106167257
name this faggot with the bad grammar
Anonymous No.106167556 [Report] >>106167611
>>106167257
This actually made me laugh
Anonymous No.106167577 [Report]
>>106167533
Seacord. Could honestly be anyone on the c committee though. They all think they're writing the next Critique of Pure Reason. They want the language to seem as perfectly unapproachable as possible.
Anonymous No.106167592 [Report]
>>106164863
A stack-allocated array costs literally nothing.
Anonymous No.106167602 [Report]
>>106167257
its an educational piece
you dont wanna be teaching bad habits
i agree with the author
learn to walk before you run

if more people got trained in the spirit of nigh-autistic attention to detail, c code would have been of much higher quality
i was, i learned in a peer learning setting where we checked eachother's programs
at first it made me paranoid but with experience this morphed into rock solid work habits
Anonymous No.106167611 [Report] >>106167712
>>106167556
The only way I could even find of triggering an error was outputting to the bit bucket. I guess if you're trying to "hello world" on a wind up alarm clock, this "safe c" will really come in handy.
Anonymous No.106167650 [Report] >>106167672
>>106158671
Why do cniles always lie?

>>106158745
Why doesn't it say it was edited next to post timestamp?
Anonymous No.106167672 [Report]
>>106167650
>t. projecting nocodeshitter
Anonymous No.106167712 [Report] >>106167841
>>106167611
thats not really the point though, is it?
how many c programs have you seen where return values are never checked?
Anonymous No.106167841 [Report] >>106168025 >>106168523
>>106167712
The author of that safe code abomination got to have fun learning c with gets() from r&k, but his "from scratch" intro book jumps from a brainfuck hello world to alignment specifiers in mere pages... with zero ramp up. Just completely out of the blue after telling us what an int is.
Anonymous No.106168019 [Report]
>>106167257
Anonymous No.106168025 [Report]
>>106167841
Anonymous No.106168064 [Report]
>>106153346 (OP)
Nice try, but I don't read.
Anonymous No.106168523 [Report] >>106168702
>>106167841
...based? Why would I need yet another programming book to explain integers? Not every book needs to be aimed at the absolute beginners.
Anonymous No.106168702 [Report]
>>106168523
Sure. The book sells itself as being for absolute beginners. Fear not though. Despite introducing very complex and niche concepts like above, it doesn't cover them in any depth whatsoever.

Incidentally, it devotes dozens of pages to drilling into into and arithmetic. Not pointer arithmetic, but a bare metal examination what happens to individual bits during basic addition and subtraction.

Ints and chars are of course covered at nauseating length after perplexing the "absolute beginner" target of the book with near zero depth forays into endianess and function pointers, which are never to be mentioned again.

Point being. Don't read c committee guys books. They're not "for" anyone.
Anonymous No.106169054 [Report]
>>106158149
>IBM Power servers still exist
I thought that architecture died in like 2015? Nobody runs anything except arm64, amd64, and a few financial dinosaurs still running s390x compatibles these days.
Your server is either an Android phone mining bitcoin on USB-C or a Dell rackable and nothing else.
Anonymous No.106169273 [Report] >>106172263 >>106173453
>>106153346 (OP)
Only C, JVM (Java, Clojure, Scala, Kotlin) and Haskell exist. All other languages are irrelevant
>I wanna webshit
Use a JVM language for the backend. If you use something that isn't a shit library like Spring Boot you're going to be ok, the JVM is really good with it's GC. Don't really like java? Use literally any other JVM based language.
>Quick CLI utility
Either Haskell or C, bash if it's really really simple. Haskell if you'll be parsing text. Parser combinators ftw.
>Performance intensive application
C or C-style C++, in the worst case (please no "modern" C++). No you don't need Rust, yes you can use Assembly if it's something that needs that much optimization.
>I want to do big data
JVM, once again
>AI! LLMs!
Java or C++. Python is a massive cope for this.
>I'm only learning programming to get a job
Java is the best language for enterprise bullshitting and moving papers around pretending to work (modern software development)

You shouldn't bother with any other languages
Anonymous No.106172247 [Report]
>>106167265
It's because 4chan strips the EXIF rotation metadata.
Anonymous No.106172263 [Report]
>>106169273
>implying JVM isn't C
Anonymous No.106172401 [Report] >>106175344
>>106154674
I'm not that fluent in C.
When calling that function you'll pass a reference to a string to store an error in (const char **errstrp). You could pass NULL if you don't care. But if you pass a valid pointer and there is an error, the pointer will be set to a string that's defined within the function: *errstrp = ev[error].errstr;

Could that not cause issues? Like memory leaks if the memory is never freed, or use after free if it is?
Anonymous No.106173453 [Report]
>>106169273
> Java is the best language for enterprise bullshitting
it's no longer 1998. java shit is rapidly depreciating since oracle went full retard with licensing, forcing people to use the openjdk or just abandoning java completely.
Anonymous No.106173635 [Report] >>106178982
>>106153346 (OP)
If I had to write a program that only reads numbers from stdin it'd look like this
#include <errno.h>
#include <unistd.h>

/* returns
* 0 success
* >0 error
* <0 EOF */
static int
next_int(int *p) {
static char buf[512];
static int i, n;
int err, sign, x;
int ret, c;
err = 1;
sign = 1;
x = 0;
for (; ; i++) {
if (i >= n) {
do
ret = read(0, buf, sizeof buf);
while (ret == -1 && errno == EINTR);
if (ret == -1)
return 1;
if (ret == 0) {
err = -err;
break;
}
n = ret;
i = 0;
}
c = buf[i];
if (err == 1)
switch (c) {
case '-':
sign = -1;
case '+':
err = 2;
case ' ': case '\n': case '\t':
continue;
}
if (c < '0' || c > '9')
break;
if (x >= 100000000)
return 1;
x = x*10 + c-'0';
err = 0;
}
if (!err)
*p = sign*x;
return err;
}
Anonymous No.106175344 [Report] >>106178923 >>106179636
>>106172401
it's static const string, not allocated. no need to free it
Anonymous No.106178923 [Report]
>>106175344
Why is it static?

I mean, I'm sure it's fine given where it's from.
Anonymous No.106178982 [Report]
>>106173635
Never write C ever again
Anonymous No.106179636 [Report]
>>106175344
It's not static, chud, it's stack allocated.