|2. Variables & Data Types
Chapter 2C Language Tutorial~1 min read

Variables & Data Types

Variables आणि Data चे प्रकार

C मध्ये variable वापरायच्या आधी declare करावी लागते — data type सांगायला लागतो. Python सारखे automatically type ओळखत नाही. int, float, char, double — हे C चे basic data types आहेत.

Basic Data Types

Data types आणि sizes

c
#include <stdio.h>

int main() {
    // int — पूर्ण संख्या, 4 bytes, %d
    int age = 20;
    int marks = -15;

    // float — दशांश संख्या, 4 bytes, %f (7 decimal digits)
    float price = 99.99;
    float temp = -3.5;

    // double — अधिक precise float, 8 bytes, %lf (15 digits)
    double pi = 3.14159265358979;

    // char — एकच character, 1 byte, %c
    char grade = 'A';
    char symbol = '#';

    printf("Age: %d\n", age);
    printf("Price: %f\n", price);
    printf("Pi: %lf\n", pi);
    printf("Grade: %c\n", grade);

    return 0;
}

Variable Declaration आणि Initialization

Declare vs Initialize

c
// Declaration only — value नंतर देता येते
int score;
float average;

// Declaration + Initialization — एकाच वेळी
int score = 95;
float average = 87.5;

// Multiple variables एकाच line मध्ये
int a = 1, b = 2, c = 3;

// const — value बदलता येत नाही
const float PI = 3.14159;
const int MAX_SIZE = 100;
// PI = 3.0;  // ❌ Error! const बदलता येत नाही

Variable Naming Rules

  • फक्त letters, digits, underscore (_) वापरा
  • Number ने सुरू करू नका — 2name चुकीचे
  • Space नको — my name चुकीचे, myName बरोबर
  • C keywords (int, if, for, while) नावे म्हणून वापरू नका
  • Case sensitive — Age आणि age वेगळे आहेत
💡

C मध्ये snake_case convention वापरतात — student_name, total_marks. C++ मध्ये camelCase (studentName) जास्त popular आहे.

Key Points — लक्षात ठेवा

  • int (%d) — whole numbers, float (%f) — decimals
  • double (%lf) — more precise than float
  • char (%c) — single character in single quotes
  • const — read-only variable, UPPER_CASE convention
  • C मध्ये type declare करणे mandatory आहे!
0/12 chapters पूर्ण