Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C++ Articles
Page 14 of 597
When do function-level static variables get initialized in C/C++?
Static variables can be defined using the static keyword. They are variables that remain in memory while the program is running, meaning their lifetime is the entire program execution. This is different from automatic variables, which remain in memory only when their function is running and are destroyed when the function exits. Function-level static variables are initialized only once − the first time the function is called. The memory for them is allocated at program load time, but the initialization occurs during the first function execution. Syntax static data_type variable_name = initial_value; Example: Static ...
Read MoreWhy C/C++ variables doesn’t start with numbers
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. Syntax Valid identifier naming rules in C − // Valid identifier patterns: [a-zA-Z_][a-zA-Z0-9_]* // Valid examples: variable_name, _count, num1, my_var // Invalid examples: 1variable, 2name, 3_count 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 ...
Read MoreHow do I find the length of an array in C/C++?
To find the length of an array in C, we can use various approaches that are essential for array manipulation. Finding the length of an array is a fundamental task used in looping through arrays, sorting, searching, and memory management operations. In this article, we will explore different methods to determine the size of an array in C programming. Syntax // Method 1: Using sizeof operator int length = sizeof(array) / sizeof(array[0]); // Method 2: Using pointer arithmetic int length = *(&array + 1) - array; Method 1: Using sizeof() Operator The ...
Read MoreWhen to use references vs. pointers in C/C++
In C programming, we work with pointers to access memory addresses and manipulate data indirectly. C does not have reference variables like C++, but understanding the difference helps when transitioning between languages. Syntax // Pointer declaration and usage datatype *pointer_name; pointer_name = &variable_name; Pointers in C Pointers are variables that store memory addresses of other variables. They provide indirect access to data and enable dynamic memory allocation. Example: Basic Pointer Usage #include int main() { int a = 8; int *ptr; ...
Read MoreWhen to use extern in C/C++
In C, the extern keyword is used to declare a variable or function that is defined elsewhere in the program. It tells the compiler that the variable or function exists, but its actual definition (memory allocation) is in another file or later in the same file. Syntax // Variable declaration using extern extern datatype variable_name; // Function declaration using extern (optional) extern return_type function_name(parameters); Parameters: datatype − The data type of the variable (int, char, float, etc.) variable_name − Name of the variable to be declared return_type − Return type of the ...
Read MoreWhat is the correct way to use printf to print a size_t in C/C++?
In C programming, size_t is an unsigned integer type used to represent the size of objects and is commonly returned by functions like sizeof and strlen. To correctly print size_t variables, we should use the %zu format specifier instead of %d or other format specifiers. Syntax printf("%zu", size_t_variable); The %zu format specifier consists of − z − length modifier that specifies the argument corresponds to a size_t type u − conversion specifier for unsigned decimal integer Example 1: Basic size_t Printing This ...
Read MoreWhat is the difference between g++ and gcc?
The gcc and g++ are both GNU compiler collection tools, but they serve different purposes. gcc is primarily designed for C programs, while g++ is designed for C++ programs. Understanding their differences helps choose the right compiler for your project. Syntax gcc program.c -o executable_name g++ program.cpp -o executable_name gcc (GNU C Compiler) The gcc compiler is specifically designed to compile C programs. It treats files with .c extension as C source code and links against the standard C library by default. Example: Using gcc for C Program #include ...
Read MorePre-increment and Post-increment concept in C/C++?
Both pre-increment and post-increment are used to increase a variable's value by 1, but they behave differently in expressions. Pre-increment (++i) increases the value before it is used, while post-increment (i++) increases it after the current expression is evaluated. Syntax // Pre-increment ++variable_name; // Post-increment variable_name++; Key Differences: Pre-increment (++i) − Increments the value first, then uses the new value in the expression Post-increment (i++) − Uses the current value in the expression, then increments it Pre-Increment Operator (++x) The pre-increment operator increments the variable's value by ...
Read MoreSwapping two variable value without using third variable in C/C++
In C programming, swapping two variables without using a third variable can be accomplished using arithmetic operations. This technique saves memory and demonstrates clever use of mathematical operations. Syntax a = a + b; b = a - b; a = a - b; Method 1: Using Addition and Subtraction This approach uses arithmetic operations to swap values without requiring additional memory − #include int main() { int a = 10, b = 20; printf("Before swapping: a ...
Read Moreexit(), abort() and assert() in C/C++
In C programming, the exit(), abort(), and assert() functions are used for program termination and debugging. Each function serves a different purpose and is defined in different header files. The exit() Function The exit() function is used to terminate a program immediately in a normal way. It is defined in the header file and allows the program to return a status code to the operating system. Syntax void exit(int status_value); Parameters: status_value − The termination status code (0 indicates success, non-zero indicates error) Example In this example, the ...
Read More