Friday, November 22, 2013

Examples For Input And Output Through Programming

Now lets look at few fairly simple examples. Let us read an input from user and show what the user has entered in the next line

For Integer

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

void main()
{
int entered;
printf("Enter An Integer :")
scanf("%d",&entered);
printf("\n The Entered Integer Is %d",entered);
getch();
}
 

For Floating

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

void main()
{
float entered;
printf("Enter An Floating Number :")
scanf("%f",&entered);
printf("\n The Entered Float Is %f",entered);
getch();
}



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

void main()
{
char entered;
printf("Enter An Character :")
scanf("%c",&entered);
printf("\n The Entered Character Is %c",entered);
getch();
}


Try these out & also try to read two or more variables in a single scanf statement .Like reading and an integer and a floating in the same program and displaying both at once. We will look at this in the next post.....


Saturday, November 2, 2013

Input & Output Through Programming

Ok now we have some idea on how to save numbers in C . Now lets look at how we can get input data from the user and how we can give the user the output . The stdio.h gives us few functions to accomplish this task.
1. printf()
2. scanf()

The printf function allows us to print data on the screen so the user can see it while the scanf is used to get the data from the keyboard.

In Detail
 Printf ()

Syntax : printf("The Data You Want To Print :placeholder", argument list);
 Example: printf("The Sum Is %d",c)

Now lets break down the syntax for printf . The stuff within the quotes gets printed as it is . The placeholder is used in place where you want the variables to appear in the screen it can be between the the line or at the end . This is followed by the argument list that is the list of variables which will appear on the screen in place of the placeholders (Thats what placeholders are for :P ).
What are placeholders???
Relax we have already looked at them . You will understand by just looking at the example of placeholder  . Example of placeholder %d,%f ,%c, %s .

Few Examples Of printf Statements
printf("The Key You Entered Is %c ",key);
printf("The Sum Of %d + %d = %d",a,b,sum);
printf(" %f x %f = %f  \n",a,b,product );

We can also include few things like \n ,\t which are special . \n is used when you want a new line
\t is used for tab space and etc . The are used as placeholders for formatting .
 So printf is a type of  formatted output .

Scanf()

Syntax : scanf("Placeholder ", address list);
 Example: scanf("%d",&a);

Now breaking down the scanf statement. Its very similar to the printf but here one thing we need to note is we use the address list instead of argument list. Now what is address list ?? Its simple instead of just writing the variable name we just add a & symbol to point to its address.

the placeholder is used to denote what type of data is being read through the keyboard. The address list is used to store the data coming from the keyboard to the variable . For example
scanf("%d",&a);
The statement above the data type read is int and the data is stored in the address of variable a . So next when we use a in the program the data in the location is used.

Few Examples Of scanf Statements

scanf("%d %d ",&a,&b);
scanf("%f %f  %f",%a,&b,&c);
scanf("%d%f %c",&integer,&float,&character);


This ends the post on getting data from the user and showing him the result