What Should main() Return in C/C++

karthikeya Boyini
Updated on 26-Jun-2020 07:46:45

5K+ Views

The return value of main() function shows how the program exited. The normal exit of program is represented by zero return value. If the code has errors, fault etc., it will be terminated by non-zero value.In C++ language, the main() function can be left without return value. By default, it will return zero.Here is the syntax of main() function in C language, int main() {    ….    return 0; }Here is an example of main() function in C language, Example Live Demo#include int main() {    int a = 10;    char b = 'S';    float c = ... Read More

fgets and gets in C

Samual Sam
Updated on 26-Jun-2020 07:46:12

2K+ Views

fgets()The function fgets() is used to read the string till the new line character. It checks array bound and it is safe too.Here is the syntax of fgets() in C language, char *fgets(char *string, int value, FILE *stream)Here, string − This is a pointer to the array of char.value − The number of characters to be read.stream − This is a pointer to a file object.Here is an example of fgets() in C language, Example Live Demo#include #define FUNC 8 int main() {    char b[FUNC];    fgets(b, FUNC, stdin);    printf("The string is: %s", b);    return 0; }OutputThe ... Read More

Malloc vs New in C/C++

karthikeya Boyini
Updated on 26-Jun-2020 07:45:24

6K+ Views

malloc()The function malloc() is used to allocate the requested size of bytes and it returns a pointer to the first byte of allocated memory. It returns null pointer, if fails.Here is the syntax of malloc() in C++ language, pointer_name = (cast-type*) malloc(size);Here, pointer_name − Any name given to the pointer.cast-type − The datatype in which you want to cast the allocated memory by malloc().size − Size of allocated memory in bytes.Here is an example of malloc() in C language, Example Live Demo#include #include int main() {    int n = 4, i, *p, s = 0;    p = ... Read More

isprint Function in C++

Samual Sam
Updated on 26-Jun-2020 07:42:01

92 Views

The function isprint() is predefined function and it checks that the passed characters are printable or not. It returns non-zero value, if successful otherwise, zero. This function is declared in “cctype” header file.Here is the syntax of isprint() in C++ language,int isprint(int character);Here,character − The character is to be checked.Here is an example of isprint() in C++ language,Example Live Demo#include #include using namespace std; int main() {    int val1 = 28;    int val2 = 's';    int val3 = '';    if(isprint(val1))    cout

Difference Between x++ and ++x in Java Programming

Arjun Thakur
Updated on 26-Jun-2020 07:41:18

751 Views

x++ automatically handles the type casting where as x= x + 1 needs typecasting in case of x is not an int variable. See the example below.Example Live Demopublic class Tester {    public static void main(String args[]) {       byte b = 2;       //Type casting is required       //as 1 is int and b is byte variable       b = (byte) (b + 1);       System.out.println(b);       byte b1 = 2;       //Implcit type casting by the compiler       b1++;       System.out.println(b1);    } }Output3 3

JVM Creates Object of Main Class in Java

Ankith Reddy
Updated on 26-Jun-2020 07:40:55

539 Views

As we know that Java needs main() method to be static in the public class to make it executable. The prime reason for this requirement is to make JVM enable to call the main() method without creating an object. That simply means that JVM does not create the object of the Main class which contains the main() method. To justify the same, we can make the Main class containing the main method as abstract and program still runs.Following example showcases the same. Here we have made the main class abstract.Exampleabstract public class Tester {    public static void main(String args[]) ... Read More

Fibonacci of Large Number in Java

George John
Updated on 26-Jun-2020 07:39:23

784 Views

Fibonacci numbers of Fibonacci series grows exponentially and can be very large for large numbers like 500 or 1000. To handle such number, long data type is not sufficient. BigInteger can handle large number easily. BigInteger is useful in scenarios where calculations results in data which is out of limit for available primitive data types. See the example below of getting Fibonacci number of 100 and 1000.Example Live Demoimport java.math.BigInteger; public class Tester {    public static void main(String args[]) {       System.out.println("Fibonacci of 100: ");       System.out.println(fibonacci(100));       System.out.println("Fibonacci of 1000: ");     ... Read More

Find Frequency of Each Word in a String in Java

Chandu yadav
Updated on 26-Jun-2020 07:38:47

2K+ Views

In order to get frequency of each in a string in Java we will take help of hash map collection of Java.First convert string to a character array so that it become easy to access each character of string.Now compare for each character that whether it is present in hash map or not in case it is not present than simply add it to hash map as key and assign one as its value.And if character is present than find its value which is count of occurrence of this character in the string (initial we put as 1 when it ... Read More

Find Max and Min Values in Array of Primitives Using Java

Ankith Reddy
Updated on 26-Jun-2020 07:37:59

711 Views

This example shows how to search the minimum and maximum element in an array by using Collection.max() and Collection.min() methods of Collection class.Example Live Demoimport java.util.Arrays; import java.util.Collections; public class Main {    public static void main(String[] args) {       Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5};       int min = (int) Collections.min(Arrays.asList(numbers));       int max = (int) Collections.max(Arrays.asList(numbers));       System.out.println("Min number: " + min);       System.out.println("Max number: " + max);    } }OutputThe above code sample will produce the following result.Min number: 1 Max number: 9Another ... Read More

How Java Objects Are Stored in Memory

Arjun Thakur
Updated on 26-Jun-2020 07:37:24

4K+ Views

A stack and a heap are used for memory allocation in Java. However, the stack is used for primitive data types, temporary variables, object addresses etc. The heap is used for storing objects in memory.Stacks and heaps in Java are explained in more detail as follows −Stack in JavaStacks are used to store temporary variables, primitive data types etc. A block in the stack exists for a variable only as long as the variable exists. After that, the block data is erased and it can be used for storing another variable.

Advertisements