Like Tree1Likes
  • 1 Post By laserlight

learn C In 21 days Appendix F ERRORS (bug in program)

This is a discussion on learn C In 21 days Appendix F ERRORS (bug in program) within the C Programming forums, part of the General Programming Boards category; I bought sams Teach yourself c in 21 days about 10 years ago, I have the fifth edition of the ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    3

    learn C In 21 days Appendix F ERRORS (bug in program)

    I bought sams Teach yourself c in 21 days about 10 years ago, I have the fifth edition of the book, I'm having some trouble with the following code
    Code:
    #include <stdio.h>
    int three_powered( int power );
    
    main()
    {
    	int a = 4;
    	int b = 9;
    
    	printf ( "\n3 to the power of %d is %d", a, three_powered );
    	printf ( "\n3 to the power of %d is %d", b, three_powered );
    
    	return 0;
    }
    
    int three_powered ( int power )
    {	
    	if ( power < 1 )
    		return( 1 );
    	else
    		return( 3 * three_powered( power - 1));
    }
    when I execute I DO NOT Get 81 and 19683, I get the following


    3 to the power of 4 is 4198888
    3 to the power of 9 is 4198888

    Is this an error in the code in the book or in my own iteration of it? please advise

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    The function is not called, e.g.,
    Code:
    printf ( "\n3 to the power of %d is %d", a, three_powered );
    should be:
    Code:
    printf ( "\n3 to the power of %d is %d", a, three_powered ( a ) );
    Paul Tidwell likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    thank you so much for your prompt reply! the book is wrong.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    What to do, they probably wrote the book and had it edited and sent for publishing all in 21 days. Sloppy job
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    I Don't have the latest edition of the book, It's in the mail. it's pretty much the one EVERYONE recs to learn C though

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    The one everyone recs huh?...interesting

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    If the book were perfect, you wouldn't learn anything from simply copy/pasting every example in the book.
    The occasional mistake (intentional or otherwise) keeps you alert.
    And no, this isn't a tacit recommendation on my part for that book either.


    Besides, books are not immune to having bugs in them, just the same as anything else.
    Even K&R-II has a list of errors spanning a couple of pages of A4.

    Getting a compiler which has greater diagnostic ability than the one you have would help.
    Code:
    $ gcc -W -Wall -ansi -pedantic baz.c
    baz.c:5: warning: return type defaults to ‘int’
    baz.c: In function ‘main’:
    baz.c:9: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int (*)(int)’
    baz.c:10: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int (*)(int)’
    The last two messages tell you that
    printf ( "\n3 to the power of %d is %d", a, three_powered );
    is probably not what you intended.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where and How did you learn to program?
    By RialnisMada in forum Tech Board
    Replies: 26
    Last Post: 08-30-2010, 05:07 AM
  2. Want to learn how to program C++ ?
    By blangela in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 06-29-2008, 07:14 PM
  3. Can you actually learn c++ in 21 days?
    By Raeliean in forum C++ Programming
    Replies: 14
    Last Post: 07-27-2005, 04:41 PM
  4. So you think you can learn C or C++ in 21 days?
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 07-23-2005, 03:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21