variables in C

Variables in C are memory locations that are given names and can be assigned values. We use variables to store data in memory for later use...


Variables in C are memory locations that are given names and can be assigned values. We use variables to store data in memory for later use. There are 2 basic kinds of variables in C which are numeric and character.
(1)Numeric variables

Numeric variables can either be integer values or they can be Real values. Integer values are whole numbers without a fraction part or decimal point in them. Real numbers can have a decimal point in them.
(2)Character variables

Character variables are letters of the alphabet as well as all characters on the ASCII chart and even the numbers 0 - 9. Characters must always be put between single quotes. A number put between single quotes is not the same thing as a number without them.
What are constants

The difference between variables and constants is that variables can change their value at any time but constants can never change their value. Constants can be useful for items such as Pi or the charge on an electron. Using constants can stop you from changing the value of an item by mistake.
Declaring variables

To declare a variable we first put the type of variable and then give the variable a name. The following is a table of the names of the types of variables as well as their ranges:
Name
Type
Range
int
Numeric - Integer
-32 768 to 32 767
short
Numeric - Integer
-32 768 to 32 767
long
Numeric - Integer
-2 147 483 648 to 2 147 483 647
float
Numeric - Real
1.2 X 10-38 to 3.4 X 1038
double
Numeric - Real
2.2 X 10-308 to 1.8 X 10308
char
Character
All ASCII characters
You can name a variable anything you like as long as it includes only letters, numbers or underscores and does not start with a number. It is a good idea to keep your variable names less than 32 characters long to save time on typing them out and for compiler compatibility reasons. Variables must always be declared at the top before any other commands are used. Now let's declare an integer variable called a and a character variable called b.
int main()
{
   int a;
   char b;
   return 0;
}
You can declare more than one variable at the same time in the following way:
int main()
{
   int a,b,c;
   return 0;
}
To declare a constant all you have to do it put the word const in front of a normal variable declaration and make assign a value to it.
int main()
{
   const float pi = 3.14;
   return 0;
}

Signed and unsigned variables

The difference between signed and unsigned variables is that signed variables can be either negative or positive but unsigned variables can only be positive. By using an unsigned variable you can increase the maximum positive range. When you declare a variable in the normal way it is automatically a signed variable. To declare an unsigned variable you just put the word unsigned before your variable declaration or signed for a signed variable although there is no reason to declare a variable as signed since they already are.
int main()
{
   unsigned int a;
   signed int b;
   return 0;
}

Using variables in calculations

To assign a value to a variable you use the equals sign.
int main()
{
   int a;
   char b;
   a = 3;
   b = 'H';
   return 0;
}
There are a few different operators that can be used when performing calculations which are listed in the following table:
Operator
Operation
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Modulus(Remainder of integer division)
To perform a calculation you need to have a variable to put the answer into. You can also use both variables and normal numbers in calculations.
int main()
{
   int a,b;
   a = 5;
   b = a + 3;
   a = a - 3;
   return 0;
}

Reading and printing variables

You can read a variable from the keyboard with the scanf command and print a variable with the printf command.
#include<stdio.h>

int main()
{
   int a;
   scanf("%d",&a);
   a = a * 2;
   printf("The answer is %d",a);
   return 0;
}
The %d is for reading or printing integer values and there are others as shown in the following table:
%d or %i
int
%c
char
%f
float
%lf
double
%s
string

Related

C TUTORIAL 9214005631604191819

Post a Comment

emo-but-icon

Search Here

Popular query

Follow Us

Ads By Google

Get free Update

Enter your email address:

E-mail verification is must for complete subscription

Delivered by FeedBurner

Circle AFS on Google Plus!

Follow AFS Google+ page
 

Side Ads

DMCA protected
Information, images and the content on this blog is Copyright ©AFS2011-2018. Please do not copy Any content for commercial purpose else we have to take a legal action. Thanks !!

Total Pageviews

Recent

free counters
 

Connect Us

Speech by ReadSpeaker

item