Results 1 to 5 of 5

Thread: Error returned when trying to run C program

  1. #1
    Join Date
    Mar 2006
    Beans
    17

    Error returned when trying to run C program

    I've been getting an error message after trying to run a program I had just compiled.

    I used this code saved as "hello.c":



    Code:
    #include <stdio.h> int main(void) 
    { 
    
         printf("A .c is used to end a C program filename.\n");
    
         return 0;
     
    }

    I used the command line and used:
    gcc hello.c
    a.out

    This should have compiled the program "hello.c" and then run the program "a.out". It didn't. Instead it gave the error "a.out: command not found". Any ideas?


    Thanks.

  2. #2
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Error returned when trying to run C program

    Run it using
    Code:
    ./a.out     # note the dot and forward-slash before the a.out

  3. #3
    Join Date
    Mar 2006
    Beans
    17

    Re: Error returned when trying to run C program

    Quote Originally Posted by dwhitney67 View Post
    Run it using
    Code:
    ./a.out     # note the dot and forward-slash before the a.out
    That did work, but...what did I just do?

  4. #4
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Error returned when trying to run C program

    Quote Originally Posted by Talruk6 View Post
    That did work, but...what did I just do?
    You executed your program. The dot & slash indicate to the bash shell the relative path to the program you wanted to run. You could alternative specify the absolute path (e.g. /home/user/myFolder/a.out).

    There's always the possibility that more than one program, bearing the same name, appear on your system. Hence as a security measure, it is always wise NOT to include the current directory (represented by the dot) in your PATH environment variable. If that dot were present in your PATH, then you would have been able to execute the 'a.out' as you originally attempted.

    Some people don't give a rat about security, and if you are one of them, then you can set your PATH environment variable in your ~/.bashrc file with:
    Code:
    export PATH=.:$PATH      # this prepends the dot to your existing PATH settings

  5. #5
    Join Date
    Mar 2006
    Beans
    17

    Re: Error returned when trying to run C program

    Thanks!

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
  •