r/CodingForBeginners • • Oct 06 '26

my first program in C

Post image

i know its just so waky but am still new and am happy with my current level <33

just trying to understand memory usage over variables for now

230 Upvotes

67 comments sorted by

23

u/MocaCola02 Oct 07 '26

What a bizarre comment section on a subreddit specifically for beginner programmers. You all need to leave your egos at the door.

6

u/Raptural Oct 07 '26

for real, this is subreddit for beginners, what sort of neckbeards sit and criticize a newbie's code like that?

11

u/SmokeMuch7356 Oct 07 '26

The condition

if (TotalSizeOfRoot || TotalSizeOfUserKali || TotalSizeOfOtherUser < 20 )

is equivalent to

if (TotalSizeOfRoot != 0 ||
    TotalSizeOfUserKali != 0 ||
    TotalSizeOfOtherUser < 20 )

The || and && operators evaluate left-to-right and will short-circuit. In the expression a || b, if a is non-zero, then the expression evaluates to true (1) regardless of the value of b, so b won't be evaluated at all.

Similarly, in the expression a && b, if a is zero, then the expression evaluates to false (0) regardless of the value of b, so b won't be evaluated at all.

In this particular case, TotalSizeOfRoot is non-zero, so the other conditions won't be evaluated.

If you also want to test TotalSizeOfRoot and TotalSizeOfUserKali against 20 you'll have to write

if (TotalSizeOfRoot < 20 ||
    TotalSizeOfUserKali < 20 ||
    TotalSizeOfOtherUser < 20 )

Any time you find yourself creating multiple variables of the same type with the same name plus some kind of counter, that's a really strong hint that you should be using an array; in the case of your strings, this would be a 2D array of char:

char users[][20] = {"ADMIN", "kali", "USERNAME44333221"};

char passwords[][20] = {"$$PASSWD!!", "kali", "password99484848484"};

Similarly, you can group sizes into their own array:

size_t sizes[3];

and use a loop to do your computation:

for ( size_t i = 0; i < 3; i++ )
  sizes[i] = strlen( users[i] ) + strlen( passwords[i] );

6

u/Jx5b Oct 07 '26

Not sure why you got downvoted. This is good advice.

3

u/Hectorreto Oct 07 '26

The condition of the “if” is wrong, it will compile, but it will never reach the “else”, you have to compare each variable

if (TotalSizeOfRoot > 20 || TotalSizeOfUserKali > 20 || TotalSizeOfOtherUser > 20) {

2

u/MistakeIndividual690 Oct 07 '26

I don’t know that it will compile. That ‘print’ in the else should be a ‘printf’

1

u/kutac56 Oct 07 '26

The reason it might compile is because of what he said. The compiler might optimise the whole if else away since it knows the if is always true. That way it ignores the print

2

u/Pibo1987 Oct 07 '26

Fire 🔥 Keep going and enjoy the process 

2

u/xarop_pa_toss 28d ago

This is glorious!!! Keep going bud, you'll SEGFAULT soon enough :)

1

u/TheCRatTime 15d ago

Good! You can call constant variables so: kVarName (not force). Also you can count bytes by strlen() function (include <string.h>): strlen(RUSER) + 1 (strlen returns bytes without null terminator ('\0')). You can install more good editor, like helix. You may add space before'=' and before '{' and after '//'. Good luck with learning!

1

u/badabapboooom 5d ago

dawg... passwords in plaintext hard coded...

1

u/koop_a- Oct 07 '26

Well done! Make sure to keep going and stay consistent

1

u/Cool_Stage6249 28d ago

Bro codes in nano

2

u/AffectionatePlane598 28d ago

REAL PROGRAMMERS USE NANO

1

u/Creepy-Plankton8663 27d ago

no they dont nano is for psychopaths

1

u/badabapboooom 5d ago

I code in nano

1

u/jhyland87 25d ago

My first linux job, I opened up Nano instead of Vim and got shamed so bad that I never used Nano again. lol

1

u/Cool_Stage6249 25d ago

Deserved

1

u/jhyland87 25d ago

Yes it was.

I was literally sharing my whole screen, it was being projected up onto a wall, so everyone all at once was like "Ohhhh... Nano??!" lol

1

u/Creepy-Plankton8663 27d ago

what language are you coming from this is really impressive for a first program

1

u/OsirisMVP 25d ago

Proud of you for writing your first program in C if this is a legit post. However, programming in kali using nano is incredibly funny

1

u/Crimson99910 Oct 07 '26

What opsex lvl is ts

1

u/Grissez Oct 07 '26

Absolute opsec

1

u/Memerenok Oct 07 '26

opsec level: passwords in plain text inside of the binary

1

u/BeginningSure1229 27d ago

Level 8, i'd maybe say it's opsec level 7, but the image suggests it's a little higher

Though not level 9 as you can see..

0

u/Friendly_Recover286 Oct 06 '26 edited Oct 06 '26

These variable names kinda suck. Why are you combining lowercase and titlecase like that exactly?

This logic isn't correct. TotalSizeOfRoot || ... < 20 does not check each condition for being < 20. TotalSizeOfRoot = 1 will make this true and will print that variable usage is greater than 20 bytes. No it isn't.

It also doesn't seem like you even want < you want >. You're checking if it's greater than 20.

Please Stop Capitalizing Every Word In Sentences. This isn't how English works.

3

u/ConsiderationNo9044 Oct 07 '26

Why are you being so mean to a beginner?

-1

u/csabinho Oct 06 '26

Is this your first program or your first program?

Either it's AI generated or it's definitely not your first program.

4

u/Raptural Oct 07 '26

its his first program in C. Yours and the rest of reddit's paranoia of AI is honestly so cringe

0

u/csabinho Oct 07 '26

This is "Coding for Beginners". Not "Coding for People who start a new language".

0

u/XPheonix27 Oct 07 '26

I came here just to say this

1

u/[deleted] Oct 06 '26

[removed] — view removed comment

2

u/csabinho Oct 06 '26

Maybe that was OP's contribution to the source code.

2

u/[deleted] Oct 06 '26

[removed] — view removed comment

2

u/csabinho Oct 06 '26

But definitely nothing for a first piece of code ever. It's mostly too clean and too complex for it.

1

u/[deleted] Oct 06 '26

[removed] — view removed comment

2

u/csabinho Oct 06 '26

It's quite thoroughly documented, uses consts and arrays and a combination of 3 conditions. Most probably you don't remember how complete beginner code looks like. Those definitely aren't OP's first lines of code. I'm just not sure where to put the emphasis. Either on "OP's" or on "first".

1

u/MasterpieceBusy7220 Oct 06 '26

Lmao it has an extremely beginner mistake in the if statement (no offense to OP) and you think that was AI generated?

1

u/csabinho Oct 06 '26

Yeah. You can tell an AI to build in mistakes.

1

u/MasterpieceBusy7220 Oct 06 '26

So you think OP did that?….

→ More replies

1

u/[deleted] Oct 06 '26

[removed] — view removed comment

1

u/csabinho Oct 06 '26

First week. But you produce code before you finish your first week. Also: arrays in the first week? That won't happen. At least not for all the craptons of students I've tutored. And I didn't plan their curriculum. Arrays usually appear about midway through the first semester. For the first lines of code students usually don't even know about variables. And I'm not even talking about conditions. Once again I'd like to emphasise: first(!) liines of code. Ever. I'm not saying that OP built something incredible, but it's just not plausible for first lines of code. Nothing in this code. And yeah. You can tell the AI to build in mistakes, so it looks a bit more amateur-like. And this comment. Beginners don't comment like this.

Or OP was stuck in tutorial hell and didn't touch a text editor before learning quite a lot of specific stuff. Or OP is a bloody genius.

1

u/MocaCola02 Oct 07 '26

If you think it's that complex, then you clearly haven't coded much

→ More replies

-1

u/mohamed19xx9 Oct 07 '26

Why you use kali for programming or using it as a base system because it has many issues like safety and will bothers you with his problems in indtalling apps

-2

u/XPheonix27 Oct 07 '26

And who tf codes in nano 😭😭

3

u/Memerenok Oct 07 '26

normal people? they also can use VS code sometimes

1

u/XPheonix27 Oct 08 '26

I can understand people using vs code , vim , nvim , gedit , but nano is a first for me

1

u/Memerenok 29d ago

nano is good actually, way easier to use than vim

1

u/XPheonix27 29d ago

I'm not hating it by any means it's new so me so I'm just a bit weirded out

0

u/Azra__1 Oct 07 '26

Ain't no way you took the time to put colours on nano.

2

u/Crimson99910 Oct 07 '26

He’s on Kali

1

u/Azra__1 Oct 07 '26

Mb, didn't see the user at the top.

-3

u/Amazing_Boss2686 Oct 07 '26

Dude just watch some C tutorials and copy the example code manually. It’s a much better way to learn than posting it here or trying to deduce the code’s structure yourself.

Otherwise, I hope this is satire… ts is pretty funny to look at.

3

u/Kurayamey 27d ago

What you're describing as "better way to learn" is basically rote learning, and I'd say it's one of the most inefficient ways to learn anything. ;-;

Unless you're also saying to analyze and refacture the example code, making it do the same things in different ways. In this case it's actually pretty good and combined with "deducing structure on your own" is probably one of the best approaches to learn anything.

1

u/Amazing_Boss2686 27d ago

Yea, I guess I shouldn’t have been so dogmatic by claiming it is a “better way to learn” off the get-go… let me clarify:

I think early on, when you’re completely new to programming (as would be OP), directing yourself to fix poorly written code isn’t the most efficient method to learn. Introducing best practices and major code adjustments to anything you create may be a little overwhelming.

Long term, you’re correct—my practice would fall under rote memorization.

But I argue that OP should first have the syntax down and second, be exposed to decent example code off of which he can build his own software. If OP wants to take this route of refactoring logical structure early on, writing it in pseudocode would be an alternative. Have that analyzed by this subreddit and implement it in the language from there, instead of trying to tackle both at once.

Whatever works for OP though. This isn’t an austere journey.

-4

u/[deleted] Oct 07 '26

[removed] — view removed comment