Find the answer to your Linux question:
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 25
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux User George Harrison's Avatar
    Join Date
    Mar 2005
    Location
    Pepperland
    Posts
    445

    Just starting C and I feel hopeless


    Hello, I'm trying to learn C and I have found a couple of guides that are kind of nifty while Googling. Although the guides are old (1990's) they are still good for beginners like myself I suppose. I feel quite newbish for bringing my problem up but I can't even get my first program to run.. To top it all off it's only a Hello World like program Here is what I have:
    Code:
    #include <stdio.h>
    
    main&#40;&#41;
    &#123;
            printf&#40;"This is a C program\n"&#41;;
    &#125;
    I saved it as "file.c" yet this is the output:
    Code:
    $ gcc file.c
    $
    So it returns with a blank line? I thought I was supposed to get an output of "This is a C program". Maybe I'm too noobish to realize that this is too small of a program and I'm not supposed to get any output but I thought it would do _something_ I dunno. My friend writes in C but says he is too busy to help me and that if I'm lucky I can borrow his C howto book :P

    I know this is a foolish question to ask, but I need to start learning somewhere and somehow. Thanks.
    Registered Linux user #393103

  2. #2
    Linux Engineer
    Join Date
    Jan 2005
    Location
    Chicago (USA)
    Posts
    1,028
    Basic compiling with GCC: gcc SOURCE -o EXECUTABLE'S-NAME. Then execute the executable like you would a shell script or any other program.

  3. #3
    Linux Guru AlexK's Avatar
    Join Date
    Feb 2005
    Location
    Earth
    Posts
    3,379
    Since gcc is ANSI compliant, you should put int main(void) rather than main(). Then just have a return 0 at the end of the code i.e.:

    Code:
    int main&#40;void&#41; 
    &#123; 
            printf&#40;"This is a C program\n"&#41;; 
    return 0; // tells the system program has finished successfully.
    &#125;
    For small programs it may be ok to do void main void... but when creating larger programs, you need to let the system know when the program has finished successfully, otherwise you may get wierd errors (Only for very large programs).
    Life is complex, it has a real part and an imaginary part.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Jun 2005
    Location
    Canada, Halifax
    Posts
    86
    The one and only C programming manual that you'll ever need is "The C Programming Language, Second Edition" ISBN 0-13-110362-8. I _HIGHLY_ recommend that you buy this book.

    I've taken a number of C and C++ programming courses during my days in university but none have stood the test of time once I got into industry. It (K&R) won't help you with "gcc" and other application and platform specific issues however it is the one and only souce needed for anyone interested in mastering C.

    To help out with the sorst of questions that you just presented here, get "Beginning Linux Programming, 3rd Edition" ISBN ISBN: 0-7645-4497-7. This book will help answer your "Hello, world!" gcc questions all the way through to much more advanced topics.

    I appreciate that you're enthusiastic and want to start programming right away (go for it!) but it is well worth your valuable time to get these books. I have them both in my personal library and always will!

  6. #5
    Linux User George Harrison's Avatar
    Join Date
    Mar 2005
    Location
    Pepperland
    Posts
    445
    ok thanks guys.

    hate to do it but I might go along with the majority and start learning C++ maybe.. C is fun and all but I want something heavily based on object orientation and such and I think C++ would just suit me a bit more. Afterwards I might go back into C and possibly Java or something later.

    Thanks again for the help.
    Registered Linux user #393103

  7. #6
    Linux Guru AlexK's Avatar
    Join Date
    Feb 2005
    Location
    Earth
    Posts
    3,379
    One thing you should keep in mind is that C is just a subset of C++. If you learn C, then C++ is just C with objects, nothing more. By learning one, you automaticially know the other.

    And good luck with your programming studies.
    Life is complex, it has a real part and an imaginary part.

  8. #7
    Linux User George Harrison's Avatar
    Join Date
    Mar 2005
    Location
    Pepperland
    Posts
    445
    Quote Originally Posted by AlexK
    One thing you should keep in mind is that C is just a subset of C++. If you learn C, then C++ is just C with objects, nothing more. By learning one, you automaticially know the other.

    And good luck with your programming studies.
    oh.. eh, in that sense it seems that C++ is actually a bit more useful but C is there to say that I'm a "1337 h4x0r"? Getting serious - I can see the pros and cons to each I suppose, I do think that I might want to go with C++ maybe..

    I did finally figure out why it was returning a blank line, I was only compiling it. I had to run the a.out file that was generated later . But I guess you can make all sorts of stupid mistakes when you're learning :P wow, I did feel really stupid though. My face was this guy:
    Registered Linux user #393103

  9. #8
    Linux Engineer
    Join Date
    Jan 2005
    Location
    Chicago (USA)
    Posts
    1,028
    Quote Originally Posted by AlexK
    One thing you should keep in mind is that C is just a subset of C++. If you learn C, then C++ is just C with objects, nothing more. By learning one, you automaticially know the other.

    And good luck with your programming studies.
    No, that's Objective C. C++ has some more.
    http://en.wikipedia.org/wiki/C_plus_plus
    http://en.wikipedia.org/wiki/Objective_C

    And GCC4 does compain if main isn't an int.

  10. #9
    Linux User George Harrison's Avatar
    Join Date
    Mar 2005
    Location
    Pepperland
    Posts
    445
    Quote Originally Posted by a thing
    And GCC4 does compain if main isn't an int.
    Yep, Ubuntu installed GCC4 and it complained when I didn't have the int in there, unfortunately all the documentation out there says little to nothing about this. I think I'm going to go pick up The C Language book.
    Registered Linux user #393103

  11. #10
    I am not really looking to change any opinions or start a flame war, but I feel like I should express my opinion on the issue, even if very briefly.

    George Harrison, C++ is a wonderful language, and learning it will stand you in good stead in the long run. (I firmly believe that it is important to acquire at least a cursory acquaintance with many different languages even when one knows two or three very deeply.)

    But it is wrong to assume that object-oriented programming is not possible with C. In fact, very well written C code can be conceptually equivalent to C++ code. (An example is the GTK+ library, written in C, but in object-oriented fashion.) Object-oriented programming should be seen as a means of implementing a particular method of solving a problem, not as a constraint to use a particular programming language. In fact, programming in object-oriented style in C will really force you to plan your solution carefully, and in the process, develop good programming habits.

    Don't shun C simply because it does not have the "class" keyword in it. In my opinion it is the best language to learn. I know that there will be those who disagree with the last statement, but I will stand by it.

Posting Permissions

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