Click here to Skip to main content
15,560,511 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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

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;
 
Share this answer
 
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).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900