Friday 3 October 2014

Method of Output

Writing Output Data-

Output data can be written from computer memory to the standard output device(monitor) using printf() library function. Output is the method to display the massage or input variable on the screen so that user cab see our program result, using printf() function.

There are two way to using printf() function in C language-
first way when we want to display only information and massage then we use the formate-

Syntax- printf(" write massage here:");
E.g.

printf('' Hi! I am sudhir Singh:");

When we want only display the massage in the program, then we write the massage in printf function in double quotes as above in the example......

the second way we want display the value of variable that is hold at the time of input means through the scanf() function. when we want to display the value of variable then use control string as used in scanf() function but major different is,in the printf function we did not use & sign with the variable because & sign used for create memory but here use only display the value of variable so only use the variable in the printf function. 

Syntax- printf("control string",variable 1, variable 2,.........);

e.g.

printf("%d%d", a,b);
here two variable a and b that is display the variables value, we did not use the & sign with the variable a and b. when we display the information then conversion specification is not used.

Example :

#include<stdio.h>
main()
{
int age;
printf("Enter any age for any one");
scanf("%d",&age);
printf("%d",age);


*******************************************************
#include<stdio.h>
main()
{
int basic=2222;
.................
printf("%d",basic);
.................
}

in this example control string contains a conversion specification %d which implies that an integer value will be displayed. The variable basic has that integer value which will be displayed as output.


*******************************************************************************
#inlude<stdio.h>
main()
{
float height=5.6;
printf("%f",height);
}

Here control string has conversion specification character %f, which means that floating point number will be displayed. The variable height has that floating point value will be displayed as output .

**************************************************************
#include<stdio.h>
main()
{
char ch='$';
printf("%c",ch);
}

In this example the control string has conversion specification character %c means that a single character will be displayed and variable ch has that character value.


**************************************************************
#include<stdio.h>
main()
{
int b=500;
float h=200.50;
char g='A';
printf("basic =%d\n hra=%f\n grade=%c\n", b,h,g);
}

Output

Basic=500
hra  = 200.50
grade=A

in this example we print the massage and value together format is above in the program, in the printf function '\n' used, '\n moves the cursor to the beginning of next line.Here we have placed a '\n' at the end of control string also. it ensure that the output of the next program starts at a new line.

**************************************************************



#include<stdio.h>
main()
{
int b=500;
float h=200.50;
char g='A';
printf("basic =%d\t hra=%f\t grade=%c\n", b,h,g);
}

Output

Basic=500     hra=200.50     g=A

in this example '\t' moves the cursor to the next tab stop.
for example we can use: \b moves the cursor one position back, \r moves the cursor to the beginning of the current line and \a alert user by a beep sound.\v moves the cursor to the next vertical tab position  and \f moves the cursor to the next page.
If we want to print character like single quote(') and double quote (") or the backslash character (\), then we have to precede them by a backslash character in the format string.

For example-

printf("10\\ 04 \\1992");       then print         10 \ 04 \1992

printf(" she said,\" sudhir mad\"."); then    she said,"sudhir mad".







No comments:

Post a Comment