Constant, Variable and Comment by G Krishna Chauhan

C - Constant:

C Constants are also like normal variables. But, only difference is, their values can not be modified by the program once they are defined.
Constants refer to fixed values. They are also called as literals
Constants may be belonging to any of the data type.

Syntax:
       const data_type variable_name; (or) const data_type *variable_name;

TYPES OF C CONSTANT:

  1. Integer constants
  2. Real or Floating point constants
  3. Octal & Hexadecimal constants
  4. Character constants
  5. String constants
  6. Backslash character constants


Constant types with its data type (Example):

Integer constants: 
int (53, 762, -478 etc ) unsigned int (5000u, 1000U etc) long int, long long int (483,647 2,147,483,680)

Real or Floating point constants:
float (10.456789) doule (600.123456789)

Octal constant:
int (Example: 013 /*starts with 0 */)

Hexadecimal constant:
int (Example: 0x90 /*starts with 0x*/)

character constant:
char (Example: ‘A’, ‘B’, ‘C’)

string constants:
char (Example: “ABCD”, “Hai”)


RULES FOR CONSTRUCTING C CONSTANT:

1. INTEGER CONSTANTS IN C:


  • An integer constant must have at least one digit.
  • It must not have a decimal point.
  • It can either be positive or negative.
  • No commas or blanks are allowed within an integer constant.
  • If no sign precedes an integer constant, it is assumed to be positive.
  • The allowable range for integer constants is -32768 to 32767.


2. REAL CONSTANTS IN C:


  • A real constant must have at least one digit
  • It must have a decimal point
  • It could be either positive or negative
  • If no sign precedes an integer constant, it is assumed to be positive.
  • No commas or blanks are allowed within a real constant.


3. CHARACTER AND STRING CONSTANTS IN C:


  • A character constant is a single alphabet, a single digit or a single special symbol enclosed within single quotes.
  • The maximum length of a character constant is 1 character.
  • String constants are  enclosed within double quotes.


4. BACKSLASH CHARACTER CONSTANTS IN C:


  • There are some characters which have special meaning in C language.
  • They should be preceded by backslash symbol to make use of special function of them.
  • Given below is the list of special characters and their purpose.

  • \b Backspace
  • \f Form feed
  • \n New line
  • \r Carriage return
  • \t Horizontal tab
  • \” Double quote
  • \’ Single quote
  • \\ Backslash
  • \v Vertical tab
  • \a Alert or bell
  • \? Question mark
  • \N Octal constant (N is an octal constant)
  • \XN Hexadecimal constant (N – hex.dcml cnst)

Variable:


In programming, a variable is a container (storage area) to hold data.
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 playerScore = 95;
Here, playerScore is a variable of integer type. The variable is assigned value: 95.

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.
  • 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.


Comment:

In computer programming, a comment is a programming language construct used to embed programmer-readable annotations in the source code of a computer program. Those annotations are potentially significant to programmers but are generally ignored by compilers and interpreters.


There are 2 different types of comments in C.

These include

1. Single Line Comment:

The single line comment is specified using the symbol //

Eg:
// this is a AbundantCode comment

2. Multi Line Comment:
The Multiline comment can be specified using the symbol /* */

Eg:

/* This is a test
comment */

No comments:

Post a Comment