GATE CS 2017 Set 1 — Question 55
Go beyond PYQs with Success TrackerAI-powered personalised practice and doubt support. Unlimited practice on eligible plans; AI usage limits apply.NAT+2 / -0MediumData Types & OperatorsC ProgrammingProgramming & Data StructuresFunctions & RecursionStorage Classes
Programming & Data Structures → C Programming → Data Types & Operators
Last updated
Question
The output of executing the following C program is __________.
#include <stdio.h>
int total(int v) {
static int count = 0;
while(v) {
count += v&1;
v >>= 1;
}
return count;
}
void main() {
static int x = 0;
int i = 5;
for(; i > 0; i--) {
x = x + total(i);
}
printf("%d\n", x);
}
Correct answer
23 to 23
Solution
Let's trace the execution of the program. The key is to note that both the
count variable in the total function and the x variable in the main function are declared as static. This means they retain their values between function calls and iterations.countintotal: Initialized to 0 once. Its value is cumulative across all calls tototal.xinmain: Initialized to 0 once.
for loop in main iterates for i = 5, 4, 3, 2, 1.1.i = 5:
total(5)is called.v = 5(binary101).countis initially 0.- The
whileloop intotalcounts the set bits invand adds them to the staticcount. - Number of set bits in 5 is 2.
countbecomes0 + 2 = 2.totalreturns the new value ofcount, which is 2.- In
main,x = x + total(5)becomesx = 0 + 2 = 2.
total(4)is called.v = 4(binary100).countis currently 2.- Number of set bits in 4 is 1.
countbecomes2 + 1 = 3.totalreturns 3.- In
main,x = x + total(4)becomesx = 2 + 3 = 5.
total(3)is called.v = 3(binary11).countis currently 3.- Number of set bits in 3 is 2.
countbecomes3 + 2 = 5.totalreturns 5.- In
main,x = x + total(3)becomesx = 5 + 5 = 10.
total(2)is called.v = 2(binary10).countis currently 5.- Number of set bits in 2 is 1.
countbecomes5 + 1 = 6.totalreturns 6.- In
main,x = x + total(2)becomesx = 10 + 6 = 16.
total(1)is called.v = 1(binary1).countis currently 6.- Number of set bits in 1 is 1.
countbecomes6 + 1 = 7.totalreturns 7.- In
main,x = x + total(1)becomesx = 16 + 7 = 23.
printf statement prints the final value of x, which is 23.Continue learning with Success Tracker
A step still unclear? Work through it with support
Use Success Tracker to ask about the reasoning, then try another GATE CS question to check your understanding.
AI-powered practice· Unlimited practice on eligible plans
- PYQs with solutions
- Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
- Practice that adapts
- Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
- AI doubt support
- Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.
Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.
This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.
More questions on C Programming
2026 Set 2 Q12The set T represents various traversals over binary tree. The set S represents the order of…2026 Set 2 Q19Consider the following three ANSI-C programs, P1, P2, and P3. P1 [code] P2 [code] **P3**…2026 Set 1 Q23Let be an odd number greater than 100. Consider a binary minheap with elements stored in an…2026 Set 1 Q24Consider a hash table that is initially empty. The hash table is maintained…2026 Set 1 Q27Consider the following C statements: Which of the following options is/are correct? [figure]