Jump to content

Help with C.

protato117
Go to solution Solved by Art Vandelay,

I'm tryna make sense out of this code but I can't seem to fully understand it...can you please explain this further? just the printf bit at least... 

I think it works out to:

		// % is the modulo operator		if ((x+1)%7 != 0) //(x+1)%7?"\t%d":"\t%d\n" this is a ternary operator, basically if-else on the same line. 0 means false, not 0 means true in a boolean expression, you don't need a comparison.		{			if ((x/7) % 2 == 1) //x/7&1?6-x%7:x%7 another ternary operator. & is the logical AND operation. doing &1 checks the last binary digit, which is 1 if odd, 0 if even.			{				printf("\t%d",(arr)[x/7*7 + 6-x%7]); //x/7 is the row at which the point is. Adding to the variable arr is the same as adding the same number to the index								     //this is because arr is the memory index of the first element of the array, and adding 1 to arr iterates                                                                      //the memory index by 1, pointing to the next index		                                                     //note that iterating the memory pointer by 1 moves you 1*(the size of the data type) in memory			}			else 			{				printf("\t%d",(arr)[x/7*7 + x%7]);			}		}		else		{			if ((x/7) % 2 == 1)			{				printf("\t%d\n",(arr)[x/7*7 + 6-x%7]);			}			else 			{				printf("\t%d\n",(arr)[x/7*7 + x%7]);			}		}	}

Hopefully that helps, and please never use modulo ternary operators in your code. They are very irritating to read. 

 

edit: I said modulo instead of ternary by accident. I don't really proof read.

Unless you're doing something like code golf, don't write code like that. Write nice code that is, at least, reasonably easy to read and understand lol

 

Golfing is fun. I should have prefaced my post with just because you can doesn't mean you always should.

 

Conditional expressions are useful. Just don't abuse them like I did.

 

I'd rather write printf("some message ... %s\n", str ? str : "<<null str>>"); than an if block though.

main(i){for(;i<101;i++)printf("Fizz\n\0Fizzz\bBuzz\n\0%d\n"+(!(i%5)^!!(i%3)*3)*6,i);}

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×