Theory
3 values, 3 shape ના ડબ્બા
Marks program વધી રહ્યો છે અને આજે એને 3 વસ્તુ રાખવાની છે: marks 78, percentage 78.5, અને grade 'A'.
ત્રણેયને int ડબ્બામાં ઠાલવીને જુઓ: 78 બરાબર બેસી જાય છે, 78.5 પોતાનો .5 ગુમાવે છે, અને 'A' એવો number બની જાય છે જે તમે માગ્યો જ નહોતો.
ડબ્બાની shape હોય છે. Shape પસંદ કરવાનું કામ data type કરે છે, અને ખોટી પસંદગી એ C program નું તમારી સામે જૂઠું બોલવાનું સૌથી શાંત બહાનું છે.
Theory
ડબ્બાનો set
રસોડાના ડબ્બાના set માં shapes કારણસર હોય છે: રોટલી માટે ચપટો, દાળ માટે ઊંચો, એલચી માટે નાનકડો. દાળ રોટલીના ડબ્બામાં રેડો તો અડધી ઢોળાય. Data types એ જ shapes છે: આખા numbers માટે int, decimal વાળી values માટે float/double, એક જ character માટે char. Type ડબ્બાનું SIZE (bytes માં) પણ fix કરે છે, જે નક્કી કરે છે કે કેટલી મોટી value સમાશે.
At a glance
C નો type પરિવાર (સામાન્ય sizes)
| Type | Bytes | શું રાખે | Marks program માં |
|---|---|---|---|
| short int | 2 | નાના આખા numbers | age |
| int | 4 | આખા numbers | marks = 78 |
| long | 4/8 | મોટા આખા numbers | college નો enrolment no. |
| float | 4 | Decimals, ~6-7 digits | percentage = 78.5 |
| double | 8 | Decimals, ~15 digits | ઝીણવટભર્યા averages |
| char | 1 | એક character (ASCII રૂપે) | grade = 'A' |
Theory
ખાસ સભ્યો: string, void, signed/unsigned
- string: C નો basic type નથી.
"Riya"જેવો text એ char array છે, char ડબ્બાની હાર, double quotes માં લખેલી. આખી વાત Unit 3 માં; અત્યારે એટલું જાણો કે 'A' અને "A" કેમ જુદાં છે. - void: "કોઈ value નહીં" વાળો type, જે functions કંઈ પાછું આપતાં નથી એમના માટે. કોઈ variable void declare થતું નથી.
- signed / unsigned: signed int માં એક bit sign રાખે છે, બચે ~±214 કરોડ.
unsigned intdeclare કરો એટલે એ bit પણ value માં જોડાય: negatives બંધ, positive પહોંચ બમણી (~429 કરોડ). જે ગણતરીઓ ક્યારેય negative થઈ જ ન શકે એના માટે perfect.
Practical
Marks program, સાચા types સાથે
#include <stdio.h>
int main() {
int marks = 78;
float percentage = 78.5;
char grade = 'A';
unsigned int total_students = 60;
printf("Marks: %d\n", marks);
printf("Percentage: %f\n", percentage);
printf("Grade: %c\n", grade);
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
તમે int percentage = 78.5; declare કરો છો. ડબ્બામાં ખરેખર શું રહેશે?
- 78, decimal વાળો ભાગ કપાઈ જાય છે
- 78.5, C ડબ્બો આપોઆપ adjust કરી લે છે
- 79, C નજીકના આખા number સુધી round કરે છે
- 0, program value લેવાની ના પાડે છે
Show the answer
78, decimal વાળો ભાગ કપાઈ જાય છે
int ડબ્બામાં decimals માટે કોઈ ખાનું નથી, એટલે C truncate કરે છે: .5 પડતો મુકાય છે, round થતું નથી, 78.9 પણ 78 જ બનત. મોટા ભાગનાં compilers માં ન error, ન warning, બસ ચૂપચાપ data નું નુકસાન, એટલે જ type ની પસંદગી મહત્વની છે. "Truncation, not rounding" એ જ શબ્દો પર exams marks આપે છે.
Think first
char નું રહસ્ય, જે તમે પહેલેથી જાણો છો
char ડબ્બો 1 byte નો છે, અને bytes numbers રાખે છે. તો 'A' store કરો ત્યારે ડબ્બામાં કયો number બેસે છે? આ તમે BCA103 માં ભણ્યા છો.
Show the answer
65, ASCII code. char ખરેખર તો character નો પોશાક પહેરેલો નાનો number-ડબ્બો છે: 'A' માં 65, 'a' માં 97, '0' માં 48 રહે છે. એટલે જ grade + 1 C માં legal છે અને 66 ('B') આપે છે. તમારી BCA103 વાળી ASCII table હમણાં જ programming નું ઓજાર બની ગઈ.
Watch out
Marks ક્યાં કપાય છે
string ને C નો basic data type કહેવું: એ char array છે, અને examiners એ જ ચકાસે છે. Sizes ને સાર્વત્રિક કાયદા તરીકે લખવા: એ typical છે (int "સામાન્ય રીતે 4 bytes", compiler પર આધાર), એવું લખો. Quotes ની ગરબડ: 'A' char છે, "A" 2 bytes ની string છે (A વત્તા એક છૂપું end-marker). અને unsigned નો અર્થ "sign લખ્યો નથી" એવો નથી: અર્થ છે એ ડબ્બા માટે negatives અશક્ય છે.
Theory
Professional ની જેમ પસંદગી
કામનો નિયમ: આખી ગણતરીઓ → int (negatives અશક્ય હોય તો unsigned), પૈસા અને percentages → float, science-કક્ષાની ઝીણવટ → double, એક અક્ષર/ચિહ્ન → char. Shapes અત્યારે જ સાચી કરી લો, કારણ કે હવે પછીનો lesson ક્રિયાપદો આપે છે, operators, અને 78 / 60 int ડબ્બામાં float ડબ્બા કરતાં સાવ જુદું વર્તે છે.
Summary
Key takeaways
- Data type ડબ્બાનું size (bytes) અને એમાં સમાતી values fix કરે છે.
- આખા numbers: short int, int, long; decimals: float (~7 digits), double (~15).
- char એક character રાખે છે, પોતાના ASCII number રૂપે; 'A' એટલે 65.
- string એ char array છે (double quotes), basic type નહીં; void એટલે કોઈ value નહીં.
- unsigned negatives નો ભોગ આપીને positive range બમણી કરે છે.
- int ડબ્બા decimals કાપી નાખે છે: 78.5 બને 78, કોઈ rounding નહીં.
- યાદ રાખવાની યુક્તિ: ડબ્બાનો set, સાચી વસ્તુ માટે સાચી shape.