C++ Articles - Page 678 of 719

exit() vs _Exit() in C/C++

Revathi Satya Kondra
Updated on 02-May-2025 13:39:59

760 Views

In C/C++, both exit() and _Exit() are used to terminate a program. The exit() performs cleanup like flushing output, closing files, and calling functions, while _Exit() ends the program immediately without doing any cleanup. Now, let us learn the difference between exit() and _Exit() individually in C/C++. C++ exit() Function The exit() function is used to clean up before terminating the program. It calls functions registered with atexit(), flushes file buffers, and returns control to the operating system. Syntax Following is the syntax for exit() function: void exit(int status_value); Example In this example, we print "Program is running..." and ... Read More

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

Tapas Kumar Ghosh
Updated on 04-Jun-2025 17:30:39

2K+ Views

In C and C++ programming, the exit(), abort(), and assert() functions are used for program termination and debugging. Each of these functions have different purpose and defined in different header files. In this article, we will learn these functions and their usage with the help of examples. The exit() Function In C/C++, exit() function is used to terminate the function call immediately without the executing further processes. This function is defined in  (in C) and / (in C++) header file and does not return any value. Syntax Following is the basic syntax of exit() function: void exit(int status_value); Here, ... Read More

Static Keyword in C++

Samual Sam
Updated on 10-Dec-2024 18:20:10

16K+ Views

When a static keyword is used, variables, data members, and functions can not be modified again. It is allocated for the lifetime of the program. Static functions can be called directly by using a class name. Key Points of Static Variables Static variables are variables that are defined using static keywords, which consist of special behavior compared to regular variables. Here we will see its key points. Static variables are initialized only once. The compiler persists the variable till the end of the program. Static variables can be defined inside or ... Read More

strftime() function in C/C++

karthikeya Boyini
Updated on 26-Jun-2020 08:36:22

743 Views

The function strftime() is used to format the time and date as a string. It is declared in “time.h” header file in C language. It returns the total number of characters copied to the string, if string fits in less than size characters otherwise, returns zero.Here is the syntax of strftime() in C language, size_t strftime(char *string, size_t size, const char *format, const struct tm *time_pointer)Here, string − Pointer to the destination array.size − Maximum number of characters to be copied.format − Some special format specifiers to represent the time in tm.time_pointer − Pointer to tm structure that contains the ... Read More

Print “Hello World” in C/C++ without using header files

Samual Sam
Updated on 26-Jun-2020 08:36:48

810 Views

Generally, we use header files in C/C++ languages to access the built-in functions like int, char, string functions. The function printf() is also a built-in function which is declared in “stdio.h” header file and it is used to print any kind of data on console.Here is an example to print without header files in C language, Exampleint printf(const char *text, ...); int main() {    printf( "Hello World" );    return 0; }OutputHello WorldIn the above program, we printed “Hello World” without using any header file in the program by declaring the printf() function. The declaration of printf() is as ... Read More

Swap two variables in one line in C/C+

Tapas Kumar Ghosh
Updated on 14-Apr-2025 18:23:50

2K+ Views

In C++, swapping two variables means exchanging the values stored in them. There can be multiple ways to implement the swapping using single line statement. In this article, we are going to learn some of the approaches to swap the values. Example Let's consider the following example with input and output scenario: Input: int a = 5; int b = 6; Output: a = 6 b = 5 You can implement swapping of two variable by using the following different ways: Using Arithmetic Operator Using Tuple Using XOR Swap Two Variables Using Arithmetic Operator In ... Read More

Print 1 to 100 in C++, without loop and recursion

Samual Sam
Updated on 26-Jun-2020 08:11:52

960 Views

There are several methods to print numbers without using loops like by using recursive function, goto statement and creating a function outside main() function.Here is an example to print numbers using goto statement in C++ language,Example Live Demo#include using namespace std; int main() {    int count=1;    int x;    cout > x;    PRINT:    cout

What are Wild Pointers in C/C++?

Tapas Kumar Ghosh
Updated on 21-Apr-2025 18:05:33

3K+ Views

In C/C++, a wild pointer is a type of pointer that has not been initialized to a valid memory address. It points to memory that has been deallocated and is called a dangling pointer. The pointer behaves like a wild pointer when it is declared but not initialized. That is why, they point to random memory location. Syntax The basic syntax of initializing wild pointer in C/C++: int *ptr; Example of Wild Pointer In this example, we create a pointer arr that doesn't assign it any memory. Then, it tries to print 5 values from it using a loop. ... Read More

C++ Program to Sum the digits of a given number

Nishu Kumari
Updated on 03-Mar-2025 13:17:07

14K+ Views

The task is to write C++ programs that calculates the sum of the digits of a given number. For example, if the number is 453, the sum of its digits would be 4 + 5 + 3 = 12. To solve this, we will extract each digit from the number and add them together. We will use basic programming concepts such as loops and arithmetic operations to achieve this. In this article, we will show you how to write a C++ program that takes a number as input and sums the digits of that number. Approaches for ... Read More

Implement your own sizeof operator using C++

Tapas Kumar Ghosh
Updated on 21-Apr-2025 18:00:20

978 Views

In C++, the sizeof() operator is a unary operator which is used to calculate the size of any data type. Implementing sizeof() OperatorWe can use the #define directive to implement a macro that copies the behavior of the sizeof() operator for objects, though it will not be the same as the sizeof() operator. Syntax Following is the syntax of C++ macro to implement the sizeof(): #define any_name(object) (char *)(&object+1) - (char *)(&object) Here, any_name : The name you want to give to your own sizeof() operator. Note: The main benefit of using MACROS is defining by once ... Read More

Advertisements