Found 7197 Articles for C++

When to use extern in C/C++

Tapas Kumar Ghosh
Updated on 22-Apr-2025 15:15:40

11K+ Views

In C/C++, the extern keyword is used to declare a variable that is defined in another file or scope. It allows the program to access a variable or function that is defined outside the file. There are two main reason of using extern in C/C++ programs which is listed below: The extern is used when the compiler needs to be informed about the existence of a variable that is declared in another file. ... Read More

What is a reference variable in C++?

Revathi Satya Kondra
Updated on 21-Apr-2025 19:22:04

23K+ Views

In C++, the 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. Reference variables are commonly used for parameter passing in functions, returning values, and aliasing existing variables. Syntax Following is the syntax for the reference variable in c++: datatype &refer_var = variable_name; Here, datatype − The datatype of variable like int, char, float etc. variable_name − This is ... Read More

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

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

2K+ 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++?

Tapas Kumar Ghosh
Updated on 04-Jun-2025 17:41:51

25K+ Views

We should use "%zu" to print the variables of size_t length. We can use "%d" also to print size_t variables, it may not always produce an error. The correct way to print size_t variables is use of "%zu", because compiler standard %zu is defined as a format specifier for this unsigned type. Following is the description of "%zu" format: z is a length modifier. u stands for an unsigned type. C Example to Print Value of size_t Variable In this example, we demonstrate a C program that correctly prints a ... Read More

What is the difference between g++ and gcc?

Samual Sam
Updated on 26-Jun-2020 09:21:24

3K+ Views

g++GNU C++ Compiler ( g++ ) is a compiler in Linux which is used to compile C++ programs. It compiles both files with extension .c and .cpp as C++ files.The following is the compiler command to compile C++ program.g++ program.cpp -o filenameHere,filename − The name of file with .c or .cpp extension.The following is an example of using g++ compiler.Example Live Demo#include using namespace std; int main() {    int a = 20;    cout

Where are static variables stored in C/C++?

karthikeya Boyini
Updated on 26-Jun-2020 09:23:06

10K+ Views

Static variables are variables that remain in memory while the program is running i.e. their lifetime is the entire program run. This is different than automatic variables as they remain in memory only when their function is running and are destroyed when the function is over.The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program.All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment). Compared ... Read More

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

Tapas Kumar Ghosh
Updated on 21-May-2025 18:16:42

12K+ Views

Both pre-increment and post-increment are used to represent the expression of increasing a value by adding 1. Their behavior are different because pre-increment (++i) increases the value before it is used in an expression, while post-increment (i++) increases it after. Below is the key-terms of pre-increment and post-increment in C/C++: Pre-increment (++i) : Before assigning the value to the variable, the value is incremented by one. Post-increment (i++) : After assigning the value to the variable, the value is incremented. Syntax of Using Pre and Post Increment Operators The following is the basic syntax of pre-increment and post-increment ... Read More

How to sum two integers without using arithmetic operators in C/C++?

karthikeya Boyini
Updated on 29-Apr-2025 18:59:46

335 Views

Given are the two integer variables, a and b. To calculate the sum without using any arithmetic operators such as + or -. We can achieve this by using bitwise operations, which allow us to calculate the sum. Here, we have some approaches in C and C++ to solve this problem as follows: Sum of Two Integers Using Bitwise XOR Sum of Two Integers Using Vector Sum of Two Integers Using malloc() and free() Sum of Two Integers Using Bitwise XOR (^) To perform the sum ... Read More

How to convert a single char into an int in C++

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

779 Views

In C++, converting a single character into an integer is possible using various techniques like STL functions say atoi() and stoi(). Also, you can solve this with the help of ASCII value or stringstream function. Converting Single Char into Int Here, we have list down of four approaches that help to convert the single character into an integer form as follows: Using ASCII Subtraction Using stoi() Function Using atoi() Function Using stringstream Function Using ASCII Subtraction ASCII subtraction converts a digit character to an integer by subtracting '0' from it. This works because digit characters are stored sequentially ... Read More

Why does C++ require a cast for malloc() but C doesn't?

Samual Sam
Updated on 26-Jun-2020 09:11:25

899 Views

In C language, the void pointers are converted implicitly to the object pointer type. The function malloc() returns void * in C89 standard. In earlier versions of C, malloc() returns char *. In C++ language, by default malloc() returns int value. So, the pointers are converted to object pointers using explicit casting.The following is the syntax of allocating memory in C language.pointer_name = malloc(size);Here, pointer_name − Any name given to the pointer.size − Size of allocated memory in bytes.The following is an example of malloc() in C language.Example Live Demo#include #include int main() {    int n = 4, ... Read More

Advertisements