Results 1 to 9 of 9

Thread: C Hello World errors

  1. #1
    Join Date
    Nov 2008
    Beans
    24
    Distro
    Ubuntu 12.04 Precise Pangolin

    C Hello World errors

    I'm trying to learn C,but,after saving "
    Code:
    hello.c
    "


    Code:
    #include<stdio.h>
    
    	int main()
    
    	{
    
    		printf(“Hello World\n”);
    
    		return 0;
    
    	}

    and typing cc,or gcc hello.c,I keep getting this error(since I copied and pasted it from a programming site,it is correct.):

    Code:
    gcc hello.c
    hello.c: In function ‘main’:
    hello.c:7:3: error: stray ‘\342’ in program
    hello.c:7:3: error: stray ‘\200’ in program
    hello.c:7:3: error: stray ‘\234’ in program
    hello.c:7:13: error: ‘Hello’ undeclared (first use in this function)
    hello.c:7:13: note: each undeclared identifier is reported only once for each function it appears in
    hello.c:7:19: error: expected ‘)’ before ‘World’
    hello.c:7:19: error: stray ‘\’ in program
    hello.c:7:19: error: stray ‘\342’ in program
    hello.c:7:19: error: stray ‘\200’ in program
    hello.c:7:19: error: stray ‘\235’ in program
    What's the problem?

    I know it has to be compiled and saved as an
    a.out
    file,then executed,right?

    Thanks
    Last edited by itpro007ca; April 2nd, 2014 at 01:03 AM.

  2. #2
    Join Date
    Nov 2008
    Beans
    24
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: C Hello World errors

    Changing the line to this got rid of the errors:

    "Hello, world!\n"
    So,what was the error and why?

    Now,that I was able to :
    gcc hello.c
    and create the
    a.out
    file,how do I get it to run?

    Is it
    chmod ?+x filename
    ?
    Last edited by itpro007ca; April 2nd, 2014 at 01:04 AM. Reason: word missing

  3. #3
    Join Date
    Nov 2008
    Beans
    24
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: C Hello World errors

    Never mind!

    I got it,thanks to a web search for "how to create executable of a C program in Ubuntu."

    ./ a.out
    I still need help to understand the error in syntax,though.

  4. #4
    Join Date
    Apr 2012
    Beans
    5,868

    Re: C Hello World errors

    Your original code had some non-ASCII characters (in particular the funky curved double quotes) - maybe because of the editor you used to create it (or was it copy/pasted from the web?)

    The a.out file should already be executable, so you should just need to do

    Code:
    ./a.out

  5. #5
    Join Date
    Nov 2008
    Beans
    24
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: C Hello World errors

    I copied and pasted from the web to nano.

    Do the comma and exclamation mark matter?

  6. #6
    Join Date
    Jan 2006
    Location
    Not heaven... Iowa
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: C Hello World errors

    Quote Originally Posted by itpro007ca View Post
    Do the comma and exclamation mark matter?
    Shouldn't...
    but it won't take long to edit the file and try again...
    Linux User #415691 Ubuntu User #8629
    Iowa Team (LoCo): [Wiki] [Launchpad]
    IRC channel: #ubuntu-us-ia on irc.freenode.net

  7. #7
    Join Date
    Nov 2008
    Beans
    24
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: C Hello World errors

    Thanks!

    I got it now.I also downloaded
    g++
    (after trying to run it and Ubuntu suggested it!) and was able to save the c++ version as
    hello1.c
    ,then after it overwrote the
    a.out
    file,I clued in that I could rename them hello and hello1 and change the text to differentiate between them.

    Now:

    I need a C/C++ IDE to get started with.I'm intending to learn C,then C++,then Java,then maybe Pascal(I read a book about that back in the 70s;can't get it out of my mind! lol),then, Perl,Python,Php and Ruby.

    I'm thinking of Netbeans,but,I know there's Eclipse,Code Blocks and others.

    Recommendations?

  8. #8
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,439
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: C Hello World errors

    you can name your binary file that is the result of compilation, add -o programname.
    Code:
    gcc hello.c -o helloc
    g++ hello.cpp -o hellocpp
    ./helloc
    ./hellocpp
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  9. #9
    Join Date
    Sep 2012
    Location
    UK
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: C Hello World errors

    To clarify what's going on here the reason you need the

    Code:
    ./
    Is that the system searches your path for the program and the current directory is not in your path.

    There are more details here on AskUbuntu

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •