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 12 of 597
Can 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 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 MoreLevels of Pointers in C/C++
In C, pointers have multiple levels, which means a pointer can point to another pointer − so the chains of indirection can go on and on. For instance, a pointer to a variable's address is stored at "*ptr" (single-level pointer) while, at "**ptr", the address of another pointer is kept (a double-level pointer), and so on. This is useful in allocating memory dynamically, working with multi-dimensional arrays, and handling complicated data structures. Syntax // Single level pointer int *ptr = &variable; // Double level pointer int **pptr = &ptr; // Triple level ...
Read MoreStandard Size of character (\'a\') in C/C++ on Linux
In C, every character including 'a' is stored using a specific size in memory. On most systems including Linux, the size of a character is 1 byte. This means that any character (like 'a') occupies 1 byte (8 bits) of memory. To determine how much memory is used by the character 'a', we can use the sizeof() operator, which returns the size in bytes of a variable or data type. Syntax sizeof(expression) sizeof(datatype) Method 1: Using sizeof with Character Literal This approach checks the size of the character literal 'a' using the sizeof() ...
Read MoreFind out the current working directory in C/C++
To find the Current Working Directory (CWD) in C or C++ is like asking your program: "Hey, where am I right now?". Simply we can say that it is like a folder of your program which is present and used to operate in. We can use functions like getcwd() from unistd.h in C/C++ or filesystem::current_path() from C++17. Below are the list of the ways to achieve this − Using getcwd() in C/C++ Using filesystem in C++17 Using getcwd() Function in C In C, we use the ...
Read More