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 Ravi Ranjan
Page 3 of 12
_Noreturn function specifier in C
The _Noreturn function specifier in C indicates to the compiler that a function will never return control to its caller. Functions marked with _Noreturn either terminate the program (using exit()) or run indefinitely (infinite loops). This helps the compiler optimize code and detect unreachable statements. Syntax _Noreturn return_type function_name(parameters) { /* Function body that never returns */ } Example 1: _Noreturn with exit() Function When a function marked with _Noreturn calls exit(), it terminates the program without returning control to the caller − #include #include ...
Read MoreWhy array index starts from zero in C/C++ ?
An array arr[i] is interpreted as *(arr+i). Here, arr denotes the address of the first array element or the 0th index element. So *(arr+i) means the element is at i distance from the first element of the array. Therefore, array index starts from 0 as initially i is 0 which means the first element of the array. In this article, we will see examples of C code to understand the reason why an array starts from index 0. Syntax data_type array_name[array_size]; // Accessing element: array_name[index] where index starts from 0 Why Array Index Starts ...
Read MoreWhy do we assume strncpy insecure in C/C++?
The strncpy() function is used to copy a specified number of characters from a source string to a destination string. While it might seem safer than strcpy() because it limits the number of characters copied, strncpy() has several security vulnerabilities that make it unsafe for general use. Syntax char *strncpy(char *destination, const char *source, size_t n); Parameters: destination: Pointer to the destination array where content is to be copied source: Pointer to the source string to be copied n: Maximum number of characters to be copied from source Why strncpy() is Insecure ...
Read MoreAccessing array out of bounds in C/C++
An array in C is a fixed-size sequential collection of elements of the same data type where all elements are stored in contiguous memory. When an array is accessed out of bounds, undefined behavior occurs in C, unlike higher-level languages that throw exceptions. Syntax type arrayName[size]; // Valid indices: 0 to (size-1) // Out of bounds: index < 0 or index >= size Accessing Out of Bounds Memory Accessing out-of-bounds memory means trying to access an array index outside its valid range (index < 0 or index >= array size). This returns garbage values ...
Read MoreHow to write long strings in Multi-lines C/C++?
To write long strings in multi-lines, you can use the 'newline character' or 'string concatenation'. It increases the readability of the code, makes the code look clean, and you can avoid horizontal scrolling. In this article, we have a long string and our task is to write the long string in multi-lines in C. Syntax // Using newline character char str[] = "First lineSecond lineThird line"; // Using string concatenation char str[] = "First part of string " "Second part of string ...
Read MoreWhy C/C++ variables doesn’t start with numbers
In C/C++, a variable name can have alphabets, numbers, and the underscore( _ ) character. There are some keywords in the C/C++ language. Apart from them, everything is treated as an identifier. Identifiers are the names of variables, constants, functions, etc. Syntax Valid identifier naming rules in C − // Valid identifier patterns: [a-zA-Z_][a-zA-Z0-9_]* // Valid examples: variable_name, _count, num1, my_var // Invalid examples: 1variable, 2name, 3_count Why Variables in C/C++ Can't Start with Numbers? In C and C++, variable names (also known as identifiers) cannot start with a digit due to ...
Read MoreBinary Search Tree to Greater Sum Tree in C++
In this article, we are given a binary search tree. Our task is to transform the given BST to a Greater Sum tree. A Greater Sum Tree with respect to the given BST is a tree where each node's value is replaced by the sum of all values greater than that node's value in the original BST. Below is an example scenarios to convert the given BST to a Greater Sum Tree: Example Scenario Input: BST inorder traversal= {0, 1, 2, 3, 4, 5, 6, 7, 8} Output: Greater Sum Tree = {36, 35, 33, 30, 26, 21, 15, ...
Read MoreDifference Between Linear Search and Binary Search
A linear search compares the target element with each array element, while a binary search uses divide-and-conquer method to efficiently search for the target element. In this article, we will compare linear search and binary search. What is Linear Search? Linear search is a sequential searching algorithm where we traverse every element within the input array and compare it with the target element to be found. If the target element is found, we return true and the index, and return false if the element is not found in the given array. Below is an animation of working of linear ...
Read MoreC++ Program to Find the Number of occurrences of a given Number using Binary Search approach
In this article, our task is to find the number of occurrences of a given number using binary search. The binary search algorithm works on the divide-and-conquer principle as it keeps dividing the array in half before searching. To search for an element in an array using binary search, it should be sorted. In the sorted array, we find the middle element and compare it with the element that has to be searched, and based on the comparison, we either search in the left or right sub-array or return the middle element. Following are some example scenarios: Scenario ...
Read MoreMaximum Sum SubArray using Divide and Conquer in C++
In this article, we have an array of integers. Our task is to find the maximum sum of a sub-array using divide and conquer algorithm. A sub-array is a continuous part of an array with consecutive array elements. For example: {2, 3, 4} is sub-array of {1, 2, 3, 4, 5} but {1, 4, 5} is not. What is Divide and Conquer Algorithm? The Divide and Conquer algorithm divides a problem into smaller sub-problems, and then each sub-problem is solved independently. We keep dividing the sub-problems till we reach a stage where no more division ...
Read More