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
Articles by Revathi Satya Kondra
Page 2 of 7
C 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 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 MoreAbsolute Difference of all pairwise consecutive elements in an array (C++)?
An array in C++ is a data structure used to store multiple values of the same type in a contiguous block of memory. To know that how values shift from one index to the next, a common and practical technique is to compute the absolute difference between each pair of consecutive elements. Absolute Difference The absolute difference is the positive distance between two numbers, regardless of which one is larger. It is computed using the abs() function from the library. Absolute difference between a and b is denoted as |a - b|. For example, |7 - 3| = ...
Read MoreHow do I define string constants in C++?
In C++, a string constant also called a string literal is a fixed sequence of characters enclosed in double quotes (" "). For example: "This is a string" and it is used to read-only memory, and its value cannot be changed during the program. Defining C++ Strings ConstantsYou can define your own named string constants by using: String Literals The const keyword The #define preprocessor directive The constexpr keyword(in modern C++) Let us understand each type of String Constant in C++ by ...
Read MoreHow to read a text file with C++?
In C++, you can read data from a text file using file handling features provided by the header. This is useful when you want your program to read input stored in a file instead of typing it every time. To do this, you use a special object called ifstream (input file stream), which helps your program open the file and read its contents line by line or word by word. Reading a text file is helpful when: You want to process saved data (like scores, settings, or logs). You want to ...
Read MoreWhat is the relation between auto and decltype in C++?
The auto and decltype serve different purposes so they don't map one-to-one. The auto is a keyword in C++11 and later that is used for automatic type deduction. The decltype type specifier yields the type of a specified expression. Unlike auto that deduces types based on values being assigned to the variable, decltype deduces the type from an expression passed to it. The value returned by decltype can directly be used to define another variable. The auto follows the rules of template parameter deduction. You can read more about these rule at Template Argument Deduction While decltype has rules it ...
Read MoreHow to append text to a text file in C++?
Appending text means adding new content to an existing file without removing its current content. In C++, this can be done by opening the file in append mode and writing the specified content to it. Steps to Append Text to a Text File You need to follow the below steps to open a file in append mode and append the content to it: First of all, you need to include the header file. Them, create an ofstream object to write to the file. Open the file in append mode using the ios::app flag. Use the
Read MoreWhat is the difference between a definition and a declaration in C++?
In C++, declaration and definition are often confused. A declaration tells the compiler about the name and type, while a definition allocates memory or provides implementation. In this article, we will understand their differences with examples. What is a Declaration in C++? A declaration means (in C or C++) that you are telling the compiler about type, size and in case of function declaration, type and size of its parameters of any variable, or user-defined type or function in your program. No space is reserved in memory for any variable in case of the declaration. Following is the syntax to ...
Read More