C++ Articles

Page 15 of 597

What is the correct way to use printf to print a size_t in C/C++?

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 15-Mar-2026 26K+ Views

In C programming, size_t is an unsigned integer type used to represent the size of objects and is commonly returned by functions like sizeof and strlen. To correctly print size_t variables, we should use the %zu format specifier instead of %d or other format specifiers. Syntax printf("%zu", size_t_variable); The %zu format specifier consists of − z − length modifier that specifies the argument corresponds to a size_t type u − conversion specifier for unsigned decimal integer Example 1: Basic size_t Printing This ...

Read More

What is the difference between g++ and gcc?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 3K+ Views

The gcc and g++ are both GNU compiler collection tools, but they serve different purposes. gcc is primarily designed for C programs, while g++ is designed for C++ programs. Understanding their differences helps choose the right compiler for your project. Syntax gcc program.c -o executable_name g++ program.cpp -o executable_name gcc (GNU C Compiler) The gcc compiler is specifically designed to compile C programs. It treats files with .c extension as C source code and links against the standard C library by default. Example: Using gcc for C Program #include ...

Read More

Pre-increment and Post-increment concept in C/C++?

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 15-Mar-2026 12K+ Views

Both pre-increment and post-increment are used to increase a variable's value by 1, but they behave differently in expressions. Pre-increment (++i) increases the value before it is used, while post-increment (i++) increases it after the current expression is evaluated. Syntax // Pre-increment ++variable_name; // Post-increment variable_name++; Key Differences: Pre-increment (++i) − Increments the value first, then uses the new value in the expression Post-increment (i++) − Uses the current value in the expression, then increments it Pre-Increment Operator (++x) The pre-increment operator increments the variable's value by ...

Read More

Swapping two variable value without using third variable in C/C++

Samual Sam
Samual Sam
Updated on 15-Mar-2026 608 Views

In C programming, swapping two variables without using a third variable can be accomplished using arithmetic operations. This technique saves memory and demonstrates clever use of mathematical operations. Syntax a = a + b; b = a - b; a = a - b; Method 1: Using Addition and Subtraction This approach uses arithmetic operations to swap values without requiring additional memory − #include int main() { int a = 10, b = 20; printf("Before swapping: a ...

Read More

exit(), abort() and assert() in C/C++

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 15-Mar-2026 2K+ Views

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 More

Swap two variables in one line in C/C+

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 15-Mar-2026 2K+ Views

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 More

What are Wild Pointers in C/C++?

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 15-Mar-2026 4K+ Views

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 More

How to clear console in C?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 22K+ Views

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 More

atexit() function in C/C++

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 471 Views

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 More

strxfrm() in C/C++

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 404 Views

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 More
Showing 141–150 of 5,962 articles
« Prev 1 13 14 15 16 17 597 Next »
Advertisements