Bu eğitimde, örnekler yardımıyla C programlamadaki farklı operatörler hakkında bilgi edineceksiniz.
Operatör, bir değer veya değişken üzerinde çalışan bir semboldür. Örneğin: +, toplama işlemini gerçekleştiren bir operatördür.
C, çeşitli işlemleri gerçekleştirmek için geniş bir operatör yelpazesine sahiptir.
C Aritmetik Operatörler
Bir aritmetik operatör, sayısal değerler (sabitler ve değişkenler) üzerinde toplama, çıkarma, çarpma, bölme vb. Gibi matematiksel işlemleri gerçekleştirir.
Şebeke | Operatörün Anlamı |
---|---|
+ | toplama veya tekli artı |
- | çıkarma veya tekli eksi |
* | çarpma işlemi |
/ | bölünme |
% | bölümden sonra kalan (modulo bölümü) |
Örnek 1: Aritmetik Operatörler
// Working of arithmetic operators #include int main() ( int a = 9,b = 4, c; c = a+b; printf("a+b = %d ",c); c = a-b; printf("a-b = %d ",c); c = a*b; printf("a*b = %d ",c); c = a/b; printf("a/b = %d ",c); c = a%b; printf("Remainder when a divided by b = %d ",c); return 0; )
Çıktı
a + b = 13 ab = 5 a * b = 36 a / b = 2 a, b = 1'e bölündüğünde kalan
Operatörler +
, -
ve *
değerlerini hesaplar toplama, çıkarma, çarpma ve sırasıyla beklendiği edeceğiniz üzere.
Normal hesaplamada 9/4 = 2.25
,. Ancak çıktı 2
programın içindedir.
Bunun nedeni hem a hem de b değişkenlerinin tamsayı olmasıdır. Dolayısıyla, çıktı da bir tamsayıdır. Derleyici, ondalık noktadan sonraki terimi ihmal eder ve 2
bunun yerine yanıtı gösterir 2.25
.
Modulo operatörü %
kalanı hesaplar. İle a=9
bölündüğünde b=4
, kalan kısımdır 1
. %
Operatörü yalnızca tamsayılar ile kullanılabilir.
Varsayalım a = 5.0
, b = 2.0
, c = 5
ve d = 2
. Sonra C programlamada,
// İşlenenlerden biri kayan nokta sayısıdır a / b = 2,5 a / d = 2,5 c / b = 2,5 // Her iki işlenen de tamsayıdır c / d = 2
C Arttırma ve Azaltma Operatörleri
C programlamada, bir işlenenin değerini (sabit veya değişken) 1 ile değiştirmek için iki operatör artışı ++
ve azaltması vardır --
.
Artış ++
, değeri 1 artırır, --
düşürme ise değeri 1 azaltır . Bu iki operatör tek operatördür, yani yalnızca tek bir işlenen üzerinde çalışırlar.
Örnek 2: Arttırma ve Azaltma Operatörleri
// Working of increment and decrement operators #include int main() ( int a = 10, b = 100; float c = 10.5, d = 100.5; printf("++a = %d ", ++a); printf("--b = %d ", --b); printf("++c = %f ", ++c); printf("--d = %f ", --d); return 0; )
Çıktı
++ a = 11 --b = 99 ++ c = 11.500000 --d = 99.500000
Burada operatörler ++
ve --
önek olarak kullanılır. Bu iki operatör, a++
ve gibi son ekler olarak da kullanılabilir a--
. Arttırma ve eksiltme operatörlerinin postfix olarak kullanıldığında nasıl çalıştığı hakkında daha fazla bilgi edinmek için bu sayfayı ziyaret edin.
C Atama Operatörleri
Bir değişkene bir değer atamak için bir atama operatörü kullanılır. En yaygın atama operatörü=
Şebeke | Misal | İle aynı |
---|---|---|
= | a = b | a = b |
+ = | a + = b | a = a + b |
- = | a - = b | a = ab |
* = | a * = b | a = a * b |
/ = | a / = b | a = a / b |
% = | a% = b | a = a% b |
Örnek 3: Atama Operatörleri
// Working of assignment operators #include int main() ( int a = 5, c; c = a; // c is 5 printf("c = %d", c); c += a; // c is 10 printf("c = %d", c); c -= a; // c is 5 printf("c = %d", c); c *= a; // c is 25 printf("c = %d", c); c /= a; // c is 5 printf("c = %d", c); c %= a; // c = 0 printf("c = %d", c); return 0; )
Çıktı
c = 5 c = 10 c = 5 c = 25 c = 5 c = 0
C İlişkisel Operatörler
İlişkisel bir operatör, iki işlenen arasındaki ilişkiyi kontrol eder. İlişki doğruysa, 1 döndürür; ilişki yanlışsa 0 değerini döndürür.
İlişkisel operatörler karar vermede ve döngülerde kullanılır.
Şebeke | Operatörün Anlamı | Misal |
---|---|---|
== | Eşittir | 5 == 3 0 olarak değerlendirilir |
> | Greater than | 5> 3 is evaluated to 1 |
< | Less than | 5 < 3 is evaluated to 0 |
!= | Not equal to | 5 != 3 is evaluated to 1 |
>= | Greater than or equal to | 5>= 3 is evaluated to 1 |
<= | Less than or equal to | 5 <= 3 is evaluated to 0 |
Example 4: Relational Operators
// Working of relational operators #include int main() ( int a = 5, b = 5, c = 10; printf("%d == %d is %d ", a, b, a == b); printf("%d == %d is %d ", a, c, a == c); printf("%d> %d is %d ", a, b, a> b); printf("%d> %d is %d ", a, c, a> c); printf("%d < %d is %d ", a, b, a < b); printf("%d < %d is %d ", a, c, a = %d is %d ", a, b, a>= b); printf("%d>= %d is %d ", a, c, a>= c); printf("%d <= %d is %d ", a, b, a <= b); printf("%d <= %d is %d ", a, c, a <= c); return 0; )
Output
5 == 5 is 1 5 == 10 is 0 5> 5 is 0 5> 10 is 0 5 < 5 is 0 5 = 5 is 1 5>= 10 is 0 5 <= 5 is 1 5 <= 10 is 1
C Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming.
Operator | Meaning | Example |
---|---|---|
&& | Logical AND. True only if all operands are true | If c = 5 and d = 2 then, expression ((c==5) && (d>5)) equals to 0. |
|| | Logical OR. True only if either one operand is true | If c = 5 and d = 2 then, expression ((c==5) || (d>5)) equals to 1. |
! | Logical NOT. True only if the operand is 0 | If c = 5 then, expression !(c==5) equals to 0. |
Example 5: Logical Operators
// Working of logical operators #include int main() ( int a = 5, b = 5, c = 10, result; result = (a == b) && (c> b); printf("(a == b) && (c> b) is %d ", result); result = (a == b) && (c < b); printf("(a == b) && (c < b) is %d ", result); result = (a == b) || (c < b); printf("(a == b) || (c < b) is %d ", result); result = (a != b) || (c < b); printf("(a != b) || (c < b) is %d ", result); result = !(a != b); printf("!(a != b) is %d ", result); result = !(a == b); printf("!(a == b) is %d ", result); return 0; )
Output
(a == b) && (c> b) is 1 (a == b) && (c < b) is 0 (a == b) || (c < b) is 1 (a != b) || (c < b) is 0 !(a != b) is 1 !(a == b) is 0
Explanation of logical operator program
(a == b) && (c> 5)
evaluates to 1 because both operands(a == b)
and(c> b)
is 1 (true).(a == b) && (c < b)
evaluates to 0 because operand(c < b)
is 0 (false).(a == b) || (c < b)
evaluates to 1 because(a = b)
is 1 (true).(a != b) || (c < b)
evaluates to 0 because both operand(a != b)
and(c < b)
are 0 (false).!(a != b)
evaluates to 1 because operand(a != b)
is 0 (false). Hence, !(a != b) is 1 (true).!(a == b)
evaluates to 0 because(a == b)
is 1 (true). Hence,!(a == b)
is 0 (false).
C Bitwise Operators
During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and saves power.
Bitwise operators are used in C programming to perform bit-level operations.
Operators | Meaning of operators |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise exclusive OR |
~ | Bitwise complement |
<< | Shift left |
>> | Shift right |
Visit bitwise operator in C to learn more.
Other Operators
Comma Operator
Comma operators are used to link related expressions together. For example:
int a, c = 5, d;
The sizeof operator
The sizeof
is a unary operator that returns the size of data (constants, variables, array, structure, etc).
Example 6: sizeof Operator
#include int main() ( int a; float b; double c; char d; printf("Size of int=%lu bytes",sizeof(a)); printf("Size of float=%lu bytes",sizeof(b)); printf("Size of double=%lu bytes",sizeof(c)); printf("Size of char=%lu byte",sizeof(d)); return 0; )
Output
Size of int = 4 bytes Size of float = 4 bytes Size of double = 8 bytes Size of char = 1 byte
Üçlü operatör ?:
, referans operatörü &
, referans operatörü *
ve üye seçim operatörü gibi diğer operatörler ->
daha sonraki eğitimlerde tartışılacaktır.