Found 7346 Articles for C++

Redeclaration of global variable in C

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

576 Views

Here we will see what is re-declaration of global variables in C. Does C supports this or not. Let us see the following code to get the idea about it.Example#include int main(){    int a;    int a = 50;    printf("a is : %d", a); }Output[Error] redeclaration of 'a' with no linkageSo we can see that we cannot re-declare local variables. Now let us see what will be the output for global variables.Example#include int a; int a = 50; int main(){    printf("a is : %d", a); }Outputa is : 50So global variables are not creating any ... Read More

How are variables scoped in C

Anvi Jain
Updated on 30-Jul-2019 22:30:25

67 Views

Here we will see how the C variables are scoped. The variables are always statically scoped in C. Binding of a variable, can be determined by the program text. These are independent of runtime function call stack.Let us see one example to get the idea.Example# include int x = 0; int my_function() {    return x; } int my_function2() {    int x = 1;    return my_function(); } int main(){    printf("The value is: %d", my_function2()); }OutputThe value is: 0Here the result is 0. Because the value returned by my_function() is not depends on the function, which is ... Read More

What is the use of cin.ignore() in C++?

Nishtha Thakur
Updated on 02-Sep-2023 13:01:37

59K+ Views

The cin.ignore() function is used which is used to ignore or clear one or more characters from the input buffer.To get the idea about ignore() is working, we have to see one problem, and its solution is found using the ignore() function. The problem is like below.Sometimes we need to clear the unwanted buffer, so when next input is taken, it stores into the desired container, but not in the buffer of previous variable. For example, after entering into the cin statement, we need to input a character array or string. So we need to clear the input buffer, otherwise ... Read More

What is difference between int and const int& in C/C++?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

3K+ Views

Here we will see what are the differences between int and const_int& in C or C++.The int is basically the type of integer type data. And const is used to make something constant. If there is int& constant, then it indicates that this will hold the reference of some int type data. This reference value is constant itself. So the const is redundant. The compiler may return warning or some error.The const int& is same as int const&. So this refers to a constant integer. The integer cannot be modified through the reference.

How to write a singleton class in C++?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

1K+ Views

Singleton design pattern is a software design principle that is used to restrict the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. For example, if you are using a logger, that writes logs to a file, you can use a singleton class to create such a logger. You can create a singleton class using the following code.Example#include using namespace std; class Singleton {    static Singleton *instance;    int data;    // Private constructor so that no objects can be created.    Singleton() {   ... Read More

What is volatile keyword in C++?

Nishtha Thakur
Updated on 01-Sep-2020 09:58:48

11K+ Views

Here we will see what is the meaning of volatile qualifier in C++. The volatile qualifier is applied to a variable when we declare it. It is used to tell the compiler, that the value may change at any time. These are some properties of volatile.The volatile keyword cannot remove the memory assignment.It cannot cache the variables in register.The value cannot change in order of assignment.Let us see, how we can use the volatile keyword.volatile int a; int volatile a;Here these two declarations are correct. Like other datatypes, we can use volatile pointers, structures, unions etc. The volatile structures and ... Read More

What's the difference between __PRETTY_FUNCTION__, __FUNCTION__, __func__ in C/C++?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

1K+ Views

Here we will see what are the differences between __FUNCTION__, __func__ and the __PRETTY_FUNCTION__ in C++.Basically the __FUNCTION__ and __func__ are same. Some old versions of C and C++ supports __func__. This macro is used to get the name of the current function. The _PRETTY_FUNCTION__ is used to return the detail about the function. Using this we can get which function is used, and in which class it is belonging, etc.Example#include using namespace std; class MyClass{    public:       void Class_Function(){          cout

What is a reentrant function in C/C++?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

4K+ Views

Here we will see what is the reentrant function in C or C++. One function is said to be a reentrant function if there is a provision to interrupt that function in the course of execution, then service the ISR (Interrupt Service Routine) and then resume the task. This type of functions is used in different cases like, recursions, hardware interrupt handling.For a reentrant function there should be some properties. These are listed below −This type of function will not use any global or static variable. There are no restrictions, but it is generally not advised. This is because the ... Read More

What is converting constructor in C++ ?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

116 Views

In this section we will see what is the conversion constructor or converting constructor in C++ class. A constructor is a special type of function of class. It has some unique property like, its name will be same as class name, it will not return any value etc. The constructors are used to construct objects of a class. Sometimes constructors may take some arguments, or sometimes it does not take arguments.When a constructor takes only one argument then this type of constructors becomes conversion constructor. This type of constructor allows automatic conversion to the class being constructed.Example#include using namespace std; ... Read More

What are __FILE__, __LINE__, and __FUNCTION__ in C++

Smita Kapse
Updated on 30-Jul-2019 22:30:25

6K+ Views

Here we will see what are the __FILE, __LINE__ and __FUNCTION__ in C++.The __FILE__This macro is used to get the path of the current file. This is useful when we want to generate log files. The following code will explain its functionality.Example#include using namespace std; int errorLog (const char* file, const std::string& msg){    cerr

Advertisements