Todd said:
OK. That code was basically the same as the author gave in the book with a
few things I threw in (mostly in the print statements). Why does the code
compile and the binary function if the code contains errors? Maybe I need
a different book. Any suggetions on a good 'Intro to C' book? I'm not too
interested in C++ at this point. I have some knowledge of VB and have done
a small bit of work in Python and Perl, so it need not be at the *most*
basic level.
I will try your suggestions and see what happens.
It seems you are on the right tract. Remember, a compiler, does not
define the C Language. Just using your compiler to determine the
validity of code will get you in trouble. The International Standard
ISO/IEC 9899 defines the C Language. So, resources like a copy of
the Standard or books that explain the standard is the right approach.
Personallly, my favorite resources are the Standard itself, 'The C
Progamming Language' (Second Edition), "The Standard C Library', and
finally the FAQ which can be found on the web at:
http://www.eskimo.com/~scs/C-faq/top.html
OK, Getting back to the code you supplied: Here it is again.
/*************** test.c *************/
#include <stdio.h>
main()
{
int intDie1 = 0;
int intDie2 = 0;
int intResult = 0;
srand(time());
intDie1 = (rand() % 6) +1;
intDie2 = (rand() % 6) +1;
intResult = (intDie1 + intDie2);
if (intResult == 7 || intResult == 11){
printf("\nYOU WIN!!\n");
printf("First roll was %d and second was %d\n", intDie1, intDie2);
}
else{
printf("\nYOU LOSE!!\n");
printf("First roll was %d and second was %d\n", intDie1, intDie2);
}
}
You said that when you compiled it with GCC 3.2.3 that it compiled and
the binary worked fine. When you compiled it did the compiler give
you any warning diagnostics? If gcc did not give you any warnings
perhaps you should increase the warning level. I compiled the above
code with gcc 3.2 and with a high level of warning. The command was:
gcc test.c -pedantic -Wall
The code compiled and a binary was built. However, I got five warning
messages. They were:
Line 4: warning, return type defaults to 'int'
In function 'main':
Line 9: warning, implicit declaration of function 'srand'
Line 9: warning, implicit declaration of function 'time'
Line 11: warning, implicit declaration of function 'rand'
Line 23: warning, control reaches end of non-void function.
The line 4 warning is the result of not explicitly declarating
function main returning an integer: int main()
The Line 9 warnings(2) is the result of not including the headers,
stdlib.h for function srand and time.h for function time. These
headers have the declarations for these functions.
The line 11 warning is the same as line 9. You did not include the
header stdlib.h which declares function rand.
The Line 23 warning is the result of function main not returning
an int. So you should return an integer, like 'return 0' at the
end of the function.
So you see, the resource that supplied you with this code has
serious deficiencies. You should seek additional resources like I
mentioned above.