What is the output of the following code? [code] #include<stdio.h> int get_sum(int n) { int sm, i; for(i = 1; i <= n; i++)

Việt Quân

New member
What is the output of the following code?
Code:
#include<stdio.h>
int get_sum(int n)
{
      int sm, i;
      for(i = 1; i <= n; i++)
        sm += i;
      return sm;
}
int main()
{
    int n = 10;
    int ans = get_sum(n);
    printf("%d",ans);
    return 0;
}
A. 55
B. 45
C. 35
D. Depends on compiler
 
Answer: D
Explanation: Since the variable “sm” is not initialized to 0, it will produce a garbage value. Some compiler will automatically initialises variables to 0 if not initialised. In that case the value is 55. Hence the value depends on the compiler.
 

Tìm kiếm

Back
Top