Thread: Run a c program

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Run a c program


    Alright first of all, i just spend 30 min making a threat, where i have put screenshots in, so it might would help me to understand, and explain what my problem was. But the site dosent allow me to????? I really dont understand why, especially when some people are newbies, this would be the perfect place for that.

    Hello everybody.

    I really hope someone can help me.

    I just startet on a course, where we have to learn C programming. I downloaded Xocde and followed this installation the installation proces.

    I also installede the GRR commandoline toll, and it looks like it is working

    I tried to make a hello world, from the page "thenewboston.org", which is running fine from within the Xcode program, but i want to run the program from my terminal on my mac, but i dont know what to write.
    my file is called "main.c". Then i go to my terminal and find the folder where my file is, then i write:

    g++ main.c (and then it just goes to the next line. Then i write): /.main.c and then i get an answer where it says: "Permission denied"

    But what am i doing wrong? I am out of options now, so i hope someone can help me?

    Best Regards
    Mads
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    The compiler generates an executable file with a different name than the file with the source code.
    Probably your compiler generated an executable file named "a.out". Try

    Code:
    $ g++ main.c
    $ ./a.out
    <OUTPUT OF YOUR PROGRAM>
    $
    You can make the compiler output a specific name instead of "a.out". For instance "executable"

    Code:
    $ g++ main.c -o executable
    $ ./executable
    <OUTPUT OF YOUR PROGRAM>
    $
    Also you really, really should enable warnings and mind them, eg

    Code:
    $ g++ -Wall main.c -o executable
    <SOME WARNINGS>
    <EDIT CODE TO ADDRESS THE WARNINGS>
    $ g++ -Wall main.c -o executable
    <NO WARNINGS>
    $ ./executable
    <OUTPUT OF YOUR PROGRAM>
    $
    Last edited by bdb; November 10th, 2012 at 08:15 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    4
    Rep Power
    0
    perfect, thank you so much for that :-) it works now. The only thing that is annoying is, that when i get printed "Hello World", it is as the same line that my name of the comuter is. So I write:

    Mads-Macbook-pro:test Mads$ get++ main.c -o main
    Mads-Macbook-pro:test Mads$./main

    and the output is:

    Hello WorldMads-Macbook-pro:test Mads$

    where i would like it said

    Mads-Macbook-pro:test Mads$
    Hello World

    But i cant do anything about that?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Originally Posted by McDuck4
    ... it is as the same line ...
    Print a full line within your program

    Code:
    puts("Hello World"); // puts automatically adds a newline
    or
    Code:
    printf("Hello World\n"); // add newline manually (the \n)

IMN logo majestic logo threadwatch logo seochat tools logo