0% found this document useful (0 votes)
4 views

c basic1

The document explains the concepts of variables and constants in programming, detailing how to declare variables, the rules for naming them, and the differences between local and global variables. It also defines data, information, bits, bytes, and constants, highlighting the distinctions between constants and variables. Additionally, it outlines naming conventions and the memory storage of local and global variables.

Uploaded by

jasimshanto20215
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

c basic1

The document explains the concepts of variables and constants in programming, detailing how to declare variables, the rules for naming them, and the differences between local and global variables. It also defines data, information, bits, bytes, and constants, highlighting the distinctions between constants and variables. Additionally, it outlines naming conventions and the memory storage of local and global variables.

Uploaded by

jasimshanto20215
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Variables and Constants

Variable is a name assign to a storage area that the program can manipulate. A variable type
determines the size and layout of the variable's memory.

It also determines the range of values which need to be stored inside that memory and nature of
operations that can be applied to that variable.

There are three places where variables you can declare variable programming language:

 Inside a function or a block: Local variables


 Outside of all functions: Global variables
 In the definition of function parameters: Formal parameters

How to declare a variable:


1. Choose the "type" you need.
2. Decide upon a name for the variable.
3. Use the following format for a declaration statement:
datatype variable identifier;
4. You may declare more than one variable of the same type by separating the variable names
with commas.
int age, weight, height;
5. You can initialize a variable (place a value into the variable location) in a declaration
statement.
double mass = 3.45;

Rules to construct a valid variable name

1. A variable name may consists of letters, digits and the underscore ( _ ) characters.

2. A variable name must begin with a letter. Some system allows to starts the variable
name with an underscore as the first character.

3. ANSI standard recognizes a length of 31 characters for a variable name. However,


the length should not be normally more than any combination of eight alphabets, digits,
and underscores.

4. Uppercase and lowercase are significant. That is the variable Totamt is not the same
as totamt and TOTAMT.

5. The variable name may not be a C reserved word (keyword).


Some valid variable names

Total Amount ctr name1

n1 M_age AMOUNT

Some invalid variable names

13th (name) 111 %nm

Naming Conventions
Generally, C programmers maintain the following conventions for naming variables.

 Start a variable name with lowercase letters.


 Try to use meaningful identifiers
 Separate "words" within identifiers with mixed upper and lowercase (for example
empCode) or underscores (for example emp_code).
 For symbolic constants use all uppercase letters (for example #define LENGTH
100, #define MRP 45).

The Programming language C has two main variable types


 Local Variables
 Global Variables

Local Variable

 Local Variable is defined as a type of variable declared within programming block or


subroutines. It can only be used inside the subroutine or code block in which it is
declared. The local variable exists until the block of the function is under execution. After
that, it will be destroyed automatically.

Example of Local Variable

int add()
{
int a =4;
int b=5;
return a+b;
}

Here, 'a' and 'b' are local variables

Global Variable

A Global Variable in the program is a variable defined outside the subroutine or function. It has
a global scope means it holds its value throughout the lifetime of the program. Hence, it can be
accessed throughout the program by any function defined within the program, unless it is
shadowed.

Example:

int a =4;
int b=5;
int add()
{
return a+b;
}

Here, 'a' and 'b' are global variables.

Differences between Local and Global Variables


Parameter Local Global
Scope It is declared inside a function. It is declared outside the function.
If it is not initialized, a garbage value is If it is not initialized zero is stored as
Value
stored default.
It is created when the function starts It is created before the program's global
Lifetime execution and lost when the functions execution starts and lost when the
terminate. program terminates.
Data sharing is not possible as data of the Data sharing is possible as multiple
Data sharing local variable can be accessed by only one functions can access the same global
function. variable.
Parameters passing is required for local Parameters passing is not necessary for a
Parameters variables to access the value in other global variable as it is visible throughout
function the program
When the value of the local variable is When the value of the global variable is
Modification of
modified in one function, the changes are modified in one function changes are
variable value
not visible in another function. visible in the rest of the program.
Local variables can be accessed with the
You can access global variables by any
Accessed by help of statements, inside a function in
statement in the program.
which they are declared.
Memory storage It is stored on the stack unless specified. It is stored on a fixed location decided by
the compiler.

Data: Data are characteristics or information, usually numerical, that are collected through
observation. In a more technical sense, data is a set of values of qualitative or quantitative
variables about one or more persons or objects, while a datum (singular of data) is a single value
of a single variable.
Information: Information is associated with data, as data represent values attributed to
parameters, and information is data in context and with meaning attached. Organized data is
called Information.
Bit: A bit is a binary digit, the smallest increment of data on a computer. The bit represents a
logical state with one of two possible values. It's a single unit of information that has a value of
either 0 or 1.
Byte: The byte is a unit of digital information that most commonly consists of eight bits.
Constant: A constant can be defined as a fixed value, which is used in algebraic expressions and
equations. A constant does not change over time and has a fixed value. For example, the size of a
shoe or cloth or any apparel will not change at any point.
In an algebraic expression, x+y = 8, 8 is a constant value, and it cannot be changed.

Difference Between Constants and Variables

Constant Variables

A constant does not change its value over time. A variable, on the other hand, changes its
value dependent on the equation.

Constants are usually written in numbers. Variables are specially written in letters or
symbols.

Constants usually represent the known values in an Variables, on the other hand, represent the
equation, expression or in line of programming. unknown values.

Constants are used in computer programming. Variables also have its uses in computer
programming and applications.

You might also like