Thread: Print statement won't print

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    9

    Print statement won't print

    Hi, I'm new to programming and am trying to learn C by doing examples out of the book. My code looks right but my print statement won't show up and neither will the decimal points. Please help. I'm using Dev-C++ as my complier.

    Code:
     #include <stdio.h>
     #include <stdlib.h>
    
    /* Print Farenheit-Celsius Table
       for fahr = 0, 20, ... ,300 */
     
       main ()
       {
            float fahr, celsius;
            int lower, upper, step;
            
            lower = 0;        /* Lower limit of temperature table   */
            upper = 300;      /* Upper limit of temperature table   */
            step = 20;        /* Step size                          */
            
            printf("Fahr Celsius \n"); // This line wont print
            fahr = lower;
            while (fahr <= upper) {
                  celsius = (5.0/9.0) * (fahr - 32.0);
                  printf("%3.0f %6.1f\n", fahr, celsius); // Decimals aren't showing up
                  fahr = fahr + step;
                  } // end while
                     
            system ("pause"); 
    
    
                  }// end main
    Last edited by Sakari; 06-17-2012 at 05:41 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Please post the smallest complete program possible that demonstrates this behavior. In other words, I can't just guess what happens above or below that code segment of yours.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    I'm sorry. I edited the code.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Try including the header needed for the system function stdlib.h

    NOTE: My guess is your code is NOT compiling; that is why you see no output.

    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

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    I just tried including stdlib.h and it still doesn't work.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Does your compiler display any warnings or errors? Post them if yes.
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest going back to "Hello World" and finding out what is wrong with your setup or understanding of the IDE and Compiler.

    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

  8. #8
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    @Greaper, The compiler doesn't show any errors. The program runs and shows the table. It just leaves out those two things.
    @Tim, ok.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Assuming you're using an IDE, try doing a "Build Clean" or equivalent (the exact label depends on IDE). That will delete all object files and executables. Then rebuild before attempting to run the program.

    It is fairly common for some IDE's, when you go through a number of iterations of code, to get confused when a source file does not compile, and then keep launching a previous version of the executable file produced from an old version of your source code.

    If you are using some script or makefile for building, then ensure that there is an option to do a cleanup, and use it. Also ensure that the script or makefile checks error status of every command (compilation, linking, etc) and terminates on error. Failure to do that can also get things out of whack between latest code version and the executable file.

    If you are compiling by hand (eg doing compilation from a command line) ensure you check each command succeeds before entering the next.



    Oh, depending on compiler and IDE: when using floating point, there may be some additional options that need to be set, or libraries included in the build. That is particularly common with unix-based compilers (eg gcc and variants).
    Last edited by grumpy; 06-17-2012 at 06:35 PM. Reason: Fixed typos
    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.

  10. #10
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    @grumpy It worked! I couldn't find "build clean" or an equivalent, so I deleted the executable file that was created. When I compiled again, everything worked.

    Thank you everyone for your help.

  11. #11
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    P.S. I also guarantee you that your compiler would output some warnings if you compiled with the correct options (e.g. -Wall for gcc, etc.) turned on.

    gcc -O0 -g3 -Wall -c -fmessage-length=0 -otest.o ..\test.c
    ..\test.c:8: warning: return type defaults to 'int'
    ..\test.c: In function 'main':
    ..\test.c:27: warning: control reaches end of non-void function

    Take the fact that you didn't get warnings as a warning in itself. On a ten-line piece of code it doesn't matter much but still helps, but on a huge program, you could catch any number of problems just by having warnings turned on.

    And, please, throw away that ancient C book you're learning from. main() and system("pause")? Really?

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  12. #12
    Registered User abhijitnumber1's Avatar
    Join Date
    Jun 2012
    Location
    kolkata
    Posts
    5

    Talking print statement solution.

    This program work for me

    In C- programming you must declare every thing before use it in your program.
    In your code you should define the system("pause") command before use it.
    OK. so, take care of this type of basic but very important topic.


    Code:
    #include <stdio.h>
    #include<conio.h> // i change the header file to conio.h
    
    
    /* Print Farenheit-Celsius Table
       for fahr = 0, 20, ... ,300 */
     
       main ()
       {
            float fahr, celsius;
            int lower, upper, step;
            
            lower = 0;        /* Lower limit of temperature table   */
            upper = 300;      /* Upper limit of temperature table   */
            step = 20;        /* Step size                          */
            
            printf("Fahr Celsius \n"); // This line wont print
            fahr = lower;
            while (fahr <= upper) {
                  celsius = (5.0/9.0) * (fahr - 32.0);
                  printf("%3.0f %6.1f\n", fahr, celsius); // Decimals aren't showing up
                  fahr = fahr + step;
                  } // end while
                     
    getch();
    
    
    
    
                  }// end main
    Last edited by abhijitnumber1; 06-19-2012 at 05:53 AM.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    abhijitnumber1: note that <conio.h> is a non-standard header, and that getch is not part of the C standard library. So, if you don't really need it, and indeed you do not here, then using it just makes your code less portable without good reason.
    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

  14. #14
    Registered User abhijitnumber1's Avatar
    Join Date
    Jun 2012
    Location
    kolkata
    Posts
    5
    laserlight, i am also new in c programming. but, i know that conio.h is Console input /output header. In my post i said that "header file", not standard header file. & i use it(<conio.h>) for console out put & that's why i use getch() function.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm the #1 fan of conio.h, BUT it did NOT solve the problem here - the program just needed a clean re-compile.

    It's a BIG error to replace stdio.h with conio.h, imo. First reason - it's not standard, and is not supported on lots of compilers. Second reason - it's nowhere near as well written as stdio.h.

    Yes, I do use conio.h on some programs, because I know it, and my compiler also supports it, but I never replace stdio.h, or forget that it's useless on any program that might be useful to anyone else.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 06-13-2012, 12:34 PM
  2. Having troubles with a simple print statement.
    By Tails in forum C Programming
    Replies: 4
    Last Post: 09-22-2011, 06:14 PM
  3. Replies: 1
    Last Post: 07-15-2011, 10:46 AM
  4. bizarre print statement glitch
    By spongefreddie in forum C Programming
    Replies: 4
    Last Post: 09-23-2010, 10:39 AM
  5. Overwriting last print statement in command line
    By PolarBear2k in forum C Programming
    Replies: 2
    Last Post: 11-09-2006, 03:08 PM