Found 7346 Articles for C++

Foreach in C++ vs Java

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

175 Views

In C++ and Java, there is another kind of loop, called the foreach loop. This is basically a modification of for loop. This loop is used to access the data from some container. This can access the elements of some array quickly without performing initialization. This loop is used to do something for each element of a container, not doing things n times.Now let us see how the foreach loop is used in C++ and Java.Example#include using namespace std; int main() {    int arr[] = { 11, 22, 33, 44, 55, 66, 77, 88, 99 };    for (int a : arr) //foreach loop    cout

Difference between Structures in C and C++

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

364 Views

Here we will see what are the differences between structures in C and structures in C++. The C++ structures are mostly like classes in C++. In C structure, all members are public, but in C++, they are private in default. Some other differences are listed below.C StructureC++ StructureStructures in C, cannot have member functions inside structures.Structures in C++ can hold member functions with member variables.We cannot initialize the structure data directly in C.We can directly initialize structure data in C++.In C, we have to write ‘struct’ keyword to declare structure type variables.In C++, we do not need to use ‘struct’ ... Read More

Type difference of character literals in C vs C++

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

103 Views

In C++ the size of the character constants is char. In C the type of character constant is integer (int). So in C the sizeof(‘a’) is 4 for 32bit architecture, and CHAR_BIT is 8. But the sizeof(char) is one byte for both C and C++.Example#include main() {    printf("%d", sizeof('a')); }Output4Example#include using namespace std; main(){    cout

Compile 32-bit program on 64-bit gcc in C and C++

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

1K+ Views

Nowadays the compiler comes with default 64-bit version. Sometimes we need to compile and execute a code into some 32bit system. In that time, we have to use thisS feature.At first, we Shave to check the current target version of the gcc compiler. To check this, we have to type this command.gcc –v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu ........... ........... ...........Here it is showing that Target is x86_64. So we are using the 64-bit version of gcc. Now to use the 32-bit system, we have to write the following command.gcc –m32 program_name.cSometimes this command may generate ... Read More

do…while loop vs. while loop in C/C++

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

7K+ Views

Here we will see what are the basic differences of do-while loop and the while loop in C or C++.A while loop in C programming repeatedly executes a target statement as long as a given condition is true. The syntax is like below.while(condition) {    statement(s); }Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true.When the condition becomes false, the program control passes to the line immediately following the loop.Example#include int main () {    int ... Read More

What is a type cast in C/C++?

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

241 Views

Type casting is a way to convert a variable from one data type to another data type. For example, if you want to store a 'long' value into a simple integer then you can type cast 'long' to 'int'. You can convert the values from one type to another explicitly using the cast operator as follows −(type_name) expressionConsider the following example where the cast operator causes the division of one integer variable by another to be performed as a floating-point operation −Example#include main() {    int sum = 17, count = 5;    double mean;    mean = (double) ... Read More

Address of a function in C or C++

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

3K+ Views

In C or C++, the variables are stored into memory, so we can get their memory addresses. Similarly, the functions also are stored into the memory, so they also have some addresses. To get the address we can use the function name only without using the parenthesis.Please check the following program to get the clear idea.Example#include void my_function() {    printf("Hello World"); } int main() {    printf("The address of the my_function is: %p", my_function);    printf("The address of the main is: %p", main); }OutputThe address of the my_function is: 0000000000401530 The address of the main is: 000000000040154BRead More

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

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

4K+ Views

Here we will see what is the pre-increment and post-increment in C or C++. The pre-increment and post-increment both are increment operators. But they have little differences.The pre-increment operator increments the value of a variable at first, then sends the assign it to some other variable, but in the case of postincrement, it at first assign to a variable, then increase the value.Example#include using namespace std; main() {    int x, y, z;    x = 10;    y = 10;    z = ++x; //z will hold 11    cout

Convert C/C++ program to Preprocessor code

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

375 Views

Here we will see how to generate the preprocessed or preprocessor code from the source code of a C or C++ program.To see the preprocessed code using g++ compiler, we have to use the ‘-E’ option with the g++.Preprocessor includes all of the # directives in the code, and also expands the MACRO function.Syntaxg++ -E program.cppExample#define PI 3.1415 int main() {    float a = PI, r = 5;    float c = a * r * r;    return 0; }Output$ g++ -E test_prog.cpp int main() {    float a = 3.1415, r = 5;    float c = a * r * r;    return 0; }

Uninitialized primitive data types in C/C++

Nishtha Thakur
Updated on 27-Aug-2020 13:56:42

218 Views

One of the most frequent question is what will be the value of some uninitialized primitive data values in C or C++? Well the answer will be different in different systems. We can assume the compiler will assign 0 into the variables. It can be done for integer as 0, for float 0.0, but what will be for character type data?Example#include using namespace std; main() {    char a;    float b;    int c;    double d;    long e;    cout

Advertisements