Newbie questions - Segmentation fault? - programming on a Mac
Hi everyone!
This should be simple for mostly everyone here, but I'm just starting out my learning curve of C programming.
I'm using an editor called "GNU nano 2.0.6" that I open through a Terminal shell in MacOS X (Lion). I was planning on using XCode from Apple, but it's a bit overwhelming. I mainly want to concentrate on the programming part right now (leave the bells and whistles for later).
I've been compiling the following simple code with g++ and gcc, and I get two different types of errors.
Code:
#include <stdio.h>
main() {
// Program that prints a random number between 1 and 10 on the screen
int iRandomNum = 0;
srand(time());
iRandomNum = (rand() % 10) + 1;
printf("This is a random number between 1 and 10: %d", iRandomNum);
} // end main()
When I try to compile this via g++, I get the following messages:
Quote:
8randomnumbers.c:9: error: ‘time’ was not declared in this scope
8randomnumbers.c:9: error: ‘srand’ was not declared in this scope
8randomnumbers.c:11: error: ‘rand’ was not declared in this scope
When I use gcc, apparently it compiles, but then when I run the program I get a "Segmentation fault" message.
From what I read in this forum, the "segmentation fault" has something to do with memory corruption, but I'm still way in the beginning to realize how to solve that...
Could someone please just point me to the right direction on what I need to be doing?
This code was mostly copied and pasted from the book I'm reading to learn (C Programming for the absolute beginner, by Michael Vine, page 76).
Should I maybe get a new compiller? Get a new book?
Any suggestions? I feel a bit alone using Mac to program (everybody keeps telling me Windows and Linux is better for that, but you use what you have).
Thanks! I'm looking forward to be an active member of this forum for some time to come.
Toscano