Found 7197 Articles for C++

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

13K+ 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

955 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

What all is inherited from parent class in C++?

Tapas Kumar Ghosh
Updated on 04-Jun-2025 14:22:42

4K+ Views

In C++, when a class inherits from another class, it inherits all members from parent class, but their accessibility are based on access specifiers. Key Points on What Inherits from Parent Class Following are some points on derived class inherits from its parent: The derived class can inherit data members, member functions of base class. If the data members are public, they can be accessed by derived class, same class and outside the class. If data members are protected, they can be accessed by derived and same ... Read More

(limits.h) in C/C++

Vivek Verma
Updated on 28-Aug-2025 16:45:39

1K+ Views

Both and "limits.h" are header files in C and C++ that are used to define constants related to data type limits. Header files are usually created with a .h extension and are used to declare functions, variables, and other entities without having a main() function. You can also create custom header files in C and C++. The header file is specific to the C++ language, whereas "limits.h" belongs to the C language. Let's discuss them one by one with the help of a suitable example to understand them and their uses in a program in a better way: ... Read More

How to clear console in C?

Samual Sam
Updated on 14-Apr-2025 19:13:13

21K+ Views

In C, we have various methods to clear the console or output screen and one of them is clrscr() function. It clears the screen as function invokes. It is declared in "conio.h" header file. There are some other methods too like system (cls) and system ("clear") and these are declared in "stdlib.h" header file. Syntax Following are the list of syntaxes to clear the console in C programming language as follows: clrscr(); Or, system("cls"); Or, system("clear"); Let’s say we have "new.txt" file with the following content: 0, hell!o 1, hello! 2, gfdtrhtrhrt 3, ... Read More

mbrlen() function in C/C++

karthikeya Boyini
Updated on 26-Jun-2020 08:19:06

190 Views

The function mbrlen() is used to get the length of multibyte character. It returns the size of multibyte character pointed by the pointer.Here is the syntax of mbrlen() in C language, size_t mbrlen(const char* pointer, size_t size, mbstate_t* state);Here, pointer − Pointer to the first byte of multibyte character.size − Number of bytes to check.state − Pointer to the object of mbstate_tHere is an example of mbrlen() in C language, Example Live Demo#include #include #include int main(void) {    char a[] = "s";    mbstate_t s;    int len;    len = mbrlen(a, 5, &s);    printf("Length of ... Read More

raise() function in C/C++

Samual Sam
Updated on 26-Jun-2020 08:05:21

400 Views

The function raise() is used to send the signals to the program. The predefined function signal() is invoked. It is implemented to check whether it will ignore the signal or invoke the signal handler. This is declared in “signal.h” header file. It returns zero, if successful otherwise, non-zero value.Here is the syntax of raise() in C language, int raise(int signal)Here, signal − The signal number to be invoked.Here is an example of raise() in C language, Example Live Demo#include #include void handler(int sig) {    printf("Signal received : %d", sig); } int main() {    signal(SIGILL, handler);    printf("Sending ... Read More

atexit() function in C/C++

karthikeya Boyini
Updated on 26-Jun-2020 08:05:54

403 Views

The function atexit() is used to call the function after the normal exit of program. The program is called without any parameters. The function atexit() is called after exit(). The termination function can be called anywhere in the program. This function is declared in “stdlib.h” header file.Here is the syntax of atexit() in C language, int atexit(void (*function_name)(void))Here, function_name − The function is to be called at the time of termination of program.Here is an example of atexit() in C language, Example Live Demo#include #include void func1 (void) {    printf("Exit of function 1"); } void func2 (void) { ... Read More

ungetc() in C/C++

Samual Sam
Updated on 26-Jun-2020 08:06:26

304 Views

The function ungetc() takes a character and pushes it back to the stream so that the character could be read again.Here is the syntax of ungetc() in C language, int ungetc(int character, FILE *stream)Here, character − The character to be pushed back to stream.stream − The pointer to the file object.Here is an example of ungetc() in C language, Example#include int main() {    int c;    while ((c = getchar()) != '0')    putchar(c);    ungetc(c, stdin);    c = getchar();    putchar(c);    puts("");    printf("The End!");    return 0; }Outputs a b c t h 0 ... Read More

Advertisements