Pages

Wednesday, 12 October 2016

Variable Input Output C Language

c-language-variable-input-o

As you have in previous chapter what is a variable now I am going to demonstrate that how to declare and read/write into a variable.
Declaration syntax

Declare single variable

Data_type Variable_name;

Declare more than one variable

Data_type Var_name1,var_name2…var_nameN

Declare and initialize

Data_type var_name1=val1,var_name2=val2…var_nameN=valM

Variable Declaration

When you make a variable. It’s known as declaration in terms of C language. Declaration request OS for claiming space on physical memory.

Initialization

First value assign to a variable after declaration. What if you didn’t initialize a variable then an unpredicted value assigned which is known as garbage so initialization   is mandatory for denied facing socking result.

Garbage

Unpredicted value when a variable has got declared. As we know the entire program in computer must be store into memory (RAM) before get run. So when you declare a variable it takes place on RAM in the form of bits and every bit on RAM must have some garbage data renounce by previous program. So we need to set some initial value like 0 before using if not result known as garbage.
C language garbage value

Output function

Printf
Syntax
Printf(“String format”,[var_list]);

Input function

Scanf
syntax
Scanf(“format string”,[&var_list’]);
Input into a single variable
void main()
{
     int a;
     printf("Enter a number>> ");
     scanf("%d",&a);
     printf("\n\tA=%",a);
}       
Input muliple data into single scanf
void main()
{
     int a,b;
     printf("Enter a first number>> ");
     scanf("%d",&a);
     printf("Enter a second number>> ");
     scanf("%d",&b);
     printf("\n\tA=%d\n\tB=%d",a,b);
     /* Input into single scanf */
     printf("Enter Two number>> ");
     scanf("%d%d",&a,&b);
}
Input multiple data type
void main()
{
     int a;
     float c;
     char b;
     printf("Enter a interger number>> ");
     scanf("%d",&a);
     printf("Enter a Decimal number>> ");
     scanf("%f",&b);
     /* clear leyboard buffer */
     fllush(stdin);
     printf("Enter a character>> ");
     scanf("%c",&c);
     printf("\n\tInteger\t%d\n\tFloat%f\n\tCharacter\t%c",a,b,c);
}

                                           

Download PDF
Download C Source Code
Next Chapter

No comments:

Post a Comment