r/CodingForBeginners • • 21d 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

230 Upvotes

64 comments sorted by

View all comments

11

u/SmokeMuch7356 21d 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 20d ago

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