Sunday 26 August 2012

Chapter 2 - Formatting the Text


In our previous chapter we wrote a program to print a line on the output screen which read
"This is my first program"
So now moving a step ahead lets try to figure out how to write two lines which will look like this.
"This is the first line"
"This is the second line" 
Any suggestions on how u think it can be done?
You may think of using two lines of printf(). So now the code looks like this :

   /*   
    Title: simple code that prints two lines.   
    Author: Ryan Sequeira   
    Created on: 25th August 2012   
   */   

   #include<stdio.h>   
   #include<conio.h>   

   void main()   
    {   
     // no need for local variables   
     clrscr();   
     printf("This is the first line");   
     printf("This is the second line");   
     getch();  
    }  

Try executing this code before reading further and see what actually happens.

So it didn't turn out as you expected. The problem here is the compiler doesn't understand when it has to insert a new line(even if you use separate printf's). It prints it as a single sentence from left to right and then top to bottom in a new line.

To get the the expected result you have to tell the compiler explicitly to insert a line break where ever you need it. So now here is the magic code that will give u the expected result.

    printf("This is the first line");   
    printf("\nThis is the second line");  

Yes the '\n' is the secret word that tells the compiler to insert a line break and hence print the next sentence in a new line. If you want you can use a single printf statement to reduce the size of the code.

 printf("This is the first line \nThis is the second line");   

Now try to implement the changes in the previous code and compare the results with what is expected.
If you are not able to figure it out, i have written the expected code below.

/*   
   Title: simple code that prints two lines.   
   Author: Ryan Sequeira   
   Created on: 25th August 2012   
*/
   
#include<stdio.h>   
#include<conio.h>   

void main()   
 {   
  // no need for local variables   
  clrscr();   
  printf("This is the first line");   
  printf("\nThis is the second line");   
  getch();   
 }   

This is not all, there are a few surprises left. Now that you know how to insert a new like there are other formatting options too.

  • \n     new line
  • \t      horizontal tab
  • \r      carriage return
  • \"      double quote
  • \\      backslash
  • \a     alert (BEL)
  • \b     backspace
Although there are a lot of options to format the text, I would advice you to only use the two of them highlighted(\n and \t).
So now that you are comfortable with the use of \n(line break) in the sentences we will introduce \t(tabs) in our sentences. Example: Try to write a code on your own to print the following paragraph, with the tabs and line breaks
                  This is the first line.
                           This is the second line.
                                    This is the third line.
Hint: The number of tabs increase every new line. If you managed to write your own code and want to compare it with the expected code, or if you didn't get the solution then check the ideal code below.
/*   
 Title: simple code that prints two lines.   
 Author: Ryan Sequeira   
 Created on: 25th August 2012   
*/   

#include<stdio.h>
#include<conio.h>   

void main()   
 {   
  // no need for local variables   
  clrscr();   
  printf("This is the first line");   
  printf("\n\tThis is the second line");   
  printf("\n\t\t This is the third line");      
  getch();   
 }    

Activities
  1. Try to pick any paragraph from your book and then print it with all the tabs and line breaks.
  2. Try to print the following design.(Solution)

                                                                 
                     *
             *
                     *
                             *



No comments:

Post a Comment