Thread: Beginner: Is the code of the professor really wrong?

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    36

    Beginner: Is the code of the professor really wrong?

    Hi!

    I will begin studying computer science at an university in about 2 months.
    In preparation for it, I have received from an older friend all his studying-material from the past (2012) in C, because regards to the subject programming, C is the first language our university starts off with.

    Hence I also got the script of his previous professor and I am wondering if C had major changes since around 2012, so that former syntax does not work anymore?

    Please take a look on this code-snippet from the script:

    Screenshot - 6971c62a191d4de587908f49a420a721 - Gyazo


    I can easily "fix it", so that it does work, but how the heck can be the prof with a simple basic example wrong?
    Obviously I do not think that he is wrong, but what do I miss?
    C was 2012 so different as opposed to now?

    Here is my "fix":

    Screenshot - 7c919543eb9289c2f3a57491244be508 - Gyazo

    Note:
    I am sorry for only being able to post links to the screenshots, but somehow the "insert image"-function does not work for me.

    Best regards,
    Placebo

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    No, there haven't been any changes since 2012. The professor's code would be wrong in all versions of C, and your fix seems to be correct.

    Next time, please post code as text, not as a screenshot. Compilers can't read pictures.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    If you mean the headers not being shown that is normal in examples.

    If you mean "%lf", instead of "%f" that depends somewhat on the Compiler/Operating System for which one is correct.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Here's the problems with the original code (besides the missing headers) that were fixed:
    • In scanf: %f should be %lf (because %f tells scanf to read a float, not a double). If float and double happen to be the same underlying type on a system then %f might "work" on that system, but it's not portable, and I wouldn't count on it. float and double are different underlying types on most systems anyway.
    • In printf: %d should be %f because the matching argument is a double, not an int.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Placebo
    how the heck can be the prof with a simple basic example wrong?
    It looks like a plausible typographical error combined with the professor neglecting to test the sample code rather than evidence that the professor did not bother to learn C before using it as a medium to teach.
    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

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Simply put, everyone makes mistakes. Don't be afraid to politely point them out, even during class if it's something glaringly wrong. Your professor's reaction to your correction will indicate whether they're capable of teaching you anything or not.
    Devoted my life to programming...

  7. #7
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Quote Originally Posted by Placebo View Post
    . . . but how the heck can be the prof with a simple basic example wrong?
    Maybe the Prof wanted to test the knowledge and the intelligence of his students . . .

  8. #8
    Registered User
    Join Date
    May 2016
    Posts
    104
    Quote Originally Posted by christop View Post
    Here's the problems with the original code (besides the missing headers) that were fixed:
    • In scanf: %f should be %lf (because %f tells scanf to read a float, not a double). If float and double happen to be the same underlying type on a system then %f might "work" on that system, but it's not portable, and I wouldn't count on it. float and double are different underlying types on most systems anyway.
    [s]Actually %f is correct. Printf does not take floats as arguments, it's always a double -unless you specify %Lf of course which is a long double. If you include the l length modifier with f it is ignored, the result is the same as if you didn't use it.[/s]
    Edit: sorry, didn't notice you were referring to scanf.
    Last edited by Dren; 12-29-2018 at 05:06 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Dren
    Actually %f is correct. Printf does not take floats as arguments, it's always a double -unless you specify %Lf of course which is a long double. If you include the l length modifier with f it is ignored, the result is the same as if you didn't use it.
    That would be true... except that christop's comment was regarding the use of %f with scanf to read a double. The printf call in the code used %d, not %f, to print a double.
    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

  10. #10
    Registered User
    Join Date
    May 2016
    Posts
    104
    Oops... I read that as printf. My bad Guess I jumped the gun too quickly.

  11. #11
    Registered User
    Join Date
    Dec 2018
    Posts
    36
    Quote Originally Posted by christop View Post
    No, there haven't been any changes since 2012. The professor's code would be wrong in all versions of C, and your fix seems to be correct.

    Next time, please post code as text, not as a screenshot. Compilers can't read pictures.
    All right, will do - thanks.
    Also thanks a lot for all other replies.

    By the way, is there any significant differen and/or advantage in using as the "opener": int main( int argc, char *argv[] ){
    instead of
    int main() {

    ?

    Best regards,
    Placebo
    Last edited by Placebo; 12-29-2018 at 08:56 AM.

  12. #12
    Guest
    Guest
    The argc, argv variant is for when you want to handle command line arguments passed to your program. If you don't do that, you can go with plain main without parameters.

  13. #13
    Registered User
    Join Date
    May 2016
    Posts
    104
    There are three standard-compliant ways to define main.
    Code:
    int main(void)
    int main(int argc, char **argv)
    int main(int argc, char *argv[])
    The last two are equivalent and will provide you with any command line arguments that were given when the program was run.
    e.g:
    Code:
    ./calculator 369 + 247
    argc, or argument count will be the number of arguments provided. The program name will always count as one, so argc will always be at least one.
    In the example above there are 4 arguments: the program name -calculator, 369, +, 247.
    The arguments themselves will be stored in argv.

  14. #14
    Registered User
    Join Date
    Dec 2018
    Posts
    36
    Thanks a lot!^^

  15. #15
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Code:
    int main()
    Is another one too - the difference is entirely compiler specific. For instance the LCC compiler will give a
    warning if the char* args version us used without explicitly giving the parameters after it's declaration.
    Other compilers (such as GCC).

    One other thing, try to get a compiler that complies to the C11 standard.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 01-23-2013, 08:38 PM
  2. Beginner: What is wrong with my code? It seems fine!
    By shivdude in forum C Programming
    Replies: 10
    Last Post: 08-03-2009, 03:36 PM
  3. Replies: 2
    Last Post: 02-21-2009, 12:11 AM
  4. sadistic calculus professor
    By sean in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 09-13-2008, 10:03 AM
  5. frustrating professor
    By vwy in forum C Programming
    Replies: 10
    Last Post: 02-07-2006, 09:55 PM

Tags for this Thread