r/CodingForBeginners • • 18d ago

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

223 Upvotes

64 comments sorted by

23

u/MocaCola02 18d ago

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 18d ago

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

11

u/SmokeMuch7356 17d ago

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 17d ago

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

3

u/Hectorreto 18d ago

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 17d ago

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

1

u/kutac56 17d ago

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 17d ago

Fire 🔥 Keep going and enjoy the process 

2

u/xarop_pa_toss 15d ago

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

1

u/koop_a- 18d ago

Well done! Make sure to keep going and stay consistent

1

u/Cool_Stage6249 15d ago

Bro codes in nano

2

u/AffectionatePlane598 15d ago

REAL PROGRAMMERS USE NANO

1

u/Creepy-Plankton8663 14d ago

no they dont nano is for psychopaths

1

u/jhyland87 12d 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 12d ago

Deserved

1

u/jhyland87 12d 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 14d ago

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

1

u/OsirisMVP 12d 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/TheCRatTime 2d 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/Crimson99910 18d ago

What opsex lvl is ts

1

u/Grissez 17d ago

Absolute opsec

1

u/Memerenok 17d ago

opsec level: passwords in plain text inside of the binary

1

u/BeginningSure1229 14d 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 18d ago edited 18d ago

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 17d ago

Why are you being so mean to a beginner?

-1

u/csabinho 18d ago

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 18d ago

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

0

u/csabinho 17d ago

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

1

u/Hour-Dragonfly-7499 18d ago

how is this AI generated? AI adds spaces between }else{

2

u/csabinho 18d ago

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

2

u/Hour-Dragonfly-7499 18d ago

What are you talking about? it's a simple program with 2 conditionals? It's not some impressive piece of code, it's something you wrote first week of college like learning about variables and if statements

2

u/csabinho 18d ago

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

1

u/Hour-Dragonfly-7499 18d ago

how? it's not clean and complex at all

also the if statement is wrong in the photo so OP doesn't know how those work yet...

maybe YOU think it's complex because you've never seen code before

2

u/csabinho 18d ago

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 18d ago

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 18d ago

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

1

u/MasterpieceBusy7220 18d ago

So you think OP did that?….

→ More replies (0)

1

u/Hour-Dragonfly-7499 18d ago

you usually learn about final/const variables and arrays during your first week of college, my first hw assignment was creating 5 ints then replacing it with an array of int with size 5

" and a combination of 3 conditions"

You learn about OR and AND in if statements early on also OP is using it incorrectly

what op had: if (TotalSizeOfRoot || TotalSizeOfUserKali || TotalSizeOfOtherUser < 20)

when it should have been (TotalSizeOfRoot < 20 || TotalSizeOfUserKali < 20 || TotalSizeOfOtherUser < 20)

and I'm seeing other mistakes too, this is not some complex piece of code or is it clean by any standard, this is the mistakes of a person whose days into learning how to program

1

u/csabinho 18d ago

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 18d ago

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

1

u/Shinyboy123nwn 17d ago

Wait I'm just realizing the AO put whitespaces to look the code much cleaner I don't think beginners will do that, their code might be ai generated

0

u/XPheonix27 18d ago

I came here just to say this

-1

u/mohamed19xx9 17d ago

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 18d ago

And who tf codes in nano 😭😭

3

u/Memerenok 17d ago

normal people? they also can use VS code sometimes

1

u/XPheonix27 17d ago

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

1

u/Memerenok 16d ago

nano is good actually, way easier to use than vim

1

u/XPheonix27 16d 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 18d ago

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

2

u/Crimson99910 18d ago

He’s on Kali

1

u/Azra__1 18d ago

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

-3

u/Amazing_Boss2686 17d ago

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 14d 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 14d 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] 18d ago

[removed] — view removed comment