Variables and Identifiers in C Language

 

VARIABLE:

Variable is a named memory location in the computer to store the data.
(Or)
Variable is a data name to a data value.

 

Note: Assigning names to the data is used for further usage in the application. It is only possible to work with some kind of data when it is having unique names.

           For example int a=10, b=10;

 

  • The Programming language C has two main variable types
        1. Local Variables
        2. Global Variables

 

Local Variables:

  • If we declare any variable within the block or function called as a local variable.
  • Local variables scope is confined to 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 initialized by the system, you must initialize it yourself.
  • When execution of the block starts the variable is available, and when the block ends the variable ‘dies’.

 

Global Variables:

If we declare any variable to the outside of all the functions comes under global variable.
The global variable is defined at the top of the program file and it can be visible and modified by any function that may reference (scope) it.
Global variables are initialized automatically with some default values according to its type at the time of compilation.

 

Data Type       Default Value
int                      0
char                  ‘\0’
float                 0.000000
pointer              NULL

 

#include<stdio.h>
#include<conio.h>
int a,b;
float f;
char ch;
void main()
{
int c=10,d=20;
clrscr();
printf(“a=%d\nb=%d\nf=%f\nch=%c\nc=%d\nf=%d”,a,b,f,ch,c,d);
}

 

Rules for constructing variable names:

  • A Variable name consists of any combination of alphabets, digits, and underscores.
  • The length of the identifier may be up to 31 characters but only the first 8 characters are significant by the compiler.
  • The first character of the variable name must either be an alphabet or underscore. It should not start with the digit.
  • No commas and blanks are allowed in the variable name.
  • No special symbols other than underscore are allowed in the variable name.
  • We need to declare the type of the variable name before making use of that name in the program.
  • Lowercase variables and upper case variables of the same name treat differently (case sensitive).
  • The variable name should not match with a keyword, because keywords are the reserved words to do their own tasks such as do, while, int, for…….

 

C Identifiers:

  • “Identifiers” or “symbols” are the names you supply for variables, types, functions, and labels in your program.
  • Identifier names must differ in spelling and case from any keywords.
  • You cannot use keywords as identifiers; they are reserved for special use.
  • A special kind of identifier, called a statement label, can be used in goto statements.
  • The first character of an identifier name must be a non-digit.
Share this