C keeps giving wrong answers

Learning C, been looking at the loop for 1 hour now. How to fix?

Attached: wrong.png (1087x646, 65K)

Other urls found in this thread:

insights.stackoverflow.com/survey/2019#technology
github.com/uutils/coreutils
github.com/coreutils/coreutils
twitter.com/SFWRedditImages

>uninitialized variables

It's clearly a stack overflow

initialize sum to 0 retard

Turns out to be solution. In what world does it make sense to randomly asign an int a value in the first place?

C assumes you know how to program

Looks like a problem with C. You clearly told them to make room for variables, which should be done by setting the memory to zero.

Shitposting aside, is it worth learning C on 2019?
I'd love to get more involved into lower level languages

this

at the memory adress could still be an old value and by assigning it to zero you're clearing it.

Granted I'm the guy that can initialize variables but yes. If you want to know how to deal with problems that python can't fix (which does happen) then it's vaulable to know how the computer is doing what it is doing.

It's a fun language.
Also your "C++ code" should be mostly C code with C++ stuff where it makes it more readable.

That's the thing, in C, uninitialized stack variables are whatever is on the stack space they take up. There is no good default value for ints to put that justifies the one machine instruction it takes to set them.
I suggest learning to program in a sane, true-to-computer-science language like python. C nothing more than abstract assembly.

it's not only C
Jow Forums says python is language for brainlets but you have to do that in python too

>You clearly told them to make room for variables, which should be done by setting the memory to zero.

Attached: brainlet.png (200x226, 14K)

If and only if you intend to work with legacy code and/or devices with tight constraints on resources. C++ has taken over a lot of C's job positions.

In C, declarations and initialisations are different things. Declaring int sum; just gives you an integer with whatever value existed in that memory location. C doesn't go out of its way to set values to 0. Although external variables (variables outside of any functions) do get initialised to 0.

Holy fucking shit!!! You are so retarded. GTFO my board you technologically illiterate turbo nigger!

I don't intend to work with it, it's just for the fun/challenge of it.
Should I go for C++ instead?

Kill yourself.

>Kill yourself.

Attached: pepe_overdrive.png (858x725, 361K)

He didn't. C's default memory management is literally a prison. You are given the dirty room and blankets of a guy who just got stabbed, and if you touch anything else, you get stabbed too. Retarded language for self-proclaimed hotshots.

Like every page allocation from the OS, stack memory should still be initialized to zero. That doesn't mean e.g. the C library initialization code can't make use of it, but it would be unusual.

I bet the jews did this

This posts reek of stack overflows and memory leaks

Oh if it's just as a hobby, pick up Rust. Or better yet, Go. Go has much better multithreading models, no generics, and you can write anything from operating systems to websties on it.

You Rust-tards never let it up, do you?

>and you can write anything from operating systems to websties on it.
I dislike it already.
I'm gonna go C++, thanks for the info tho.

I'm not shilling Rust, it's already a popular language.

No user, you should really read a book or something.

bud, if it took you over an hour to have someone else solve your problem, you're gonna have a rough go with any language involving manual memory management.

Okay but you should know you're getting into the software engineering world's deepest rabbit hole. I say this as a C++ developer of 12 years. I gave up at C++17 and went back to writing and encouraging people to write "C++ with classes".

int count = 0;

You're welcome.

>if it took you over an hour
lol I'm not the OP.
Also I wanna learn a lower level language to fuck with memory management

>insights.stackoverflow.com/survey/2019#technology

based

github.com/uutils/coreutils

you will likely fail that on any x86 system because everything uses virtual memory maps
buy some stm32 mcu and do fun stuff on that one instead

>github.com/coreutils/coreutils

C and C++ deal with memory in the exact same way all the same: you are given a giant discrete contiguous linear address space and pointers.

>no features
>depends on 20 unaudited libraries

GNU is also unaudited but also barely readable. rm.c and grep.c are a travesty.
>>no features
What???????????

c is better than c++ for that.
The only real difference between C and C++ is virtual lookup tables, and generics.
Though, if you care about speed, you’ll end up devirtualizing everything anyway

you sound pajeet

wow you Rust shills really reply at the speed of light
do you get paid?

C++ has sensible compile-time type deduction so you can more easily write correct object factories and allocators behind them.
No, never been paid to shill.

I mean I've never shilled, so I don't get paid.

C wants to save cpu cycles by not setting memory to zero when it pushes an address to an "int" to the stack? What are they even trying to acheive?

In many cases, you initialize the variable while using it, so its faster than initializing it twice.

*heartbleeds*

Common beginner mistake, user, don't overthink it just pay attention to these details. Next time you get stuck in trivial shit like this, use /sqt/ instead of creating a thread. Be a positive force, help to improve the quality of this board.

Attached: 1536960057152.png (485x409, 55K)

Wtf this user is really positive. Are you sure you are not from reddit?

Attached: 313-3135586_204kib-1018x763-pepe-hug-happy-pepe-hug-png.jpg (820x759, 153K)

Well would you rather have the board full of cp and gore threads?
Positive is better imo

What book are you reading that isn't telling you to initialize your variables before calling them.

Mentioned near the start of book, but doesn't go into detail as to why so I had no idea what was wrong here before.

Interestingly, the program seems to initialize sum to 16 when compiled under mingw64 with GCC 9.1.0 and current libraries, compared to the old 32-bit version of TDM-GCC Code::Blocks is shipping.
If you write
int sum, count, input;
the variable count starts out with 16 instead, but that's not an issue because count is manually initialized.

Attached: gdb.png (979x512, 29K)

This is one of the ways "work on my machine" works.
Depending on the computer, some random value is assigned, and on some computers this value will be harmless, while on others it will crash the whole shit.

A Linux program will always see these variables initialized as zero, as the library startup code makes sure the stack pointer is aligned to a multiple of 16 bytes according to the System V AMD64 ABI before even calling the library initialization function, which in turn eventually invokes the main function in its own pristine stack frame.

So you can write programs that refuse to boot on windows?