Found 7346 Articles for C++

What is the difference between g++ and gcc?

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

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

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

Samual Sam
Updated on 26-Jun-2020 09:23:37

11K+ Views

Increment operators are used to increase the value by one while decrement works opposite increment. Decrement operator decrease the value by one.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.The following is the syntax of pre and post increment.++variable_name; // Pre-increment variable_name++; // Post-incrementHere,variable_name − Any name of the variable given by user.Here is an example of pre and post increment in C++.Example Live Demo#include using namespace std; int main() {    int i = 5;    cout

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

karthikeya Boyini
Updated on 26-Jun-2020 09:27:33

170 Views

The following is an example to add two numbers without using arithmetic operators.Example Live Demo#include #include using namespace std; int add(int val1, int val2) {    while(val2 != 0) {       int c = val1 & val2;       val1 = val1 ^ val2;       val2 = c

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

Samual Sam
Updated on 26-Jun-2020 09:09:53

596 Views

The following is an example to convert a character into int.Example Live Demo#include using namespace std; int main() {    char c = '8';    int i = c - 48;    cout

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

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

711 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

Why are global and static variables initialized to their default values in C/C++?

karthikeya Boyini
Updated on 26-Jun-2020 09:11:54

3K+ Views

Global and static variables are initialized to their default values because it is in the C or C++ standards and it is free to assign a value by zero at compile time. Both static and global variable behave same to the generated object code. These variables are allocated in .bss file and at the time of loading it allocates the memory by getting the constants alloted to the variables.The following is an example of global and static variables.Example Live Demo#include int a; static int b; int main() {    int x;    static int y;    int z = 28; ... Read More

Swapping two variable value without using third variable in C/C++

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

438 Views

The following is an example of swapping two variables.Example Live Demo#include int main() {    int a, b;    printf("Enter the value of a : ");    scanf("%d", &a);    printf("Enter the value of b : ");    scanf("%d", &b);    a += b -= a = b - a;    printf("After Swapping : %d\t%d", a, b);    return 0; }OutputEnter the value of a : 23 Enter the value of b : 43 After Swapping : 4323In the above program, two variables a and b are declared and initialized dynamically at run time.int a, b; printf("Enter the value of ... Read More

Variable initialization in C++

karthikeya Boyini
Updated on 26-Jun-2020 09:12:44

8K+ Views

Variables are the names given by the user. A datatype is also used to declare and initialize a variable which allocates memory to that variable. There are several datatypes like int, char, float etc. to allocate the memory to that variable.There are two ways to initialize the variable. One is static initialization in which the variable is assigned a value in the program and another is dynamic initialization in which the variables is assigned a value at the run time.The following is the syntax of variable initialization.datatype variable_name = value;Here, datatype − The datatype of variable like int, char, float ... Read More

How to catch a divide by zero error in C++?

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

521 Views

The following is an example to catch a divide by zero error.Example Live Demo#include using namespace std; int display(int x, int y) {    if( y == 0 ) {       throw "Division by zero condition!";    }    return (x/y); } int main () {    int a = 50;    int b = 0;    int c = 0;    try {       c = display(a, b);       cout

Advertisements