Skip to content

3-dars: Operatorlar (matematik, mantiqiy, taqqoslash)

Dars haqida

Davomiyligi: 90 daqiqa Maqsad: Talaba C tilining operatorlarini bilishi, ulardan to'g'ri foydalanishi va operatorlarning tartibini tushunishi kerak.

1. Operator nima?

Operator — qiymatlar ustida amal bajaruvchi belgi.

c
int natija = 5 + 3;  // + — operator

2. Operator turlari

3. Arithmetic — matematik operatorlar

OperatorVazifaMisol
+Qo'shish5 + 3 = 8
-Ayirish5 - 3 = 2
*Ko'paytirish5 * 3 = 15
/Bo'lish10 / 2 = 5
%Modulo (qoldiq)10 % 3 = 1
c
int a = 10, b = 3;
printf("%d\n", a + b);  // 13
printf("%d\n", a - b);  // 7
printf("%d\n", a * b);  // 30
printf("%d\n", a / b);  // 3 (integer division)
printf("%d\n", a % b);  // 1 (qoldiq)

Modulo (%) foydasi

Toq/juft tekshirish:

c
int x = 10;
if (x % 2 == 0) {
    printf("Juft\n");
} else {
    printf("Toq\n");
}

Yil oxirgi raqami:

c
int yil = 2026;
int oxirgi = yil % 10;  // 6

4. Assignment — belgilash operatorlari

OperatorMisolMa'no
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 2x = x * 2
/=x /= 2x = x / 2
%=x %= 3x = x % 3
c
int x = 10;

x += 5;      // x = 15
x -= 3;      // x = 12
x *= 2;      // x = 24
x /= 4;      // x = 6
x %= 4;      // x = 2

printf("%d\n", x);  // 2

5. Increment va Decrement

c
int x = 5;

x++;     // x = 6 (postfix)
++x;     // x = 7 (prefix)

x--;     // x = 6
--x;     // x = 5

Prefix vs Postfix farq

c
int a = 5;
int b = a++;      // b = 5, a = 6 (postfix: avval ber, keyin oshir)

int c = 5;
int d = ++c;      // d = 6, c = 6 (prefix: avval oshir, keyin ber)

Tavsiya

Boshlovchi uchun postfix ishlating (x++). Aralashtirib ishlatish — chigallik.

6. Comparison — taqqoslash operatorlari

Natija — 0 (false) yoki 1 (true).

OperatorVazifaMisol
==Teng5 == 5 → 1
!=Teng emas5 != 3 → 1
>Katta5 > 3 → 1
<Kichik5 < 3 → 0
>=Katta yoki teng5 >= 5 → 1
<=Kichik yoki teng5 <= 3 → 0
c
int a = 5, b = 3;

printf("%d\n", a == b);   // 0
printf("%d\n", a != b);   // 1
printf("%d\n", a > b);    // 1
printf("%d\n", a < b);    // 0
printf("%d\n", a >= 5);   // 1
printf("%d\n", b <= 3);   // 1

= vs ==

= — belgilash (assignment) == — taqqoslash (comparison)

Eng keng tarqalgan xato:

c
if (x = 5) { ... }   // XATO! x ni 5 ga belgilaydi va doim true
if (x == 5) { ... }  // To'g'ri — taqqoslash

7. Logical — mantiqiy operatorlar

OperatorVazifaMisol
&&AND (va)(5 > 3) && (10 < 20) → 1
||OR (yoki)(5 > 3) || (10 > 20) → 1
!NOT (emas)!(5 > 3) → 0
c
int yosh = 25;

// Yosh 18-60 oralig'idami?
if (yosh >= 18 && yosh <= 60) {
    printf("Ishlash yoshi\n");
}

// Shahar kodi 'T' (Toshkent) yoki 'S' (Samarqand) bo'lsa
char shahar_kodi = 'T';
if (shahar_kodi == 'T' || shahar_kodi == 'S') {
    // ...
}
// Diqqat: butun matnni (string) == bilan taqqoslab BO'LMAYDI —
// buning uchun strcmp() kerak (10-darsda o'rganamiz)

Truth table

ABA && BA || B!A
00001
01011
10010
11110

Short-circuit evaluation

C lazy — natija oldindan ma'lum bo'lsa, qolganini hisoblamasdan to'xtaydi.

c
if (0 && very_slow_function()) {
    // very_slow_function() umuman chaqirilmaydi (chap tomon 0)
}

if (1 || very_slow_function()) {
    // very_slow_function() umuman chaqirilmaydi (chap tomon 1)
}

8. Bitwise operatorlar (qisqacha)

Bitwise — bit darajasida amallar.

OperatorVazifa
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise NOT
<<Left shift
>>Right shift
c
int a = 5;   // 0101
int b = 3;   // 0011

printf("%d\n", a & b);   // 0001 = 1
printf("%d\n", a | b);   // 0111 = 7
printf("%d\n", a ^ b);   // 0110 = 6
printf("%d\n", ~a);      // -6
printf("%d\n", a << 1);  // 1010 = 10
printf("%d\n", a >> 1);  // 0010 = 2

Bitwise foydasi

  • Tezkor matematik amallar
  • Bit flaglar (sozlamalar, ruxsatlar)
  • Hardware dasturlash

Boshlovchi uchun — kelajakda kerak.

9. Conditional (Ternary) operator

Qisqa if-else.

c
sharti ? agar_rost : agar_yolgon

Misol:

c
int yosh = 25;
char *natija = (yosh >= 18) ? "Kattalar" : "Bola";
printf("%s\n", natija);  // "Kattalar"

Ekvivalent:

c
char *natija;
if (yosh >= 18) {
    natija = "Kattalar";
} else {
    natija = "Bola";
}

Ternary — bir qatorda yozish uchun foydali.

10. sizeof operator

c
printf("%zu\n", sizeof(int));        // 4
printf("%zu\n", sizeof(double));     // 8
printf("%zu\n", sizeof(5 + 3.14));   // 8 (double)

sizeof — funksiya emas, operator.

11. Operatorlar tartibi (Precedence)

C'da har operator prioritetga ega.

c
int x = 2 + 3 * 4;     // 14 (NOT 20)

Sabab: * +'dan oldin.

Asosiy tartib (yuqori → past)

  1. () — qavslar
  2. ++, -- (postfix)
  3. ++, -- (prefix), !, ~, +, - (unary), (type), sizeof
  4. *, /, %
  5. +, -
  6. <<, >>
  7. <, <=, >, >=
  8. ==, !=
  9. &
  10. ^
  11. |
  12. &&
  13. ||
  14. ?:
  15. =, +=, -= va h.k.

Qavslar bilan aniq qiling

Tartibni eslab qolish o'rniga — qavslar ishlating:

c
// Tushunarsiz
int x = a + b * c - d / e;

// Tushunarli
int x = a + (b * c) - (d / e);

12. Murakkab misol

c
#include <stdio.h>

int main(void) {
    int a = 10, b = 3;
    
    // Arithmetic
    int sum = a + b;
    int diff = a - b;
    int prod = a * b;
    int quot = a / b;
    int rem = a % b;
    
    // Comparison
    int gt = (a > b);
    int eq = (a == b);
    
    // Logical
    int both = (a > 0) && (b > 0);
    int one = (a > 0) || (b < 0);
    
    // Ternary
    int max = (a > b) ? a : b;
    
    // Compound assignment
    a += 5;        // a = 15
    b *= 2;        // b = 6
    
    printf("Sum: %d\n", sum);
    printf("Diff: %d\n", diff);
    printf("Prod: %d\n", prod);
    printf("Quot: %d\n", quot);
    printf("Rem: %d\n", rem);
    printf("a > b? %d\n", gt);
    printf("a == b? %d\n", eq);
    printf("Both positive? %d\n", both);
    printf("Max: %d\n", max);
    printf("New a: %d, New b: %d\n", a, b);
    
    return 0;
}

13. Common pitfalls (keng tarqalgan xatolar)

1. = va ==

c
if (x = 5) { ... }   // XATO
if (x == 5) { ... }  // To'g'ri

2. Integer division

c
int natija = 5 / 2;        // 2 (NOT 2.5)
double natija2 = 5.0 / 2;  // 2.5

3. Operator priority

c
int x = 2 + 3 * 4;     // 14 (NOT 20)
int y = (2 + 3) * 4;   // 20

4. Modulo va negative

c
printf("%d\n", -7 % 3);  // -1 (C99+)
printf("%d\n", 7 % -3);  // 1

C'da modulo natijasi — birinchi argument bilan bir xil belgi.

5. Overflow

c
int max = 2147483647;  // INT_MAX
max = max + 1;          // overflow! → -2147483648

int chegarasidan oshib ketsa — noto'g'ri natija.

Darsdagi topshiriqlar

Topshiriq 1 — Asosiy operatorlar

asosiy.c:

c
#include <stdio.h>

int main(void) {
    int a = 17, b = 5;
    
    printf("a = %d, b = %d\n", a, b);
    printf("a + b = %d\n", a + b);
    printf("a - b = %d\n", a - b);
    printf("a * b = %d\n", a * b);
    printf("a / b = %d\n", a / b);
    printf("a %% b = %d\n", a % b);
    printf("(float) a / b = %.2f\n", (float)a / b);
    
    return 0;
}

Sinab ko'ring.

Topshiriq 2 — Toq/juft tekshirish

toq-juft.c:

c
#include <stdio.h>

int main(void) {
    int x = 10;
    
    if (x % 2 == 0) {
        printf("%d juft\n", x);
    } else {
        printf("%d toq\n", x);
    }
    
    return 0;
}

x qiymatini turli qiymatlarga o'zgartirib sinang:

  • 7
  • 100
  • 0
  • -5
  • 1

Topshiriq 3 — Compound assignment

compound.c:

c
#include <stdio.h>

int main(void) {
    int score = 100;
    
    printf("Boshlang'ich: %d\n", score);
    
    score += 50;
    printf("+50: %d\n", score);
    
    score -= 30;
    printf("-30: %d\n", score);
    
    score *= 2;
    printf("*2: %d\n", score);
    
    score /= 4;
    printf("/4: %d\n", score);
    
    score %= 10;
    printf("%%10: %d\n", score);
    
    return 0;
}

Topshiriq 4 — Increment/decrement

increment.c:

c
#include <stdio.h>

int main(void) {
    int a = 5;
    int b;
    
    // Postfix
    b = a++;
    printf("Postfix: a = %d, b = %d\n", a, b);
    
    int c = 5;
    int d;
    
    // Prefix
    d = ++c;
    printf("Prefix: c = %d, d = %d\n", c, d);
    
    return 0;
}

Natija nima? Nima uchun?

Topshiriq 5 — Logical operatorlar

logical.c — har xil shartlar:

c
#include <stdio.h>

int main(void) {
    int yosh = 25;
    int ish_haqi = 5000000;
    int tajriba = 3;
    
    // Ishga olish: yosh 18-60 VA tajriba 2+
    int qabul = (yosh >= 18 && yosh <= 60) && (tajriba >= 2);
    printf("Qabul: %d\n", qabul);
    
    // Stipendiya: tajriba 1+ YOKI ish_haqi 3M+
    int stipendiya = (tajriba >= 1) || (ish_haqi >= 3000000);
    printf("Stipendiya: %d\n", stipendiya);
    
    // NOT
    int tugatadi = !(yosh < 18);
    printf("Tugatadi: %d\n", tugatadi);
    
    return 0;
}

Topshiriq 6 — Ternary

ternary.c:

c
#include <stdio.h>

int main(void) {
    int a = 15, b = 23;
    
    int max = (a > b) ? a : b;
    int min = (a < b) ? a : b;
    
    printf("Max: %d\n", max);
    printf("Min: %d\n", min);
    
    int yosh = 17;
    char *kategoriya = (yosh >= 18) ? "Kattalar" : "Bola";
    printf("Kategoriya: %s\n", kategoriya);
    
    // 3 ta variant
    int ball = 75;
    char *baho = (ball >= 90) ? "A" :
                 (ball >= 75) ? "B" :
                 (ball >= 60) ? "C" : "F";
    printf("Baho: %s\n", baho);
    
    return 0;
}

Topshiriq 7 — Tartib

tartib.c:

c
#include <stdio.h>

int main(void) {
    int a = 2 + 3 * 4;
    int b = (2 + 3) * 4;
    int c = 2 * 3 + 4 * 5;
    int d = 10 - 5 + 3;
    int e = 10 / 2 * 3;
    int f = 2 * 3 + 4 / 2 - 1;
    
    printf("a = %d\n", a);
    printf("b = %d\n", b);
    printf("c = %d\n", c);
    printf("d = %d\n", d);
    printf("e = %d\n", e);
    printf("f = %d\n", f);
    
    return 0;
}

Har birini oldindan o'ylab daftarga yozing. Keyin kompilyatsiya qiling va to'g'rimi tekshiring.

Topshiriq 8 — Yosh kalkulatori

yosh-kalkulator.c:

Foydalanuvchi tug'ilgan yili. Hozirgi yoshini va kasr qismi:

c
#include <stdio.h>

int main(void) {
    int hozir = 2026;
    int tugilgan = 2005;
    
    int yosh = hozir - tugilgan;
    int kun_soni = yosh * 365;
    int soat = kun_soni * 24;
    
    printf("Yosh: %d yil\n", yosh);
    printf("Kun soni: %d\n", kun_soni);
    printf("Soat soni: %d\n", soat);
    printf("Daqiqa: %d\n", soat * 60);
    
    return 0;
}

O'zingizning ma'lumotlaringizni qo'ying.

Topshiriq 9 — GitHub'ga

Hamma fayllarni push qiling:

bash
$ cd ~/c-darslari
$ mkdir 4-oy-dars-3
$ # fayllarni shu joyga
$ git add .
$ git commit -m "feat: dars 3 - operators"
$ git push

Asosiy tushunchalar (lug'at)

TerminQisqacha izoh
OperatorAmal bajaruvchi belgi
ArithmeticMatematik (+, -, *, /, %)
Modulo (%)Bo'lish qoldigi
AssignmentBelgilash (=, +=, -=)
ComparisonTaqqoslash (==, !=, >, <)
LogicalMantiqiy (&&, ||, !)
BitwiseBit darajasidagi
Increment/Decrement++ / --
Prefix/Postfix++x / x++
Ternary? : qisqa if-else
PrecedenceTartib (qaysi avval)
Short-circuitLazy hisob
OverflowChegaradan oshib ketish

Keyingi dars

4-dars: Shartli operatorlar — if, else, switch →

Master IT o'quv markazi — o'qitish rejasi