# Main programming question/ a few misc. questions..



## Mac Osxtopus (May 31, 2002)

Ok, i'm starting to learn C (since im taking C++/Java next year in high school, im learning C first to get ahead), but the tutorial for it must be dated or something, because the "while" and "do-while" commands always have syntax errors, even when i copy and paste:
#include <stdio.h> .... well the stdio.h thing isnt showing due to the < 's

int main()
while (a < b)
{
	printf("%d\n", a);
	a = a + 1;
}
Whats wrong with this picture?
Ok, now to the misc. questions:

1: What the heck is cocoa? java for people who don't like coffee?


2: Carbon and cocoa? whats next, Sulfur and Soda?


3: How do i allow people to see my compiled executables? I sent a really basic 2 variable calculator to a friend of mine i made out of C. When he tried to run it on terminal he got the error message of permission denied. Any advice? I set the file for read/write priveleges for everyone.


----------



## whitesaint (May 31, 2002)

Well it looks like your problem is - what exactly are you using to compile this?


----------



## Mac Osxtopus (Jun 1, 2002)

i use terminal


----------



## Ghoser777 (Jun 1, 2002)

Uhmm... in your code, you never defined the type of your variables.  it should be something like this:


```
#include <stdio.h>

int main()
{
     int a = 0;
     int b = 5;
    while (a < b)
    {
        printf("%d\n", a);
        a = a + 1;
    }
}
```

If you compile that, you should get the numbers 0 through 4 printed out on separate lines.  You also needed curly braces to encapsulate your main method.

Cocoa is a set of APIs that apple created (well NeXT made most of them...) that allows applications to be made quickly and effictively.  You can use the programming language java, c, or obj-C to use the powerful Cocoa APIs.

Carbon refers to code that can run on both legacy (OS 9) and OS X.  Apps are said to be carbonized if they use the old Toolkit but make specific modifications to work natively under OS X.  I have no idea why they picked Cocoa for the name of their new API's; I prefer hot chocolate 

Hey, are you using Practicle C by O'Reilly?  I think they had a two variable calculator in there.  Anywho, if you have a mac and your friend has a pc, that won't work.  The C compiler your using will compile the code to specifically run on a mac.  This code will not run on an x86 machine.  They will need to compile it on their machine to get it to work.  If they have an OS X machine, and they're getting that error, have them run ls -al to check the permissions on the file, and then make sure your not sending them source file instead of the executable.

The nice thing about java is that you would be able to compile once, and then the JRE allows it to run on any platform with a JVM.

HTH,
F-bacher


----------



## Mac Osxtopus (Jun 1, 2002)

well how should i compile it as? mycalculator.exe or something? i have a mac on os x and so does my friend. I set the priveleges to read/write for everybody.


...(edit), oh, I don't use o'reillys stuff since when i look at public opinion of his stuff it says you have to know a lot ahead of time in order to understand anything. Right now im learning C at http://www.howstuffworks.com/c2.htm
It explains it pretty good, but for some things it only gives chunks.


----------



## Ghoser777 (Jun 1, 2002)

Really?  I didn't think they moved that quickly.  Although, I already new Java before I learned C, and they're very similiar.

To compile your C code, create a file like so:
name_of_my_source_file.c

All your source files (where you put your code) should have that general form.  To compile it to an executable, you do something like this:
gcc -g -Wall -ansi -pedantic -o name_of_executable name_of_my_source_file.c

(a lot of those flags are unnecessary, you probably could get away with just gcc -0 name_...) (cc and gcc point to the same thing, gcc2, so if someone tells you to use cc, it doesn't really matter)

Now you'll have a file called name_of_executable.  But, read and write permissions aren't the important thing (write is completely unnecessary).  You need execution privledges!  So:
chmod a+x name_of_executable

Send that to a friend and have them execute it on the commandline as so:
./name_of_executable

Does that help?
F-bacher


----------



## Mac Osxtopus (Jun 1, 2002)

yes, very, but my new command i was making for the while command that you tried to help me still doesnt work! i keep getting syntax errors, what the heck is a parse error?

see code:
#include -stdio.h-    ...yes the < 's are there but i used -'s to show that they are in this post.

int main()
{

        int a = 0;
        int b = 10;
        while (a < b);
        {
                printf("%d\n", a);
                a = a + 1;
        }
}
what did i do wrong?


----------



## Colin (Jun 1, 2002)

Two problems in your code:

1. You terminated your while with a semicolon before it hits the statements to be executed.

2. You need to return 0 (that's a number, not a letter) at the end of main to indicate success.  That's why it's "int main(void)", not "void main(void)".  main() must return an integer.

Other than that, it looks like it should work now.


----------



## Mac Osxtopus (Jun 1, 2002)

well i did what you said and now it looks like this:

#include <stdio.h>

int main()
{

        int a = 0;
        int b = 5; 
        while (a < b)
        {
        printf("%d\n", a);
        a = a + 1;
        return 0;
        }
}
but when i run it all i get is the number 0. It was supposed to make the variable a to go up by 1 until it reached the value of b, what happened?


----------



## NuCleuZ (Jun 1, 2002)

it's main that needs to return 0;
#include stdio.h

int main() 
{ 

int a = 0; 
int b = 5; 
while (a < b) 
{ 
printf("%d\n", a); 
a = a + 1; 
}
return 0; // end of main() return 0 to indicate success.
}


----------



## Mac Osxtopus (Jun 1, 2002)

#include stdio.h

int main()
{
        int a = 0;
        int b = 5;
        while (a < b)
        {
                printf("%d\n", a);
                a = a + 1;
                return 0; // end of main()
        }
}

I tried this, it doesnt do anything at all except just display 0 like before! dangit! Is my terminal word processor(pico) bugged or something?


----------



## Ghoser777 (Jun 2, 2002)

Okay, here's what you had before with my commentary:


```
#include stdio.h 
//html doesn't like alligators, so I'll leave them
//out for now

//this is our main method
int main()
{
   //initialize an integer to the value 0
   int a = 0;
   //initialize an integer to the value 5
   int b = 5;
   //while the first integer is less than the 
   //second integer...
   while (a < b)
   {
      //...print a...
      printf("%d\n", a);
      //..increment a...
      a = a + 1;
      //return 0... which means your main method
      //ends
      return 0;
   }
}
```

So, the first time through your while loop, it hits the return statement and exits out of your main method, essentially terminating your program.  With that return statement where it is, your program never goes back to the top of the while loop, and that explains why only 0 gets printed out.  Simply move the return 0; line outside of the while loop (just below the first closed bracket instead of right above), and it works fine.

F-bacher


----------



## Mac Osxtopus (Jun 2, 2002)

yay! it works! thanks! I'm learning C/C++ from multiple sources since some seem a bit...well...dated.

im currently was using this resource and was wondering if it's reliable?
http://www.intap.net/~drw/cpp/cpp04_02.html


----------



## seb2 (Jun 2, 2002)

first of all, please don't misunderstand my post, i seriously don't want to be picking on anybody.

i think this real life example makes it clear how important indention is. suppose your source code files grow even bigger -- there is absolutely no chance you can see what belongs where, what loop goes from where to where and what closing bracket belongs to what opening bracket.

as much as i love pico to edit smaller text files or configuration files, i think it's the wrong choice as text editor for programming.

if you really don't want to use project builder -- which automatically does indention for you -- a few years ago i used emacs on solaris and iirc it also does indention for you. you might have to configure it to do so, but many people swear emacs was the best editor, even for source code.

personally, i like project builder because it also helps you in lots of other ways, like a small popup at the top with the names of all methods in your code that allows you to directly jump to a method implementation or a nice feature that lets you directly jump to the definition of a variable (command-double click on the name of a variable)

you might also have a look at some of the free source code editors written in java that also offer some nice features.

well, i guess the essence of what i want to say is: as much as it might get on your nerves at the beginning to have to take care of indention and all that "formal stuff", the much of a help it will be later once you made it a habit. -- believe me.


----------



## Mac Osxtopus (Jun 2, 2002)

i think i'll use something other than project builder for c/c++ at the moment since i have to connect files to projects in order to actually check the syntax, since terminal notices syntax errors.


----------



## Ghoser777 (Jun 2, 2002)

Yeah, indentation is good 

Do you know that project builder can check your syntax for you?  It's just an apple-k away!  Really, Project Builder is going to be a lot easier to use in the long run, but it's up to you.  I did a lot of my early code (some still) using pico, but it can become a pain.

F-bacher


----------



## nkuvu (Jun 2, 2002)

If you don't want to use project builder, but want something more user-friendly than pico, try BBEdit -- the lite version is free and does everything you might want in a basic text editor.

I think it's important to be able to compile and run programs from the command line, so for new users I wouldn't choose an IDE (integrated development environment) like project builder or (what we used in college) MS Visual Studio.

Another point:  you don't have to chmod the output file to be executable.  It already is after compilation.

I also wanted to point out the reason for the return statement -- the main() function is just a collection of commands that returns a value.  Any function in C returns at one point or another, and may or may not return something back.  the int main() declaration means that it is a function that returns an integer and requires nothing sent to it (parameters).  So as soon as you say return it jumps out of that particular function.  Since it's the main function, it jumps out of your program.  When you had it in your while loop you jumped out of your program before it could run more than once, as Ghoser777 already pointed out.  Just a little more explanation...


----------



## Mac Osxtopus (Jun 3, 2002)

well dont mind project builder except for two major things: I can't compile my C++ files to see if they work and i cant even see if the syntax right cuz all the syntax checker do is save the file, even when i put the word "chicken" to replace main() and i put { project builder sucks } for the command.


----------



## kainjow (Jun 4, 2002)

I knew that Project Builder could do auto indent, but I never enabled it. I just did and it's wonderful. Makes my code sooo much easier to read. Thanks seb2!!!


----------



## Mac Osxtopus (Jun 4, 2002)

YES! I FOUND IT! Project builder needs some more specific things, in order to experiment around with C++, do new project: make a c++ tool


----------

