Found 1401 Articles for C

How to write long strings in Multi-lines C/C++?

George John
Updated on 26-Jun-2020 14:10:35

11K+ Views

Long strings can be written in multiple lines by using two double quotes ( “ “ ) to break the string at any point in the middle.A program that demonstrates this in C is given as follows.Example Live Demo#include int main() {    char *str = "This is the method "                "to write long strings "                "in multiple lines in C";    puts(str);    return 0; }OutputThe output of the above program is as follows.This is the method to write long strings in multiple lines in ... Read More

Data Types we cannot use to create array in C

Chandu yadav
Updated on 26-Jun-2020 14:11:13

294 Views

An array can be created using all data types such as int, char, float, double etc. But creating an array by using the void data type is not possible. An error will be displayed if that is done.A program that demonstrates this is given as follows.Example Live Demo#include #include int main() {    void arr1[4];    printf("A void array");    return 0; }OutputThe above program returns the following error.error: declaration of ‘arr1’ as array of voids void arr1[4];Now let us understand the above program.An array arr1 of void data type is created in the above program. Since this is ... Read More

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

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

945 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

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

585 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

654 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

George John
Updated on 26-Jun-2020 13:43:26

2K+ Views

In C/C++, a variable name can have alphabets, numbers and underscore( _ ) character. There are some keywords in C/C++ language, apart from them everything is treated as identifier. Identifiers are the name of variable, constants, functions etc.We can not specify the identifier which starts with a number because there are seven phases of compiler as follows.Lexical AnalysisSyntax AnalysisSemantic AnalysisIntermediate Code GenerationCode OptimizationCode GenerationSymbol TableNone of above supports that a variable starts with a number. This is because the compiler gets confused if is a number or identifier until it reaches an alphabet after the numbers. So the compiler will ... Read More

Declare variable as constant in C

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

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

4K+ 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 31-Oct-2023 02:49:20

20K+ Views

Some of the methods to find the length of an array are given as follows −Method 1 - Using sizeof operatorThe sizeof() operator can be used to find the length of an array. A program that demonstrates the use of the sizeof operator in C++ is given as follows.Example Live Demo#include using namespace std; int main() {    int arr[5] = {4, 1, 8, 2, 9};    int len = sizeof(arr)/sizeof(arr[0]);    cout

Advertisements