×
>
<

C++ Programming

C++ Variables And Constants | CrackEase

Variables and Constants

Variables in programming

In programming, a variable is a name that refers to a memory location used to store data. The value stored in a variable can be changed during program execution. A variable is declared with a data type and (optionally) initialized with a value, for example:

int x = 5; or float a = 10.0f;

In C/C++ you must declare a variable before using it. Variables may be assigned or updated both at compile time (initialization) and at runtime (during execution).

Rules to declare a Variable ?
Variable declaration example

Rules for Variables

  • Variables store values in memory.
  • A variable name refers to a memory location.
  • Variable names are case-sensitive: a and A are different.
  • Variable names must start with a letter (a–z, A–Z) or underscore (_).
  • Variable names can contain letters, digits and underscores (e.g., var1).
  • Variable names cannot contain spaces.
  • Variable names cannot be C/C++ keywords.
  • Variable names cannot start with a digit.

Datatype of Variables:

Each variable has a type that determines what kind of data it can hold (for example: char, int, float, double).

DECLARATION OF A VARIABLE :

When you declare a variable, the compiler reserves memory according to the variable's data type.

  • Declaration allocates memory based on the data type.
  • Uninitialized local variables may contain an indeterminate (garbage) value; always initialize when possible.

Single Variable Declaration

data_type single_variable_name;

Example:

  
#include <stdio.h>
int main(){
    int a = 25;
    printf("value of a : %d", a);
    return 0;
}
  
output
  
value of a : 25
  
Declaring Multiple Variables
  data_type var1, var2, var3;
Source Code
  
#include <stdio.h>
int main()
{
   int a=25, b=4, c=67;
   printf("value of a: %d", a);
   printf("\nvalue of b: %d", b);
   printf("\nvalue of c: %d", c);
   return 0;
}
  
output
  
value of a: 25
value of b: 4
value of c: 67 
  
Scope of variables
Scope of Variables

The scope of a variable determines which parts of the code can access (see) that variable. Common scopes include:

  • Local variables — declared inside a block or function and visible only within that block.
  • Global variables — declared outside all functions and visible throughout the program.

Local Variable

Local variables are defined inside functions or blocks and are accessible only within that block. Uninitialized local variables have indeterminate values (garbage).

Example :

  
#include <stdio.h> 
int main()
{
     int a, b, c; // local variables
     a = 10;
     b = 30;
     c = a + b;
     printf("%d", c);
     return 0;
}
  

Here a, b, c are local to main. The program prints 40.

Global variable

Global variables are defined outside functions and are accessible by any function in the program. They exist for the program's lifetime.

  • Global variables are declared outside functions.
  • They have program-wide visibility.
  • Uninitialized global variables default to zero in C/C++.

Example :

Global Variable

  
#include <stdio.h>
int d = 20; // global variable
int main()
{
      int a, b, c; // local variables
      a = 10;
      b = 30;
      d = d + 10;
      c = a + b + d;
      printf("%d", c);
      return 0;
}
  
output
  
70
  
Constants in C++
Constants example

Constants are values that cannot be changed during program execution. Once defined, attempting to modify a constant will cause a compile-time error.

  • A constant has a fixed value.
  • Constants can be of any data type.
  • Constants are sometimes called literals.
Floating constant :
  • A floating constant is a decimal value (for example, 3.14).
  • It behaves like a float or double literal depending on suffix or context.
  • It contains a decimal point or an exponent.

Example: 3.14 is a floating constant.

  
#include <stdio.h>
int main()
{
    const float num1 = 5.0f;
    const float num2 = 3.123f; 
    printf("floating constant is %f\n", num1);
    printf("floating constant is %f\n", num2);
    return 0;
}
  
output
  
floating constant is 5.000000
floating constant is 3.123000
  
Character Constant :
  • A character constant stores a single character enclosed in single quotes, e.g., 'S'.
  • Character constants have type char and are typically stored as their integer (ASCII/encoding) value.
  • Escape sequences (like '\n', '\t') are allowed.

Example:

  
#include <stdio.h>
int main()
{
    const char ch = 'S';
    const char escape[] = "Hello\tWorld";
    printf("character constant is %c\n", ch);
    printf("string constant is %s\n", escape);
    return 0;
}
  
output
  
character constant is S
string constant is Hello    World
  
String constant :
  • A string constant is a null-terminated array of characters (e.g., "Hello").
  • String literals are enclosed in double quotes.
  • Escape sequences (like \n) work inside string literals.

Example : const char s[] = "books"; // {'b','o','o','k','s','\0'}.

  
#include <stdio.h>
int main()
{
    const char st1[] = "S";              // string with single character
    const char st2[] = "Hey Somya";      // normal string
    const char st3[] = "Hey\nSomya";     // string with escape sequence
    printf("string constant with single character: %s\n", st1);
    printf("normal string constant: %s\n", st2);
    printf("string with escape sequences: %s\n", st3);
    return 0;
}
  
output
  
string constant with single character : S
normal string constant : Hey Somya
string with escape sequences : Hey
Somya
  
Constant keyword

You can define constants using preprocessor macros (#define) or the const keyword. const provides typed constants and is generally preferred over macros for most use cases.

Example: area of a rectangle

  
#include <stdio.h>
#define LENGTH 10
int main()
{
    const int BREADTH = 5;
    int area = LENGTH * BREADTH;
    printf("%d\n", area);
    return 0;
}
  
output
  
50
  
Footer Content | CrackEase