Found 7197 Articles for C++

Determining how many digits there are in an integer in C++

Nishu Kumari
Updated on 30-May-2025 18:01:08

32K+ Views

Here we will see how to check how many digits are there in an integer in C++. At first we will see the traditional rule, then a shorter method and finally another approach to find the digit count. For example, given a single integer (which can be positive, negative, or zero): //Example 1 Input: 12345 Output: 5 //Example 2 Input: -789 Output: 3 Note: The minus sign (if present) is not counted as a digit. Determining how many digits there are in an integerWe can count the number of digits in an integer using different methods ... Read More

How to get memory usage under Linux in C++

Samual Sam
Updated on 30-Jul-2019 22:30:26

3K+ Views

Here we will see how to get the memory usage statistics under Linux environment using C++.We can get all of the details from “/proc/self/stat” folder. Here we are taking the virtual memory status, and the resident set size.Example#include #include #include #include #include using namespace std; void mem_usage(double& vm_usage, double& resident_set) {    vm_usage = 0.0;    resident_set = 0.0;    ifstream stat_stream("/proc/self/stat", ios_base::in); //get info from proc    directory    //create some variables to get info    string pid, comm, state, ppid, pgrp, session, tty_nr;    string tpgid, flags, minflt, cminflt, majflt, cmajflt;    string ... Read More

Functions that can’t be overloaded in C++

Anvi Jain
Updated on 30-Jul-2019 22:30:26

383 Views

In C++, we can overload the functions. But sometimes the overloading is not done. In this section, we will see what are the different cases, in which we cannot overload the functions.When function signatures are same, only the return type is different, then we cannot overload the function.int my_func() {    return 5; } char my_func() {    return 'd'; }When the member functions have the same name and same parameter list in a class, then they cannot be overloaded.class My_Class{    static void func(int x) {       //Something    }    void func(int x) {     ... Read More

How to generate a random number in C++?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

533 Views

Let us see how to generate random numbers using C++. Here we are generating a random number in range 0 to some value. (In this program the max value is 100).To perform this operation we are using the srand() function. This is in the C library. The function void srand(unsigned int seed) seeds the random number generator used by the function rand.The declaration of srand() is like below −void srand(unsigned int seed)It takes a parameter called seed. This is an integer value to be used as seed by the pseudorandom number generator algorithm. This function returns nothing.To get the number ... Read More

How does generic lambda work in C++14?

Samual Sam
Updated on 30-Jul-2019 22:30:26

285 Views

In C++11, the lambda was introduced. Lambdas are basically a part of, that can be nested inside other function call statements. By combining lambda expressions with the auto keyword, they can be used later.In C++14, these lambda expressions are improved. Here we can get the generalized or generic lambda. For example, if we want to create a lambda, that can add integers, add numbers, also concatenate strings, then we have to use this generalized lambda.Syntax of the lambda expression is looking like this −[](auto x, auto y) { return x + y; }Let us see one example to get the ... Read More

Generate random numbers using C++11 random library

Samual Sam
Updated on 30-Jul-2019 22:30:26

2K+ Views

In C++11, we can get the random library to generate random numbers. Here we have used random_device once to seed the random number generator object called mt. This random_device is slower than the mt19937, but we do not need to seed it. It requests for random data to the operating system.Example #include #include using namespace std; int main() {    random_device rd;    mt19937 mt(rd());    uniform_real_distribution dist(20.0, 22.0); //range is 20 to 22    for (int i=0; i> dist(mt) >> endl; }Output21.5311 21.7195 21.0961 21.9679 21.197 21.2989 20.6333 20.441 20.7124 20.2654 21.1877 20.4824 20.0575 20.9432 21.222 21.162 21.1029 20.2253 21.5669 20.3357

What is a free function in C++?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

376 Views

The C/C++ library function void free(void *ptr) deallocates the memory previously allocated by a call to calloc, malloc, or realloc. Following is the declaration for free() function.void free(void *ptr)This function takes a pointer ptr. This is the pointer to a memory block previously allocated with malloc, calloc or realloc to be deallocated. If a null pointer is passed as argument, no action occurs.Example#include #include #include using namespace std; int main () {    char *str;    /* Initial memory allocation */    str = (char *) malloc(15);    strcpy(str, "tutorialspoint");    cout

Convert an int to ASCII character in C/C++

Nishu Kumari
Updated on 30-May-2025 18:01:36

37K+ Views

In C and C++, every character like 'A', 'b', '3', or '@' is stored as a number called its ASCII value. For example, 'A' is 65, and 'a' is 97. Given an integer like 97, we can convert it to its corresponding ASCII character which is 'a'. In this article, we will learn how to write a C and C++ program to convert an integer into its ASCII character. For example, we're given any integer from 0 to 127 (because the ASCII table contains 128 characters), and we need to convert it into its corresponding ASCII character: Input: 65 ... Read More

How to use Reference Parameters in C++?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

241 Views

Here we will see how to pass reference of some variable in C++. Sometimes we call it as “Call by Reference”.The call by reference method of passing arguments to a function copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument.To pass the value by reference, argument reference is passed to the functions just like any other value. So accordingly you need to declare the function parameters as reference types as in ... Read More

How to check if input is numeric in C++?

Nishu Kumari
Updated on 30-May-2025 18:07:17

6K+ Views

Numeric input means a value that contains only digits from 0 to 9, without any letters or special characters. In this article, we'll show you how to write a C++ program to check whether the input is numeric. Let's understand this with a few examples: //Example 1: Input: 12345 Output: Valid numeric input //Example 2: Input: 12a5 Output: Not a numeric input We will cover two common ways to check if the input is numeric or not in C++. Using std::getline with std::isdigit Check Using stringstream to Parse ... Read More

Advertisements