Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Am I doing my C codes right? They are failing to compile
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
php111




PostPosted: Sun Oct 05, 2008 2:33 pm   Post subject: Am I doing my C codes right? They are failing to compile

I am not sure how to write the code tags here. I am going to try to write them with tags. If I am doing them wrong, will someone please edit my post and fix it? Thank you.



I was trying to compile code and it was failing to compile.

Here is the code from the C Primer Plus 5th Edition book.


code:
#include <stdio.h>

int main(void)                /* a simple program             */

{

    int num;                  /* define a variable called num */

    num = 1;                  /* assign a value to num        */



    printf("I am a simple "); /* use the printf() function    */

    printf("computer.\n");

    printf("My favorite number is %d because it is first.\n",num);



    return 0;

}




Here is my code:



code:
#include <stdio.h>
int main(void) /* a simple program */
{
        int num; /* define a variable called num */
                num=1; /* assign a value to num */
                printf("I am a simple "); /* use the printf function */
                printf("computer. \n");
                printf("My favorite number is %d because it is first. \n",num);

                return0;
}




Here is the error I get.



1>------ Build started: Project: C Programming.c, Configuration: Debug Win32 ------
1>Compiling...
1>stdafx.cpp
1>Compiling...
1>C Programming.c.cpp
1>c:\documents and settings\baseball\my documents\visual studio 2008\projects\c programming.c\c programming.c.cpp(14) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
1>Build log was saved at "file://c:\Documents and Settings\baseball\My Documents\Visual Studio 2008\Projects\C Programming.c\Debug\BuildLog.htm"
1>C Programming.c - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Sponsor
Sponsor
Sponsor
sponsor
php111




PostPosted: Sun Oct 05, 2008 2:33 pm   Post subject: Re: Am I doing my C codes right? They are failing to compile

Wow. I seen I done the code tags right. Lol.
Clayton




PostPosted: Sun Oct 05, 2008 3:42 pm   Post subject: RE:Am I doing my C codes right? They are failing to compile

code:
return0;


Should be:

code:
return 0;
Note the space.
php111




PostPosted: Sun Oct 05, 2008 3:59 pm   Post subject: Re: Am I doing my C codes right? They are failing to compile

Clayton,

I did put a space this time and it's still failing.





code:
#include <stdio.h>
int main(void) /* a simple program */
{
        int num; /* define a variable called num */
                num=1; /* assign a value to num */
                printf("I am a simple "); /* use the printf function */
                printf("computer. \n");
                printf("My favorite number is %d because it is first. \n",num);
               

                return 0;
}





1>------ Build started: Project: C Programming.c, Configuration: Debug Win32 ------
1>Compiling...
1>C Programming.c.cpp
1>c:\documents and settings\baseball\my documents\visual studio 2008\projects\c programming.c\c programming.c.cpp(17) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
1>Build log was saved at "file://c:\Documents and Settings\baseball\My Documents\Visual Studio 2008\Projects\C Programming.c\Debug\BuildLog.htm"
1>C Programming.c - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
CodeMonkey2000




PostPosted: Sun Oct 05, 2008 4:19 pm   Post subject: RE:Am I doing my C codes right? They are failing to compile

I don't think you installed your compiler properly. Either that or try replacing
code:
#include <stdio.h>
with
code:
#include "stdio.h"
php111




PostPosted: Sun Oct 05, 2008 4:48 pm   Post subject: Re: RE:Am I doing my C codes right? They are failing to compile

CodeMonkey2000 @ Sun Oct 05, 2008 4:19 pm wrote:
I don't think you installed your compiler properly. Either that or try replacing
code:
#include <stdio.h>
with
code:
#include "stdio.h"


Tried it and failed.

To make sure I had downloaded the right one.

http://www.microsoft.com/express/product/default.aspx

I got the one in the Orange color.
michaelp




PostPosted: Sun Oct 05, 2008 5:57 pm   Post subject: RE:Am I doing my C codes right? They are failing to compile

Did you try building a Windowed app instead of a Console(Command Line) app?
md




PostPosted: Sun Oct 05, 2008 6:12 pm   Post subject: RE:Am I doing my C codes right? They are failing to compile

Do not use Microsoft Visual Studio. It expects you to use precompiled headers (most useless thing ever in my experience), when you don't even know what a header is.

Use a text editor and a command-line compiler, you will learn things a lot quicker and easier.
Sponsor
Sponsor
Sponsor
sponsor
php111




PostPosted: Sun Oct 05, 2008 6:28 pm   Post subject: Re: Am I doing my C codes right? They are failing to compile

Alright, everyone is going with command line. I got Ch again.

The code up in my original, how do I write it and compile it using Ch?
btiffin




PostPosted: Sun Oct 05, 2008 7:44 pm   Post subject: RE:Am I doing my C codes right? They are failing to compile

Type it in to a text file

From Ch prompt, this is how I plowed my way through ... the beauty of an interactive system ... do something stupid, it'll let you fix it. Wink

code:

lang/ch6/src> ./sample.c
lang/ch6/src/sample.c: Permission denied
lang/ch6/src> sample.c
ERROR: variable 'sample.c' not defined
ERROR: command 'sample.c' not found
lang/ch6/src> chmod +x sample.c
lang/ch6/src> which chmod
/bin/chmod
lang/ch6/src> ./sample.c
I am a simple computer.
My favorite number is 1 because it is first.
lang/ch6/src>


So basically you execute the code by naming the source in Ch shell. I used ./ (dot slash being a POSIX thingy for; execute the program from this directory and don't bother looking through the PATH) and chmod +x to get permissions. Early Unix and C coders didn't use many vowels. chmod reads as Change Permission and or Attribute Modes. chmod is NOT a normal Windows command, but Ch is a fairly complete shell space. chmod +x tells the machine to allow execute permissions under a POSIX system. Get used to POSIX. POSIX good. Wink

And when I say POSIX I mean Unix and GNU/Linux and the Ch shell, in spirit of standard if not technically "compliant". And realize ... Ch interprets the code, not really learning some of the ins and outs of compiling code.

Cheers
wtd




PostPosted: Mon Oct 06, 2008 12:52 am   Post subject: RE:Am I doing my C codes right? They are failing to compile

Don't spend too much time on Ch, Learning C is worthless if you don't learn to use standard tools. GCC, by way of being both very good and very free is exceedingly popular.

It also scales well. Compiling a very simply program with gcc is exceedingly simple.

code:
$ cat simple.c
#include <stdio.h>

int main()
{
    printf("Listen to the duck. The duck is good.\n");

    return 0;
}

$ gcc -o simple simple.c
$ ./simple
Listen to the duck.  The duck is good.
$
php111




PostPosted: Mon Oct 06, 2008 4:05 am   Post subject: Re: RE:Am I doing my C codes right? They are failing to compile

What? I am sorry but I do not understand. I do not know how to fix it and even if it was the right way, I still don't know how to write it in a Ch editor.













btiffin @ Sun Oct 05, 2008 7:44 pm wrote:
Type it in to a text file

From Ch prompt, this is how I plowed my way through ... the beauty of an interactive system ... do something stupid, it'll let you fix it. Wink

code:

lang/ch6/src> ./sample.c
lang/ch6/src/sample.c: Permission denied
lang/ch6/src> sample.c
ERROR: variable 'sample.c' not defined
ERROR: command 'sample.c' not found
lang/ch6/src> chmod +x sample.c
lang/ch6/src> which chmod
/bin/chmod
lang/ch6/src> ./sample.c
I am a simple computer.
My favorite number is 1 because it is first.
lang/ch6/src>


So basically you execute the code by naming the source in Ch shell. I used ./ (dot slash being a POSIX thingy for; execute the program from this directory and don't bother looking through the PATH) and chmod +x to get permissions. Early Unix and C coders didn't use many vowels. chmod reads as Change Permission and or Attribute Modes. chmod is NOT a normal Windows command, but Ch is a fairly complete shell space. chmod +x tells the machine to allow execute permissions under a POSIX system. Get used to POSIX. POSIX good. Wink

And when I say POSIX I mean Unix and GNU/Linux and the Ch shell, in spirit of standard if not technically "compliant". And realize ... Ch interprets the code, not really learning some of the ins and outs of compiling code.

Cheers
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: