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 11 of 597
How to find the size of an int[] in C/C++?
In C programming, finding the size of a statically declared int[] array means determining how many elements it contains. This is different from finding the memory size − we want the element count. The sizeof operator is the primary method for this in C. Syntax int array_length = sizeof(array) / sizeof(array[0]); Method 1: Using sizeof Operator The sizeof operator returns the total memory occupied by an array in bytes. Dividing this by the size of one element gives us the number of elements. This works only for statically declared arrays − #include ...
Read MoreAddress of a function in C or C++
In C programming, every function is stored in the computer's memory and has a unique memory address, just like variables. We can access and display these function addresses to understand how functions are stored in memory. Syntax functionName // Returns address of the function (void*)functionName // Cast to void pointer for printing Accessing Address of a Function To access the address of a function, we use its name without parentheses. When we write hello() with parentheses, we're calling the function. But when we write just hello, it ...
Read MoreConvert C/C++ program to Preprocessor code
Here we will see how to generate the preprocessed or preprocessor code from the source code of a C or C++ program. To see the preprocessed code using gcc compiler, we have to use the '-E' option with the gcc. The preprocessor includes all of the # directives in the code, and also expands the MACRO function. Syntax gcc -E program.c Example Let's create a simple C program with macro definitions and see how the preprocessor expands them − #include #define PI 3.1415 #define SQUARE(x) ((x) * (x)) int ...
Read MoreUninitialized primitive data types in C/C++
In C programming, uninitialized primitive data types contain unpredictable values called "garbage values". The C standard does not guarantee that these variables will be initialized to zero or any specific value. The actual values depend on what was previously stored in those memory locations. Syntax data_type variable_name; // Uninitialized variable data_type variable_name = initial_value; // Initialized variable Example: Uninitialized Variables Let's examine what happens when we declare variables without initializing them − #include int main() { char a; float b; ...
Read MoreConvert C/C++ code to assembly language
Assembly language is a low-level programming language that provides a human-readable representation of machine code. The GNU Compiler Collection (GCC) allows us to convert C/C++ source code into assembly language for analysis and optimization purposes. Syntax gcc -S source_file.c gcc -S source_file.cpp Note: To use gcc, you need to install it on your system. On Ubuntu/Debian: sudo apt install gcc, on Windows: install MinGW or use WSL. Parameters -S − Generate assembly code and stop before assembling source_file − The C/C++ source file to convert Example: Simple C ...
Read MoreC/C++ Struct vs Class
In C programming, there is no class keyword − only structures (struct) are available. However, understanding the conceptual differences between struct and class is important for C programmers who may work with C++. In C, structures are used to group related data together. Syntax struct structure_name { data_type member1; data_type member2; // ... more members }; Example: Basic Structure in C In C, all structure members are accessible by default − #include struct my_struct { ...
Read MoreWhat does the operation c=a+++b mean in C/C++?
In C, the expression c = a+++b is parsed by the compiler using the "maximal munch" rule, which means it reads the longest possible token sequence. This expression is interpreted as c = (a++) + b, where a++ is the post-increment operator applied to variable a. Syntax c = a++ + b; // Post-increment a, then add b c = a + ++b; // Pre-increment b, then add to a The key difference lies in operator precedence and associativity. The post-increment operator (++) has higher precedence than the addition ...
Read MoreAssertions in C/C++
An assertion in C is a debugging tool that checks if a condition is true during program execution. When an assertion fails, the program displays an error message and terminates immediately. This helps catch programming errors early in the development process. In C programming, assertions are implemented using the assert() macro defined in the header file. The macro evaluates an expression and aborts the program if the expression is false. Syntax #include assert(expression); Parameters: expression − A condition that should be true. If it evaluates to false (0), the program terminates ...
Read MoreHow do I add two numbers without using ++ or + or any other arithmetic operator in C/C++?
In this article we will see how to add two numbers without using arithmetic operators like +, ++, -, or --. This technique uses bitwise operations to simulate binary addition. To solve this problem, we can use binary adder logic. In digital circuits, we design half adder and full adder circuits that can add one-bit binary numbers. By cascading multiple adders, we can create circuits to add bigger numbers. In binary addition, we perform XOR operation for the sum bits, and AND operation for the carry bits. These principles are implemented here to add two numbers using only ...
Read MoreHow does “void *” differ in C and C++?
Both C and C++ support void pointers, but their behavior differs significantly. In C, a void pointer can be directly assigned to any other pointer type without explicit typecasting. However, in C++, assigning a void pointer to any other pointer type requires an explicit typecast. Syntax void *pointer_name; Void Pointer in C A void pointer (also called a generic pointer) in C is a special type of pointer that can point to any data type, but doesn't have any type by itself. It can hold the address of any variable (int, float, char, etc.). ...
Read More