
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 1339 Articles for C

6K+ Views
An array arr[i] is interpreted as *(arr+i). Here, arr denotes the address of the first array element or the 0th index element. So *(arr+i) means the element is at i distance from the first element of the array. So, array index starts from 0 as initially i is 0 which means the first element of the array. In this article, we will see an example of C and C++ code to understand the reason why an array starts from index 0. Why Array Index Starts from Zero? The array index starts from zero as it provides better efficiency and ... Read More

605 Views
The function strncpy() is used to copy the specified number of characters to the destination from the source. It is similar to the strcpy() function. In strncpy() function, we can specify the at most how many characters we want to copy from source to destination. In this article, we have a source string. Our task is to copy this string into a destination string using the strncpy() function and understand why it is insecure to use in C++. Syntax of strncpy() Function The syntax of the strncpy() function is as follows: char *strncpy(char *destination, char *source, size_t n); ... Read More

2K+ Views
An array in C/C++ is a fixed-size sequential collection of elements of the same data type where all the elements are stored in the contiguous memory allocation. If an array is accessed out of bounds then an undefined behavior will occur in C/C++, unlike Java where an exception such as java.lang.ArrayIndexOutOfBoundsException will occur. Accessing Out of Bound Memory Accessing out-of-bounds memory in an array means we are trying to access the array index outside its valid range size (i.e., index = array size). It returns any garbage value in the output. Example In ... Read More

15K+ Views
To write long strings in multi-lines, you can use 'newline character' or 'raw string literal'. It increases the readability of the code, makes the code look clean, and you can avoid scrolling. In this article, we have a long string and our task is to write the long string in multi-lines in C++. Approaches to Write Long Strings in Multi Lines Here is a list of approaches to write long strings in multiple lines which we will be discussing in this article with stepwise explanation and complete example codes. Using Newline Character ... Read More

373 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

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

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

744 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

840 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

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