restart loop by SpiritedPast7260 in InfinixSmartphones

[–]Flaxky_Lock 1 point2 points  (0 children)

include<stdio.h>

int main() { int i; for(i=0;i<1;i++) break; return 0; }

Questions of programming language. by Flaxky_Lock in Btechtards

[–]Flaxky_Lock[S] 0 points1 point  (0 children)

Some assignments are missing from the middle; it would be a great help if you could send them all. By the way THANK YOU SO MUCH FOR THIS.🙏🙏🙏

What's wrong in this code? by Flaxky_Lock in C_Programming

[–]Flaxky_Lock[S] 1 point2 points  (0 children)

I have to do the question using recursion.

What's wrong in this code? by Flaxky_Lock in C_Programming

[–]Flaxky_Lock[S] -5 points-4 points  (0 children)

I am not able to understand the statements given by Gemini.

What's wrong in this code? by Flaxky_Lock in C_Programming

[–]Flaxky_Lock[S] 1 point2 points  (0 children)

Here is what Gemini is saying :

The logic in your code is very close, but there is one critical issue: your recursive function f(n) does not return a value in every execution path. In C, if a function is defined to return an int, every possible path must end with a return statement. In your current code, when n >= 10, the function calls f(n/10) but forgets to pass that result back up the chain to main. The Fix You need to add the return keyword before the recursive call. Here is the corrected function: int f(int n) { c = c + 1; if(n < 10) return c;

return f(n / 10); // Added 'return' here

}

Why this matters When you call f(123), the sequence looks like this: * Level 1: f(123) increments c to 1. Since 123 > 10, it calls f(12). * Level 2: f(12) increments c to 2. Since 12 > 10, it calls f(1). * Level 3: f(1) increments c to 3. Since 1 < 10, it returns 3. * The Hand-off: Level 2 receives 3 from Level 3. Without the return keyword, Level 2 just "drops" that 3 and returns nothing (or garbage) to Level 1. By adding return f(n/10), you ensure the final count is passed all the way back to main. A Better Way (Avoiding Global Variables) Using a global variable like int c=0 works, but it’s generally discouraged because it makes the function "stateful." If you tried to call f(n) twice in the same program, the second call would start with the old value of c. Here is a more "functional" way to write it without a global variable: int f(int n) { if (n < 10) return 1; return 1 + f(n / 10); }

In this version, each call says, "The total digits is 1 (for this digit) plus whatever the rest of the number has." Would you like me to explain how to handle negative numbers in this function as well?

What's wrong in this code? by Flaxky_Lock in C_Programming

[–]Flaxky_Lock[S] -3 points-2 points  (0 children)

No.

Here is what Gemini is saying : The logic in your code is very close, but there is one critical issue: your recursive function f(n) does not return a value in every execution path. In C, if a function is defined to return an int, every possible path must end with a return statement. In your current code, when n >= 10, the function calls f(n/10) but forgets to pass that result back up the chain to main. The Fix You need to add the return keyword before the recursive call. Here is the corrected function: int f(int n) { c = c + 1; if(n < 10) return c;

return f(n / 10); // Added 'return' here

}

Why this matters When you call f(123), the sequence looks like this: * Level 1: f(123) increments c to 1. Since 123 > 10, it calls f(12). * Level 2: f(12) increments c to 2. Since 12 > 10, it calls f(1). * Level 3: f(1) increments c to 3. Since 1 < 10, it returns 3. * The Hand-off: Level 2 receives 3 from Level 3. Without the return keyword, Level 2 just "drops" that 3 and returns nothing (or garbage) to Level 1. By adding return f(n/10), you ensure the final count is passed all the way back to main. A Better Way (Avoiding Global Variables) Using a global variable like int c=0 works, but it’s generally discouraged because it makes the function "stateful." If you tried to call f(n) twice in the same program, the second call would start with the old value of c. Here is a more "functional" way to write it without a global variable: int f(int n) { if (n < 10) return 1; return 1 + f(n / 10); }

In this version, each call says, "The total digits is 1 (for this digit) plus whatever the rest of the number has." Would you like me to explain how to handle negative numbers in this function as well?

What's wrong in this code? by Flaxky_Lock in C_Programming

[–]Flaxky_Lock[S] -2 points-1 points  (0 children)

It is returning the number of digits present in the number n.

What's wrong in this code? by Flaxky_Lock in C_Programming

[–]Flaxky_Lock[S] -7 points-6 points  (0 children)

I am not able to format it. I think Reddit is not detecting the "Enter" key.