Found 7197 Articles for C++

How to print size of array parameter in a function in C++?

Ravi Ranjan
Updated on 15-May-2025 19:34:00

314 Views

To print the size of an array parameter in a function in C++, we will use the typeOf() operator. In this article, we have passed an array as an argument to the function. Our task is to print the size of this array in C++. Printing Size of Static Array Parameter When we pass an array as an argument of function in C++, it is considered as a pointer. The sizeOf() operator returns the size of the pointer, depending on the system(64-bit or 32-bit) rather than returning the size of the array. Here is a code example explaining this. ... Read More

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

Why do we assume strncpy insecure in C/C++?

Ravi Ranjan
Updated on 16-May-2025 17:26:16

605 Views

The function strncpy() is used to copy the specified number of characters to the destination from the source. It is similar to the strcpy() function. In strncpy() function, we can specify the at most how many characters we want to copy from source to destination. In this article, we have a source string. Our task is to copy this string into a destination string using the strncpy() function and understand why it is insecure to use in C++. Syntax of strncpy() Function The syntax of the strncpy() function is as follows: char *strncpy(char *destination, char *source, size_t n); ... 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

Accessing array out of bounds in C/C++

Ravi Ranjan
Updated on 23-Jul-2025 18:57:52

2K+ Views

An array in C/C++ is a fixed-size sequential collection of elements of the same data type where all the elements are stored in the contiguous memory allocation. If an array is accessed out of bounds then an undefined behavior will occur in C/C++, unlike Java where an exception such as java.lang.ArrayIndexOutOfBoundsException will occur. Accessing Out of Bound Memory Accessing out-of-bounds memory in an array means we are trying to access the array index outside its valid range size (i.e., index = array size). It returns any garbage value in the output. Example In ... Read More

How to write long strings in Multi-lines C/C++?

Ravi Ranjan
Updated on 10-Apr-2025 10:24:43

15K+ Views

To write long strings in multi-lines, you can use 'newline character' or 'raw string literal'. It increases the readability of the code, makes the code look clean, and you can avoid scrolling. In this article, we have a long string and our task is to write the long string in multi-lines in C++. Approaches to Write Long Strings in Multi Lines Here is a list of approaches to write long strings in multiple lines which we will be discussing in this article with stepwise explanation and complete example codes. Using Newline Character ... Read More

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

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

329 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

Advertisements