-4

I'm currently preparing for gate; I have come across a question

#include<stdio.h>
main()
{
    static int var=6;
    printf("%d\t",var--);
    if(var)
        main();
}

The output is 6 5 4 3 2 1

I wanna know why it terminated after 1?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 4
    Note that Standard C has required an [explicit return type for `main()`](https://stackoverflow.com/q/204476/15168) — and it should be `int` — for the whole of the current millennium. Use `int main(void)` unless you're going to use the command line arguments, in which case use `int main(int argc, char **argv)` or equivalent. – Jonathan Leffler Jan 01 '20 at 09:11
  • 3
    The question seems to be using code from a document [C Sample Technical Questions](https://www.scribd.com/document/222587837/C-Sample-Technical-Questions) claiming that memorizing such trivia will make you successful in a job interview. The easiest way to pass a technical interview is to *learn* the language, preferably from a **book**, and use it, before going to an interview. – Antti Haapala -- Слава Україні Jan 01 '20 at 09:29
  • `if(x)` is equivalent to `if ( x != 0 )` – M.M Jan 01 '20 at 10:00

2 Answers2

2

An if statement always tests to see if the expression inside the parentheses evaluates to true.

In this case, var is an positive integer, so it evaluates to true. Since 0 always evaluates to false, as soon as var = 0 the if statement evaluates to false and the loop exits.

Note that if(var) is not specific to C (re the question title), it applies to many languages.

half of a glazier
  • 1,864
  • 2
  • 15
  • 45
  • 1
    "All languages" is a sweeping claim — I'd dispute that it applies to Pascal, for instance. It applies to many languages, especially those influenced by C, but "all" is not justifiable. (Pascal would only allow `if var then` (parentheses not required) if `var` is of type `boolean`; C is far more generous about which types can appear in the condition, allowing any scalar type to be used.) – Jonathan Leffler Jan 01 '20 at 09:20
  • You used 'loop' for the recursion too — see my notes to what is currently the [other answer](https://stackoverflow.com/a/59551052/15168). – Jonathan Leffler Jan 01 '20 at 09:28
0

if (var) checks whether var is not zero! If var is zero it finish.

Khalil Harar
  • 121
  • 6
  • 1
    An `if` statement is not a loop. Maybe you should say "If `var` is zero, it skips the body of the `if` statement, going to the `else` clause if there is one", or something similar. – Jonathan Leffler Jan 01 '20 at 09:09
  • yes it is not loop but it calls recursive function if its true – Khalil Harar Jan 01 '20 at 09:13
  • 3
    Being sloppy in your terminology won't get you credit on SO. You need to be careful and precise. Recursion is also not a a loop. You're right that recursion is involved, and it's simple tail recursion, so it could be replaced by a loop. But it is not itself a loop — there is no `for`, `while` or `do … while` involved (nor `goto`, but that's a whole separate bag'o'worms!). – Jonathan Leffler Jan 01 '20 at 09:17