Variables, Types & the C Type System

C-তে ভ্যারিয়েবল ও টাইপ — কোডের ভিত্তি

~30 min Beginner 10 practice problems Live code

1. What Is a Variable?

A variable is a named region of memory you can read from and write to. The compiler reserves some bytes, attaches a name to them, and gives you operations to use them.

Variable মানে মেমরির একটি নামকরা অঞ্চল, যেখান থেকে আপনি ডেটা পড়তে ও লিখতে পারেন। Compiler নির্দিষ্ট সংখ্যক byte বরাদ্দ করে, একটি নাম দেয়, এবং সেগুলো ব্যবহারের সুযোগ তৈরি করে।
inttypeকী ধরনের agenameনাম =assignবসানো 20;initial valueপ্রাথমিক মান Figure 6.1 — একটি variable declaration-এর চারটি অংশ।

2. The Primitive Types

TypeTypical sizeRange (signed)Note
char1 byte−128 to 127Small int / a byte
short2 bytes±32,767Rarely used today
int4 bytes±2.1 billionDefault integer
long4 or 8 bytesplatform-dependent⚠ size varies
long long8 bytes±9.2 × 10¹⁸Guaranteed ≥ 64-bit
float4 bytes~7 digit precisionIEEE 754 single
double8 bytes~15 digit precisionIEEE 754 double
bool1 byte0 or 1via <stdbool.h>
C guarantees minimum sizes, not exact sizes 64-bit Linux-এ long হয় 8 byte, কিন্তু 64-bit Windows-এ 4 byte। তাই portable কোডে কখনো নির্দিষ্ট size ধরে নেবেন না।

3. sizeof — Ask the Compiler

sizeof compile-time-এই একটি type বা expression-এর byte-size দিয়ে দেয়।

sizeof.c
#include <stdio.h>

int main(void) {
    printf("char       : %zu byte\n",  sizeof(char));
    printf("short      : %zu bytes\n", sizeof(short));
    printf("int        : %zu bytes\n", sizeof(int));
    printf("long       : %zu bytes\n", sizeof(long));
    printf("long long  : %zu bytes\n", sizeof(long long));
    printf("float      : %zu bytes\n", sizeof(float));
    printf("double     : %zu bytes\n", sizeof(double));
    return 0;
}

4. Fixed-Width Types — <stdint.h>

When you need exact sizes (cross-platform code, binary protocols, embedded systems), use these:

#include <stdint.h>

int8_t   a;   // exactly 8 bits, signed
uint8_t  b;   // exactly 8 bits, unsigned
int16_t  c;
uint32_t d;
int64_t  e;
uint64_t f;
Portable কোড লিখতে চাইলে int ভুলে যান — <stdint.h>-এর fixed-width type ব্যবহার করুন।

5. Declaration vs Initialization vs Assignment

int x;           // declaration — value is indeterminate!
x = 5;           // assignment

int y = 10;      // declaration + initialization

int a = 1, b = 2, c = 3;  // multiple at once
Always initialize. Uninitialized variable পড়া undefined behavior। সবসময় int x = 0; লিখুন, যদি অন্য কারণ না থাকে।

6. Type Conversions

Implicit — compiler does it
int i = 3;
double d = i;      // int → double, safe

double d2 = 3.7;
int j = d2;        // double → int, truncates to 3 (lossy)
Explicit — you cast
casting.c
#include <stdio.h>

int main(void) {
    int a = 7, b = 2;
    printf("int  / int  = %d\n", a / b);              // 3
    printf("real division = %.2f\n", (double)a / b);  // 3.50
    return 0;
}
Integer দিয়ে ভাগ করলে দশমিক অংশ truncate হয়। সঠিক ভাগফল পেতে যেকোনো একটি operand-কে (double) দিয়ে cast করুন।

7. Type Qualifiers — const & volatile

const int MAX = 100;      // cannot be modified
volatile int flag;          // value may change outside program control
  • const — compiler-কে প্রতিশ্রুতি: এই variable-এ আর লিখা হবে না। এটি কোড নিরাপদ এবং কখনো কখনো দ্রুতও করে।
  • volatile — compiler-কে বলা হচ্ছে "এই মান hardware, interrupt বা অন্য thread পরিবর্তন করতে পারে, register-এ cache করবেন না"। Memory-mapped I/O ও signal handler-এর জন্য জরুরি।

8. A Complete Example

types_demo.c
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

int main(void) {
    const double PI = 3.14159265;
    int32_t radius = 5;
    double  area   = PI * radius * radius;
    bool    valid  = (radius > 0);

    printf("radius = %d\n", radius);
    printf("area   = %.4f\n", area);
    printf("valid  = %s\n", valid ? "true" : "false");
    return 0;
}

9. Practice Problems

  1. Print sizeof of all primitive types on your machine. Compare with a classmate's output.
    সব primitive type-এর sizeof প্রিন্ট করুন এবং সহপাঠীর output-এর সাথে তুলনা করুন।
    ✨ Show Answer

    উপরের Section 3 এর sizeof.c-ই পুরো উত্তর। Run করে দেখুন।

  2. Declare a const float for the speed of light (m/s) and print it in scientific notation with %e.
    আলোর গতির জন্য একটি const float ঘোষণা করুন এবং %e দিয়ে scientific notation-এ প্রিন্ট করুন।
    ✨ Show Answer
    light.c
    #include <stdio.h>
    int main(void) {
        const double C = 299792458.0;
        printf("Speed of light = %e m/s\n", C);
        return 0;
    }
  3. Convert a Celsius temperature to Fahrenheit using F = C*9/5 + 32 and print both.
    Celsius থেকে Fahrenheit-এ রূপান্তর করুন (F = C*9/5 + 32) এবং দুটোই প্রিন্ট করুন।
    ✨ Show Answer
    celsius.c
    #include <stdio.h>
    int main(void) {
        double c = 30.0;
        double f = c * 9.0 / 5.0 + 32;
        printf("%.1f C = %.1f F\n", c, f);
        return 0;
    }
  4. What does printf("%d\n", 7/2) print? What about 7.0/2? Explain.
    printf("%d\n", 7/2) কী ছাপাবে? আর 7.0/2? ব্যাখ্যা করুন।
    ✨ Show Answer

    প্রথমটি 3 (integer division)। দ্বিতীয়টি 3.5 (floating-point division)। এক operand float হলেই পুরো expression float হয়।

  5. Declare a uint8_t and assign 300 to it. Print the result. What happened?
    একটি uint8_t-এ 300 বসান। Output দেখে ব্যাখ্যা করুন কী ঘটেছে।
    ✨ Show Answer
    overflow.c
    #include <stdio.h>
    #include <stdint.h>
    int main(void) {
        uint8_t x = 300;        // 300 mod 256 = 44
        printf("x = %u\n", x);
        return 0;
    }

    uint8_t-এর সর্বোচ্চ 255। 300-এর শেষ 8 bit = 44। তাই mod-256 আচরণ হয়েছে।

  6. Print a student's name, ID, GPA and is_enrolled nicely formatted.
    একজন শিক্ষার্থীর নাম, ID, GPA এবং is_enrolled সুন্দরভাবে ফর্ম্যাট করে প্রিন্ট করুন।
    ✨ Show Answer
    student.c
    #include <stdio.h>
    #include <stdbool.h>
    int main(void) {
        const char *name = "Nusrat";
        int   id   = 220101042;
        double gpa = 3.92;
        bool  enrolled = true;
        printf("Name     : %s\n",    name);
        printf("ID       : %d\n",    id);
        printf("GPA      : %.2f\n",  gpa);
        printf("Enrolled : %s\n",    enrolled ? "yes" : "no");
        return 0;
    }
  7. Can you declare int 2var;? Explain.
    int 2var; ঘোষণা কি বৈধ? ব্যাখ্যা করুন।
    ✨ Show Answer

    না। C-তে variable name অবশ্যই একটি অক্ষর বা _ দিয়ে শুরু হতে হবে। সংখ্যা দিয়ে শুরু করা যাবে না।

  8. Print min and max of int, unsigned int and long long using <limits.h>.
    <limits.h> ব্যবহার করে int, unsigned int ও long long-এর min/max প্রিন্ট করুন।
    ✨ Show Answer
    limits.c
    #include <stdio.h>
    #include <limits.h>
    int main(void) {
        printf("INT_MIN  = %d\n",  INT_MIN);
        printf("INT_MAX  = %d\n",  INT_MAX);
        printf("UINT_MAX = %u\n",  UINT_MAX);
        printf("LLONG_MIN= %lld\n", LLONG_MIN);
        printf("LLONG_MAX= %lld\n", LLONG_MAX);
        return 0;
    }
  9. Using only int, compute the average of three integers correctly (no truncation).
    শুধু int ব্যবহার করে তিনটি পূর্ণসংখ্যার সঠিক গড় বের করুন (truncation ছাড়া)।
    ✨ Show Answer
    avg3.c
    #include <stdio.h>
    int main(void) {
        int a = 7, b = 8, c = 9;
        double avg = (a + b + c) / 3.0;  // divide by 3.0, not 3
        printf("avg = %.4f\n", avg);
        return 0;
    }
  10. Why is volatile int x; useful for embedded systems? Explain in 3 lines.
    Embedded system-এ volatile int x; কেন জরুরি? ৩ লাইনে লিখুন।
    ✨ Show Answer

    Embedded system-এ কিছু variable hardware register বা interrupt-এর মাধ্যমে যেকোনো সময় পরিবর্তন হতে পারে। volatile ছাড়া compiler মান register-এ cache করে রেখে দেয় এবং নতুন মান কখনো পড়ে না। volatile compiler-কে নির্দেশ দেয় প্রতিবার memory থেকেই ফ্রেশ মান পড়তে।

Glossary (শব্দকোষ)

TermMeaningবাংলায়
VariableA named location in memory that holds a value.মেমরির একটি নামকরা স্থান যেখানে মান রাখা হয়।
IdentifierThe name you give a variable, function, or type.Variable, function বা type-এর জন্য আপনার দেওয়া নাম।
Data TypeThe kind of data a variable holds (int, float, char...).Variable কী ধরনের data ধারণ করবে তা নির্ধারণ করে।
DeclarationTells the compiler a variable's name and type.Variable-এর নাম ও type compiler-কে জানানো।
InitializationGiving a variable its first value.Variable-কে প্রথম মান দেওয়া।
int / char / float / doubleThe basic primitive types of C.C-র মৌলিক primitive type।
signed / unsignedWhether a type can hold negative values.Type negative মান রাখতে পারবে কিনা তা নির্দেশ করে।
short / longType modifiers that change size.Type-এর size পরিবর্তনকারী modifier।
sizeofOperator returning a type or variable's size in bytes.Type বা variable-এর byte-সাইজ ফেরত দেওয়া operator।
Type ConversionChanging a value from one type to another.একটি type-এর মানকে অন্য type-এ রূপান্তর।
CastExplicit type conversion using (type) syntax.(type) ব্যবহার করে স্পষ্ট type-পরিবর্তন।
Implicit PromotionAutomatic conversion of small types to larger ones.ছোট type স্বয়ংক্রিয়ভাবে বড় type-এ রূপান্তর।
constType qualifier marking a variable as read-only.Variable-কে read-only করার qualifier।
<stdint.h>Header providing fixed-width types like int32_t.Fixed-width type যেমন int32_t দেওয়া header।

Summary — Module 06

A variable is named memory. Types decide size and interpretation. C's primitive sizes are platform-dependent; <stdint.h> fixes that. Always initialize. Understand implicit promotion and explicit casts. Use const generously.

Variable মানে নামকরা মেমরি। Type ঠিক করে size এবং interpretation। C-র primitive size platform-ভেদে বদলায় — তাই <stdint.h> ব্যবহার করুন। সবসময় variable initialize করুন। Implicit promotion ও explicit cast ভালোভাবে বুঝে নিন। const যতটা পারা যায় ব্যবহার করুন।

Next Module → Operators — arithmetic, bitwise, logical, precedence এবং বিটের মজার কৌশল।