A question

Discussion in 'C Programming' started by bob, Jul 24, 2006.

  1. bob

    bob Guest

    I've read (almost) all the C tutorials online. I understand everything
    in the tutorials, but I have no clue how to do anything practical. For
    instance, I can't understand any code anyone else has written. Help,
    anyone?
     
    bob, Jul 24, 2006
    #1
    1. Advertising

  2. bob said:

    > I've read (almost) all the C tutorials online. I understand everything
    > in the tutorials, but I have no clue how to do anything practical. For
    > instance, I can't understand any code anyone else has written. Help,
    > anyone?


    Unfortunately, most people don't write very readable code. I suggest you do
    the following:

    (a) swear a solemn swear /never/ to write unreadable code yourself;
    (b) have a go at solving the following exercise:

    Exercise BOB1: Write a C program to display the words "Hello, world!" on the
    standard output device.

    (c) post your solution here, inviting comments.

    The exercise may or may not sound trivial, but I invite you to have a go
    anyway. You may be surprised at just how much juice can be squeezed from
    such a simple program (depending largely on how good your tutorials were).

    Once that's all done, we'll know better how to advise you.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jul 24, 2006
    #2
    1. Advertising

  3. Bob,

    Only reading the tutorials is not very much help. You need to start
    coding yourself. The first code that you write will probably be code
    from the tutorials. The only way to go about using a computer language
    tutorial is to write and execute the code as you go along. The phrase
    'no pain, no gain' comes to mind. Find yourself a good C book (look at
    reviews online to find the best ones) that has exercises after each
    chapter then try to solve those exercises as you go along.

    Hope this helps,
    Nick Vatamaniuc


    bob wrote:
    > I've read (almost) all the C tutorials online. I understand everything
    > in the tutorials, but I have no clue how to do anything practical. For
    > instance, I can't understand any code anyone else has written. Help,
    > anyone?
     
    Nick Vatamaniuc, Jul 24, 2006
    #3
  4. bob

    John Bode Guest

    bob wrote:
    > I've read (almost) all the C tutorials online. I understand everything
    > in the tutorials, but I have no clue how to do anything practical. For
    > instance, I can't understand any code anyone else has written. Help,
    > anyone?


    Programming is a skill, and like all skills it requires a non-trivial
    amount of practice to master. The only way to understand other
    people's code is to write a lot of code yourself; then you begin to
    understand why other people did what they did.
     
    John Bode, Jul 24, 2006
    #4
  5. "John Bode" <> wrote in message
    news:...
    >
    > bob wrote:
    >> I've read (almost) all the C tutorials online. I understand everything
    >> in the tutorials, but I have no clue how to do anything practical. For
    >> instance, I can't understand any code anyone else has written. Help,
    >> anyone?

    >
    > Programming is a skill, and like all skills it requires a non-trivial
    > amount of practice to master. The only way to understand other
    > people's code is to write a lot of code yourself; then you begin to
    > understand why other people did what they did.
    >


    You could try writing a small calculator offering certain mathematical
    functions - this will get you used to using functions, getting user input
    and displaying some output.

    You could try writing a linked list to see well you cope with pointers, this
    list can then be used to create a queue and stack which have different
    behaviours.

    Once you are happy with this, I suggest you write something you are
    interested in, for example I wrote a small game, HexMine, then a fractal
    generator. Find something you like thats doable and get your hands dirty
    but remember to keep your code neat and well documented.

    Allan
     
    Allan M. Bruce, Jul 24, 2006
    #5
  6. Sorry, my post was meant to be in response to the OP!
    Allan
     
    Allan M. Bruce, Jul 24, 2006
    #6
  7. "bob" <> wrote in message
    news:...
    > I've read (almost) all the C tutorials online. I understand everything
    > in the tutorials, but I have no clue how to do anything practical. For
    > instance, I can't understand any code anyone else has written. Help,
    > anyone?
    >


    You could try writing a small calculator offering certain mathematical
    functions - this will get you used to using functions, getting user input
    and displaying some output.

    You could try writing a linked list to see well you cope with pointers, this
    list can then be used to create a queue and stack which have different
    behaviours.

    Once you are happy with this, I suggest you write something you are
    interested in, for example I wrote a small game, HexMine, then a fractal
    generator. Find something you like thats doable and get your hands dirty
    but remember to keep your code neat and well documented.

    Allan
     
    Allan M. Bruce, Jul 24, 2006
    #7
  8. bob <> wrote:

    > I've read (almost) all the C tutorials online.


    That actually may have been a bad idea, as the quality of such
    tutorials varies. An excellent online resource that you should avail
    yourself of, if you have not yet done so, is the FAQ for this
    newsgroup:

    http://c-faq.com

    Also consider buying, borrowing, or otherwise procuring "The C
    Programming Language" by Kernighan and Ritchie. It is full of sample
    problems to test and improve your skills, not to mention authoritative
    information concerning the language.

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Jul 24, 2006
    #8
  9. bob

    Matt Junx Guest

    bob wrote:

    > I've read (almost) all the C tutorials online. I understand everything
    > in the tutorials, but I have no clue how to do anything practical. For
    > instance, I can't understand any code anyone else has written. Help,
    > anyone?

    I'd recommend continuing by learning more about general computer science
    concepts first. You should read more about data structures, algorithms,
    and if it fancies you, compression and cryptology.
     
    Matt Junx, Jul 24, 2006
    #9
  10. bob

    bob Guest

    Richard Heathfield wrote:
    > bob said:
    >
    > > I've read (almost) all the C tutorials online. I understand everything
    > > in the tutorials, but I have no clue how to do anything practical. For
    > > instance, I can't understand any code anyone else has written. Help,
    > > anyone?

    >
    > Unfortunately, most people don't write very readable code. I suggest you do
    > the following:
    >
    > (a) swear a solemn swear /never/ to write unreadable code yourself;
    > (b) have a go at solving the following exercise:
    >
    > Exercise BOB1: Write a C program to display the words "Hello, world!" on the
    > standard output device.
    >
    > (c) post your solution here, inviting comments.
    >
    > The exercise may or may not sound trivial, but I invite you to have a go
    > anyway. You may be surprised at just how much juice can be squeezed from
    > such a simple program (depending largely on how good your tutorials were).
    >
    > Once that's all done, we'll know better how to advise you.
    >
    > --
    > Richard Heathfield
    > "Usenet is a strange place" - dmr 29/7/1999
    > http://www.cpax.org.uk
    > email: rjh at above domain (but drop the www, obviously)


    Thanks for all the replies. Here's the Hello World program you asked
    for:

    #include <stdio.h>
    main()
    {
    printf("Hello, world!");
    return 0;
    }
     
    bob, Jul 24, 2006
    #10
  11. bob

    Tom St Denis Guest

    bob wrote:
    > #include <stdio.h>
    > main()


    int main(void)

    You must specify a return type, it must be int.

    You must specify arguments (or void).

    > {
    > printf("Hello, world!");


    You need a newline if you plan to print out text and not have the shell
    prompt at the end of that string.

    > return 0;


    Technically you should #include stdlib.h and return EXIT_SUCCESS which
    is the official "all clear" return value.

    > }


    Isn't C fun :)

    Tom
     
    Tom St Denis, Jul 24, 2006
    #11
  12. bob

    Michael Mair Guest

    bob schrieb:
    > Richard Heathfield wrote:
    >
    >>bob said:
    >>
    >>
    >>>I've read (almost) all the C tutorials online. I understand everything
    >>>in the tutorials, but I have no clue how to do anything practical. For
    >>>instance, I can't understand any code anyone else has written. Help,
    >>>anyone?

    >>
    >>Unfortunately, most people don't write very readable code. I suggest you do
    >>the following:
    >>
    >>(a) swear a solemn swear /never/ to write unreadable code yourself;
    >>(b) have a go at solving the following exercise:
    >>
    >>Exercise BOB1: Write a C program to display the words "Hello, world!" on the
    >>standard output device.
    >>
    >>(c) post your solution here, inviting comments.
    >>
    >>The exercise may or may not sound trivial, but I invite you to have a go
    >>anyway. You may be surprised at just how much juice can be squeezed from
    >>such a simple program (depending largely on how good your tutorials were).
    >>
    >>Once that's all done, we'll know better how to advise you.

    >
    > Thanks for all the replies. Here's the Hello World program you asked
    > for:
    >
    > #include <stdio.h>
    > main()


    main() returns int and takes in this case an empty argument list.
    int main (void)

    The necessity of the "void" is open to debate but says exactly
    what you want.
    Relying on "implicit int" for the function return type is a bad
    habit and takes away the chance of easy transition to C99 (where
    implicit int no longer applies).
    Your compiler should warn you about that. If it didn't, then turn
    up the warning level.

    > {
    > printf("Hello, world!");


    You forgot the '\n' at the end of the format string.
    This can lead to your not actually seeing the output or only
    part of it.
    printf("Hello, world!\n");
    or
    puts("Hello, world!");
    are better.

    > return 0;
    > }



    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Jul 24, 2006
    #12
  13. "Tom St Denis" <> writes:
    > bob wrote:
    >> #include <stdio.h>
    >> main()

    >
    > int main(void)
    >
    > You must specify a return type, it must be int.


    You certainly should, but it isn't actually required in C90.

    > You must specify arguments (or void).


    In a function *definition* (as opposed to a declaration), empty
    parentheses mean that the function takes no arguments (though there's
    been some debate about this). Specifying void is certainly better
    style.

    To summarize: "main()" is at best of questionable style and legality,
    whereas "int main(void)" is definitely correct. If you use
    "int main(void)", you won't have to put up with silly debates like
    this one.

    >> {
    >> printf("Hello, world!");

    >
    > You need a newline if you plan to print out text and not have the shell
    > prompt at the end of that string.


    Yes. Leaving off the newline raises some more questions for us
    standards geeks; be sure to include it, and you won't have to worry
    about it. (Tom: see C99 7.19.2p2; IMHO, leaving off the newline
    invokes undefined behavior, though in practice I'd be very surprised
    by any consequence worse than losing the last (partial) line of
    output.)

    >> return 0;

    >
    > Technically you should #include stdlib.h and return EXIT_SUCCESS which
    > is the official "all clear" return value.


    No, "return 0;" is perfectly valid. Returning either EXIT_SUCCESS or
    0 indicates successful termination of the program. Returning
    EXIT_FAILURE indicates failure. Any other values are
    implementation-defined. (In particular, you may see "return 1;" or
    "exit(1);" in some programs; this is intended to indicate an error,
    but it doesn't do so portably.)

    >> }

    >
    > Isn't C fun :)


    Indeed.

    bob: If this stuff is over your head, don't worry about it. I'll post
    a more reasonable response directly to you original aricle
    (geek-to-newbie rather than geek-to-geek).

    bob and Tom: Just to be clear, I'm using the term "geek" in a jocular
    sense; no offense is intended.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 24, 2006
    #13
  14. "bob" <> writes:
    [...]
    > Thanks for all the replies. Here's the Hello World program you asked
    > for:
    >
    > #include <stdio.h>
    > main()
    > {
    > printf("Hello, world!");
    > return 0;
    > }


    As promised, here's my newbie-level critique.

    "main()" should be "int main(void)". "main()" is likely to work, but
    it's better to state explicitly that your main function takes no
    arguments and returns a result of type int.

    You need a newline at the end of your output:

    printf("Hello, world!\n");

    And proper indentation is important (the compiler doesn't care, but
    anyone reading your code will). There's a lot of debate about
    indentation style and placement of braces; consistency is more
    important than picking the "right" style. (You can't go too far wrong
    copying the style in K&R2, but if you're working on existing code you
    should follow its style.)

    The "return 0;" is fine. (If you wanted to terminate the program from
    within some function other than main, you'd need to call exit(), but
    you needn't worry about that for now.) (There are circumstances in
    which the "return 0;" cam be omitted, but even then it's harmless, so
    it's a good idea to always use it.)

    Applying these suggestions, we get:

    #include <stdio.h>
    int main(void)
    {
    printf("Hello, world!\n");
    return 0;
    }

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 24, 2006
    #14
  15. bob

    boa Guest

    * Keith Thompson wrote, On 24.07.2006 21:10:
    > "Tom St Denis" <> writes:
    >> bob wrote:

    [snip]

    >>> {
    >>> printf("Hello, world!");

    >> You need a newline if you plan to print out text and not have the shell
    >> prompt at the end of that string.

    >
    > Yes. Leaving off the newline raises some more questions for us
    > standards geeks; be sure to include it, and you won't have to worry
    > about it. (Tom: see C99 7.19.2p2; IMHO, leaving off the newline
    > invokes undefined behavior,


    ITYM IB, not UB. From C99 7.19.2p2

    > 2 A text stream is an ordered sequence of characters composed into lines, each line
    > consisting of zero or more characters plus a terminating new-line character. Whether the
    > last line requires a terminating new-line character is implementation-defined.



    boa

    [snip]
     
    boa, Jul 24, 2006
    #15
  16. boa <> writes:
    > * Keith Thompson wrote, On 24.07.2006 21:10:
    >> "Tom St Denis" <> writes:
    >>> bob wrote:

    > [snip]
    >
    >>>> {
    >>>> printf("Hello, world!");
    >>> You need a newline if you plan to print out text and not have the shell
    >>> prompt at the end of that string.

    >> Yes. Leaving off the newline raises some more questions for us
    >> standards geeks; be sure to include it, and you won't have to worry
    >> about it. (Tom: see C99 7.19.2p2; IMHO, leaving off the newline
    >> invokes undefined behavior,

    >
    > ITYM IB, not UB. From C99 7.19.2p2
    >
    >> 2 A text stream is an ordered sequence of characters composed into
    >> lines, each line consisting of zero or more characters plus a
    >> terminating new-line character. Whether the last line requires a
    >> terminating new-line character is implementation-defined.


    N, IM UB, not IB.

    (*ahem*)

    No, I mean undefined behavior, not implementation-defined behavior.

    The implementation has to define whether a terminating new-line
    character is required. If the implementation says it's required, I
    see nothing in the standard that says the implementation has to define
    its behavior when the new-line is missing.

    Possibly this should be tightened up a bit; I think there are
    reasonable limits to what can actually go wrong if the new-line is
    missing. But the standard currently doesn't define the behavior,
    which means the behavior is undefined.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 24, 2006
    #16
  17. bob said:

    <snip>
    >
    > Thanks for all the replies. Here's the Hello World program you asked
    > for:
    >
    > #include <stdio.h>
    > main()
    > {
    > printf("Hello, world!");
    > return 0;
    > }


    Well, lots of people have bombarded you with comments about this already, so
    I won't add to their number. (See how much juice they squeezed from it?)

    I hope the exercise gave you some idea of the sorts of problems that can
    arise from an informal approach to the language (which is not to say there
    aren't problems with the formal approach too). I must admit you surprised
    me by remembering to return 0 from main - well done. :)

    Your next task, should you choose to accept it, is to think up your own
    project. Let's not make this too complicated - I recommend some kind of
    text-filtering program, as these are very easy to do and can be useful
    additions to your toolbox. Here are some possibilities:

    Exercise BOB2: write a correct C program to display the first n lines of
    input. (Easy)

    Exercise BOB3: write a correct C program to display the leftmost n
    characters of each line of input. (Easy)

    Exercise BOB4: write a correct C program to display the *last* n lines of
    input. (Harder.)

    Exercise BOB5: write a correct C program to display the *rightmost* n
    characters of each line of input. (Harder.)

    (In all the above exercises, make n configurable via the command line.)

    Exercise BOB6: write a correct C program to replace all occurrences in the
    input of the string argv[1] with the string argv[2]. (Pretty hard.) Make
    sure your user supplies a sufficient number of arguments, and complain if
    he doesn't.

    I'm not suggesting you write all the above, but try at least some of them.

    Look for commonality between your solutions. Are there any parts you could
    usefully abstract into a separate library, so that they only need be
    written once?

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jul 24, 2006
    #17
  18. bob

    Jack Klein Guest

    On Mon, 24 Jul 2006 21:30:39 GMT, Keith Thompson <> wrote
    in comp.lang.c:

    > boa <> writes:
    > > * Keith Thompson wrote, On 24.07.2006 21:10:
    > >> "Tom St Denis" <> writes:
    > >>> bob wrote:

    > > [snip]
    > >
    > >>>> {
    > >>>> printf("Hello, world!");
    > >>> You need a newline if you plan to print out text and not have the shell
    > >>> prompt at the end of that string.
    > >> Yes. Leaving off the newline raises some more questions for us
    > >> standards geeks; be sure to include it, and you won't have to worry
    > >> about it. (Tom: see C99 7.19.2p2; IMHO, leaving off the newline
    > >> invokes undefined behavior,

    > >
    > > ITYM IB, not UB. From C99 7.19.2p2
    > >
    > >> 2 A text stream is an ordered sequence of characters composed into
    > >> lines, each line consisting of zero or more characters plus a
    > >> terminating new-line character. Whether the last line requires a
    > >> terminating new-line character is implementation-defined.

    >
    > N, IM UB, not IB.
    >
    > (*ahem*)
    >
    > No, I mean undefined behavior, not implementation-defined behavior.


    Then what you mean is incorrect. Undefined behavior, implementation
    defined behavior, and unspecified behavior, as used in the C standard,
    are mutually exclusive. Something that the standard describes as
    "implementation-defined" or "unspecified" may not ever produce
    undefined behavior in a conforming implementation.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jul 25, 2006
    #18
  19. Jack Klein <> writes:
    > On Mon, 24 Jul 2006 21:30:39 GMT, Keith Thompson <> wrote
    > in comp.lang.c:
    >
    >> boa <> writes:
    >> > * Keith Thompson wrote, On 24.07.2006 21:10:
    >> >> "Tom St Denis" <> writes:
    >> >>> bob wrote:
    >> > [snip]
    >> >
    >> >>>> {
    >> >>>> printf("Hello, world!");
    >> >>> You need a newline if you plan to print out text and not have the shell
    >> >>> prompt at the end of that string.
    >> >> Yes. Leaving off the newline raises some more questions for us
    >> >> standards geeks; be sure to include it, and you won't have to worry
    >> >> about it. (Tom: see C99 7.19.2p2; IMHO, leaving off the newline
    >> >> invokes undefined behavior,
    >> >
    >> > ITYM IB, not UB. From C99 7.19.2p2
    >> >
    >> >> 2 A text stream is an ordered sequence of characters composed into
    >> >> lines, each line consisting of zero or more characters plus a
    >> >> terminating new-line character. Whether the last line requires a
    >> >> terminating new-line character is implementation-defined.

    >>
    >> N, IM UB, not IB.
    >>
    >> (*ahem*)
    >>
    >> No, I mean undefined behavior, not implementation-defined behavior.

    >
    > Then what you mean is incorrect. Undefined behavior, implementation
    > defined behavior, and unspecified behavior, as used in the C standard,
    > are mutually exclusive.


    Agreed.

    > Something that the standard describes as
    > "implementation-defined" or "unspecified" may not ever produce
    > undefined behavior in a conforming implementation.


    I don't think that's correct. C99 7.19.2p2 doesn't use the magic
    phrase "implementation-defined behavior", just
    "implementation-defined". It requires the implementation to document
    whether the new-line is required or not. I don't see a requirement to
    document the *behavior* in the case where it's required.

    The choice, not the behavior, is implementation-defined. The
    behavior, lacking any definition, is undefined.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 25, 2006
    #19
  20. bob

    Scott W Guest

    Christopher Benson-Manica said:
    > bob <> wrote:
    >
    >> I've read (almost) all the C tutorials online.

    >
    > That actually may have been a bad idea, as the quality of such
    > tutorials varies. An excellent online resource that you should avail
    > yourself of, if you have not yet done so, is the FAQ for this
    > newsgroup:
    >
    > http://c-faq.com
    >
    > Also consider buying, borrowing, or otherwise procuring "The C
    > Programming Language" by Kernighan and Ritchie. It is full of sample
    > problems to test and improve your skills, not to mention authoritative
    > information concerning the language.
    >


    or get C Programming: A Modern Approach by K.N. King. K&R seems to be
    geared towards people who can already program, have CS degrees, probably
    a good maths understanding etc. but then, if you already have those
    backgrounds then you may be ok with K&R ;)
     
    Scott W, Jul 29, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sean
    Replies:
    1
    Views:
    619
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    691
    Iain Norman
    Feb 16, 2005
  3. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    968
    Scott Allen
    Oct 6, 2005
  4. Philip Meyer
    Replies:
    0
    Views:
    442
    Philip Meyer
    Nov 30, 2003
  5. Bit Byte
    Replies:
    1
    Views:
    875
    Teemu Keiski
    Jan 28, 2007
Loading...

Share This Page