Found 7346 Articles for C++

What is the lifetime of a static variable in a C++ function?

Arjun Thakur
Updated on 26-Jun-2020 13:35:45

16K+ Views

A static variable is a variable that is declared using the keyword static. The space for the static variable is allocated only one time and this is used for the entirety of the program.Once this variable is declared, it exists till the program executes. So, the lifetime of a static variable is the lifetime of the program.A program that demonstrates a static variable is given as follows.Example Live Demo#include using namespace std; void func() {    static int num = 1;    cout

When should you use a class vs a struct in C++?

Chandu yadav
Updated on 26-Jun-2020 13:36:53

326 Views

Structures and classes are very similar in C++ except for some differences. So details about these differences are given below that help to decide when to use a class or structure in C++.Differences between Class and StructureAll the members of a class are private by default. This is different compared to structures as all the members of a structure are public by default.A program that demonstrates a class in C++ is given as follows −Example#include using namespace std; class Example {    int val; }; int main() {    Example obj;    obj.val = 20;    return 0; }This ... Read More

How do I find the length of an array in C/C++?

George John
Updated on 31-Oct-2023 02:49:20

20K+ Views

Some of the methods to find the length of an array are given as follows −Method 1 - Using sizeof operatorThe sizeof() operator can be used to find the length of an array. A program that demonstrates the use of the sizeof operator in C++ is given as follows.Example Live Demo#include using namespace std; int main() {    int arr[5] = {4, 1, 8, 2, 9};    int len = sizeof(arr)/sizeof(arr[0]);    cout

When to use references vs. pointers in C/C++

karthikeya Boyini
Updated on 26-Jun-2020 09:35:51

407 Views

Reference variableReference variable is an alternate name of already existed variable. It cannot be changed to refer another variable and should be initialized at the time of declaration. It cannot be NULL. The operator ‘&’ is used to declare a reference variable.The following is the syntax of reference variable.datatype variable_name; // variable declaration datatype& refer_var = variable_name; // reference variableHere, datatype − The datatype of variable like int, char, float etc.variable_name − This is the name of variable given by user.refer_var − The name of reference variable.The following is an example of reference variable.Example Live Demo#include using namespace std; int ... Read More

What is the size of an object of an empty class in C++?

Samual Sam
Updated on 26-Jun-2020 09:17:42

255 Views

The following is an example to find the size of object of an empty class.Example Live Demo#include using namespace std; class p1 {    public:    void first() {       cout

How to call a parent class function from derived class function in C++?

karthikeya Boyini
Updated on 26-Jun-2020 09:18:28

9K+ Views

The following is an example to call parent class function from derived class function.Example Live Demo#include using namespace std; class p1 {    public:    void first() {       cout

When to use extern in C/C++

Samual Sam
Updated on 26-Jun-2020 09:18:57

10K+ Views

External variables are also known as global variables. These variables are defined outside the function and are available globally throughout the function execution. The “extern” keyword is used to declare and define the external variables.The keyword [ extern “C” ] is used to declare functions in C++ which is implemented and compiled in C language. It uses C libraries in C++ language.The following is the syntax of extern.extern datatype variable_name; // variable declaration using extern extern datatype func_name(); // function declaration using externHere, datatype − The datatype of variable like int, char, float etc.variable_name − This is the name of ... Read More

What is a reference variable in C++?

karthikeya Boyini
Updated on 26-Jun-2020 09:19:43

19K+ Views

Reference variable is an alternate name of already existing variable. It cannot be changed to refer another variable and should be initialized at the time of declaration and cannot be NULL. The operator ‘&’ is used to declare reference variable.The following is the syntax of reference variable.datatype variable_name; // variable declaration datatype& refer_var = variable_name; // reference variableHere, datatype − The datatype of variable like int, char, float etc.variable_name − This is the name of variable given by user.refer_var − The name of reference variable.The following is an example of reference variable.Example Live Demo#include using namespace std; int main() { ... Read More

What is the difference between an int and a long in C++?

Samual Sam
Updated on 26-Jun-2020 09:20:19

1K+ Views

intThe datatype int is used to store the integer values. It could be signed or unsigned. The datatype int is of 32-bit or 4 bytes. It requires less memory area than long to store a value. The keyword “int” is used to declare an integer variable.The following is the syntax of int datatype.int variable_name;Here,variable_name − The name of variable given by user.The following is an example of int datatype.Example Live Demo#include using namespace std; int main() {    int a = 8;    int b = 10;    int c = a+b;    cout

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

karthikeya Boyini
Updated on 26-Jun-2020 09:20:48

20K+ Views

We should use “%zu” to print the variables of size_t length. We can use “%d” also to print size_t variables, it will not show any error. The correct way to print size_t variables is use of “%zu”.In “%zu” format, z is a length modifier and u stand for unsigned type.The following is an example to print size_t variable.Example Live Demo#include int main() {    size_t a = 20;    printf("The value of a : %zu", a);    return 0; }OutputThe value of a : 20In the above program, a variable of size_t length is declared and initialized with a value.size_t ... Read More

Advertisements