Programming Articles - Page 2642 of 3366

How to declare a global variable in C++

Tapas Kumar Ghosh
Updated on 02-Jun-2025 14:02:21

2K+ Views

In C++, a global variable is a variable that is declared outside of any function or class. It can be accessible from any part of the function. Declaration of a Global Variable The global variables are declared after the heading file inclusions section and before starting the main() function. The declaration is simple just like a normal variable declaration. You need to use data type and variable name. You can also initialize them if required. Syntax The following is the basic syntax of declaring global variable: datatype global_var_name = var_value; Example of Global Variable Declaration In this example, we ... Read More

What is the difference between a destructor and a free function in C++?

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

613 Views

Here we will see what are the differences between destructor and the free() functions in C++. The destructor is used to perform some action, just before the object is destroyed. This action may not freeing up the memory, but can do some simple action such as displaying one message on screen.The free() function is used in C, in C++, we can do the same thing using delete keyword also. When the object is deleted using free() or delete, the destructor is invoked. The destructor function takes no argument and returns nothing. This function is called when free or delete is ... Read More

What is difference between GCC and G++ Compilers?

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

815 Views

We use gcc and g++ compilers in different times. Here we will see what are the differences between gcc and g++.The gcc is GNU C compiler, and g++ is GNU C++ compiler. The main differences are like below −gcc can compile *.c or *.cpp files as C and C++ respectivelyg++ can also compile *.c and *.cpp files, but take both as C++ fileIf we want to use g++ to link the object files, it automatically links in the STD C++ libraries. The gcc does not do thatgcc compiles C files which has fewer predefined macrosgcc compiles C++ files with more ... Read More

Compiling a C++ program with GCC

Vivek Verma
Updated on 28-Aug-2025 15:43:45

31K+ Views

To compile a C++ program with GCC, we need to download GCC or install the g++ compiler on our system. Otherwise, you will not be able to compile C++ code using GCC. What is GCC? The GCC is a tool chain that compiles code. It stands for GNU Compiler Collection, which was developed by the GNU project. The GCC supports various programming languages such as C, C++, Go, etc. Specific to the C++ language, the GCC provides the g++ compiler, which is developed or designed to compile the C++ source code into executable programs. Note: When the C++ program compiles ... Read More

How to prevent class inheritance in C++

Nishtha Thakur
Updated on 11-Dec-2024 09:52:41

2K+ Views

Here we will see how to prevent inheritance in C++. The concept of preventing inheritance is known as the final class. In Java or C#, we can use final classes. In C++ there are no such direct ways. Here we will see how to simulate the final class in C++. Approach Firstly, we will create one extra class called MakeFinalClass (its default constructor is private). This function is used to solve our purpose. The main Class MyClass can call the constructor of the MakeFinalClass as they are friend classes. One thing ... Read More

What does it mean when a numeric constant in C/C++ is prefixed with a 0?

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

201 Views

Sometimes we may see some numeric literals, which is prefixed with 0. This indicates that the number is octal number. So octal literals contain 0 at the beginning. For example, if an octal number is 25, then we have to write 025.Example#include int main() {    int a = 025;    int b = 063;    printf("Decimal of 25(Octal) is %d", a);    printf("Decimal of 63(Octal) is %d", b); }OutputDecimal of 25(Octal) is 21 Decimal of 63(Octal) is 51

wprintf() and wscanf in C Library

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

2K+ Views

Here we will see the wprintf() and wscanf() functions in C. These are the printf() and scanf() functions for wide characters. These functions are present in the wchar.hThe wprintf() function is used to print the wide character to the standard output. The wide string format may contain the format specifiers which is starting with % sign, these are replaced by the values of variables which are passed to the wprintf().The syntax is like below −int wprintf (const wchar_t* format, ...);This function takes the format. This format is a pointer to a null terminated wide string, that will be written in ... Read More

Program for Christmas Tree in C

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

1K+ Views

Here we will see one interesting problem. In this problem, we will see how to print Christmas tree randomly. So the tree will be flickering like Christmas tree lights.To print a Christmas tree, we will print pyramids of various sizes just one beneath another. For the decorative leaves a random character is printed from a given list of characters. The height and randomness is adjustable.Here after generating a tree, the whole screen is cleared, then generate again, that’s why this is looking like flickering tree.Example#include #include #include #include #define REFRESH_RATE 40000 #define RANDOM_NESS 5 // The ... Read More

Why strict aliasing is required in C?

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

211 Views

Here we will see, why we should use the strict aliasing in C. Before discussing that part, let us see one code, and try to analyze the output.Example#include int temp = 5; int* var = &temp; int my_function(double* var) {    temp = 1;    *var = 5.10; //this will change the value of temp    return (temp); } main() {    printf("%d",  my_function((double*)&temp)); }Output1717986918If we call the function my_function, then it will return 1. We can also call this using my_function((double*)&temp). This is supposed to return 1, but here we can see that this is returning something else. This code was made to return constant 1 only. To fix this problem, we can use Strict ... Read More

Linear search using Multi-threading in C

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

918 Views

Here we will see how to apply the multi-threading concept to search one element in an array. Here the approach is very simple. We will create some threads, then divide the array into different parts. Different thread will search in different parts. After that, when the element is found, enable the flag to identify this.Example#include #include #define MAX 16 #define THREAD_MAX 4 int array[MAX] = { 1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 30, 64, 110, 220 }; int key = 18; int flag = 0; //flag to indicate that item is found ... Read More

Advertisements