Help with the output.

Zns

Joined
Mar 24, 2024
Messages
2
Reaction score
0
Hello, I can't find out why the output is the one in the picture, I can't understand the "k%4" condition
 

Attachments

  • 1.PNG
    1.PNG
    27.5 KB · Views: 22
Joined
Sep 21, 2022
Messages
171
Reaction score
24
K%4 is not a logical expression. % is the modulo operator. In this case it returns 0, 1, 2, or 3 depending on the value of K.

My guess is, C is treating 0 as false, and non zero as true.

The inner loop prints world when K is 150 (150%4=2), when K is 125 (125%4=1), and stops when K is 100 (100%4=0)

This is poor programming style. Just my opinion.
 

Zns

Joined
Mar 24, 2024
Messages
2
Reaction score
0
K%4 is not a logical expression. % is the modulo operator. In this case it returns 0, 1, 2, or 3 depending on the value of K.

My guess is, C is treating 0 as false, and non zero as true.

The inner loop prints world when K is 150 (150%4=2), when K is 125 (125%4=1), and stops when K is 100 (100%4=0)

This is poor programming style. Just my opinion.
Thank you for the reply.
 
Joined
Sep 21, 2022
Messages
171
Reaction score
24
On the other hand, I would have no gripe with...

for(j=0;0<(k%4);j++)

That would be fine.

The reason I used the words "my guess is" was because the last time I used a C compiler, it didn't support a -= operator, so I can't make assumptions.

Speaking of true and false values, some programmers define them as constants at the start of their programs. That would make it easier to translate a program into another language. But then they use numeric literals elsewhere in their code, which makes the logical constants kind of pointless.

Perhaps its not about portability, I could be wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,188
Members
46,755
Latest member
HudsonP082

Latest Threads

Top