Thursday, July 14, 2011

C LANGUAGE GENERAL TERM

C LANGUAGE GENERAL TERM

Variable

A variable is used to hold data within your program. A variable represents a location in your computer's memory. You can put data into this location and retrieve data out of it. Every variable has two parts, a name and a data type.

Variable Names

  • Valid names can consist of letters, numbers and the underscore
  • The first character must not start with a number.
  • A variable name may not be a C keyword.
  • Variable names are case sensitive. So, Age, AGE, aGE and AgE could be names for different variables
  • No special symbols other than underscore (such as fourth_Dimension).

Keyword

Keywords are reserved identifier and they can not be used as names for the program variables or other user defined program elements. The meanings of the keywords have already been given to the compiler. The keywords are also called reserved words.

List of C Keywords

auto
double
if
static
break
else
int
struct
case
enum
long
switch
char
extern
near
typedef
const
float
register
union
continue
far
return
unsigned
default
for
short
void
do
goto
signed
while

Data Types

C provides built in data types for character, float and integer data. As an aside, in C you may assign a value to a variable when you declare it.

Integer variables

Integer variables are used to store whole numbers. There are several keywords used to declare integer variables, including int, short, long, unsigned short, unsigned long. The difference deals with the number of bytes used to store the variable in memory, long vs. short, or whether negative and positive numbers may be stored, signed vs. unsigned. These differences will be explained in more advanced tutorials. For now, use int to declare integer variables. On most 32-bit systems, int is synonymous with signed long.
Examples:
int count;
int number_of_students = 30;

Float variables

Float variables are used to store floating point numbers. Floating point numbers may contain both a whole and fractional part, for example, 52.7 or 3.33333333. There are several keywords used to declare floating point numbers in C including float, double and long double. The difference here is the number of bytes used to store the variable in memory. Double allows larger values than float. Long double allows even larger values. These differences will be explained in more advanced tutorials. For now, use float to declare floating point variables.
Examples:
float owned = 0.0;
float owed = 1234567.89;

Character variables

Character variables are used to store character values. The use of characters and strings will be covered in a latter tutorial. Character variables are declared with the keyword char.
Examples:
char firstInitial = 'J';
char secondInitial = 'K';

Datatypes in C

Data Type
Category
Range
Bytes
Format
Character
signed char
-128 to +127
1
%c
unsigned char
0 to 255
1
%c
Integer
short signed int
-32768 to +32767
2
%d
short unsigned int
0 to 65535
2
%u
signed int
-32768 to +32767
2
%d
unsigned int
0 to 65535
2
%u
long signed int
-2147483648 to +2147483647        
4
%ld
long unsigned int
0 to 4294967295
4
%lu
Float
float
-3.4e38 to +3.4e38
4
%f
Double
double
-1.7e308 to +1.7e308
8
%lf
long double
-1.7e4932 to +1.7e4932
10
%Lf

Constants

A constant is similar to a variable in the sense that it represents a memory location. It differs, as I sure you can guess, in that it cannot be reassigned a new value after initialization. In general, constants are a useful feature that can prevent program bugs and logic errors. Unintended modifications are prevented from occurring. The compiler will catch attempts to reassign new values to constants.
#include
void main()
{
       int pi=3.1415; /*constant value of pi*/
       int r=5;    /*constant value given to r varable */
       printf("Area of circle=%d",pi*r*r);
}

Using const variables

The second technique is to use the keyword const when defining a variable. When used the compiler will catch attempts to modify variables that have been declared const.
const float pi = 3.1415;
const int id_no = 12345;
There are two main advantages over the first technique. First, the type of the constant is defined. "pi" is float. "id_no" is int. This allows some type checking by the compiler. Second, these constants are variables with a definite scope. The scope of a variable relates to parts of your program in which it is defined. Some variables may exist only in certain functions or in certain blocks of code. You may want to use "id_no" in one function and a completely unrelated "id_no" in your main program. Sorry if this is confusing, the scope of variables will be covered in a latter

2 comments:

Satya J. said...
This comment has been removed by the author.
Satya J. said...

nice blog- for IT students- once take a look of Answering Machine

Post a Comment