Variables in C

Discussion in 'C' started by lionaneesh, Jun 12, 2011.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Data types are basically used to store different types of data that can be easily accessed by the program. C provides us with data-types giving us immense functionality to store different types of data in a C program . like : characters , integers , decimals , floats etc.

    Variables, Constants and Keywords in C


    As we all know Constant is a quantity which has a fixed and Variable is a quantity which do not have a fixed value. Understanding Variables, Constants and Keywords with respect to C is one of the most fundamental/basic points in learning C. You will use them in almost every useful C code you’ll write.

    C Mainly deals with 2 types of Constants and Variables
    1. Primary
    2. Secondary
    Primary

    These include set of integers, Characters, long, Floats etc.

    Secondary

    These include set of Arrays, Pointers, Structures, and Unions etc. This type will be discussed later. For the time being we’ll only discuss about Primary Constants and Variables.

    C uses some keywords to define a variable before using it.

    Almost every declaration in C follow the following pattern :-

    Code:
    keyword name [= value];
    
    ‘int’ for integer :-
    Code:
    int name = 0;
    
    ‘char’ for character
    Code:
    char name = ‘a’;
    
    ‘float’ for floating point numbers (eg: 1.01)
    Code:
    float name = 1.010;
    
    ‘double’ for large floating point digits
    Code:
    double name = 0.10122234;
    
    ‘long’ for large integers
    Code:
     long name = 1111110;
    
    To declare a constant in C simply add the keyword ‘const’ followed by variable type and name.
    Code:
     const type name = 100; 
    
    Note: The main difference between constants and variables in c is that variables can be changed after initialization while constants cannot be changed once initialized.

    One more way of declaring constants is using the ‘#define’ preprocessor which has a advantage that it does not includes any storage.

    That’s all for this article stay tuned for more.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice