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 15 of 597
exit(), abort() and assert() in C/C++
In C programming, the exit(), abort(), and assert() functions are used for program termination and debugging. Each function serves a different purpose and is defined in different header files. The exit() Function The exit() function is used to terminate a program immediately in a normal way. It is defined in the header file and allows the program to return a status code to the operating system. Syntax void exit(int status_value); Parameters: status_value − The termination status code (0 indicates success, non-zero indicates error) Example In this example, the ...
Read MoreSwap two variables in one line in C/C+
In C programming, swapping two variables means exchanging the values stored in them. There are multiple ways to implement swapping using a single line statement. This article demonstrates various approaches to swap variable values efficiently. Syntax // General concept variable1 = new_value_of_variable2; variable2 = new_value_of_variable1; Example Scenario Let's consider the following input and expected output − Input: int a = 5; int b = 10; Output: a = 10 b = 5 Method 1: Using Arithmetic Operations This method uses arithmetic operations to swap variables without temporary storage. ...
Read MoreWhat are Wild Pointers in C/C++?
In C, a wild pointer is a pointer that has not been initialized to a valid memory address. When a pointer is declared but not assigned a value, it contains garbage data and points to an unknown memory location, making it unpredictable and dangerous to use. Syntax data_type *pointer_name; // Uninitialized pointer (wild pointer) Example: Wild Pointer Behavior Here's an example demonstrating a wild pointer that attempts to access uninitialized memory − #include int main() { int *ptr; // Wild pointer - ...
Read MoreHow to clear console in C?
In C, clearing the console or output screen can be achieved using several methods. While clrscr() from conio.h was commonly used in older compilers, modern standard C uses system() calls for cross-platform compatibility. Note: The examples below use system() calls which work in most environments. For online compilers, console clearing may not be visible as output is captured after program execution. Syntax The following are standard methods to clear the console in C − #include system("cls"); /* Windows */ Or, #include system("clear"); /* ...
Read Moreatexit() function in C/C++
The atexit() function in C is used to register functions that will be called automatically when the program terminates normally. These registered functions are executed in reverse order (LIFO - Last In, First Out) after the main function completes but before the program ends. This function is declared in the header file. Syntax int atexit(void (*function_name)(void)) Parameters: function_name − A pointer to the function that will be called at program termination. The function must take no parameters and return void. Return Value: Returns 0 on success, or a non-zero value if ...
Read Morestrxfrm() in C/C++
The strxfrm() function transforms a source string according to the current locale's collating sequence and copies the transformed string to a destination buffer. It is declared in header file and is useful for locale-sensitive string comparisons. Syntax size_t strxfrm(char *destination, const char *source, size_t number); Parameters destination − Pointer to the destination buffer where transformed characters will be copied source − Pointer to the null-terminated source string to be transformed number − Maximum number of characters to copy to destination Return Value Returns the length of the transformed string ...
Read MoreWhat is the size of void pointer in C/C++ ?
The size of void pointer varies from system to system. If the system is 16-bit, size of void pointer is 2 bytes. If the system is 32-bit, size of void pointer is 4 bytes. If the system is 64-bit, size of void pointer is 8 bytes. This is because a pointer stores memory addresses, and the size depends on the system's addressing capability. Syntax To find the size of a void pointer, use the sizeof() operator: sizeof(void*) sizeof(pointer_variable) Example: Finding Size of Void Pointer The following example demonstrates how to find the size ...
Read Moreisgreater() in C/C++
In C programming, the isgreater() function is a type-generic macro used to compare two floating-point values safely. It is declared in the header file and returns a non-zero value (true) if the first argument is greater than the second, otherwise returns 0 (false). Unlike the regular > operator, isgreater() never raises floating-point exceptions. Syntax int isgreater(x, y); Parameters: x − First floating-point value to compare y − Second floating-point value to compare Return Value: Returns 1 if x is greater than y, otherwise returns 0. Example 1: Basic Usage with ...
Read Morestrdup() and strdndup() in C/C++
The functions strdup() and strndup() are used to duplicate strings in C. These functions allocate memory dynamically and create copies of existing strings. Note that these are POSIX functions, not part of the C standard library. Note: To use strdup() and strndup(), you may need to compile with -D_GNU_SOURCE flag or ensure your system supports POSIX extensions. strdup() Function The strdup() function duplicates an entire string by allocating memory and copying the string content. Syntax char *strdup(const char *string); Parameters string − Pointer to the null-terminated string to ...
Read MoreList of Common Reasons for Segmentation Faults in C/C++
The main reason for segmentation fault is accessing memory that is either not initialized, out of bounds for your program or trying to modify string literals. These may cause a segmentation fault though it is not guaranteed that they will cause a segmentation fault. Here are some of the common reasons for segmentation faults − Accessing an array out of bounds Dereferencing NULL pointers Dereferencing freed memory Dereferencing uninitialized pointers Incorrect use of the "&" (address of) and "*" (dereferencing) operators Improper formatting specifiers in printf and scanf statements Stack overflow Writing to read-only memory Common ...
Read More