Thread: Please grade my answers

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    Please grade my answers

    Hi, everyone.

    I'm working my way through Sam's Teach Yourself C in 21 days and I want to post my answers to the questions for your assessment. I'm not officially studying C, I'm just doing it for my own education.

    1. Write the perfect Hello, world program.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      puts("Hello, World!");
      return EXIT_SUCCESS;
    }
    2. Give three reasons why C is the best choice of a programming language.

    It's multiplatform, very widely supported, has heaps of documentation available, relatively easy to learn, can run on embedded/low-power CPUs, has standard libraries, and mature compilers.

    3. What does the compiler do?

    It turns my source code into object code. My compilers also turn it into an executable file. So it turns source code (my description of the program) into the actual program.

    4. What are the steps in the program development cycle?

    Write my code
    Compile it - if it doesn't compile, fix the code and try again
    Link it - if it doesn't link properly, fix the code and try again
    Run the program - if it doesn't run properly, fix the code and try again
    Test the program - if it doesn't do what it's supposed to, fix the code and try again

    5. What command do you need to enter in order to compile a program called program1.c with your compiler?

    I'm using OS X, so I can use either:

    gcc program1.c
    clang program1.c

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    They all sound fine. Correct me if I'm wrong but isn't there an answer guide somewhere? The SAMS book I own comes with answers to these questions.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Richardcavell View Post
    1. Write the perfect Hello, world program.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      puts("Hello, World!");
      return EXIT_SUCCESS;
    }
    Your book actually asks you that question???? Perfection, like beauty is in the eye of the beholder and a book that implies there is some absolute concept of perfection in software is not one I would trust. What may be perfect for you may not be perfect for someone else.

    Personally,

    1) I wouldn't bother with the void in int main(void). It is part and parcel when you are defining (aka implementing) any function without argument, and the void keyword adds no additional information.

    2) I would add a \n to the end of the string fed to puts().

    3) "return 0" is equivalent to "return EXIT_SUCCESS" as, by convention, zero indicates successful exit. It also does not require #include <stdlib.h>. Strictly speaking, you can also eliminate the return statement altogether (falling off the end is equivalent to return 0 according to the latest standard) - but that causes warnings from some compilers - mostly older compilers. If your notion of perfection includes "support all possible C compilers, then you need a return statement.

    As I said, my notion of perfection may differ from yours. And it may differ from other people's view of perfection.

    Quote Originally Posted by Richardcavell View Post
    2. Give three reasons why C is the best choice of a programming language.

    It's multiplatform, very widely supported, has heaps of documentation available, relatively easy to learn, can run on embedded/low-power CPUs, has standard libraries, and mature compilers.
    Also another bad question. C is not the absolute "best choice" of programming language. Depending on your task at hand, C may be the worst language to use, it may be the best, or it may be one of several mediocre or good choices.

    The answer you have given are some of the claimed strengths of C, which might make it a good choice for some tasks.

    Incidentally, it is easy to learn the basics of C. There are a lot of aspects of C that are difficult to learn or, even for people who have been programming in C for years, easy to make mistakes with.

    Quote Originally Posted by Richardcavell View Post
    3. What does the compiler do?

    It turns my source code into object code. My compilers also turn it into an executable file. So it turns source code (my description of the program) into the actual program.
    I assume the question is referring to "C compiler", not just compiler (which is just a program for converting a source language into a target langauge). In any event, only your first sentence in response is correct.

    C compilers, in practice, rarely convert source code into executable files. They usually output an object file only. The compiler itself does not convert object files into executables: another program, called a linker, often does that.

    The gcc program , to mention one you mentioned you use later in your post, is not formally a compiler. It is a driver program that orchestrates the execution of several other programs. One of those orchestrated programs is a C compiler. Another of those orchestrated programs is a linker. The gcc driver, when given the -c option, passes the required source file to the compiler and an object file is produced. Without the -c option, when given a source file, the driver will invoke a compiler to produce an object file, then invoke the linker to produce an executable from that object file. That is a very simplistic view of what the gcc driver does: there are many other things it does as well, depending on compilation or linking options it is supplied - and whether errors occur at various phases.

    clang is also a driver program, not a compiler in its own right.

    Quote Originally Posted by Richardcavell View Post
    4. What are the steps in the program development cycle?

    Write my code
    Compile it - if it doesn't compile, fix the code and try again
    Link it - if it doesn't link properly, fix the code and try again
    Run the program - if it doesn't run properly, fix the code and try again
    Test the program - if it doesn't do what it's supposed to, fix the code and try again
    That is a hackers development cycle.

    Frankly, if you answer that question in that manner in a job interview, you would rarely get a job.

    If you want to have some idea of what software development cycles are really like, google for "software development process" and "software development lifecycle". Admittedly, the theory and practice of software development are very different from each other, but both the theory and practice are very different from what you have described.

    Quote Originally Posted by Richardcavell View Post
    5. What command do you need to enter in order to compile a program called program1.c with your compiler?

    I'm using OS X, so I can use either:

    gcc program1.c
    clang program1.c
    What you've actually described are command lines to compile and link a program.
    Last edited by grumpy; 04-28-2012 at 10:34 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by grumpy
    2) I would add a \n to the end of the string fed to puts().
    puts actually already appends a new line character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    144
    Thanks for your answers, all. It's always helpful to hear other people's opinions on things.

    Tomorrow you will receive the next batch of answers.

    Richard

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    puts actually already appends a new line character.
    Sure. Like I said, perfection is in the eye of the beholder.

    Personally, I would prefer fputs() too, but decided to push that barrow.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    144
    And fputs() doesn't add a \n for some reason.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Richardcavell
    And fputs() doesn't add a \n for some reason.
    Because it wasn't defined as such.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Grade my first lab? (and program :D)
    By Cowmoogun in forum C Programming
    Replies: 21
    Last Post: 02-22-2012, 08:08 AM
  2. Grade Program
    By Jbrubin in forum C Programming
    Replies: 2
    Last Post: 03-28-2011, 02:02 PM
  3. Replies: 2
    Last Post: 01-29-2011, 12:58 PM
  4. Help me pls...About computing a grade
    By Sunday in forum C Programming
    Replies: 2
    Last Post: 11-03-2007, 12:41 PM
  5. Grade Evaluator
    By Takumi8374 in forum C Programming
    Replies: 5
    Last Post: 05-24-2005, 12:01 PM

Tags for this Thread