Types of C Variables

  • An entity that may vary during program execution is called a variable.
  • Variable names are names given to locations in memory. These locations can contain integer, real or character constants.
  • In any language, the types of variables that it can support depend on the types of constants that it can handle. This is because a particular type of variable can hold only the same type of constant. For example, an integer variable can hold only an integer constant, a real variable can hold only a real constant and a character variable can hold only a character constant.
  • The rules for constructing different types of constants are different. However, for constructing variable names of all types the same set of rules apply.

Rules for Constructing Variable Names

  • A variable name is any combination of 1 to 31 alphabets, digits or underscores. Some compilers allow variable names whose length could be up to 247 characters. Still, it would be safer to stick to the rule of 31 characters. Do not create unnecessarily long variable names as it adds to your typing effort.
  • The first character in the variable name must be an alphabet or underscore.
  • No commas or blanks are allowed within a variable name.
  • No special symbol other than an underscore (as in gross_sal) can be used in a variable name.

C compiler is able to distinguish between the variable names by making it compulsory for you to declare the type of any variable name that you wish to use in a program. This type declaration is done at the beginning of the program. Following are the examples of type declaration statements:

For Example:

int v, hra, satya;

float test;

char p;

Since, the maximum allowable length of a variable name is 31 characters, an enormous number of variable names can be constructed using the above-mentioned rules.

Best practices to define variable name:

It is a good practice to exploit this enormous choice in naming variables by using meaningful variable names. Thus, if we want to calculate simple interest, it is always advisable to construct meaningful variable names like prin, roi, noy to represent Principle, Rate of interest and Number of years rather than using the variables a, b, c.

Related Posts

  • 72
    Welcome to the C Knowledge Base and Tutorial   Introduction to C Programming Start Learning C Programming What is C? Why We should learn C C Character Set : Constants, Variables and Keywords Constant and Variable in C: C Keywords: Types of C Constant Rules for Constructing Integer Constants Rules…
    Tags: variable, types, tutorial, names, learn
  • 49
    The three primary constants and variable types in C are integer, float and character. A variable name can be of maximum 31 characters. Do not use a keyword as a variable name. An expression may contain any sequence of constants, variables and operators. Operators having equal precedence are evaluated using…
    Tags: variable, types, learn, tutorial
  • 44
    What are functions? Functions are a convenient way to divide your code into useful blocks, allowing us to order our code, make it more readable, reuse it and save some time. Functions are a key way to define interfaces so programmers can share their code. Function is a block of…
    Tags: tutorial, learn, define
  • 37
    A character denotes any alphabet, digit or special symbol used to represent information. Here is the list of valid character set (Alphabets, Numbers and Symbols) allowed in C. The alphabets, numbers and special symbols when properly combined form constants, variables and keywords. Let us see what are ‘constants’ and ‘variables’…
    Tags: variable, names, tutorial, learn
  • 35
    The if statement can take any of the following forms: if ( condition ) do this ; if ( condition ) { do this ; and this ; } if ( condition ) do this ; else do this ; if ( condition ) { do this ; and this…
    Tags: learn, tutorial

Satya Prakash

VOIP Expert: More than 8 years of experience in Asterisk Development and Call Center operation Management. Unique Combination of Skill Set as IT, Analytics and operation management.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: