C Variables
Learn how to declare, initialize, and use variables in C with 10 beginner-friendly examples.
C में variables को declare, initialize और use करने के 10 आसान उदाहरण सीखें।
In C, variables are used to store data like numbers, characters, or decimal values. Each variable must be declared with a type before use.
C में variables का उपयोग डेटा जैसे numbers, characters और decimals को store करने के लिए किया जाता है। हर variable को उपयोग करने से पहले declare करना पड़ता है।
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 करता है और उसका मान 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 करके उनका जोड़ निकालकर print किया गया है।
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.
float variable दशमलव मानों को 2-digit precision के साथ store करता है।
Output: Value of pi: 3.14
Example 4: Character variable
#include <stdio.h>
int main() {
char grade = 'A';
printf("Grade: %c", grade);
return 0;
}
Character values are stored using `char` data type.
`char` data type का उपयोग 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;
}
A variable’s value can be changed after declaration.
Variable की value को declaration के बाद बदला जा सकता है।
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.
एक ही पंक्ति में कई 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.
User से input लेने के लिए `scanf` का उपयोग किया गया है।
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 expressions like area calculation.
Variables का उपयोग गणितीय expressions में किया जा सकता है।
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 might output garbage value.
बिना initialize किया गया variable 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 fixed value variable.
`const` का उपयोग permanent value वाले variable के लिए होता है।
Output: Area: 3.14