1
#include <stdio.h>

int main()
{
    int a, b, sum;
    sum = a + b; // initializing variable "sum" here gives incorrect answer to a+b
    printf("Enter value a: ");
    scanf("%d", &a);
    printf("Enter value b: ");
    scanf("%d", &b);

    printf("sum of %d + %d = %d\n\n", a, b, c);
    return 0;
}

This is a program that is supposed to sum two integers. I noticed that when I initialize the variable "sum" before I pass any arguments in my print and scan statements my output is not a correct sum of variables "a" and "b". But when I initialize variable "sum" after the print and scan statements the output is correct:

#include <stdio.h>

int main()
{
    int a, b, sum;
    printf("Enter value a: ");
    scanf("%d", &a);
    printf("Enter value b: ");
    scanf("%d", &b);
    sum = a + b; // variable "sum" is initialized after statement arguments
    printf("sum of %d + %d = %d\n\n", a, b, c);
    return 0;
}

So my questions are:

  1. Why does a variable need to be initialized after the statement arguments?
  2. What is the actual logic behind the order of variable declarations and initializations?
4
  • 3
    The calculation of sum = a + b; occurs before either a or b has a defined value. The calculation isn't held over until the values are set; it is done at the point in time represented by its position in the source code. Move the addition after the two inputs. Don't forget to check both input operations to ensure they succeeded before continuing with your calculations. Commented Feb 8, 2017 at 3:47
  • 1
    In both code samples the identifier c used in printf() is not declared anywhere. In the first code sample, in the statement sum=a+b the values of a and b are undefined. Commented Feb 8, 2017 at 3:48
  • 3
    Detail: int a,b,sum; sum=a+b; is not initializing sum. It is assigning sum. Assigning sum to a sum of unknown values, which is UB. Commented Feb 8, 2017 at 3:49
  • You know that the computer does the things in the order they're written, right? Commented Feb 8, 2017 at 3:52

2 Answers 2

3

First of all, the variable 'c' has never been declared, and the compiler is giving the error. Revise your code.

As for the 'sum' variable: the problem is not initialization, but evaluation. In C the order of expression evaluation is known as 'applicative' order, means all inner expressions have to be evaluated before the outer one.

In your case the oreder is: we need to assign the 'sum' a value, thus, it has to be evaluated! The addition operator ('+') is called. But there're two more variables the 'a' and the 'b'. However, they don't have any values stored. They have not been initialized yet! The result of the intention to take the data from uninitialized varialbe is unknown (called undefined behaviour).

If you read the data to the 'a' and 'b' variables before invokation of the assignment expression, the same oreder of evaluation results in a correct answer, because 'a' and 'b' store some defined numbers.

So... When writing in C you should always bear in mind that the order of evaluation is applicative. It's very important if you start to play with side effects. Also, the order of evaluation of the function parameters is undefined.

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

Comments

2

In the first code of yours ,you have just declared int variables a and b and not initialized them with any certain value so they basically contains Garbage value , and you instantly add them in sum variable which will now contain garbage value . After this you are taking input a and b but you add them into sum variable so sum has garbage value and you bring new variable c which is not declared which will throw error on compile time

In 2nd code of yours ,you declared a and b,then you scan them and put input values into a and b and add these fresh values to sum but you still print c value which is undeclared . If you write

  printf("sum of %d + %d = %d\n\n",a,b,sum);

Instead of

 printf("sum of %d + %d = %d\n\n",a,b,c);

you can now see the output. 1st code prints garbage value and 2nd one prints actual sum

Why does a variable need to be initialized after the statement arguments?

If you don't initialize them with certain value by default they will be holding some garbage value,and operating on garbage value is useless.Also it doesn't matter where you initialize them,it depends on your functionality of code. I can also do it as int a =5; which is before the statements. In order to avoid this things either you initialize them when you declare them or take input and assign value to them so they won't be holding garbage values.

What is the actual logic behind the order of variable declarations and initializations?

Above explaination says itself.

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.