How to: A long printf statement??

Discussion in 'Mac Programming' started by cybrscot, Jan 23, 2011.

  1. cybrscot macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #1
    My book says "Statements can be divided over any number of lines. The following statement for example, is so long that it would be hard to squeeze it onto a single line."

    printf ("Dimensional weight (pounds): %d\n",
    (volume + cubic_in_per_lb - 1 / cubic_in_per_lb) ;

    As you can see above, the part of the statement that printf actually "prints", which is between the quotations is all in the first line.

    My book also shows this statement,

    printf ("To C, or not to C: ") ;
    printf ("that is the question. \n) ;

    This "to C or not to C" also prints the two lines together when the program is run.

    I have taken my below code, which was a working proven code before, and I just changed the last printf to be a very long statement, just to experiment and see how it would output, but no matter what I tried, I couldn't get it to work. This particular code doesn't compile, but I have done a few diff things, such as paranthesizing each line, and also added the printf to the last two lines, etc. In those other experiments, it would compile, but when conditions further up were tested and were true, sometimes my compiler would print the correct printf, and also part of the printf from the last two lines at the bottom.

    Sorry to be so confusing, it's probably difficult to know exactly what I mean, but anyway, what must I do to that last long printf to make it all print, without part of it printing for another unrelated printf. I know I can put it all on one long line, my text editor will allow me to write a very long line, but I'm trying to figure out how to split up the line into shorter parts and have it compile and run the same. My book says it can be done as above, but I have tried to make mine look like those examples but it didn't work. If you need further evidence, I will rewrite the code a couple times, compile, then copy past the results if need be.

    Code:
    #include <stdio.h>
    
    main ()
    {
    
    	int velocity ;
    	
    	printf ("Enter wind velocity (knots): ") ;
    	scanf ("%d", &velocity) ;
    	
    	if (velocity < 1 )
    		printf ("Conditions are calm.\n") ;
    	else if (velocity < 4 )
    		printf ("Conditions are light.\n") ;
    	else if (velocity < 28 )
    		printf ("Conditions are breezy.\n") ;
    	else if (velocity < 48 )
    		printf ("Conditions are gale, monitor conditions closely.\n") ;
    	else if (velocity < 64 )
    		printf ("Conditions are stormy, take precautions.\n") ;
    	else if (velocity > 63 ) 
    		printf ("Hurricane conditions, seek shelter,
    		        immediately, call 800-555-8733 for safety information,
    	                and the location of your nearest shelter.\n") ;
    		
    		return 0 ;
    		
    }		 
     
  2. dmi macrumors regular

    Joined:
    Dec 21, 2010
    #2
    Code:
    		printf ("Hurricane conditions, seek shelter, "
    		        "immediately, call 800-555-8733 for safety information, "
    	                "and the location of your nearest shelter.\n") ;
    
     
  3. LPZ macrumors 65816

    Joined:
    Jul 11, 2006
    #3
    The following is one option. Note that the (velocity > 63) test is not needed.


    Code:
    #include <stdio.h>
    
    main ()
    {
    
            int velocity ;
            
            printf ("Enter wind velocity (knots): ") ;
            scanf ("%d", &velocity) ;
            
            if (velocity < 1 )
                    printf ("Conditions are calm.\n") ;
            else if (velocity < 4 )
                    printf ("Conditions are light.\n") ;
            else if (velocity < 28 )
                    printf ("Conditions are breezy.\n") ;
            else if (velocity < 48 )
                    printf ("Conditions are gale, monitor conditions closely.\n") ;
            else if (velocity < 64 )
                    printf ("Conditions are stormy, take precautions.\n") ;
            else 
                    {
                      printf ("Hurricane conditions, seek shelter immediately. ");
                      printf ("Call 800-555-8733 for safety information ");
                      printf ("and the location of your nearest shelter.\n") ;
                    }
    
            return 0 ;
                    
    }
    
     
  4. chown33, Jan 23, 2011
    Last edited: Jan 23, 2011

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Brobdingnag
    #4
    A quoted string can't cross multiple lines.

    If you look carefully at the working examples, you'll see that neither one has a quoted string that crosses multiple lines.

    Others have already posted how to solve the problem. I've simply stated what the rule is.
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    A corollary demonstrated by dmi's example is that adjacent string literals will be concatenated for you by the compiler.

    -Lee
     
  6. dmi macrumors regular

    Joined:
    Dec 21, 2010
    #6
    or
    Code:
    		printf ("Hurricane conditions, seek shelter\n\
    		        immediately, call 800-555-8733 for safety information,\n\
    	                and the location of your nearest shelter.\n") ;
    
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Brobdingnag
    #7
    That will output the leading whitespace on the continued lines. Actual output:
    Code:
    Hurricane conditions, seek shelter
                            immediately, call 800-555-8733 for safety information,
                            and the location of your nearest shelter.
    
     
  8. dmi macrumors regular

    Joined:
    Dec 21, 2010
    #8
    Yes, which is what would have been printed if newlines were allowed in string constants, if we assume that was what was originally intended.
     

Share This Page