Joined: 3/9/2004
Posts: 4588
Location: In his lab studying psychology to find new ways to torture TASers and forumers
Bleh, evil magic numbers, don't bother defining len, and use sizeof(str) instead.
Warning: Opinions expressed by Nach or others in this post do not necessarily reflect the views, opinions, or position of Nach himself on the matter(s) being discussed therein.
Bleh, evil magic numbers, don't bother defining len, and use sizeof(str) instead.
I did that because this is C, not C++ -- "const int x = <expression>;" is not a constant in C as it is in C++, and I opted to do this instead of using #define. Also, I'm not fond of (sizeof(x)-1) -- the nul terminator there is a trap.
Joined: 3/9/2004
Posts: 4588
Location: In his lab studying psychology to find new ways to torture TASers and forumers
Bisqwit wrote:
Nach wrote:
Bleh, evil magic numbers, don't bother defining len, and use sizeof(str) instead.
I did that because this is C, not C++ -- "const int x = <expression>;" is not a constant in C as it is in C++, and I opted to do this instead of using #define. Also, I'm not fond of (sizeof(x)-1) -- the nul terminator there is a trap.
If you must declare len, at the very least use sizeof(str), as opposed to magic numbers. What you did is pure evil, and should be avoided. You should make your code least prone to errors when something changes. If he decided to change the text to "Hi, my name is Sally!", he'd have to completely redo the len with your definition.
Warning: Opinions expressed by Nach or others in this post do not necessarily reflect the views, opinions, or position of Nach himself on the matter(s) being discussed therein.
Consider what happens to the loop terminating condition "p < LTH" when you execute "p = -n;". When do you suppose the loop ends?
The simplest solution for making this _work_ involves the modulo operator, but I'm providing thought fodder here, not the solution handed on a silver platter…
I'll take a look at this; thanks. I don't know if I can use your example, because eyeing over it it seems a bit difficult for my level. Maybe it isn't, I'll see.
Well, looks like I've gotten into a problem too difficult for me. I should start working on pointers instead, and I can pick this up later (by the end of summer, perhaps?). Also, I must stop posting so much in this thread.
By the way, Warp: is
Of course it is, you need two semicolons inside the parenthesis, Warp is simply declaring an initial value using the loop initialization argument, leaving the test empty so it loops until break, and then leaving the update argument empty as well.
All of the following are legal C (assuming that the variables are defined):
for (;;) {
for (int i=0;;) {
for (;var!=2.3;) {
for (;;i++) {
Not to mention, an empty statement is completely legal which means that the following is valid, but useless code:
void main() {
;;;;;;;;;;;;;;;;return;
}
Build a man a fire, warm him for a day,
Set a man on fire, warm him for the rest of his life.
Well, looks like I've gotten into a problem too difficult for me. I should start working on pointers instead, and I can pick this up later (by the end of summer, perhaps?). Also, I must stop posting so much in this thread.
By the way, Warp: is
for(int i = 0;;)
even legal C?
It is in c99, but not in earlier standards.
I think this is what Warp was alluding to...
Warp wrote:
(although making it C89-compatible is completely trivial).
Thoughtfodder.
This example code is designed to maximize the density of pointer use in a small (one page) example program while still being meaningful and not just complex for the sake of being complex. (That is, I could have also made the payload a pointer, but decided not to.)
This example code is designed to maximize the density of pointer use
I think it's also a good example of how obfuscated and hard to follow C quickly becomes with complex data containers. Not to talk about how easily it becomes unsafe (with unexpected memory leaks, accessing freed memory or out-of-bounds accesses). C doesn't offer many tools for encapsulation, modularity or safety.
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Find the sum of all the even-valued terms in the sequence which do not exceed four million.
#include <stdio.h>
int main()
{
int add = 0;
int a, b = 1;
int c;
for(; b < 4000000; )
{
c = b;
b += a;
a = c;
if((b % 2) == 0)
add += b;
}
printf("%i", add);
return 0;
}
this sets b to 1 and leaves a uninitialized. You want:
int a=1, b=1
hint: in such cases, it's often useful to use a debugger that allows you to view variable contents, or to sprinkle some printf's inside your program so you can see how the loop progresses.
I haven't looked at the code with much thought, but at least you are using the variable 'a' uninitialized the first time the loop is executed. The behavior is thus undefined.
Joined: 3/9/2004
Posts: 4588
Location: In his lab studying psychology to find new ways to torture TASers and forumers
OmnipotentEntity wrote:
void main() {
;;;;;;;;;;;;;;;;return;
}
That is not valid C89/C99/C++.
main must return an int.
Warning: Opinions expressed by Nach or others in this post do not necessarily reflect the views, opinions, or position of Nach himself on the matter(s) being discussed therein.
That is not valid C89/C99/C++.
main must return an int.
I'd've sworn otherwise, I rather habitually void main for trivial programs.
edit: I suppose it's probably not standards-compliant though.
Current standards for both C and C++ both explicitly state that main() must be int, although most compilers will only warn if main() is some other type. The reasoning is that the caller expects an int to be returned that it can interpret as an error code.
EDIT: Here's something to ponder: http://homepages.tesco.net/J.deBoynePollard/FGA/legality-of-void-main.html
I meant that the program output looks complicated, I tried to make the code itself as simple as possible.
The algorithm is really simple as well: (Wolfram MathWorld)
A plot of the cells on a grid satisfying bitwise XOR(a,b)<n for consecutive values of n=1, 2, ....
While those are good tips, I just meant be careful as you go on that clean, elegant, readable code are much more important that trying to do strange one liners.
I'm not accusing you of doing any of those above, just a free floating tip.