Day 37 – Chapter 5: Variables and Constants in C++
Class 11 Computer Science
What are Variables? (वेरिएबल क्या हैं?)
A variable in C++ is a symbolic name or identifier for a memory location where data is stored. It allows the program to manipulate the stored data.
C++ में वेरिएबल एक प्रतीकात्मक नाम या पहचानकर्ता है जो डेटा संग्रहीत करने के लिए एक मेमोरी स्थान को संदर्भित करता है। यह प्रोग्राम को संग्रहीत डेटा में हेरफेर करने की अनुमति देता है।
Key Points about Variables (वेरिएबल के प्रमुख बिंदु):
- Unique Name (अद्वितीय नाम): Every variable must have a unique name.
- Type-Specific (प्रकार-विशिष्ट): Variables are declared with a specific type, such as int, float, or char.
- Declared Before Use (उपयोग से पहले घोषणा): A variable must be declared before it is used in the program.
- Modifiable (संशोधित योग्य): The value of a variable can be changed during the execution of the program.
Declaring and Initializing Variables (वेरिएबल की घोषणा और प्रारंभ):
Declaration means informing the compiler about the type and name of a variable. Initialization assigns a value to the variable during declaration.
घोषणा का अर्थ है कंपाइलर को वेरिएबल के प्रकार और नाम के बारे में सूचित करना। प्रारंभ का अर्थ है घोषणा के दौरान वेरिएबल को मान निर्दिष्ट करना।
Syntax:
variable_name = value;
Example:
int age = 18; // Integer variable float pi = 3.14; // Float variable char grade = 'A'; // Character variable
Types of Variables in C++ (C++ में वेरिएबल के प्रकार):
- Local Variables (लोकल वेरिएबल): Declared inside a function or block and accessible only within that scope.
किसी फंक्शन या ब्लॉक के अंदर घोषित वेरिएबल जो केवल उसी दायरे में सुलभ होता है। - Global Variables (ग्लोबल वेरिएबल): Declared outside all functions and accessible throughout the program.
सभी फंक्शन के बाहर घोषित वेरिएबल जो पूरे प्रोग्राम में सुलभ होता है। - Static Variables (स्टैटिक वेरिएबल): Retains its value between function calls.
फंक्शन कॉल्स के बीच अपने मान को बनाए रखता है।
What are Constants? (कॉनस्टेंट क्या हैं?)
Constants in C++ are fixed values that do not change during the execution of the program. They are declared using the const keyword.
C++ में कॉनस्टेंट स्थिर मान होते हैं, जो प्रोग्राम के निष्पादन के दौरान नहीं बदलते। इन्हें const कीवर्ड का उपयोग करके घोषित किया जाता है।
Characteristics of Constants (कॉनस्टेंट की विशेषताएँ):
- Constants cannot be modified once they are defined.
कॉनस्टेंट को एक बार परिभाषित करने के बाद संशोधित नहीं किया जा सकता। - They improve code readability and prevent accidental changes to important values.
ये कोड को पढ़ने में आसान बनाते हैं और महत्वपूर्ण मानों में आकस्मिक बदलाव को रोकते हैं। - They are declared using the
constkeyword.
इन्हेंconstकीवर्ड का उपयोग करके घोषित किया जाता है।
Declaring Constants (कॉनस्टेंट की घोषणा):
The syntax for declaring a constant in C++ is:
C++ में कॉनस्टेंट घोषित करने का सिंटैक्स है:
constconstant_name = value;
Example:
const float pi = 3.14; const int max_marks = 100;
Difference Between Variables and Constants (वेरिएबल और कॉनस्टेंट के बीच अंतर):
| Variable (वेरिएबल) | Constant (कॉनस्टेंट) |
|---|---|
| Value can change during program execution. प्रोग्राम निष्पादन के दौरान मान बदल सकता है। |
Value remains fixed throughout program execution. प्रोग्राम निष्पादन के दौरान मान स्थिर रहता है। |
Declared without the const keyword.const कीवर्ड के बिना घोषित।
|
Declared using the const keyword.const कीवर्ड का उपयोग करके घोषित।
|
Importance of Variables and Constants (वेरिएबल और कॉनस्टेंट का महत्व):
- Variables allow programs to process and manipulate data dynamically.
वेरिएबल प्रोग्राम को डेटा को गतिशील रूप से संसाधित और हेरफेर करने की अनुमति देते हैं। - Constants ensure critical values remain unchanged during execution.
कॉनस्टेंट यह सुनिश्चित करते हैं कि महत्वपूर्ण मान निष्पादन के दौरान अपरिवर्तित रहें। - Both improve program clarity and maintainability.
दोनों प्रोग्राम की स्पष्टता और रखरखाव को बेहतर बनाते हैं।
Practice Questions
Multiple Choice Questions (MCQs)
- Which keyword is used to declare a constant in C++?
(a) static | (b) const | (c) final | (d) let - What is the scope of a local variable?
(a) Throughout the program | (b) Within a function | (c) Within a file | (d) None - Which of the following is a valid variable name in C++?
(a) 1value | (b) value_1 | (c) value@ | (d) None - What happens if you try to modify a constant variable?
(a) It changes | (b) It remains unchanged | (c) Compilation Error | (d) None - What does the following code output?
#include <iostream> using namespace std; int main() { const int x = 10; cout << x; return 0; }(a) 10 | (b) x | (c) Error | (d) None - Which data type is used to store a single character in C++?
(a) int | (b) float | (c) char | (d) string - What is a static variable?
(a) A variable that keeps its value between function calls | (b) A global variable | (c) A temporary variable | (d) None - Which of the following is not a valid C++ data type?
(a) int | (b) double | (c) real | (d) char - Which of the following constants is correctly declared in C++?
(a) int pi = 3.14; | (b) const float pi = 3.14; | (c) constant int pi = 3.14; | (d) None - What will the following code output?
#include <iostream> using namespace std; int main() { int a = 5, b = 10; cout << a + b; return 0; }(a) 5 | (b) 10 | (c) 15 | (d) None
Answers to MCQs:
1: (b), 2: (b), 3: (b), 4: (c), 5: (a), 6: (c), 7: (a), 8: (c), 9: (b), 10: (c)
Short Answer Questions
- What is a variable in C++?
Answer: A variable is a named memory location used to store data that can be modified during program execution. - What is the difference between a local and a global variable?
Answer: Local variables are accessible only within the function where they are declared, while global variables are accessible throughout the program. - Write the syntax to declare a constant in C++.
Answer:constconstant_name = value; - What are the benefits of using constants in a program?
Answer: Constants prevent accidental modification of critical values, improve code readability, and make the program more reliable. - Write a program to demonstrate the use of a static variable in C++.
Answer:#include <iostream> using namespace std; void demo() { static int count = 0; count++; cout << count << endl; } int main() { demo(); demo(); return 0; }
Long Answer Questions
- Explain the difference between variables and constants in C++ with examples.
- Discuss the types of variables in C++ with suitable examples.
- Write a program in C++ to demonstrate the use of global and local variables.
Post a Comment