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
C Articles
Found 953 articles
C/C++ Ternary Operator
The ternary operator (?:) in C/C++ is a shorthand for an if-else statement. It is the only operator in C/C++ that takes three operands, which is why it is called "ternary". Syntax (expression-1) ? expression-2 : expression-3 This operator returns one of two values depending on the result of an expression. If expression-1 evaluates to Boolean true, then expression-2 is evaluated and its value is returned as the final result. Otherwise, expression-3 is evaluated and its value is returned. Ternary Operator Flow condition ? ...
Read MoreAnti Clockwise spiral traversal of a binary tree?
Here we will see one interesting problem. We have a binary tree and we have to traverse it in an anti-clockwise manner. The traversal alternates between printing levels from right-to-left (starting from the top) and left-to-right (starting from the bottom), creating a spiral-like anti-clockwise pattern. For the binary tree shown below, the anti-clockwise traversal sequence is − 1, 8, 9, 10, 11, 12, 13, 14, 15, 3, 2, 4, 5, 6, 7 Binary Tree — Anti-Clockwise Traversal ...
Read MoreA comma operator question in C/C++ ?
The comma (, ) in C/C++ programming language has two contexts − As a separator − Used to separate variable declarations, function arguments, and initializer values. In this context, the comma is not an operator. As an operator − The comma operator is a binary operator that evaluates the first expression, discards its result, then evaluates and returns the value of the second expression. This operator has the lowest precedence of all C/C++ operators — even lower than the assignment operator. Syntax result = (expr1, expr2); // ...
Read MoreDifference between %p and %x in C/C++
Here we will see what are the differences between %p and %x in C or C++. The %p format specifier is used to print pointer values (memory addresses), while %x is used to print unsigned integers in hexadecimal format. Though pointers can also be displayed using %u or %x, the correct and portable way to print a pointer is %p. The visible difference is that %p prints with leading zeros and is platform-width aware (16 hex digits on 64-bit systems, 8 on 32-bit), while %x prints only the significant digits without padding. Syntax printf("%p", pointer); ...
Read MoreWhy are global and static variables initialized to their default values in C/C++?
Global and static variables are initialized to their default values because it is mandated by the C and C++ standards. The compiler assigns zero at compile time, and it costs nothing extra to do so. Both static and global variables behave the same way in the generated object code — they are allocated in the .bss segment (Block Started by Symbol), and when the program is loaded into memory, the OS zeroes out this entire segment automatically. In contrast, local (automatic) variables are stored on the stack and are not initialized — they contain whatever garbage value was previously ...
Read MoreC Program to Find if there is any subarray with a sum equal to 0
A subarray is a contiguous part of the array. The sum of elements in a subarray is the cumulative sum of elements of a subarray. For example: in the given array [1, 2, 3, 4, 5, 6] the subarray is [3, 4, 5]. In this article, we are given an array and need to find if any subarray has a sum equal to zero using C. Problem Statement Example 1 Input: arr[] = {4, 2, -3, 1} Output: Yes Explanation: The subarray {2, -3, 1} has a sum equal to 0. Example ...
Read MoreProgram to Rotate a matrix by 90 degrees in the clockwise direction in C
In this article, we are given an N x N matrix, and our task is to rotate it by 90 degrees clockwise in C. Syntax void rotateMatrix(int matrix[N][N]); // For brute force approach void rotateMatrixInPlace(int matrix[N][N]); // For in-place rotation approach Example Input: [1 2 3], [4 5 6], [7 8 9] Output: [7 4 1], [8 5 2], [9 6 3] Below are different approaches to rotate a matrix by 90 degrees in the clockwise direction − Using Brute Force Approach ...
Read MoreC program to Implement Kadane’s Algorithm
We are given an array of integers, and we need to find the maximum sum of a contiguous subarray using Kadane's Algorithm. Kadane's Algorithm is an efficient way to find the maximum subarray sum in O(n) time complexity. For example, in the array {-2, 1, -3, 4, -1, 2, 1, -5, 4}, the subarray [4, -1, 2, 1] has the maximum sum 6. What Is Kadane's Algorithm? Kadane's algorithm is a popular and optimal algorithm used to find the maximum sum of a contiguous subarray in a given array of integers. This algorithm efficiently solves the Maximum Subarray ...
Read MoreC Program to Find an Automorphic Number
In C, an automorphic number is a positive integer whose square ends with the number itself. For example, 52 = 25 (ends with 5), and 252 = 625 (ends with 25). This article demonstrates how to check whether a given number is automorphic in C programming. Syntax int isAutomorphicNumber(int num); Examples of Automorphic Numbers Consider the following examples to understand automorphic numbers − 5: 52 = 25 (ends with 5) − Automorphic 25: 252 = 625 (ends with 25) − Automorphic 7: 72 = 49 (ends with 49, not 7) − Not ...
Read MoreMaximum count of characters that can replace ? by at most A 0s and B 1s with no adjacent duplicates
In this problem, we need to replace '?' characters in a string with '0's and '1's such that no two adjacent characters are the same, while maximizing the number of replacements using at most A zeros and B ones. Syntax int maximumChar(char *str, int aCount, int bCount); Problem Statement Given a string containing '*' and '?' characters, and two integers A and B representing available 0s and 1s, find the maximum number of '?' characters that can be replaced without creating adjacent duplicates. Algorithm The approach involves the following steps − ...
Read More