Found 1339 Articles for C

Declare variable as constant in C

Ankith Reddy
Updated on 26-Jun-2020 13:44:11

20K+ Views

Variables can be declared as constant using the const keyword or the #define preprocessor directive. Details about these are given as follows.The const keywordVariables can be declared as constants by using the “const” keyword before the datatype of the variable. The constant variables can be initialized once only. The default value of constant variables are zero.A program that demonstrates the declaration of constant variables in C using const keyword is given as follows.Example Live Demo#include int main() {    const int a;    const int b = 12;    printf("The default value of variable a : %d", a);    printf("The ... Read More

Initialization of global and static variables in C

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

5K+ Views

In C language both the global and static variables must be initialized with constant values. This is because the values of these variables must be known before the execution starts. An error will be generated if the constant values are not provided for global and static variables.A program that demonstrates the initialization of global and static variables is given as follows.Example Live Demo#include int a = 5; static int b = 10; int main() {    printf("The value of global variable a : %d", a);    printf("The value of global static variable b : %d", b);    return 0; }OutputThe ... Read More

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

George John
Updated on 09-Apr-2025 19:14:40

21K+ Views

To find the length of an array in C++, we can use various functions and approaches that we are going to discuss in this article. Finding the length of an array is a very common task and is used in looping through an array, sorting the array, finding maximum and minimum, and in many more scenarios. In this article, we have an array and our task is to find the length of the array in C++. Approaches to Find Length of Array Here is a list of approaches to find the length of an array in C++ which ... Read More

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

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

529 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

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 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

334 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

Advertisements