Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C++ Articles
Page 9 of 597
C/C++ Program for Triangular Matchstick Number?
Here we will see how to count number of matchsticks required to make a triangular pyramid. The base of the pyramid is given. So if the base is 1, it will take 3 matchsticks to make a pyramid, for base 2, 9 matchsticks are needed, for base size 3, it will take 18 matchsticks. Base = 1 3 matchsticks Base = 2 9 matchsticks ...
Read MoreC/C++ Program for Maximum height when coins are arranged in a triangle?
In this section, we will see one interesting problem. There are N coins, and we have to find the maximum height we can make if we arrange the coins as a pyramid. In this fashion, the first row will hold 1 coin, the second will hold 2 coins, and so on. Coin Pyramid 1 1 1 1 ...
Read Morec32rtomb() function in C/C++?
In C programming, the c32rtomb() function is used to convert a 32-bit wide character (char32_t) to its corresponding multibyte character representation. This function is defined in the uchar.h header file and is part of the C11 standard for Unicode support. Syntax size_t c32rtomb(char *s, char32_t c32, mbstate_t *ps); Parameters s − Pointer to a character array where the multibyte character will be stored c32 − The 32-bit wide character to convert ps − Pointer to an mbstate_t object that holds the conversion state Return Value The function returns the number ...
Read MorePower Function in C/C++
The power function in C is used to calculate the power of a given number. The pow() function finds the value of a raised to the power b i.e., ab. Syntax double pow(double base, double exponent); The pow() function accepts two double values as parameters and returns a double value as output. It is defined in the math.h header file. If you pass integers to the power function, they are automatically converted to double data type. However, there's a potential precision issue with this conversion. Sometimes floating-point representation might store values slightly differently (e.g., ...
Read MoreWhy do we check for a NULL pointer before deleting in C/C++?
In C programming, checking for a NULL pointer before calling free() is often seen in code, but it's technically unnecessary. The free() function is designed to safely handle NULL pointers by doing nothing when passed a NULL value. Syntax void free(void *ptr); Why NULL Check is Unnecessary According to the C standard, free(NULL) is guaranteed to be a no-op (no operation). This means the following code is redundant − #include #include int main() { int *ptr = NULL; ...
Read MoreIs it safe to delete a void pointer in C/C++?
The void pointer is a pointer which is not associated with any data type. It points to some data location in storage means points to the address of variables. It is also called general purpose pointer. However, deleting void pointers requires careful consideration for memory safety. Is It Safe to Delete a Void Pointer in C/C++? It is not safe to delete a void pointer directly because the compiler needs to know the exact type to call the appropriate destructor and determine the correct memory size to deallocate. Deleting without proper type information can lead to undefined behavior. ...
Read MoreHow to declaring pointer variables in C/C++?
A pointer is a variable that stores the memory address of another variable. To declare pointer variables in C, we use the asterisk (*) operator before the variable name during declaration. Syntax data_type *pointer_name; Where data_type is the type of variable the pointer will point to, and pointer_name is the name of the pointer variable. Basic Pointer Declaration and Usage Here's how to declare and use pointer variables in C − #include int main() { // A normal integer variable int ...
Read MoreHow to compare pointers in C/C++?
Pointers in C can be directly compared using relational operators to check their memory addresses. This allows us to determine equality, ordering, and relative positions of pointers in memory. Syntax pointer1 == pointer2 // Equality comparison pointer1 != pointer2 // Inequality comparison pointer1 < pointer2 // Less than comparison pointer1 > pointer2 // Greater than comparison pointer1 = pointer2 // Greater than or equal Pointer Comparison in C In C, we can compare pointers using relational operators ...
Read MoreThe best way to check if a file exists using standard C/C++
In C programming, checking if a file exists is a common task that can be accomplished by attempting to open the file for reading. If the file opens successfully, it exists; otherwise, it doesn't exist. Syntax FILE *fopen(const char *filename, const char *mode); Method 1: Using fopen() Function The most straightforward approach is to use fopen() to attempt opening the file in read mode − #include int main() { FILE *file; /* Try to open file for ...
Read MoreHow to use use an array of pointers (Jagged) in C/C++?
In C programming, an array of pointers (also known as jagged arrays) allows you to store multiple pointers in a single array. This is particularly useful when working with strings of different lengths or when you need to point to different memory locations dynamically. Syntax data_type *array_name[size]; Where data_type is the type of data the pointers will point to, and size is the number of pointers the array can hold. Example 1: Array of Integer Pointers The following example demonstrates how to create an array of pointers that point to integer values − ...
Read More