1

I'm writing my first-ever program and I've hit an immediate roadblock. I'm just trying to get some simple lines of text on the screen, but when I compile and run the program I get:

/Users/myname/Desktop/program -bash: /Users/myname/Desktop/program: Permission denied name-mbp:~ myname$

I'm using a Mac, and don't have much familiarity with the command line. Is there something I'm doing wrong? Here's the program:

/* Prints a message on the screen */
#include <stdio.h>
main ()
{

    printf("Just one small step for coders. One giant leap for") ;
    printf(" programmers!\n") ;
    return 0;

}

Any thoughts or feedback would be truly appreciated. Thanks in advance.

ilent2
  • 5,171
  • 3
  • 21
  • 30
  • 4
    You need to compile the program and execute the generated binary. Did you compile the program? – devnull May 22 '14 at 02:37
  • Thanks you for your responses - yes, I did compile and run the program. I'm using the Code:Blocks IDE and have tried both compiling and running separately, as well as the combined compile/run feature. – user3648858 May 22 '14 at 02:40
  • @WilliamAndrewMontgomery - I'm sorry - I'm not familiar with chmod +x a.out - so I can't confirm running that. – user3648858 May 22 '14 at 02:45
  • 3
    if you compiled it and ran it from the shell, what commands did you use and what was the exact output of the commands? As you have presented it, there is not enough information to even guess what you might have gotten wrong. – msw May 22 '14 at 02:45
  • Thanks for your response - I didn't use any commands from the shell, really. I simply compiled and ran the program from the compiler and then the terminal screen opened and provided the above permission denied response. The actual program was copied line by line from the "Absolute Beginner's Guide to C." – user3648858 May 23 '14 at 02:11

1 Answers1

4

This is an example of me making mistakes in trying to get your simple program to run. Shell prompts begin with $ and my commentary begins with # and reads like commentary:

$ cat step.c
/* Prints a message on the screen */
#include <stdio.h>
main ()
{
    printf("Just one small step for coders. One giant leap for") ;
    printf(" programmers!\n") ;
    return 0;
}
$ gcc -Wall step.c
step.c:3:1: warning: return type defaults to ‘int’ [-Wreturn-type]
 main ()
 ^
# running gcc with -Wall turns on all warnings
# there is a warning on line 3, but it is tolerable
$ ls step
ls: cannot access step: No such file or directory
# gcc does not make a binary named after the source file
$ ls -l a.out
-rwxrwxr-x 1 msw msw 8562 May 22 03:50 a.out
# it does make a binary file called a.out which is executable (x)
$ a.out
a.out: command not found
# oops, the current directory is not in my path so the shell doesn't
# look here for executables
$ ./a.out
Just one small step for coders. One giant leap for programmers!
# but if I tell it to look in my current directory, it does
$ gcc -o step step.c
#what does gcc do if I tell it the binary should be named "step"?
$ ls -l step
-rwxrwxr-x 1 msw msw 8562 May 22 03:51 step
# it makes a bnary called "step"
$ ./step
Just one small step for coders. One giant leap for programmers!
# which I can run
msw
  • 42,753
  • 9
  • 87
  • 112