You are on page 1of 9

Rules for variable name in C:

1. A Variable name consists of any combination of alphabets, digits and underscores. Some
compiler allows variable names whole length could be up to 247 characters. Still it would be
safer to stick to the rule of 31 characters. Please avoid creating long variable name as it adds to
your typing effort
2. The first character of the variable name must either be alphabet or underscore. It should not start
with the digit
3. No commas and blanks are allowed in the variable name
4. It is case sensitive language.
5. 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.
Type declaration can be done as follows:
To declare a variable as integer, follow the below syntax:
int variable_name;
Here int is the type of the variable named variable_name. int denotes integer type.
Following are the examples of type declaration statements:
int p, n;
float r;

Rules for variable name in C++:

Variable names in Visual C++ can range from 1 to 255 characters. To make
variable names portable to other environments stay within a 1 to 31 character range.
All variable names must begin with a letter of the alphabet or an
underscore( _ ). For beginning programmers, it may be easier to begin all variable
names with a letter of the alphabet.
After the first initial letter, variable names can also contain letters and
numbers. No spaces or special characters, however, are allowed.
Uppercase characters are distinct from lowercase characters. Using all uppercase
letters is used primarily to identify constant variables.
You cannot use a C++ keyword (reserved word) as a variable name.
It is case sensitive language.

Rules for variable name in cobol:
Variables is declared in the working-storage section of the data division. Most of the
rules apply equally when declaring records in the file section and in the linkage
section. Cobol is not the case sensitive language.


Each entry in a DATA DIVISION starts with a level number, followed by a variable
name, followed by a description of the data type, e.g.


77 CUSTOMER-NAME PIC X(30).
The variable is called CUSTOMER-NAME and is an alphanumeric field containing
30 characters. Note the hyphen in the name: COBOL does not usually allow you to
use an underline "_". An alphanumeric variable will always contain the number of
characters it has been defined with, i.e. not the actual length of the variable as in
Pascal or C++. Level number 77 here means that the variable is not connected to other
variables.
An introduction to level numbers.
[This chapter] [TOC] [Tutorial]

A level number is placed in front of every variable. The level number is used to
specify the variables relation to surrounding variables.

Numbers 1-49 are used when defining records and will be dealt with in a later chapter:
"Data definitions in COBOL (2)".

All subsequent numbers are to be placed in Column A and are used to define variables
that are not connected to other variables.

66 Used when redefining the data type of a prior variable.

77 Variable.

78 A Micro Focus speciality. The same as 77, but with the added element that the
field is a named constant which cannot alter its value while the program is being run.

88 Used to define symbolic conditions. More on this later.
An introduction to data types.


See the manual for actual selection of data types. However, the following are sure to
be among the options available:


X(n)
An alphanumeric field with space for n characters.

S9(n1)V9(n2)
A calculation field with sign, space for n1 characters before the decimal point and n2
characters after. Note that the field does not contain a decimal point. The decimal
point is represented by the "V".

--.---.--9,99
--.---.--9vbb99
A numeric edited field which fills 13 print positions.

The "V" represents the position of the decimal point here too (for example, on giro
payment slips). "b" is used to indicate space characters in the output. If the figure is
negative there will be a minus sign in front of the first significant digit; leading zeros
are replaced by blanks. Commas for thousands, etc., will only be displayed if the
figure is larger than (+/-)999 or (+/-)999999.

After this PICTURE mask, it is possible to add an internal format, e.g. DISPLAY or
COMP. Alphanumeric fields or numeric-edited fields always use the DISPLAY
format.
An example:


DATA DIVISION.
WORKING-STORAGE SECTION.
77 ENTERED-VALUE PIC 999.
77 INTERNAL-VALUE PIC S999 COMP.
..

PROCEDURE DIVISION.
...
MOVE ENTERED-VALUE TO INTERNAL-VALUE.
...
An explanation of the example:

The two variables are, respectively, a text field (that may only contain three-character
figures) and a binary numeric variable (value set: -999 to 999). The MOVE statement
copies the contents of ENTERED-VALUE to INTERNAL-VALUE; at the same time
the contents are converted from ASCII to binary.

Rules for variable name Fortran:
Variable names in Fortran consist of 1-6 characters chosen from the letters a-z and the
digits 0-9. The first character must be a letter. Fortran 77 does not distinguish between
upper and lower case, in fact, it assumes all input is upper case. However, nearly all
Fortran 77 compilers will accept lower case. If you should ever encounter a Fortran 77
compiler that insists on upper case it is usually easy to convert the source code to all
upper case.Fortran is a case sensitive language.
The words which make up the Fortran language are called reserved words and cannot
be used as names of variable. Some of the reserved words which we have seen so far
are "program", "real", "stop" and "end".
Types and declarations
Every variable should be defined in a declaration. This establishes the type of the
variable. The most common declarations are:
integer list of variables
real list of variables
double precision list of variables
complex list of variables
logical list of variables
character list of variables
The list of variables should consist of variable names separated by commas. Each
variable should be declared exactly once. If a variable is undeclared, Fortran 77 uses a
set of implicit rules to establish the type. This means all variables starting with the
letters i-n are integers and all others are real. Many old Fortran 77 programs uses
these implicit rules, but you should not! The probability of errors in your program
grows dramatically if you do not consistently declare your variables.

Rules for variable name in PHP:
A variable starts with the $ sign, followed by the name of the variable
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-
9, and _ )
Variable names are case sensitive ($y and $Y are two different variables)
PHP is the case sensitive language.

Rules for variable name in LISP:
1. You should use lower case. You should follow the rules for Spelling and Abbreviations You
should follow punctuation conventions.
2. Name your variables according to their intent, not their content.
3. Name globals according to convention.
4. Names of predicate functions and variables end with a "P".
5. You should not include a library or package name as a prefix within the name of symbols.
6. Use packages appropriately.
7. LISP is the case sensitive language.



Task 2:
How variables are defined and declared ?


In C:
All definitions are declarations. A declaration is more or less when you tell the compiler
what a particular identifier is, so:
int i;
void f(void);
Both are declarations. A definition is a declaration that either creates the variable or
includes a function body. So:
int i; /* in a function */
void f(void) { return; }
These are definitions. You can create a variable declaration that's not a definition by
doing something like:
extern int i;
This says that i is declared somewhere else; so you're telling the compiler what i is,
but not to reserve space for it.

The whole in a function comment above is because if you do "int i" globally, it's what's
called a tentative definition, which is more or less a definition, eventually. Don't worry
about that one too much.

In short: a definition is a declaration that either reserves space for a variable or includes
a function body.

In C++:
A declaration introduces one or more names into a program. Declarations can occur more than once in a
program. Therefore, classes, structures, enumerated types, and other user-defined types can be declared
for each compilation unit. The constraint on this multiple declaration is that all declarations must be
identical. Declarations also serve as definitions, except when the declaration:
1. Is a function prototype (a function declaration with no function body).
2. Contains the extern specifier but no initializer (objects and variables) or function body (functions).
This signifies that the definition is not necessarily in the current translation unit and gives the
name external linkage.
3. Is of a static data member inside a class declaration.
Because static class data members are discrete variables shared by all objects of the class, they
must be defined and initialized outside the class declaration. (For more information about classes
and class members, see Classes.)
4. Is a class name declaration with no following definition, such as class T;.
5. Is a typedef statement.
Examples of declarations that are also definitions are:
// Declare and define int variables i and j.
int i;
int j = 10;

// Declare enumeration suits.
enum suits { Spades = 1, Clubs, Hearts, Diamonds };

// Declare class CheckBox.
class CheckBox : public Control
{
public:
Boolean IsChecked();
virtual int ChangeState() = 0;
};
Some declarations that are not definitions are:
extern int i;
char *strchr( const char *Str, const char Target );
Definitions
A definition is a unique specification of an object or variable, function, class, or enumerator. Because
definitions must be unique, a program can contain only one definition for a given program element. There
can be a many-to-one correspondence between declarations and definitions. There are two cases in which
a program element can be declared and not defined:
A function is declared but never referenced with a function call or with an expression that takes the
function's address.
A class is used only in a way that does not require its definition be known. However, the class must be
declared. The following code illustrates such a case:
// definitions.cpp
class WindowCounter; // Forward reference; no definition

class Window
{
// Definition of WindowCounter not required
static WindowCounter windowCounter;
};

int main()
{
}
In COBOL:
Declare the variables according to the following rules and guidelines:
You must explicitly declare all host variables and host variable arrays that are used in SQL
statements in the WORKING-STORAGE SECTION or LINKAGE SECTION of your program's
DATA DIVISION.
You must explicitly declare each host variable and host variable array before using them in an
SQL statement.
You can specify OCCURS when defining an indicator structure, a host variable array, or an
indicator variable array. You cannot specify OCCURS for any other type of host variable.
You cannot implicitly declare any host variables through default typing or by using the IMPLICIT
statement.
If you specify the ONEPASS SQL processing option, you must explicitly declare each host
variable and each host variable array before using them in an SQL statement. If you specify the
TWOPASS precompiler option, you must declare each host variable before using it in the
DECLARE CURSOR statement.
If you specify the STDSQL(YES) SQL processing option, you must precede the host language
statements that define the host variables and host variable arrays with the BEGIN DECLARE
SECTION statement and follow the host language statements with the END DECLARE SECTION
statement. Otherwise, these statements are optional.
Ensure that any SQL statement that uses a host variable or host variable array is within the scope
of the statement that declares that variable or array.

In Fortron:
Programs make use of objects whose value is allowed to change while it is running,
these are known as variables. A variable must have an associated data type, such as
REAL or INTEGER, and be identified at the start of the section of the program in
which it is used (see later). This is referred to as declaring a variable, for example:
REAL :: temperature, pressure
INTEGER :: count, hours, minutes
declares five variables, two which have values that are real numbers and three that
have integer values.
The variable declaration statement may also be used to assign an initial value to
variables as they are declared. If an initial value is not assigned to a variable it should
not be assumed to have any value until one is assigned using the assignment statement
described below.
REAL :: temperature=96.4
INTEGER :: daysinyear=365, monthsinyear=12, weeksinyear=52
The general form of a variable declaration is:
TYPE [,attr] :: variable list
Where attr are optional Fortran 90 `commands' to further define the properties of
variables. Attributes will be described throughout the course as they are introduced.
In php:
All variables in PHP start with a $ (dollar) sign followed by the name of the variable.
A valid variable name starts with a letter (A-Z, a-z) or underscore (_), followed by any number of
letters, numbers, or underscores.
If a variable name is more than one word, it can be separated with underscore (for example
$employee_code instead of $employeecode).
'$' is a special variable that can not be assigned.
<?php
$abc = 'Welcome'; //valid
$Abc = 'W3resource.com'; //valid
$9xyz = 'Hello world'; //invalid; starts with a number
$_xyz = 'Hello world'; //valid; starts with an underscore
$_9xyz = 'Hello world'; //valid
$aa = 'Hello world'; //valid; '' is (Extended) ASCII 228.
?> - See more at: http://www.w3resource.com/php/variables/declaring-php-
variables.php#sthash.GlB6gN13.dpuf

You might also like