Logo

dev-resources.site

for different kinds of informations.

Variables, Constants, Data Types, and Namespaces in C++

Published at
6/24/2024
Categories
cpp
variable
datatypes
coding
Author
komsenapati
Categories
4 categories in total
cpp
open
variable
open
datatypes
open
coding
open
Author
11 person written this
komsenapati
open
Variables, Constants, Data Types, and Namespaces in C++

hello

Welcome to the second blog of the series "Unlocking C++"!

In this blog, I will discuss variables, constants, data types, and namespaces.

It's the basics required for any programming language.

Let's start with variables.

Variable

Variables are named memory locations used for storing the data.

For high level, it's just like we used to do with maths
2x + 4 = 0
=> x = -2

Variables are of many types.
For C++ variable types can be stated as

  • Global Variable: Any function or class can access The variable declared globally.
  • Local Variable: The local or block-scoped variable can only be accessed within the block. As the block ends, the variable is discarded.
#include <iostream>

int y = 3.14;

int main() {

    { // block
        int x = 5;
        std::cout << x << std::endl;
        std::cout << y << std::endl;
    }

    std::cout << x << std::endl; // gives error 'x' was not declared in this scope
    std::cout << y;
}
Enter fullscreen mode Exit fullscreen mode

Here x is a block-scoped variable so inside the block we can access them but as the block ends it can't be accessed. And y is a global variable so it can be accessed anywhere in the program.

Constants

Constants are also named memory locations used for data storage but their values can't be changed once given.

For example, π.
We know it's a constant and its value will not be changed.

In C we used macros ( #define PI 3.14 ) or manifest constants but constants in C++ are better as they come with type safety.

#include <iostream>

int main() {

    const double pi = 3.14;
    std::cout << pi;

    pi = 4; // error: assignment of read-only variable

}
Enter fullscreen mode Exit fullscreen mode

Here as its const, its value can't be changed.

Another important concept for variables and constants.

Declaration is the statement where the variable is declared without any value. Space is given to a variable as it may be given value in future.

int x;
Enter fullscreen mode Exit fullscreen mode

Initialisation is the statement where we give a value to the variable.

x = 5;
Enter fullscreen mode Exit fullscreen mode

In the case of variables declaration and initialisation can happen in the same line or different lines as well but for constant it must be done in the same line.

Data types

Each variable or constant is associated with a data type. It specifies the type of data stored in the variable.

The primary data types in C++ are:

  • int: store integer values, 2 bytes size (maybe 4 bytes in some systems)
  • long: store integer values but of higher size (4 bytes)
  • float: store decimal numbers, less precision, 4 bytes size
  • double: store decimal numbers, high precision, 8 bytes size
  • char: store a single character, 1 byte size
  • bool: either true(1) or false(0), 1 byte size

Here primary data types mean built-in (predefined) data types to be used by the user directly.

There are Derived and User-defined data types as well.
For now, I am stating them but I will make separate blogs for them in the future.

Derived data types

These are the data types derived from primitive data types.

  • Function
  • Array
  • Pointer
  • Reference

User-defined data types

These data types are created by users to use in the program.

  • struct
  • union
  • class
  • enum
  • typedef

Namespaces

Namespace is a declarative region under which variables, functions, and classes exist. It's used to avoid conflict between the same identifiers.

Identifiers means names given to variables, constants, functions, and classes.

In C++ we have a standard namespace (std) but we can create our namespace as well.

#include <iostream>

namespace apple {
    int x = 1;
}

namespace banana {
    int x = 2;
}

int main() {
    std::cout << apple::x << std::endl; // 1
    std::cout << banana::x; // 2

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Here the same identifier x is used but in different namespaces.

So this is for this blog. Let's meet in the next blog.

bye

datatypes Article's
30 articles in total
Favicon
14. Longest Common Prefix - Using Trie
Favicon
Variables & Data types
Favicon
Handling Data in SQL: Signed vs. Unsigned Types
Favicon
Representação numérica na computação
Favicon
Understanding Floats in Python: Essential Tips and Examples
Favicon
Everything You Need to Know About Python Integers: Tips, Tricks, and Examples
Favicon
Understanding Python Data Types: A Comprehensive Guide
Favicon
Why I Revisited MS SQL Server Basics: A Deep Dive into String Data Types
Favicon
Understanding Data Types in JavaScript
Favicon
Disjoint Unions in C
Favicon
PYTHON-FUNDAMENTALS: CONSTANTS, VARIABLES AND DATA TYPES
Favicon
Understanding Your Data: The Essentials of Exploratory Data Analysis"
Favicon
Data Types of Typescript
Favicon
C# {Data Types except Int}
Favicon
Variables, Constants, Data Types, and Namespaces in C++
Favicon
Data Types in Python
Favicon
JS Data types (Ma'lumot turlari)
Favicon
Data Types
Favicon
C# da ratsional sonlar bilan ishlovchi (float, double, decimal) ma'lumot turlari
Favicon
Big Integer in Java
Favicon
Oracle Data Types: An Overview
Favicon
Understanding Float vs. Double in C and C++
Favicon
Data Types - Python
Favicon
The Art of Series Summation in C: Navigating Data Types, Casting Magic, and the Dance of Incrementation
Favicon
Understanding Why We Don't Use Pointers to change the value of the element in Slice Data Type in Go Lang!
Favicon
A step by step guide to Converting a Column to Date Data Type in a Dataset using R
Favicon
Choosing the Right Java Data Types
Favicon
Evolution of Ruby Data Types
Favicon
Composite Data types part 1
Favicon
Network Address Types in PostgreSQL. Why you need to know?

Featured ones: