Tuesday, 3 March 2015

Program for Displaying Text


The printf is the most important function used to display text on the screen. It has two parentheses which contains the string to be displayed, enclosed in quotation marks. Consider Program-2.4 given below. It displays two successive statements using the printf function.

/* Program-2.4 */

#include <stdio.h>
int main()
{
printf("Hi there");
printf("How are you?");
return 0;
}

If you compile this program and run it, the displayed output is:

Hi thereHow are you? $

How does this happens? The printf function first prints the string “Hi there”; the second time printf function starts printing the second string from next position on the screen. This can be modified by


Therefore the modified program can either be Program-2.5a or Program-2.5b.

/* Program-2.5a */

#include <stdio.h>
int main()
{
printf("Hi there ");
printf("\nHow are you?");
return 0;
}

/* Program-2.5b */

#include <stdio.h>
int main()
{
printf("Hi there\n");
printf("How are you?");
return 0;
}

Now the output is
Hi there
How are you?$

By adding another \n at then end of the second string you can move the prompt ($) to the next line.

Exercise 2.1: Write a C program to display the following text on the screen.

University of Moratuwa
Katubedda,
Moratuwa,
Sri Lanka


No comments:

Post a Comment