15,560,511 members
See more:
so i have to write a program in C for my info class with 2 variables a and b that are lets say 1 and 5 and i must interchanged them in 5 and 1. so i wrote this program :

int main() {
double a, b;
a = a - b;
b = a + b;
a = b - a;
printf("Dupa interschimbare, a = %.2lf", a);
printf("Dupa interschimbare, b = %.2lf", b);
return 0;

and i m getting this error that "a" is used uninitialized in this function and " b" is used uninitialized in this function

What I have tried:

i just dont get it pls help me solve this
Posted
Updated 30-Oct-22 3:11am

## Solution 1

Yes because you have declared both `a` and `b`, but you have not given them any values. So change that line to:
C++
```double a = 1;
double b = 5;```

## Solution 2

The more normal way to swap them is to use a temporary variable:
C
```int a = 1;
int b = 5;
int tmp = a;
a = b;
b = tmp;```
This is a little safer when using floating point values as you can get distorted results due to imprecise arithmetic caused by the way floating point values are stored. The difference is normally pretty tiny, but using a temporary variable eliminates the problem (and is often more efficient).