Computer Science Canada

Is this code suppose to work, or is it for learning?

Author:  php111 [ Wed Jan 07, 2009 1:49 pm ]
Post subject:  Is this code suppose to work, or is it for learning?

Is this code suppose to print something? I will post the code from the book, the code I wrote, and the errors.


Code from the book:


code:
/*

I hope this works.

*/

x = 100;

y = 200;

/* Now for something else. */




My code:



code:
/*

I hope this works.

*/

x = 100;

y = 200;

/* Now for something else. */




The errors:



Quote:
ERROR: variable 'x' not defined
ERROR: command 'x' not found
ERROR: syntax error before or at line 7 in file test2.c
==>: x = 100;
BUG: x = 100;<== ???
ERROR: cannot execute command 'test2.c'
H:/Documents and Settings/baseball>





Author:  DemonWasp [ Wed Jan 07, 2009 2:00 pm ]
Post subject:  RE:Is this code suppose to work, or is it for learning?

Read what the error message says. Think about what it says. You'll have to learn to interpret error messages on your own, and now is as good a time as any. I'll walk you through this one:

ERROR: variable 'x' not defined
This means that you haven't defined x. You need to say, explicitly, what x is, or the computer can't possibly know. In this case, it looks like you should probably have made a variable x as an integer. This is done as follows:

code:
int x;
OR
int x = 100;


ERROR: command 'x' not found
This is the compiler getting lost. It assumed x was a variable, but it wasn't, so now it's trying to figure out what to do. It's next plan is to see if x happened to be a command, like a call to a method or similar. It isn't, of course, which is why it says it couldn't find it.

ERROR: syntax error before or at line 7 in file test2.c
==>: x = 100;
BUG: x = 100;<== ???
ERROR: cannot execute command 'test2.c'
H:/Documents and Settings/baseball>

This is telling you exactly where the failure is: it's on line 7 of test2.c, which looks like "x = 100;". This isn't always exactly where the error is, it's just where the compiler runs into the actual problem. It's usually the right line, but there are some kinds of errors where it won't be (such as your missing-semicolon error above).

For your next error, please try to solve it yourself, based on the error message. You will learn a lot more if you think hard about it and puzzle it out carefully than if you just ask us for the answer.

Author:  michaelp [ Wed Jan 07, 2009 3:54 pm ]
Post subject:  RE:Is this code suppose to work, or is it for learning?

Are you still using Ch?

Author:  php111 [ Wed Jan 07, 2009 4:02 pm ]
Post subject:  Re: RE:Is this code suppose to work, or is it for learning?

michaelp @ Wed Jan 07, 2009 3:54 pm wrote:
Are you still using Ch?



I am writing the code in notepad. After that, I use Ch by doing ./my file.c

I would like to use something different then Ch.

Author:  DemonWasp [ Wed Jan 07, 2009 4:22 pm ]
Post subject:  RE:Is this code suppose to work, or is it for learning?

Switching to a standard Compiler
You will need to find a program called a compiler (specifically, you'll need a C compiler). GCC is a good one; if you're on Windows, you can install it by installing MinGW: http://www.mingw.org/ .

Using GCC
Once you have GCC (or any other compiler, really), you will use it something like this:

1. Edit your code file, or create a new one with whatever code you want to run. For this example, we'll assume the file is called "test.C" .

2. At the command-line, run:

code:

gcc test.C -o test.exe


This step compiles your code in test.C into an executable program called test.exe .

3. You can now run test.exe by the following, in the command-line:

code:

test.exe


Each time you change test.C, you will need to run step #2 again before running step #3 again.

Author:  php111 [ Wed Jan 07, 2009 4:59 pm ]
Post subject:  Re: Is this code suppose to work, or is it for learning?

I get 'gcc' is not recognized as internal or external command.

Author:  michaelp [ Wed Jan 07, 2009 5:20 pm ]
Post subject:  RE:Is this code suppose to work, or is it for learning?

You probably didn't install gcc correctly.
http://www.computerhope.com/issues/ch000549.htm
Go to your path thing, and find the directory where the gcc.exe is located. Put that in the path.

Author:  wtd [ Wed Jan 07, 2009 6:00 pm ]
Post subject:  RE:Is this code suppose to work, or is it for learning?

PATH tells your system where to look for programs to run. It's composed of file paths.

My Windows XP Pro x64 PATH:

Quote:
F:\Perl\site\bin;F:\Perl\bin;c:\ruby\bin;F:\WINDOWS\system32;F:\WINDOWS;F:\WINDOWS\System32\Wbem;F:\Program Files (x86)\QuickTime\QTSystem\;F:\Mingw\bin;F:\parrot-0.8.1\bin;F:\Program Files (x86)\Objective Caml\bin;F:\Perl\bin\

Author:  php111 [ Wed Jan 07, 2009 6:33 pm ]
Post subject:  Re: Is this code suppose to work, or is it for learning?

I just copied the gcc.exe file to the baseball folder. It works fine. It's less errors. It wasn't complicated. It worked. I just leave it in the baseball folder.


Now, I have a question about this command: By the way, thank you all for the replies. gcc test.c -o test.exe: Does that command compile my code? Will it give me errors if I type my code wrong? I am writing my code in notepad. It's less headaches then writing it in a command line. I just use command line to compile.

Author:  Vertico [ Wed Jan 07, 2009 6:39 pm ]
Post subject:  Re: Is this code suppose to work, or is it for learning?

php111 @ Wed Jan 07, 2009 7:33 pm wrote:

Now, I have a question about this command: By the way, thank you all for the replies. gcc test.c -o test.exe: Does that command compile my code? Will it give me errors if I type my code wrong? I am writing my code in notepad. It's less headaches then writing it in a command line. I just use command line to compile.


Why not test it? Place some code into a file with a missing semicolon or something and try to compile it. You may be pleasantly surprised.

Basically what that lines does is compiles the code, then saves it as a file in the same directory named test.exe or whatever you want.

Author:  php111 [ Wed Jan 07, 2009 6:55 pm ]
Post subject:  Re: Is this code suppose to work, or is it for learning?

Vertico @ Wed Jan 07, 2009 6:39 pm wrote:
php111 @ Wed Jan 07, 2009 7:33 pm wrote:

Now, I have a question about this command: By the way, thank you all for the replies. gcc test.c -o test.exe: Does that command compile my code? Will it give me errors if I type my code wrong? I am writing my code in notepad. It's less headaches then writing it in a command line. I just use command line to compile.


Why not test it? Place some code into a file with a missing semicolon or something and try to compile it. You may be pleasantly surprised.

Basically what that lines does is compiles the code, then saves it as a file in the same directory named test.exe or whatever you want.



Your amazing. LOL. I get syntax errors. LOL. Thank you.

Author:  michaelp [ Wed Jan 07, 2009 7:19 pm ]
Post subject:  Re: Is this code suppose to work, or is it for learning?

php111 @ Wed Jan 07, 2009 6:33 pm wrote:
I just copied the gcc.exe file to the baseball folder. It works fine. It's less errors. It wasn't complicated. It worked. I just leave it in the baseball folder.


Now, I have a question about this command: By the way, thank you all for the replies. gcc test.c -o test.exe: Does that command compile my code? Will it give me errors if I type my code wrong? I am writing my code in notepad. It's less headaches then writing it in a command line. I just use command line to compile.


That works, but if you ever make a new folder, you will have to copy it there too.

Author:  DemonWasp [ Thu Jan 08, 2009 9:46 am ]
Post subject:  RE:Is this code suppose to work, or is it for learning?

Modify your path, it's the correct solution. Plus, you won't have to deal with copying the files every time, as michaelp notes.

You should consider upgrading your text-editor from Notepad to something (pretty well anything) else. Notepad++ (google it) is a great editor, and will make writing code easier. You want something that will provide syntax highlighting, and probably bracket-matching; Notepad++ does both while Notepad does neither.

And above all, try things on your own. If you have a question, think to yourself: "Can I answer this on my own through a simple test?" If you can, do!

Author:  btiffin [ Thu Jan 08, 2009 10:55 am ]
Post subject:  RE:Is this code suppose to work, or is it for learning?

php111; Alright, graduating to gcc.

Now, back to my original suggestion of Ch; Leave Ch behind for now, forget I told you about it. Wink

Cheers

Author:  php111 [ Thu Jan 08, 2009 5:29 pm ]
Post subject:  Re: Is this code suppose to work, or is it for learning?

I have been trying to figure this out. I am looking at my code and the error. It's an error before "use". I see "first" before use. What does that have to do with anything? Could it be that I am trying to write C99 code? If so, I just skip the C99? It seems like something in my code is making the whole thing wrong. It's most likely C99.




My errors:


Quote:
H:\Documents and Settings\baseball>gcc test1.c -o test1.exe
test1.c: In function `main':
test1.c:15: error: `first' undeclared (first use in this function)
test1.c:15: error: (Each undeclared identifier is reported only once
test1.c:15: error: for each function it appears in.)
test1.c:15: error: syntax error before "use"

H:\Documents and Settings\baseball>





My code:



code:
int main(void) // C99 rules

{

// some statements

int doors;

doors = 5; // first use of doors

// more statements

int dogs;

dogs = 3; first use of dogs

// other statements

}


Author:  wtd [ Thu Jan 08, 2009 5:37 pm ]
Post subject:  RE:Is this code suppose to work, or is it for learning?

What's line 15?

Author:  Nick [ Thu Jan 08, 2009 5:45 pm ]
Post subject:  RE:Is this code suppose to work, or is it for learning?

it seems to me that, for some reason, your comments aren't, well, commenting

try to remove the comments then compile

Author:  php111 [ Thu Jan 08, 2009 5:46 pm ]
Post subject:  Re: RE:Is this code suppose to work, or is it for learning?

wtd @ Thu Jan 08, 2009 5:37 pm wrote:
What's line 15?


I don't know. Hold on, I'll post the code from the book that Dan recommended (C Primer Plus).



code:
int main()          // C99 rules

{

// some statements

    int doors;

    doors = 5;      // first use of doors

// more statements

    int dogs;

    dogs = 3;       // first use of dogs

    // other statements

}


Author:  Vertico [ Thu Jan 08, 2009 5:55 pm ]
Post subject:  Re: Is this code suppose to work, or is it for learning?

php111 @ Thu Jan 08, 2009 6:29 pm wrote:

code:
int main(void) // C99 rules

{

// some statements

int doors;

doors = 5; // first use of doors

// more statements

int dogs;

dogs = 3; first use of dogs

// other statements

}



c:
dogs = 3; first use of dogs

Right here you have a comment without it being set as one. You need // or /* */ around it else the compiler sees "first use of dogs" as a line of code, which of course does not work.
c:
dogs = 3//first use of dogs

Author:  OneOffDriveByPoster [ Thu Jan 08, 2009 7:30 pm ]
Post subject:  Re: Is this code suppose to work, or is it for learning?

The comments are inaccurate (even if they came from the book)--comments changed below.
c:
// function definition
int main(void) // function prototype for main() suitable for hosted environment:  no implicit int allowed in C99
{
// a declaratation
int doors;

// a statement
doors = 5; // first use of doors

// another declaration
int dogs;

// another statement
dogs = 3; // first use of dogs
}


Author:  wtd [ Thu Jan 08, 2009 7:39 pm ]
Post subject:  RE:Is this code suppose to work, or is it for learning?

Thanks for just giving php11 the answer.

Author:  php111 [ Thu Jan 08, 2009 7:43 pm ]
Post subject:  Re: Is this code suppose to work, or is it for learning?

I got it. It worked. Thank you. The // is what I needed.


: