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
Programming Articles
Page 1009 of 2547
How 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 MoreCan we use function on left side of an expression in C and C++?
In C and C++, you normally cannot use a function call on the left side of an assignment if it returns a value by copy, because function calls return non-assignable temporary values. However, there are specific cases where this is possible − Syntax // Invalid - function returns value by copy function_name() = value; // Compiler error // Valid - function returns pointer (C) *function_name() = value; // Dereference pointer // Valid - function returns reference (C++ only) function_name() = value; ...
Read MorePrecedence of postfix ++ and prefix ++ in C/C++
In C programming, understanding operator precedence between prefix increment (++var), postfix increment (var++), and dereference operator (*) is crucial for writing correct pointer code. The precedence order is: postfix operators have highest precedence, followed by prefix operators, then dereference. Syntax ++*ptr // Equivalent to ++(*ptr) - increment value at ptr *ptr++ // Equivalent to *(ptr++) - dereference then increment pointer (*ptr)++ // Increment value at ptr (postfix) Precedence Rules Postfix ++/-- has highest precedence Prefix ++/-- and dereference * have same precedence (right-to-left associativity) When ptr is a pointer: *ptr++ means ...
Read MoreType difference of character literals in C and C++
Character literals are values assigned to character data type variables. They are written as single characters enclosed in single quotes (' ') like 'A', 'b' or '2'. However, the type of character literals differs between C and C++. In C, character literals are stored as type int, whereas in C++ they are stored as type char. This fundamental difference affects memory usage and type safety. Syntax 'character' // Character literal syntax Type of Character Literal in C In C, character literals have type int and occupy 4 bytes of memory. This happens ...
Read MoreLine Splicing in C/C++
In C programming, line splicing is a preprocessor feature that allows you to split one long line of code into multiple lines by using a backslash (\) at the end of a line. The backslash tells the preprocessor to treat the next line as a continuation of the current line. Line splicing is processed before compilation during the preprocessing phase. It does not have parameters or return values − it simply affects how lines of code are interpreted by joining them together. Syntax line_of_code \ continuation_of_line The backslash must be the last character on ...
Read MoreRead/Write structure to a file using C
In C programming, structures can be written to and read from files using the fwrite() and fread() functions. This allows you to store complex data types persistently and retrieve them later. fwrite() Syntax size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); Parameters: ptr − A pointer to the data to be written size − Size in bytes of each element nmemb − Number of elements to write stream − Pointer to the FILE object fread() Syntax size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); ...
Read MoreHow can I get the list of files in a directory using C or C++?
Listing files in a directory is used to write a program that opens a specified folder (e.g: "/myfiles"), reads its contents, and displays the names of each file and subfolder one by one. In C, to see all the files in a directory, you can use special system functions that let you read the directory's contents. In real life, we open folder to see the contents inside the files. Similarly, in C, we can write a program to display all the files and folders in a directory. Syntax DIR *opendir(const char *dirname); struct dirent *readdir(DIR ...
Read MoreC Program to find size of a File
The size of a file refers to the number of bytes it occupies in memory. In C, size of a file can be found by moving the file pointer to the end of the file and checking its position. The position indicates the number of bytes the file contains. The most common way to do this is by using two functions: fseek() (to move to the end) and ftell() (to get the current position, which is the size of the file). Syntax FILE *fp = fopen("filename", "rb"); fseek(fp, 0, SEEK_END); long size = ftell(fp); fclose(fp); ...
Read Moreexit() vs _Exit() function in C and C++
In C, both exit() and _Exit() functions terminate a program, but they differ in how they handle cleanup operations. The exit() function performs cleanup operations before termination, while _Exit() terminates immediately without any cleanup. Syntax void exit(int status); void _Exit(int status); The exit() function calls registered cleanup functions (via atexit()), flushes buffers, and closes open streams before termination. The _Exit() function immediately terminates the program without performing any cleanup operations. Example 1: Using exit() Function Here's how exit() executes cleanup functions registered with atexit() − #include #include void ...
Read More