C Articles

Found 953 articles

C/C++ Ternary Operator

Narendra Kumar
Narendra Kumar
Updated on 18-Mar-2026 5K+ Views

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 More

Anti Clockwise spiral traversal of a binary tree?

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Mar-2026 351 Views

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 More

A comma operator question in C/C++ ?

karthikeya Boyini
karthikeya Boyini
Updated on 18-Mar-2026 367 Views

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 More

Difference between %p and %x in C/C++

Nishtha Thakur
Nishtha Thakur
Updated on 18-Mar-2026 10K+ Views

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 More

Why are global and static variables initialized to their default values in C/C++?

karthikeya Boyini
karthikeya Boyini
Updated on 18-Mar-2026 3K+ Views

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 More

C Program to Find if there is any subarray with a sum equal to 0

C
AYUSH MISHRA
AYUSH MISHRA
Updated on 15-Mar-2026 516 Views

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 More

Program to Rotate a matrix by 90 degrees in the clockwise direction in C

C
AYUSH MISHRA
AYUSH MISHRA
Updated on 15-Mar-2026 4K+ Views

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 More

C program to Implement Kadane’s Algorithm

C
Akansha Kumari
Akansha Kumari
Updated on 15-Mar-2026 490 Views

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 More

C Program to Find an Automorphic Number

C
AYUSH MISHRA
AYUSH MISHRA
Updated on 15-Mar-2026 5K+ Views

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 More

Maximum count of characters that can replace ? by at most A 0s and B 1s with no adjacent duplicates

Thanweera Nourin A V
Thanweera Nourin A V
Updated on 15-Mar-2026 805 Views

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
Showing 1–10 of 953 articles
« Prev 1 2 3 4 5 96 Next »
Advertisements