Data types (signed and unsigned): Numeric (int, short int, long, float, double), Character (char, string), void

Data type નક્કી કરે છે કે ડબ્બાનું size શું અને અંદર શું સમાય: આખા numbers માટે int, decimals માટે float/double, એક character માટે char, string એટલે char array, void એટલે કંઈ નહીં, અને signed/unsigned negative range ના બદલામાં positive પહોંચ બમણી કરે છે.

11 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


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)

TypeBytesશું રાખેMarks program માં
short int2નાના આખા numbersage
int4આખા numbersmarks = 78
long4/8મોટા આખા numberscollege નો enrolment no.
float4Decimals, ~6-7 digitspercentage = 78.5
double8Decimals, ~15 digitsઝીણવટભર્યા averages
char1એક 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 int declare કરો એટલે એ 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 કરો છો. ડબ્બામાં ખરેખર શું રહેશે?

  1. 78, decimal વાળો ભાગ કપાઈ જાય છે
  2. 78.5, C ડબ્બો આપોઆપ adjust કરી લે છે
  3. 79, C નજીકના આખા number સુધી round કરે છે
  4. 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.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to C language

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Data types (signed and unsigned): Numeric (int, short int, long, float, double), Character (char, string), void · Computer Programming and Programming Methodology (CPPM) · Gri-Learn