Docsity

Variables and Constants, Summaries of Computer science

Partial preview of the text

Download Variables and Constants and more Summaries Computer science in PDF only on Docsity!

2026 Edition

Variables & Constants

Welcome to Module 4 2026 Edition

Variables and constants are fundamental elements of the 'C' programming language used to store data in a program. Variables are named memory locations whose values can change during program execution, while constants represent fixed values that do not change during execution. Understanding variables and constants is essential for performing calculations, processing data, and controlling program behavior. In 'C', variables and constants must be used according to specific rules related to their data types, declaration, initialization, scope, and lifetime.

'C' Variable Syntax 2026 Edition

•The syntax to declare a variable in 'C' specifies the name and the type of the variable. data_type variable_name = value; // defining single variable or data_type variable_name1, variable_name2; // defining multiple variable Here,

  • data_type: Type of data that a variable can store.
  • variable_name: Name of the variable given by the user.
  • value: value assigned to the variable by the user. •'C' is a strongly typed language so all the variable’s types must be specified before using them.

2026 Edition

Defining Variable

There are 3 aspects of defining a variable: 1.Variable Declaration 2.Variable Definition 3.Variable Initialization

'C' Variable Declaration Variable declaration in 'C' tells the compiler about the existence of the variable with the given name and data type. No memory is allocated to a variable in the declaration. 'C' Variable Definition In the definition of a 'C' variable, the compiler allocates some memory and some value to it. A defined variable will contain some random garbage value till it is not initialized. int var; char var2; Most of the modern 'C' compilers declare and define the variable in single step. 'C' Variable Initialization Initialization of a variable is the process where the user assigns some meaningful value to the variable. int var; // variable definition var = 10; // initialization or int var = 10; // variable declaration and definition

Rules for Naming Variables 2026 Edition

Following are the rules to assign any name to the variable

  1. A variable name must only contain alphabets, digits, and underscore.
  2. A variable name must start with an alphabet or an underscore only.
  3. It cannot start with a digit.
  4. No whitespace is allowed within the variable name.
  5. A variable name must not be any reserved word or keyword.

2026 Edition

Valid Names

_name, first_name

nameIIsem,

Name_2sem

Invalid Names

First name

34name

goto, for, switch

It contains a whitespace between First and name

Starting with a number

Can’t declare them variable as they are keywords

Types of 'C' Variables 2026 Edition

The 'C' variables can be classified into the following types: 1.Local Variables 2.Global Variables 3.Static Variables 4.Automatic Variables 5.Extern Variables 6.Register Variables

Constants 2026 Edition

•The constants in 'C' are the read-only variables. •Its value cannot be modified once they are declared in the 'C' program. •The type of constant can be an integer constant, a floating pointer constant, a string constant, or a character constant •constant in 'C' language is always declared using the const keyword. •It is also known as a const type qualifier , the const keyword is placed at the start of the variable declaration to declare that variable as a constant. Syntax – const data_type var_name = value;

Type of Constant 2026 Edition

The type of the constant is the same as the data type of the variables. Following is the list of the types of constants

  1. Integer Constant
  2. Character Constant
  3. Floating Point Constant
  4. Double Precision Floating Point Constant
  5. Array Constant
  6. Structure Constant

Properties of Constant 2026 Edition

•The important properties of constant variables in 'C' defined using the const keyword are as follows:

1. Initialization with Declaration Initialize the constant variable at the time of its declaration. Otherwise, it will store the garbage value. 2. Immutability The constant variables in c are immutable after its definition, i.e., they can be initialized only once in the whole program. After that, we cannot modify the value stored inside that variable.