0

I wrote the below code to find the sum of all digits in C, and when I compiled and ran it, it was successful. But, only later I realized that i had not entered any value for the variable 'n' in the for loop's condition. I'm confused on how this program works, even when there is no value assigned to the condition variable. I would like to be clarified of the same.

#include<stdio.h>
void main()
{
int no,a,b,n,sum=0;
printf("Enter the number to be added");
scanf("%d",&no);
for(int i=0;i<n;i++)
    {
     a=no%10;
     b=no/10;
    sum=a+sum;
    no=b;
    }   
printf("The sum is %d",sum);
}
3
  • 2
    it has random value, uninitialized, undefined behaviour. Commented Mar 6, 2017 at 14:56
  • can you please elaborate? Commented Mar 6, 2017 at 14:58
  • Augment the warning level for your compilation. Any decent compiler should warn you on the fact that n isn't initialized. Commented Mar 6, 2017 at 15:19

3 Answers 3

3

I'm confused on how this program works

Well, "works" is a very poor observation / decision here. This is undefined behavior.

You're attempting to use the value of an automatic local variable n while it is indeterminate. This invokes the UB.

To quote the C11 standard, chapter §6.7.9

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. [...]

So, in your case, n meets the criteria described above, and hence the content is indeterminate.

Now, after that, in case you try to use a variable while it holds indeterminate value and either

  • does not have the address taken
  • can have trap representation

the usage will lead to undefined behavior. That is exactly the case here.

That said, for a hosted environment, the conforming signature of main() is int main(void), at least.

Sign up to request clarification or add additional context in comments.

2 Comments

can you please elaborate?
@ArunRajasekar Updated, hope it helps.
2

An uninitialized variable MUST contain some value - every address in a computer must have some combination of 1's and 0's even if those values are useless. Therefore it is given a random one when first created if you do not initialize it to something yourself. Often it isn't even "given" data so much as it "takes on" whatever value was at the address it was given to live at when created, but different environments will handle this "non-initialization" differently. You are essentially getting lucky right now under your current conditions.

For that reason this kind of code is considered to have undefined behavior because you are not guaranteed to get lucky like that in every situation. Be safe and always initialize values for variables because this can be a hard thing to catch later when code that used to work suddenly doesn't.

1 Comment

"Be safe and always initialize values for variables because ..." would not help that much as code does not check the return value of scanf() which is essential for good code. scanf("%d",&no); may leave no in an unspecified state should a rare input error occur - prior initialization does not help in that case.
1

n is not initialized, so is worth anything present at the allocated memory location when run. So if you're lucky, after a few iterations no is 0 and the result is correct but it's just luck.

In your case, you don't need n, just stop when division yields 0 (that's probably why you forgot to initialize n)

while(no!=0)
{
 a=no%10;
 b=no/10;
 sum+=a;
 no=b;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.