C Articles - Page 123 of 134

How to pass a 2D array as a parameter in C?

George John
Updated on 26-Jun-2020 14:14:28

1K+ Views

A 2-D array can be easily passed as a parameter to a function in C. A program that demonstrates this when both the array dimensions are specified globally is given as follows.Example Live Demo#include const int R = 4; const int C = 3; void func(int a[R][C]) {    int i, j;    for (i = 0; i < R; i++)    for (j = 0; j < C; j++)    a[i][j] += 5; ; } int main() {    int a[R][C];    int i, j;    for (i = 0; i < R; i++)    for (j = 0; ... Read More

How to dynamically allocate a 2D array in C?

Chandu yadav
Updated on 30-Jul-2019 22:30:23

17K+ Views

A 2D array can be dynamically allocated in C using a single pointer. This means that a memory block of size row*column*dataTypeSize is allocated using malloc and pointer arithmetic can be used to access the matrix elements. A program that demonstrates this is given as follows. Example Live Demo #include #include int main() { int row = 2, col = 3; int *arr = (int *)malloc(row * col * sizeof(int)); int i, j; for (i = 0; i < row; i++) ... Read More

Difference between pointer and array in C

Arjun Thakur
Updated on 26-Jun-2020 13:57:26

763 Views

The details about a pointer and array that showcase their difference are given as follows.PointerA pointer is a variable that stores the address of another variable. When memory is allocated to a variable, pointer points to the memory address of the variable. Unary operator ( * ) is used to declare a pointer variable.The following is the syntax of pointer declaration.datatype *variable_name;Here, the datatype is the data type of the variable like int, char, float etc. and variable_name is the name of variable given by user.A program that demonstrates pointers is given as follows.Example Live Demo#include int main () { ... Read More

When do function-level static variables get initialized in C/C++?

George John
Updated on 26-Jun-2020 13:59:45

853 Views

Static variables can be defined using the static keyword. They 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.Function-level static variables are created and initialized the first time that they are used although the memory for then is allocated at program load time.A program that demonstrates function-level static variables in C is given as follows −Example Live Demo#include int func() {    static int num = ... Read More

Why C/C++ variables doesn’t start with numbers

Ravi Ranjan
Updated on 26-May-2025 11:51:12

2K+ Views

In C/C++, a variable name can have alphabets, numbers, and the underscore( _ ) character. There are some keywords in the C/C++ language. Apart from them, everything is treated as an identifier. Identifiers are the names of variables, constants, functions, etc. Why Variables in C/C++ Can't Start with Numbers? In C and C++, variable names (also, known as identifiers) cannot start with a digit due to how the compiler processes code during compilation. First, we need to understand the phases of compilation or compiler. There are seven phases in a typical compiler: Lexical Analysis ... Read More

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

545 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

Advertisements