B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP

C Variables

In C, variables store values like numbers, characters, and decimals. Every variable must be declared with a data type.

C में variables मानों को store करते हैं जैसे numbers, characters, और decimals। हर variable को एक data type के साथ declare करना जरूरी है।

Syntax:

type variableName = value;

Example 1: Declare and print an integer

#include <stdio.h>

int main() {
    int age = 20;
    printf("Age is: %d", age);
    return 0;
}

This program declares an integer variable `age` and prints its value.

यह प्रोग्राम एक integer variable `age` को declare करता है और उसका value print करता है।

Output: Age is: 20


Example 2: Add two integers

#include <stdio.h>

int main() {
    int a = 10, b = 5;
    int sum = a + b;
    printf("Sum: %d", sum);
    return 0;
}

Two integers are declared and added, result is printed.

दो integers declare किए जाते हैं, उनका जोड़ निकाला जाता है और output में दिखाया जाता है।

Output: Sum: 15


Example 3: Floating-point variable

#include <stdio.h>

int main() {
    float pi = 3.14;
    printf("Value of pi: %.2f", pi);
    return 0;
}

A float variable stores decimal values with 2 digit precision.

यह program एक float variable declare करता है जो decimal value रखता है।

Output: Value of pi: 3.14


Example 4: Character variable

#include <stdio.h>

int main() {
    char grade = 'A';
    printf("Grade: %c", grade);
    return 0;
}

A character is stored using the `char` data type.

`char` data type का use करके एक character को store किया गया है।

Output: Grade: A


Example 5: Changing a variable value

#include <stdio.h>

int main() {
    int x = 10;
    x = 15;
    printf("x is now: %d", x);
    return 0;
}

You can change a variable’s value any time after declaration.

Variable की value को बाद में बदला जा सकता है।

Output: x is now: 15


Example 6: Declare multiple variables

#include <stdio.h>

int main() {
    int a = 1, b = 2, c = 3;
    printf("%d %d %d", a, b, c);
    return 0;
}

You can declare and initialize multiple variables in one line.

एक ही line में multiple variables को declare और initialize किया गया है।

Output: 1 2 3


Example 7: Input using scanf

#include <stdio.h>

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("You entered: %d", num);
    return 0;
}

User input is taken using scanf and printed.

`scanf` के माध्यम से input लिया गया है और फिर print किया गया है।

Output: You entered: 42


Example 8: Variables in arithmetic

#include <stdio.h>

int main() {
    float radius = 2.0;
    float area = 3.14 * radius * radius;
    printf("Area: %.2f", area);
    return 0;
}

Variables can be used in mathematical formulas like area calculation.

Variables का उपयोग formulas में किया जा सकता है जैसे area निकालने के लिए।

Output: Area: 12.56


Example 9: Uninitialized variable

#include <stdio.h>

int main() {
    int x;
    printf("x: %d", x); // May print garbage
    return 0;
}

An uninitialized variable may give garbage output.

बिना initialize किया variable random (garbage) value दिखा सकता है।

Output: x: ??? (garbage value)


Example 10: Constant variable

#include <stdio.h>

int main() {
    const float PI = 3.14159;
    float r = 1.0;
    float area = PI * r * r;
    printf("Area: %.2f", area);
    return 0;
}

`const` is used to declare a variable whose value doesn't change.

`const` keyword ऐसे variable के लिए use किया जाता है जिसकी value नहीं बदलती।

Output: Area: 3.14