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

    Join Date
    Feb 2015
    Posts
    4
    Rep Power
    0

    Beginner C - Running C in Terminal


    Hello I am attempting to write and run my first C program. Below is what my code looks like. I am having trouble running the program in terminal.

    Code:
    main()
    
    (
        printf("hello, world\n")
    )
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,325
    Rep Power
    2226
    Insufficient information. For example, what operating system are you using? By your terminology, I would assume Linux. And just what exactly are you doing when you try to run the program? You are forcing us to make assumptions and wild guesses.

    By "running the program in terminal", this is what I'm assuming:
    1. What you display here is not the entire program. I assume that you did #include the header file for printf, stdio.h .
    2. You have opened the terminal (AKA "DOS window" or "Command Prompt" in Windows) and have navigated to the directory where the executable file (.exe in Windows) resides.
    3. You run the executable from the command line.

    If those assumptions are wrong, then you need to tell us what's really happening.

    For example, if you are using Windows and your development environment ("integrated development environment" (IDE) that combines editor, compiler, linker, debugger, project manager, and other functionalities, commonly mis-named "my compiler") is a GUI app and you are running your program from the GUI, then your problem is very well-known and is due to trying to run a console app in a manner that it was not intended to be run. In that situation, the OS creates a console (the technical Windows term for "DOS window") for your program to run in. Then when your program terminates normally, that console automatically closes. What you need to do in that case is to keep the console from closing until you tell it to. A common kludge is to make an input function (eg, getchar()) the last line.

    Besides having left out the inclusion of stdio.h, you also failed to declare main's return type, which should be int, and the return 0; statement at the end to honor the function's return type.

    PS

    Most likely scenario is that you are using a GUI IDE on Windows.

    However, if you are indeed trying to run it from the command line, then the problem is more subtle. Did you indeed neglect to #include stdio.h? If so, then the compiler wouldn't have known anything about printf and would have generated warnings about it (what did your warnings say?). I've seen compilers still be able to link to the standard C library function in such situations, while I've also seen some not be able to do that and cause "weird problems". So then if you are indeed running from the command line (as God had intended this type of program to be run) and you did indeed neglect to #include stdio.h, and you do not get any output when you run that program, then #include that header file and see whether that fixes it.

    Whatever happens, when you have a problem that you need help with, then you do need to tell us exactly what you are doing. Working from sufficient information is far better than having to make wild guesses, plus you get your answer much quicker.

    Also, do not ignore warnings! Getting warnings means that there's something very questionable about your code, namely that it's broken in some way. Even if a compiler generates an executable, if you're getting warnings then you have no quarantee that that executable is going to do what you expect it to do. Too many IDEs turn warnings off by default. Always turn warnings on and up! Warnings are far more important than error messages. Never ignore warnings.
    Last edited by dwise1_aol; February 26th, 2015 at 10:29 AM.
  4. #3
  5. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,861
    Rep Power
    509
    Also, printf ("a string\n") is more expensive than puts("a string") or fputs("a string without newline", stdout);

    printf processes the format string while puts / fputs can just pass that string along its happy way.

    In 2014 the US nationwide consumed the extra coal carried by three barges for that mere bit of lazy programming.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,131
    Rep Power
    2010
    Originally Posted by b49P23TIvg
    In 2014 the US nationwide consumed the extra coal carried by three barges for that mere bit of lazy programming.
    Interesting fact, where do you have the source for that. :D
  8. #5
  9. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,574
    Rep Power
    2341
    If by Terminal you mean OS X's Terminal, you need to make sure you install Xcode and the Xconde command-line utilities. Then you should be able to use make or raw gcc. If your program file is test.c, then typing
    Code:
    make test
    in the terminal will yield an executable file in the same directory; for raw gcc you would type
    Code:
    gcc -o test test.c
    to get the same result.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2015
    Posts
    4
    Rep Power
    0

    Beginner C - Running C in Terminal complete with more information.


    Alright so I am using linux via my mac which operates on Mac OS Yosemite 10.10. As to what exactly I am trying to do. Currently I am working through the first example in The C-programming book by Brian Kernighan and Dennis Ritchie.
    As to what I am expecting to happen. When I call the file(I have named this file hello.c), I expect to see "hello, world" printed in the command line. I appreciate all the help. I am knew to devshed so do not hesitate educate me on proper forum etiquette. Again thanks for the offering a your expertise.

    Within terminal this are my exact steps:

    Code:
    ocu149-160-240-157:~ Michael$ cd documents
    149-160-240-157:documents Michael$ gcc hello.c -o hello.out
    hello.c:4:12: error: expected parameter declarator
        printf("hello, world\n")
               ^
    hello.c:4:12: error: expected ')'
    hello.c:4:11: note: to match this '('
        printf("hello, world\n")
              ^
    hello.c:4:5: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
        printf("hello, world\n")
        ^~~~~~
    hello.c:5:2: error: expected function body after function declarator
    )
     ^
    1 warning and 3 errors generated.
    149-160-240-157:documents Michael$
    I realize that I may not have the proper libraries installed on my computer. If some what is the proper quick command to install a library(ies) to linux.
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,325
    Rep Power
    2226
    Show us the code listing for your entire program. The warning on line 5 ("warning: type specifier missing, defaults to 'int'") tells me that you failed to #include stdio.h at the top of your code, which pulls in the function prototype for printf and would therefore inform the editor of that function and what its format is. That line of code would be:

    Code:
    #include <stdio.h>
    As I had already said.

    Also, look at main:
    Code:
    main()
    
    (
        printf("hello, world\n")
    )
    In C, every statement ends with the statement terminator, ; (semicolon). You left that out, so the compiler assumed that you were trying to create a new function inside of main, which is also why you got the first error because "hello, world\n" is not a valid parameter declaration (read the error message to verify that that is indeed what the compiler is complaining about).

    Give that poor statement the semicolon termination that it needs. And #include stdio.h. Then report your progress. I have absolutely no programming experience on a Mac, so I don't know how it handles launching a console program from a GUI. Re-read my description of what happens when you do that under Windows for a possible clue in case it still "doesn't work" ("Oh, it works just fine! It did exactly what you told it to do! Too bad that's not the same thing as what you wanted it to do.").

    Also, K&R are literally "old school", since they had literally created the language and the first edition of that book was the de facto reference manual for C. It's a good book and well worth having and using, but their writing style is out-of-date and deprecated in places.

    They use the old C feature of allowing you to use functions and variables that you haven't declared; the compiler just assumes that they are all int (functions default to having one int parameter and returning int). That is bad practice and should be avoided at all times; always declare all variables and functions before you use them. Instead, main should be declared on one of two ways:

    Code:
    int main()
    {
    
        return 0;
    }
    or

    Code:
    void main()
    {
    
    }
    Declaring main() as int and returning zero is very much preferred, since your program is expected to return an exit code to the process that had launched it -- returning zero means it ran successfully while returning a positive non-zero means that it terminated abnormally because of some kind of error (you choose arbitrarily what failure code to use for what type of error, eg a 2 for "file not found").

    Make the corrections and report the results back to us.

    Comments on this post

    • ffelix agrees : Useful info--thanks!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2015
    Posts
    4
    Rep Power
    0
    So after following your instructions, I revised my code. Included a header, changed to main to int main, declared 0. I assigned a variable to prinf(), then assigned a function to c to get the characters, and display those characters. However, I am still receiving some errors.


    Code:
    #include <stdio.h>
    
    int main()
    {
    	char c;
    	printf(“hello, world\n”);
    	c = getchar();
    	putchar(c);
    	
    	return 0;
    }
    The errors that were returned were:

    Code:
    Michaels-MacBook-Pro:documents Michael$ gcc hello.c -o hello.out
    hello.c:6:9: error: non-ASCII characters are not allowed outside of literals and
          identifiers
            printf(“hello, world\n”);
                   ^
    hello.c:6:12: error: use of undeclared identifier 'hello'
            printf(“hello, world\n”);
                    ^
    hello.c:6:26: error: non-ASCII characters are not allowed outside of literals
          and identifiers
            printf(“hello, world\n”);
                                  ^
    3 errors generated.
  16. #9
  17. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,325
    Rep Power
    2226
    Consider this hex dump of your code:
    Code:
    C:\otros\dcw>xxd mitjohn.c
    0000000: 2369 6e63 6c75 6465 203c 7374 6469 6f2e  #include <stdio.
    0000010: 683e 0d0a 0d0a 696e 7420 6d61 696e 2829  h>....int main()
    0000020: 0d0a 7b0d 0a09 6368 6172 2063 3b0d 0a09  ..{...char c;...
    0000030: 7072 696e 7466 2893 6865 6c6c 6f2c 2077  printf(.hello, w
    0000040: 6f72 6c64 5c6e 9429 3b0d 0a09 6320 3d20  orld\n.);...c =
    0000050: 6765 7463 6861 7228 293b 0d0a 0970 7574  getchar();...put
    0000060: 6368 6172 2863 293b 0d0a 090d 0a09 7265  char(c);......re
    0000070: 7475 726e 2030 3b0d 0a7d                 turn 0;..}
    
    C:\otros\dcw>

    What did you use to write your code? A text editor or a word processor? You used a word processor, didn't you? A word processor will "do you a big favor" and give you real purdy open and close quotes: 0x93 and 0x94 respectively. Those are extended ASCII codes, which means that they are greater than the regular 7-bit ASCII codes (max code is 127) which are standard; extended codes are not standard. Only the standard 7-bit codes are recognized by the compiler. For the double quote, it expects 0x22.

    I know that the Mac has a text editor hidden away somewhere. I was very surprised when I found it. Look for that text editor and use it for all your coding. Or you could use a Linux text editor instead if you are able to. Just stay completely away from word processors when you write code.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2007
    Posts
    1
    Rep Power
    0
    This answer is excellent. Thanks for not taking the low road and berating the noob without offering anything valuable.
  20. #11
  21. No Profile Picture
    Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    6
    Rep Power
    0
    Such a grateful answer as you given. thanks for the sharing :)

    PHP Traning in Noida

IMN logo majestic logo threadwatch logo seochat tools logo