variables, constant and data types in c programming

variables in c | constant in c | data types in c | data types | c programming | about c | c | c language

Variables in c programming.


  • In programming, a variable is a container to hold data or value.
  • To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the symbolic representation of a memory location. For example:int num1=12;Here, num is a variable of integer type. The variable is assigned value: 12.
  • The value of a variable can be changed, hence the name 'variable'.
  • In C programming, you have to declare a variable before you can use it.

Rules for naming a variable in C


  • A variable name can have letters (both uppercase and lowercase letters), digits and underscore only.ex. int num;sum etc
  • The first letter of a variable should be either a letter or an underscore. However, it is discouraged to start variable name with an underscore. It is because variable name that starts with an underscore can conflict with system name and may cause error.
  • There is no rule on how long a variable can be. However, only the first 31 characters of a variable are checked by the compiler. So, the first 31 letters of two variables in a program should be different.
  • C is a strongly typed language. What this means it that, the type of a variable cannot be changed.
  • suppose we declare variable of type integer then it can store only integer value.

variable is considered as one of the building block of c programming.
a variable is a name given to the memory location where the actual data is stored. 

 types of variable 

  1 local variable
  2 global variable

 1. local variable 


  •   Local variable is a variable having local scope.
  •   Local variable is accesible only from function or block in which is declared.
  •   Local variable is given higher priority than global variable.
  •   Local variables scope is confined within the block or function where it is defined.
  •  Local variables must always be defined at the top of a block.
  • When a local variable is defined - it is not initalised by the system, you must initalise it yourself.
  • When execution of the block starts the variable is available, and when the block ends the variable 'dies'.

  2.global variable


  • global variable is variable that is globally declared.
  • scope of global variable is in all function including in main().
  • global variable can be accessed from any function.
  • Global variable is defined at the top of the program file and it can be visible and modified by any function that may reference it.
  • Global variables are initalised automatically by the system when you define them!
           Data Type Initialser
            int 0
            char '\0'
            float 0
            pointer NULL

  • If same variable name is being used for global and local variable then local variable takes preference in its scope. But it is not a good practice to use global variables and local variables with the same name.

Constants in c language


  • A constant is a value or an identifier whose value cannot be altered in a program. For example: 1, 2.5, "number is this", etc.
  • As mentioned, an identifier also can be defined as a constant.
  • const double PI = 3.14
  • n C programming, variables or memory locations should be declared before it can be used. Similarly, a function also needs to be declared before use.

datatypes n c language



  • Data types simply refers to the type and size of data associated with variables and functions.

    Fundamental Data Types



  • Integer types
  • Floating type
  • Character type

    Derived Data Types


  • Arrays
  • Pointers
  • Structures
  • Enumeration


No comments:

Post a Comment