Šajā apmācībā jūs uzzināsit par dažādiem operatoriem C programmēšanā, izmantojot piemērus.
Operators ir simbols, kas darbojas ar vērtību vai mainīgo. Piemēram: + ir operators, kurš veic saskaitīšanu.
C ir plašs operatoru loks dažādu darbību veikšanai.
C Aritmētiskie operatori
Aritmētiskais operators ar skaitliskām vērtībām (konstantēm un mainīgajiem) veic matemātiskas darbības, piemēram, saskaitīšanu, atņemšanu, reizināšanu, dalīšanu utt.
Operators | Operatora nozīme |
---|---|
+ | pievienošana vai unāra plus |
- | atņemšana vai unāra mīnus |
* | pavairošana |
/ | sadalīšana |
% | atlikums pēc dalīšanas (modulo sadalījums) |
1. piemērs: Aritmētiskie operatori
// 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; )
Rezultāts
a + b = 13 ab = 5 a * b = 36 a / b = 2 Atlikums, dalot ar b = 1
Operatorus +
, -
un *
Aprēķina saskaitīšanu, atņemšanu un reizināšanu attiecīgi, kā jūs varētu būt gaidāms.
Normālā aprēķinā 9/4 = 2.25
. Tomēr izeja ir 2
programmā.
Tas ir tāpēc, ka abi mainīgie a un b ir veseli skaitļi. Tādējādi izeja ir arī vesels skaitlis. Sastādītājs novārtā atstāj terminu aiz komata un parāda atbildi, 2
nevis 2.25
.
Moduļa operators %
aprēķina atlikušo daļu. Kad a=9
tiek dalīts ar b=4
, atlikušais ir 1
. %
Operators var izmantot tikai ar veseliem skaitļiem.
Pieņemsim a = 5.0
, b = 2.0
, c = 5
un d = 2
. Tad C programmēšanā,
Jebkurš no operandiem ir peldošā komata skaitlis a / b = 2,5 a / d = 2,5 c / b = 2,5 // Abi operandi ir veseli skaitļi c / d = 2
C Pieauguma un samazināšanas operatori
C programmēšanai ir divi operatoru pieaugumi ++
un samazinājumi, --
lai operanda vērtību (nemainīgu vai mainīgu) mainītu par 1.
Pieaugums ++
palielina vērtību par 1, bet samazinājums --
samazina par 1. Šie divi operatori ir vienoti operatori, kas nozīmē, ka tie darbojas tikai vienā operandā.
2. piemērs: pieauguma un samazināšanas operatori
// 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; )
Rezultāts
++ a = 11 - b = 99 ++ c = 11,500000 --d = 99,500000
Šeit operatori ++
un --
tiek izmantoti kā priedēkļi. Šos divus operatorus var izmantot arī kā pēcfiksus, piemēram, a++
un a--
. Apmeklējiet šo lapu, lai uzzinātu vairāk par to, kā pieauguma un samazināšanas operatori darbojas, lietojot tos kā postfix.
C Piešķiršanas operatori
Piešķiršanas operators tiek izmantots, lai mainīgajam piešķirtu vērtību. Visizplatītākais piešķiršanas operators ir=
Operators | Piemērs | Tāds pats kā |
---|---|---|
= | 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 |
3. piemērs: uzdevumu operatori
// 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; )
Rezultāts
c = 5 c = 10 c = 5 c = 25 c = 5 c = 0
C Relāciju operatori
Relāciju operators pārbauda attiecības starp diviem operandiem. Ja saistība ir patiesa, tā atgriež 1; ja saistība ir nepatiesa, tā atgriež vērtību 0.
Relāciju operatori tiek izmantoti lēmumu pieņemšanā un ciklos.
Operators | Operatora nozīme | Piemērs |
---|---|---|
== | Vienāds ar | 5 == 3 tiek novērtēts līdz 0 |
> | Varenāks, kā | 5> 3 tiek vērtēts līdz 1 |
< | Mazāk nekā | 5 < 3 tiek novērtēts līdz 0 |
! = | Nav vienāds ar | 5 != 3 tiek vērtēts līdz 1 |
> = | Lielāks vai vienāds ar | 5>= 3 tiek vērtēts līdz 1 |
<= | Mazāks par vai vienāds ar | 5 <= 3 tiek novērtēts līdz 0 |
4. piemērs: Relāciju operatori
// 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; )
Rezultāts
5 == 5 ir 1 5 == 10 ir 0 5> 5 ir 0 5> 10 ir 0 5 <5 ir 0 5 = 5 ir 1 5> = 10 ir 0 5 <= 5 ir 1 5 <= 10 ir 1
C loģiskie operatori
Izteiksme, kurā ir loģisks operators, atgriež vērtību 0 vai 1 atkarībā no tā, vai izteiksme ir patiesa vai nepatiesa. Loģiskos operatorus parasti izmanto lēmumu pieņemšanā C programmēšanā.
Operators | Nozīme | Piemērs |
---|---|---|
&& | Loģiski UN. Patiesi tikai tad, ja visi operandi ir patiesi | Ja c = 5 un d = 2, tad izteiksme ((c==5) && (d>5)) ir vienāda ar 0. |
|| | Loģiski VAI. Patiesi tikai tad, ja ir taisnība vai nu vienā no operandiem | Ja c = 5 un d = 2, tad izteiksme ((c==5) || (d>5)) ir vienāda ar 1. |
! | Loģiski NAV. Patiesi tikai tad, ja operands ir 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
Citi operatori, piemēram, trīskāršais operators ?:
, atsauces operators &
, novirzīšanas operators *
un dalībnieku atlases operators, ->
tiks apspriesti turpmākajās apmācībās.