C Programming Constants and Variables and Naming Rules for variables

Posted by

C Programming Constants and Variables and Naming Rules for variables

In this article of PodinaTutorials.com, we are going to talk about C Programming Constants and Variables and Naming Rules for variables. So let us discuss all the detail in the below article.

C++ Constant and Variables

Variables and Constants are the important components of every programing language.

Constants

Constants are the expressions which have fixed value. Constants are generally categorized into Literal, Boolean, and Symbolic constants. They are used to express particular values within the source code of a program.

Consider the following line of code:

VarA = 555;

In this line of code, 555 is a literal constant. The literal constants are further categorized into the following types.

  • String constants
  • Numeric constants
  • Character constants

String constants

A string constant is a sequence of characters enclosed in double quotation marks is called string constant. Its maximum length is 256 characters. Following are some examples of valid string constants.

“Welcome to the first C++ Program”

“The result=”

Numeric constants

Numeric constants consist of positive or negative signed numerals. There are four types of numeric constants. These are:

  • Integer constant
  • Floating point constant
  • Hexadecimal constant
  • Octal constant

Integer constants

Integer constants are those positives or negative signed numbers that do not contain a decimal point e.g. 2010, -321 etc

Floating point constants

The numeric constants having a decimal point are called floating point constants. E.g. 33.55 and -0.22 etc. These constants can also be either positive signed or negative signed. These constants can also be represented in its exponential form by the use of alphabet ‘e’ of ‘E’ to denote the exponents of the numbers. For example.

9010E10
7810.11E-11
-10.990e8
-1.001e-1

Hexadecimal constants

The constants represented in hexadecimal number system are called hexadecimal constants. To represent a hexadecimal constant, begin the constant with 0x of 0X, followed by a sequence of digits in the range 0 through 9 and f (or A through F). the digits a to f (or A to F) represent values in the range 10 through 15. For example.

int I = 0x3fff; // Hexadecimal constant

int j = 0X3FFF; // Hexadecimal constant

Octal constants

The representation of constants in octal number system is called octal constants. To specify an octal constants, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. Consider the following example:

int I = 0377; // Octal constant

int j = 0397; // Error: 9 is not an octal digit

Octal numbers are integer numbers of base 8 and their digits are 0 to 7.

Character constants

A character enclosed within single quotes is called character constant. Consider the following examples:

'A', 'a', '.', '?'

C Programming Constants and Variables and Naming Rules for variables

Variables

A variable in C++ is a name for a piece of memory that can be used to store information. A variable can be thought as a mailbox where information can be put and retrieved from. All computers have memory, called Random Access Memory (RAM) that is available for programs to use. When a variable is declared, a piece of that memory is set aside for that variable.

Consider the following code example:

int  a = 5;

int b = 2;

int result = a – b;

In this segment, a, b and result are the integer variables that reside in RAM and store integer values.

Naming Rules for variables

The symbol used for a variable is called an identifier or variable name. a set of rules are used for naming variables. These rules are:

  • Valid identifier is a sequence of one or more letters, digits or underscores (_) character.
  • Neither spaces nor punctuation marks or symbols can be part of an identifier. Only letters, digits and single underscore characters are valid.
  • Variable identifiers always have to begin with a letter. They can also begin with an underscore (_) character.
  • In no case, a variable can begin with a digit.
  • A Reserved word of the C++ language cannot be used as an identifier.
  • Names should be meaningful.
  • Names should not be too long.

C++ language is a “case sensitive” language which means that an identifier written in capital letters is not equivalent to the one written in small letters.

 

Also Read  What is Header files and Reserved words in C++

Keyword: C Programming Constants and Variables and Naming rules for variables

2 comments

Leave a Reply

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