Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

Data types and Variables

Posted By: Adelaide Miller     Category: C Programming     Views: 4160

This article explains about variable, data types, size and range of data types and examples of it in C programming.

Variables

  • A variable is a data name that may to used to store a data value
  • It may assume different values throughout the execution of the program
  • The variable name should be chosen in a meaningful way by the programmer
  • Ex. Average, height, Total, Counter_1, Class_Strength


Rules for Valid Variable Names

  • It must begin with a letter, some systems allow _ as well
  • ANSI standard recognizes first 31 chars, but for many compilers it is just 8
  • C is case sensitive. I.e. Total is not same as total or TOTAL
  • The variable name must not be a keyword
  • White space is not allowed

Examples of Variable Names

 Valid

 Invalid

 John

 Value

 123

 Price$

 Delhi

 x1

 %

 group one

 T_raise

 mark

 (area)

 

 ph_value

 sum1

 25th

 

 distance

 int_type

 char

 


ANSI C Data Types

  • It supports four classes of datatypes
    • Primary (or fundamental) data types with their extensions
    • Ex. integer (int), character (char), floating point(float) and double (precession floating pt)
    • User-defined data types
    • Derived data types(array, record etc)
    • Empty data set

Primary Data Types in C

 Integral type

 Integer

 Character

 Signed Type

 Unsigned Type

 Signed char

 int

 unsigned int

 unsigned char

 Short int

 unsigned short

 

 long int

 unsigned long

 

 Floating Point Type

 float

 double

 long double


Size and Range of Basic Data Types

 Data Type

 Range of Values

 char or signed char

 -128 to 127

 unsigned char

 0 to 255

 int or signed int

 -32768 to 32767

 unsigned int

 0 to 65536

 long int

 -2,147483,648 to + ..47

 float

 3.4e-38 to 3.4e+38

 double

 1.7e-308 to 1.7e+308


Floating Point Types

  • They are stored in 32 bits in all 16 or 32 bit machines with 6 digits of precision
  • Double provides more accuracy than float
  • It uses 64 bits giving a precision of 14 digits
  • It represents the same data type as float but with double precision, hence double
  • To extend the precision further, one may use long double with 80 bits

Declaration of Variables

  • We must declare variable before using
  • Declaration does two things
    • It tells compiler what the variable name is
    • It specifies what type of data will be hold by the variable
    • It, therefore, makes the compiler to reserve some memory space for that variable, wherein the data hold by the variable will be stored


Primary Type Declaration

  • The syntax of declaring variable is as follows
 data-type v1,v2,…,vn ;
 examples 
 int count;
 int number, total;
 double ratio;
 char c;
 long l;

User Defined Type Declaration

  • C allows users to define an identifier that would represent an existing data type
  • It takes the general form
 typedef   type  identifier


Examples are 

 typedef   int units;
 typedef   float marks;
 units batch1, batch2;
 marks name1[50], name2[50]

Enumerated Data Type

 enum identifier {val1, val2, …,valn};
 enum day {Monday, Tuesday, .., Sunday};
 enum day week_st, week_end;
 week_st = Monday;
 week_end = Friday;
 if (week_st == Tuesday) 
      week_end = Saturday
 enum day (Monday=1, Tuesday, ..,Sunday};
enum day {Monday, Tuesday, .., Sunday} week_st;

Storage Classes

  • auto : Local variable known to only to the function in which they are declared. Default is auto
  • static : Local variables which exists and retains its value even after the control passed back to calling function
  • extern : global variable declared elsewhere
  • register : local var which is stored in regs  

Assigning Values to Variables

  • Variable can be used only after values assigned to them
  • They carry garbage values when not assigned any values except global and static
  • Ex. Value = 25; amount = 12.5, discount = value * amount / 300; year = year + 1;
  • The “lvalue required”  message!
  •  int j = 20; p = q = r = s = 10;

Declaring As Const or Volatile

  • const int class_size = 40;
  • const is a new class identifier added by ANSI
  • volatile int date; (it indicates that the value of variable can be changed at any time by some external sources)
  • So, the value of variable may be changed when not on the RHS of the assignment
  • volatile const int location =100;

Overflow and Underflow of Data

  • Value of variable either too big or too small for the data type to hold
  • The largest value that a variable can hold also depends on the machine
  • Floating point values are rounded off to the number of significant digits allowed
  • Integers usually produce negative results when overflowed

Examples of Valid Define Statements

 Statement

 Validity 

 Remark

 #define X = 25

 Invalid

 = sign not allowed

 #define M 10

 Invalid

Space not allowed 

 #define N 25;

 Invalid

 ; not allowed

 #define N 5, M 2

 Invalid

 Only one in statement

 #Define A 10 

 Invalid

 D is not valid

 #define price$ 4

 Invalid

 $ is not permitted


  
Share: 

 
 

Didn't find what you were looking for? Find more on Data types and Variables Or get search suggestion and latest updates.

Adelaide Miller
Adelaide Miller author of Data types and Variables is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!