Theory
ડબ્બા ભરાયા, હવે કંઈક કરો
Marks program 3 subject ના marks રાખે છે: 78, 65, 92. રાખવાનું ગયા lesson માં થયું. આજે accountant નું કામ: total, percentage, અને એ સવાલ જે દરેક student ખરેખર પૂછે છે, હું પાસ થયો કે નહીં?
એમાંનું દરેક કોઈક operator નું કામ છે. Operators એ C નાં verbs (ક્રિયાપદો) છે, અને કોઈ પણ ભાષાનાં ક્રિયાપદોની જેમ, 2 જે લગભગ સરખાં દેખાય છે (= અને ==) સાવ જુદો અર્થ ધરાવે છે, અને એમને ગૂંચવવાથી ખરેખરના payroll runs crash થયા છે.
At a glance
Operator પરિવારો
| પરિવાર | Operators | એક ઉદાહરણ |
|---|---|---|
| Arithmetic | + - * / % | total = m1 + m2 + m3 |
| Relational | < > <= >= == != | marks >= 35 આપે 1 કે 0 |
| Logical | && || ! | બંને subjects માં પાસ: a && b |
| Assignment | = += -= *= /= | marks += 5 (grace marks) |
| Ternary | ? : | result = (m >= 35) ? 'P' : 'F' |
| Bitwise | & | ^ << >> | અલગ અલગ bits પર કામ |
| sizeof() | sizeof(int) | ડબ્બાનું size bytes માં: 4 |
Theory
Arithmetic ના 2 જાણીતા surprise
Surprise 1, integer division: int ડબ્બા સાથે 235 / 3 એ 78 છે, 78.33 નહીં. 2 ints ભાગીને એક int બને છે; decimals કાઢી નાખાય છે (ફરી એ જ truncation). Decimals જોઈએ? એક બાજુને floating બનાવો: 235 / 3.0 આપે 78.33.
Surprise 2, % operator: % એ modulus છે, ભાગ પછી બચેલો remainder: 7 % 2 એ 1, 10 % 5 એ 0. એને percentages સાથે કંઈ લેવાદેવા નથી, ચિહ્ન ભલે ગમે તે હોય. (એકી/બેકી test, cyclic rolls, અને exam ના અડધા tricky સવાલ % પર ચાલે છે.)
Practical
Marks program ગણતરી કરે છે
#include <stdio.h>
int main() {
int m1 = 78, m2 = 65, m3 = 92;
int total = m1 + m2 + m3;
float percentage = total / 3.0; /* 3.0, not 3 */
char result = (percentage >= 35) ? 'P' : 'F';
printf("Total: %d\n", total);
printf("Percentage: %f\n", percentage);
printf("Result: %c\n", result);
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Think first
Ternary ને વાક્યની જેમ વાંચો
Line result = (percentage >= 35) ? 'P' : 'F'; એ ternary operator છે. પહેલા એને English વાક્યની જેમ મોટેથી વાંચો, પછી કહો કે percentage 78.33 માટે result માં શું રહે છે.
Show the answer
"શું percentage ઓછામાં ઓછું 35 છે? હા તો 'P' લો; નહીંતર 'F' લો." 78.33 માટે condition સાચી છે, એટલે result માં 'P' રહે છે.
Ternary એ એક line નું if-else છે: condition ? valueIfTrue : valueIfFalse. Exams કોઈ value માટે એનું output પૂછવાનું પસંદ કરે છે, અને એનું નામ: C નું એકમાત્ર operator જે 3 operands લે છે.
Quiz
C માં 7 % 2 ની value શું છે, અને અલગથી 7 / 2 ની (બંને ints)?
- 7 % 2 એ 1, અને 7 / 2 એ 3
- 7 % 2 એ 3.5, અને 7 / 2 એ 3.5
- 7 % 2 એ 3, અને 7 / 2 એ 1
- 7 % 2 એ 1, અને 7 / 2 એ 3.5
Show the answer
7 % 2 એ 1, અને 7 / 2 એ 3
Integer division 7 / 2 ફક્ત આખો ભાગ રાખે છે: 3. Modulus 7 % 2 ફક્ત remainder રાખે છે: 1. બંને મળીને કોઈ પણ ભાગને quotient અને remainder માં વહેંચી દે છે. Option C એમને ઉલટાવે છે, standard trap, અને 3.5 વાળું કોઈ પણ option ભૂલી ગયું કે બંને બાજુ ints છે.
Quiz
એક student perfect score ચકાસવાના ઇરાદે if (marks = 100) લખે છે. ખરેખર શું થાય છે?
- marks ને 100 ASSIGN થઈ જાય છે, અને condition હંમેશાં સાચી રહે છે
- C ઇરાદા મુજબ marks ને 100 સાથે સરખાવે છે
- Compiler એને હંમેશાં error કહીને reject કરે છે
- Condition હંમેશાં ખોટી રહે છે
Show the answer
marks ને 100 ASSIGN થઈ જાય છે, અને condition હંમેશાં સાચી રહે છે
= assign કરે છે: marks 100 બની જાય છે, એની ખરી value ભૂંસાઈ જાય છે, અને expression ની value (100, non-zero) દર વખતે TRUE ગણાય છે. મોટા ભાગનાં compilers એને ચૂપચાપ સ્વીકારે છે. સરખામણી માટે == જોઈતું હતું. મંત્ર ગાંઠે બાંધો: = assign કરે, == પૂછે. આ એક જ character થી ખરી દુનિયામાં outages થયા છે.
Watch out
3 mark-ખાઉ
= વિરુદ્ધ == (assign વિરુદ્ધ compare, ઉપર). Integer division: ints સાથે total / 3 ચૂપચાપ decimals પાડી દે છે; 3.0 લખો કે float વાપરો. Relational results: C માં સરખામણી 1 કે 0 આપે છે, "true/false" શબ્દો નહીં, એટલે printf("%d", 5 > 3) 1 print કરે છે, predict-the-output નો માનીતો સવાલ. Bitwise operators છે (& | ^ << >>) અને bits પર કામ કરે છે; આ stage પર exams એમનાં NAMES માગે છે, નિપુણતા નહીં.
Theory
sizeof: માપવાનું ટેપ
sizeof(int) જવાબ આપે છે "આ ડબ્બો કેટલા bytes નો છે?", સામાન્ય રીતે 4, અને sizeof(marks) એક variable માપે છે. એ function જેવું દેખાય પણ છે એક operator, exams બરાબર એ જ પૂછે છે. એ તમને arrays અને pointers સાથે ફરી મળશે, જ્યાં ડબ્બા માપવા એ રોજનું કામ બની જાય છે. હવે પછીનો lesson: દરેક program ની ઉપરની toolbox lines, #include અને #define, આખરે સમજાવેલી.
Summary
Key takeaways
- Operator પરિવારો: arithmetic, relational, logical, assignment, ternary, bitwise, sizeof.
- Integer division કાપી નાખે (7/2 એ 3); % remainder આપે (7%2 એ 1).
- = assign કરે, == પૂછે; if (marks = 100) assign કરે અને હંમેશાં સાચું છે.
- Relational અને logical results 1 (true) કે 0 (false) હોય છે.
- Ternary: condition ? ifTrue : ifFalse, C નું એકમાત્ર 3-operand operator.
- sizeof એ operator છે જે ડબ્બાના bytes જણાવે છે.
- યાદ રાખવાની યુક્તિ: operators એ C નાં verbs છે; = assign કરે, == પૂછે.