Thread: Examples from Kernighan book

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    2

    Examples from Kernighan book

    Hi. I am new to programming and i started with C. I bought the Kernighan book, but some example programmes do not work for me. I am running GCC on a Linux system. Specifically, the example included in section 1.7 Functions does not work for me (power function). Thank you very much

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Can you be more specific than "doesn't work"?

    OK, I'm going to take a stab that you need to link with the math library, since you did mention pow()
    Try invoking the compiler with a command line such as this.
    gcc prog.c -lm

    The last bit (minus-el-em) tells the linker to also search the math library.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    2
    Sorry. I meant the output is not the expected. But, i tried the same code on other computer (newer), and it does work on this new laptop. I am suprised. But i do not know how it can be. Thank you very much

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The example he's referring to just raises an integer to a nonnegative integer power by using a loop. It doesn't link to the math library. On exactly what compiler and platform does this code not work, and are you sure you didn't mistype something? It's pretty simple.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    On what machine did that work. What compiler was it on the working machine. Did you try the same input as with the not working machine?

    ssharish2005

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    *shrug*
    Copy / paste your code and terminal session, showing the compilation and running steps.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a phone book program
    By mackieinva in forum C Programming
    Replies: 2
    Last Post: 09-19-2007, 06:31 AM
  2. Looking for a c++ book, didn't were to post this...
    By Rune Hunter in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2004, 06:32 PM
  3. Must read book!
    By RealityFusion in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 07-15-2004, 09:05 PM
  4. Should i get a new reference book?
    By Raison in forum Windows Programming
    Replies: 2
    Last Post: 06-15-2004, 10:43 PM