C/C++ C++ kodunda hata nerede?

İkisi de arttırma işlemi ama şöyle çalışıyor.

int a = 5;
int b = ++a;

dersek a = 6, b = 6 olur.
Yani a değişkenini önceden artırıyor. O yüzden b değeri 6 oluyor.

int a = 5;
int b = a++;

dersek eğer a = 6, b = 5 olur.
Yani a değişkenini sonrada artırıyor. O yüzden b değeri 5 oluyor.
Peki mesela
Given the following program:

int main() {
int a = 1;
cout << ++a + a++ << endl;
cout << a << endl;
}
Predict the output of the program and explain the result.
Burda ne yapmak lazım?
 
Peki mesela
Given the following program:

int main() {
int a = 1;
cout << ++a + a++ << endl;
cout << a << endl;
}
Predict the output of the program and explain the result.
Burda ne yapmak lazım?
Sırayla gidersek cout << ++a + a++ << endl; ifadesi için
++a demiş, bu kısım 2 eder.
Daha sonra + a++, burada da 2 eder. 1. çıktı 4 verir.

Ama cout < a << endl;
için yukarıda işlemlerde bulunan + a++ sonradan ekleme olduğu için a = 3 olur ve çıktı olara 3 verir.
 

Yeni konular

Geri
Yukarı