Variables, Types & the C Type System
C-তে ভ্যারিয়েবল ও টাইপ — কোডের ভিত্তি
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.
2. The Primitive Types
| Type | Typical size | Range (signed) | Note |
|---|---|---|---|
char | 1 byte | −128 to 127 | Small int / a byte |
short | 2 bytes | ±32,767 | Rarely used today |
int | 4 bytes | ±2.1 billion | Default integer |
long | 4 or 8 bytes | platform-dependent | ⚠ size varies |
long long | 8 bytes | ±9.2 × 10¹⁸ | Guaranteed ≥ 64-bit |
float | 4 bytes | ~7 digit precision | IEEE 754 single |
double | 8 bytes | ~15 digit precision | IEEE 754 double |
bool | 1 byte | 0 or 1 | via <stdbool.h> |
long হয় 8 byte, কিন্তু 64-bit Windows-এ 4 byte। তাই portable কোডে কখনো নির্দিষ্ট size ধরে নেবেন না।
3. sizeof — Ask the Compiler
sizeof compile-time-এই একটি type বা expression-এর byte-size দিয়ে দেয়।
#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;
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
int x = 0; লিখুন, যদি অন্য কারণ না থাকে।
6. Type Conversions
int i = 3;
double d = i; // int → double, safe
double d2 = 3.7;
int j = d2; // double → int, truncates to 3 (lossy)
#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;
}
(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
#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
-
Print
sizeofof all primitive types on your machine. Compare with a classmate's output.সব primitive type-এরsizeofপ্রিন্ট করুন এবং সহপাঠীর output-এর সাথে তুলনা করুন।✨ Show Answer
উপরের Section 3 এর
sizeof.c-ই পুরো উত্তর। Run করে দেখুন। -
Declare a
const floatfor 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; } -
Convert a Celsius temperature to Fahrenheit using
F = C*9/5 + 32and 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; } -
What does
printf("%d\n", 7/2)print? What about7.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 হয়।
-
Declare a
uint8_tand assign300to 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 আচরণ হয়েছে। -
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; } -
Can you declare
int 2var;? Explain.int 2var;ঘোষণা কি বৈধ? ব্যাখ্যা করুন।✨ Show Answer
না। C-তে variable name অবশ্যই একটি অক্ষর বা
_দিয়ে শুরু হতে হবে। সংখ্যা দিয়ে শুরু করা যাবে না। -
Print min and max of
int,unsigned intandlong longusing<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; } -
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; } -
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 করে রেখে দেয় এবং নতুন মান কখনো পড়ে না।volatilecompiler-কে নির্দেশ দেয় প্রতিবার memory থেকেই ফ্রেশ মান পড়তে।
Glossary (শব্দকোষ)
| Term | Meaning | বাংলায় |
|---|---|---|
| Variable | A named location in memory that holds a value. | মেমরির একটি নামকরা স্থান যেখানে মান রাখা হয়। |
| Identifier | The name you give a variable, function, or type. | Variable, function বা type-এর জন্য আপনার দেওয়া নাম। |
| Data Type | The kind of data a variable holds (int, float, char...). | Variable কী ধরনের data ধারণ করবে তা নির্ধারণ করে। |
| Declaration | Tells the compiler a variable's name and type. | Variable-এর নাম ও type compiler-কে জানানো। |
| Initialization | Giving a variable its first value. | Variable-কে প্রথম মান দেওয়া। |
int / char / float / double | The basic primitive types of C. | C-র মৌলিক primitive type। |
signed / unsigned | Whether a type can hold negative values. | Type negative মান রাখতে পারবে কিনা তা নির্দেশ করে। |
short / long | Type modifiers that change size. | Type-এর size পরিবর্তনকারী modifier। |
sizeof | Operator returning a type or variable's size in bytes. | Type বা variable-এর byte-সাইজ ফেরত দেওয়া operator। |
| Type Conversion | Changing a value from one type to another. | একটি type-এর মানকে অন্য type-এ রূপান্তর। |
| Cast | Explicit type conversion using (type) syntax. | (type) ব্যবহার করে স্পষ্ট type-পরিবর্তন। |
| Implicit Promotion | Automatic conversion of small types to larger ones. | ছোট type স্বয়ংক্রিয়ভাবে বড় type-এ রূপান্তর। |
const | Type 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.
<stdint.h> ব্যবহার করুন। সবসময় variable initialize করুন। Implicit promotion ও explicit cast ভালোভাবে বুঝে নিন। const যতটা পারা যায় ব্যবহার করুন।