Found 33676 Articles for Programming

C/C++ Struct vs Class

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

419 Views

In C++ the structure and class are basically same. But there are some minor differences. These differences are like below.The class members are private by default, but members of structures are public. Let us see these two codes to see the differences.Example#include using namespace std; class my_class {    int x = 10; }; int main() {    my_class my_ob;    cout

Difference between void main and int main in C/C++

Akansha Kumari
Updated on 09-Jun-2025 15:28:49

37K+ Views

The main() function in C and C++ is anentry point of a program from where the execution begins. And there are two common ways available to define the main function; int main() and void main(). In this following article we will learn the differences between these two in detail. The main() function is the same like other functions, which takes arguments and returns some value. A point to keep in mind is that the execution of a program always begins from the main() function. When a program runs, the operating system calls the main() and the value returned from it ... Read More

How to generate different random numbers in a loop in C++?

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

4K+ Views

Let us see how to generate different random numbers using C++. Here we are generating random numbers 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 pseudo-random number generator algorithm. This function returns nothing.To get the number ... Read More

Returning multiple values from a C++ function

Aman Kumar
Updated on 12-Jun-2025 17:51:03

9K+ Views

In C++, we cannot return multiple values from a function directly. But, by using some methods (i.e., techniques), we can return multiple values from a function. How to Return Multiple Values from a Function? A function can return only one value using the return statement. We can return multiple values (more than one value) from a function by using the "call by address" and "call by reference" approach in the invoker function. Return Multiple Values from a Function Using Call by Address In C++, the call is an address, also known as a call by pointer. It passes ... Read More

Checking if a double (or float) is NaN in C++

Aman Kumar
Updated on 18-Jun-2025 18:47:53

14K+ Views

In this article we will check whether a double or floating point number is NaN (Not a Number) in C++. Checking if a Double or Floating Point Number is NaN To check, we can utilise the isnan() method. The isnan() function is available in the cmath library. This function was introduced in C++ version 11. So From C++11 next, we can use this function. The isnan() function is used to determine whether a double or floating point number is not-a-number (NaN) value. Return true if num is NaN, false otherwise. C++ Program to Check Double or Float Number is NaN ... Read More

How do you get assembler output from C/C++ source in gcc?

Aman Kumar
Updated on 30-Jun-2025 13:22:39

3K+ Views

In this article, we will see how to generate the assembler output from C or C++ code using GCC. What is GCC The GCC, which stands for GNU Compiler Collection, is a set of compilers and development tools available for various operating systems such as Linux, Windows, and a wide variety of other OSs (operating systems). It supports mostly C and C++, but also Objective-C, Ada, Go, Fortran, and D. The Free Software Foundation (FSF) created GCC and distributed it as totally free (as in libre) software. How to Get Assembler Output The GCC has a great feature that allows ... Read More

Order of evaluation in C++ function parameters

Aman Kumar
Updated on 04-Aug-2025 16:02:00

247 Views

In C++, when we pass multiple arguments to a function, a common question arises, in what order are these arguments evaluated? Is it from left to right, right to left, or does it depend on the compiler? In this article, we will learn how function parameter evaluation works in C++, why the order of evaluation is important, and how it can vary across different compilers. Is the Order of Evaluation Defined in C++? The C++ standard does not guarantee a fixed order of evaluation for function arguments. This means compilers are free to evaluate arguments from left to ... Read More

Java Program to create Stream from a String/Byte Array

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

590 Views

Create an input stream and set the string:DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream("pqrs tu v wxy z".getBytes()));The getBytes() method is used to convert a string into sequence of bytes and returns an array of bytes.Now return one single input byte:(char) inputStream.readByte()Exampleimport java.io.ByteArrayInputStream; import java.io.DataInputStream; public class Demo { public static void main(String[] args) throws Exception {    DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream("pqrs tu v wxy z".getBytes()));       System.out.print((char) inputStream.readByte());       System.out.print((char) inputStream.readByte());       inputStream.close();    } }OutputPq

How to sort an array with customized Comparator in Java?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

2K+ Views

Let’s say the following is our string array and we need to sort it:String[] str = { "Tom", "Jack", "Harry", "Zen", "Tim", "David" };Within the sort() method, create a customized comparator to sort the above string. Here, two strings are compared with each other and the process goes on:Arrays.sort(str, new Comparator < String > () {    public int compare(String one, String two) {       int val = two.length() - one.length();       if (val == 0)       val = one.compareToIgnoreCase(two);       return val;    } });Exampleimport java.util.Arrays; import java.util.Comparator; public class Demo ... Read More

Java Program to get duration between two time instants

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

298 Views

Create two time instants:Instant time1 = Instant.now(); Instant time2 = Instant.now().plusSeconds(50);Use between() to get the duration between two time instants:long resMilli = Duration.between(time1, time2).toMillis();Exampleimport java.time.Duration; import java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant time1 = Instant.now();       Instant time2 = Instant.now().plusSeconds(50);       long resMilli = Duration.between(time1, time2).toMillis();       System.out.println("Duration between two time intervals = "+resMilli);    } }OutputDuration between two time intervals = 50000

Advertisements