Found 33676 Articles for Programming

When are static objects destroyed in C++?

Ravi Ranjan
Updated on 16-May-2025 17:25:38

3K+ Views

C++ Static Object A static object is declared with the static keyword. The static objects are initialized only once and stored in the static storage area. The static objects are only destroyed when the program terminates i.e. they live until program termination. In this article, we will understand static object, their types, and their examples. Syntax of Static Object The syntax for declaring a static object is as follows: Animal cat; //object declaration static Animal dog; //static object declaration Types of Static Objects The static objects can be of two types, which are mentioned below: ... Read More

How static variables in member functions work in C++?

Ravi Ranjan
Updated on 16-May-2025 17:24:40

1K+ Views

The static variables in C++ are defined using the static keyword. The static member variables in a class are shared by all the class objects as there is only one copy of them in memory, regardless of the number of objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present. In this article, we will understand the static member variables, their characteristics, and how static variables work in member functions with the help of example code. The characteristics of the static keyword are mentioned below: Characteristics ... Read More

How to calculate combination and permutation in C++?

Ravi Ranjan
Updated on 02-May-2025 15:19:10

13K+ Views

Combination and permutation are a part of combinatorics. The permutation is known as the different arrangements that a set of elements can make if the elements are taken one at a time, some at a time, or all at a time. Combination is the different ways of selecting elements if the elements are taken one at a time, some at a time, or all at a time. In this article, we have the value of 'n' and 'r' such that 'r' should be less than n. Our task is the find the permutation and combination using the value of 'n' ... Read More

How do I convert a double into a string in C++?

Ravi Ranjan
Updated on 11-Apr-2025 17:13:47

31K+ Views

A double datatype is used to represent a decimal or exponential value and string represents a sequence of characters. In this article, we are having a double value and our task is to convert the given double into string. Here is a list of approaches to convert double to string in C++ that we will be discussing in this article with stepwise explanation and complete example codes. Using to_string() Function Using ostringstream Using stringstream Using sprintf() Function Using ... Read More

C++ static member variables and their initialization

Ravi Ranjan
Updated on 13-May-2025 19:34:21

6K+ Views

The static member variables in C++ are defined using the static keyword. The static member variables in a class are shared by all the class objects as there is only one copy of them in memory, regardless of the number of objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present. In this article, we will understand the static member variables and their characteristics, and go through various examples explaining the characteristics of static member variables. The characteristics of the static keyword are mentioned below: Characteristics ... Read More

Data Types we cannot use to create array in C

Chandu yadav
Updated on 26-Jun-2020 14:11:13

376 Views

An array can be created using all data types such as int, char, float, double etc. But creating an array by using the void data type is not possible. An error will be displayed if that is done.A program that demonstrates this is given as follows.Example Live Demo#include #include int main() {    void arr1[4];    printf("A void array");    return 0; }OutputThe above program returns the following error.error: declaration of ‘arr1’ as array of voids void arr1[4];Now let us understand the above program.An array arr1 of void data type is created in the above program. Since this is ... Read More

Initializing array with variable vs a real number in C++

Arjun Thakur
Updated on 26-Jun-2020 14:12:22

332 Views

An array is a collection of same type of elements at contiguous memory location. The lowest address in the array corresponds to the first element while highest address corresponds to the last element. The array index starts with zero(0) and ends with the size of array minus one(array size - 1).An array can be initialized with variables as well as real numbers. A program that demonstrates this is given as follows.Example Live Demo#include using namespace std; int main() {    int a = 5;    int b = 3;    int arr[4];    arr[0] = a;    arr[1] = 8;    arr[2] = b;    arr[3] = 2;    cout

How to return local array from a C++ function?

Ankith Reddy
Updated on 26-Jun-2020 14:13:36

843 Views

A local array cannot be directly returned from a C++ function as it may not exist in memory after the function call. A way to resolve this is to use a static array in the function. As the lifetime of the static array is the whole program, it can easily be returned from a C++ function without the above problem.A program that demonstrates this is given as follows.Example Live Demo#include using namespace std; int *retArray() {    static int arr[10];    for(int i = 0; i

How to pass a 2D array as a parameter in C?

George John
Updated on 26-Jun-2020 14:14:28

1K+ Views

A 2-D array can be easily passed as a parameter to a function in C. A program that demonstrates this when both the array dimensions are specified globally is given as follows.Example Live Demo#include const int R = 4; const int C = 3; void func(int a[R][C]) {    int i, j;    for (i = 0; i < R; i++)    for (j = 0; j < C; j++)    a[i][j] += 5; ; } int main() {    int a[R][C];    int i, j;    for (i = 0; i < R; i++)    for (j = 0; ... Read More

How to dynamically allocate a 2D array in C?

Chandu yadav
Updated on 30-Jul-2019 22:30:23

17K+ Views

A 2D array can be dynamically allocated in C using a single pointer. This means that a memory block of size row*column*dataTypeSize is allocated using malloc and pointer arithmetic can be used to access the matrix elements. A program that demonstrates this is given as follows. Example Live Demo #include #include int main() { int row = 2, col = 3; int *arr = (int *)malloc(row * col * sizeof(int)); int i, j; for (i = 0; i < row; i++) ... Read More

Advertisements