चर#

परिचय#

वेरिएबल डेटा को स्टोर करते हैं और आपको अपने प्रोग्राम में इसका पुन: उपयोग और हेरफेर करने की अनुमति देते हैं। C++ एक स्टैटिकली टाइप्ड भाषा है, जिसका अर्थ है कि वेरिएबल बनाते समय आपको उसका प्रकार स्पष्ट रूप से घोषित करना होगा। घोषणा के बाद प्रकार को बदला नहीं जा सकता। उदाहरण के लिए:

int angle = 90;           // angle is an integer
char dist[] = "Distance"; // dist is an array of characters
double steps = 2.5;       // steps is a double

C++ भी स्ट्रॉन्गली टाइप्ड है, जिसका अर्थ है कि आप स्पष्ट रूपांतरण के बिना असंगत प्रकारों पर ऑपरेशन नहीं कर सकते। उदाहरण के लिए:

int blocks = 2;              // blocks is an integer
char rings[] = "4";          // rings is a string
int result = blocks + rings; // Creates a compilation error

यह API C++ में उपयोग होने वाले सामान्य वेरिएबल प्रकारों की व्याख्या करता है। यह पूरी सूची नहीं है, लेकिन इसमें उन प्रकारों को शामिल किया गया है जिनका उपयोग आप व्यवहार में सबसे अधिक करेंगे।

  • स्थानीय चर — फ़ंक्शन के अंदर घोषित और केवल उसी दायरे में उपयोग किया जाता है; अस्थायी या पृथक मानों के लिए सबसे अच्छा।

  • ग्लोबल वैरिएबल — किसी भी फ़ंक्शन के बाहर घोषित किया जाता है और पूरे प्रोजेक्ट में उपयोग किया जाता है; फ़ंक्शंस के बीच डेटा साझा करने के लिए अच्छा है।

  • पूर्णांक — गिनती, दूरी या दशमलव के बिना किसी भी चीज़ के लिए उपयोग की जाने वाली पूर्ण संख्याएँ।

  • Double — High-precision decimal numbers for mathematical calculations requiring accuracy.

  • फ्लोट — मानक परिशुद्धता दशमलव संख्याएँ, माप या गणना के लिए उपयोगी।

  • सी-शैली स्ट्रिंग — पाठ मान, संदेशों, लेबलों या पठनीय आउटपुट प्रदर्शित करने के लिए उपयोग किया जाता है।

  • बूलियन (बूल) — true या false तर्क और निर्णय लेने के लिए मान।

  • सरणी — एक ही प्रकार के तत्वों के निश्चित आकार के संग्रह।

  • 2डी सरणियाँ — सरणियों की सरणियाँ; पंक्तियों, ग्रिडों या तालिका जैसे डेटा का प्रतिनिधित्व करने के लिए आदर्श।

  • कॉन्स्ट — const के साथ घोषित चर को संशोधित नहीं किया जा सकता है।

किसी वेरिएबल को घोषित करना और उसे असाइन करना#

C++ में एक वेरिएबल बनाने के लिए, आपको पहले उसका प्रकार घोषित करना होगा, उसके बाद वेरिएबल का नाम लिखना होगा, और फिर वैकल्पिक रूप से = ऑपरेटर का उपयोग करके एक मान असाइन करना होगा:

int distance = 100;

किसी वेरिएबल का नामकरण करते समय, निम्नलिखित नियमों का पालन करना आवश्यक है:

  • नाम में विशेष वर्ण (जैसे विस्मय चिह्न) नहीं हो सकते।

  • नाम की शुरुआत किसी संख्या से नहीं हो सकती।

  • नाम में रिक्त स्थान नहीं हो सकते।

  • यह नाम पूर्वनिर्धारित VEXcode ऑब्जेक्ट्स (उदाहरण के लिए, Drivetrain ) के साथ विरोधाभास नहीं कर सकता है।

  • यह नाम C++ का आरक्षित कीवर्ड नहीं हो सकता (उदाहरण के लिए, int, for, आदि)।

  • चरों के नाम केस-सेंसिटिव होते हैं (उदाहरण के लिए, Distance और distance ) अलग-अलग चर हैं।

Local Variables#

स्थानीय चर (लोकल वेरिएबल) किसी फ़ंक्शन या कोड ब्लॉक के अंदर परिभाषित किए जाते हैं। वे केवल उसी फ़ंक्शन या ब्लॉक के दायरे में ही पहुंच योग्य होते हैं और उसके बाहर दिखाई नहीं देते।

फ़ंक्शन को परिभाषित करें ( int main() के बाहर)

void show_local() {
  // This variable only exists inside this function
  int angle = 90;
  Brain.Screen.print("%d", angle);
}

int main() के अंदर फ़ंक्शन को कॉल करें

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Begin project code
  show_local();
}

स्थानीय चरों का उपयोग आमतौर पर अस्थायी मानों को संग्रहीत करने के लिए किया जाता है जो केवल किसी विशिष्ट फ़ंक्शन या प्रोग्राम के भाग के भीतर ही प्रासंगिक होते हैं।

Global Variables#

ग्लोबल वेरिएबल किसी भी फंक्शन या ब्लॉक के बाहर परिभाषित किए जाते हैं। इन्हें प्रोग्राम में कहीं भी एक्सेस और रीड किया जा सकता है, जिसमें फंक्शन के अंदर का भाग भी शामिल है।

ध्यान दें: ग्लोबल वेरिएबल प्रोग्राम में कहीं से भी एक्सेस किए जा सकते हैं, जिससे फंक्शन्स के बीच डेटा साझा करना सुविधाजनक हो जाता है। हालांकि, ग्लोबल वेरिएबल पर अत्यधिक निर्भरता अनपेक्षित दुष्प्रभावों को जन्म दे सकती है, क्योंकि प्रोग्राम के एक भाग में वेरिएबल में किए गए परिवर्तन अप्रत्याशित रूप से अन्य भागों को प्रभावित कर सकते हैं। इस कारण, जहां संभव हो, लोकल वेरिएबल को प्राथमिकता दी जाती है, क्योंकि ये वेरिएबल के स्कोप को उस विशिष्ट फंक्शन तक सीमित कर देते हैं जहां इसे परिभाषित किया गया है। इससे टकराव की संभावना कम हो जाती है और डीबगिंग आसान हो जाती है।

फ़ंक्शन को परिभाषित करें ( int main() के बाहर)

// The variable is defined outside a function
int angle = 90;

void show_global() {
  // You can access 'angle' inside a function
  Brain.Screen.print("%d", angle);
  Brain.Screen.newLine();
}

int main() के अंदर फ़ंक्शन को कॉल करें

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Begin project code
  show_global();
  Brain.Screen.print("%d", angle);
}

C++ में, ग्लोबल वेरिएबल्स को किसी विशेष कीवर्ड के बिना सीधे फंक्शन के अंदर से एक्सेस और मॉडिफाई किया जा सकता है। हालांकि, यदि आपके पास उसी नाम का कोई लोकल वेरिएबल है, तो आप ग्लोबल वेरिएबल को एक्सेस करने के लिए स्कोप रिजॉल्यूशन ऑपरेटर :: उपयोग कर सकते हैं।

फ़ंक्शन को परिभाषित करें ( int main() के बाहर)

// Define the global variable
int count = 0;

void increase_count() {
  // Global variables can be accessed directly
  count = count + 1;
  Brain.Screen.print("Count: %d", count);
  Brain.Screen.newLine();
}

int main() के अंदर फ़ंक्शन को कॉल करें

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Begin project code
  increase_count();
  increase_count();
}

डेटा के प्रकार#

C++ में कई प्रकार के डेटा को वेरिएबल्स में स्टोर किया जा सकता है। कुछ प्रकारों को पुनः असाइन किया जा सकता है, जबकि अन्य में व्यक्तिगत तत्वों को संशोधित किया जा सकता है। नीचे सबसे अधिक उपयोग किए जाने वाले प्रकार दिए गए हैं:

Integer#

एक पूर्णांक (int) एक पूर्ण संख्या होती है।

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Begin project code
  int distance = 100;

  // Move the robot forward for the variable value in mm
  Drivetrain.driveFor(forward, distance, mm);

  // Add to the variable and move forward the new value, 
  // for 200mm total
  wait(1, seconds);
  distance = distance + 100;
  Drivetrain.driveFor(forward, distance, mm);
}

Double#

एक डबल एक उच्च परिशुद्धता (आमतौर पर 64-बिट) वाली दशमलव-बिंदु संख्या होती है।

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Begin project code
  // Store a value with decimal points
  double raw_value = 0.88;

  // Print the decimal value as a percentage
  Brain.Screen.print("%.1f%%", raw_value * 100);
}

Float#

एक फ्लोट दशमलव संख्याएँ संग्रहीत करता है। अधिकांश VEX प्रोजेक्ट्स में, मेमोरी उपयोग की चिंता न होने पर फ्लोट की तुलना में डबल को प्राथमिकता दी जाती है।

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Begin project code
  // Store a value with decimal points
  float raw_value = 0.88;

  // Print the decimal value as a percentage
  Brain.Screen.print("%.1f%%", raw_value * 100);
}

String#

C-शैली की स्ट्रिंग (char variable_name[]) वर्णों का एक क्रम है जो एक वर्ण सरणी में संग्रहीत होता है। C-शैली की स्ट्रिंग इतनी बड़ी होनी चाहिए कि उसमें टेक्स्ट और एक नल टर्मिनेटर दोनों समाहित हो सकें।

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Begin project code
  // Set the variable to a string then print the string
  char message[] = "Ready!";
  Brain.Screen.print(message);
}

Boolean#

एक बूलियन (bool) true या false मानों का प्रतिनिधित्व करता है।

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Begin project code
  // Set the state of the variable
  bool delivered = false;

  // Print different messages depending on the Boolean.
  if (delivered) {
      Brain.Screen.print("Package delivered!");
  } else {
      Brain.Screen.print("Delivering...");
  }
}

बूलियन मानों को प्रोजेक्ट के किसी भी चरण में बदला जा सकता है।

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Begin project code
  // Print the value of the delivered variable
  bool delivered = false;
  // Print different messages depending on the Boolean.
  if (delivered) {
      Brain.Screen.print("Package delivered!");
  } else {
      Brain.Screen.print("Delivering...");
  }
  wait(2, seconds);

  // Clear the screen and print the value of the variable again
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1,1);
  delivered = true;
  // Print different messages depending on the Boolean.
  if (delivered) {
      Brain.Screen.print("Package delivered!");
  } else {
      Brain.Screen.print("Delivering...");
  }
}

Arrays#

एरे एक ही प्रकार के तत्वों का निश्चित आकार का संग्रह होता है, जो इंडेक्स 0 से शुरू होता है। एरे की लंबाई जानना और उसे बनाते समय घोषित करना एक अच्छी प्रथा है।

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Begin project code
  // Declare an array of 5 integers
  int distances[5] = {100, 200, 150, 300, 250};

  // Drive and turn 4 times to move the distance of the first four distances of the array
  for (int i = 0; i < 4; i++) {
      Drivetrain.driveFor(forward, distances[i], mm);
      Drivetrain.turnFor(right, 90, degrees);
  }
}

2D Arrays#

एक 2D ऐरे का उपयोग आमतौर पर ग्रिड, टेबल या मैट्रिक्स को दर्शाने के लिए किया जाता है। प्रत्येक पंक्ति डेटा के एक विशिष्ट समूह का प्रतिनिधित्व करती है।

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Begin project code
  // Assign the values in the matrix 2D array
  int matrix[3][3] = {
    { 0, 1, 2 },
    { 3, 4, 5 },
    { 6, 7, 8 }
  };

  // Loop through each row
  for (int i = 0; i < 3; i++) {
      // Loop through each column in the row
      for (int j = 0; j < 3; j++) {
          Brain.Screen.print("%d, ", matrix[i][j]);
      }
      Brain.Screen.newLine();
  }
}

आप 2D ऐरे के भीतर विशिष्ट तत्वों को संशोधित कर सकते हैं:

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Begin project code
  // Assign the values in the matrix 2D array
  int matrix[3][3] = {
    { 0, 1, 2 },
    { 3, 4, 5 },
    { 6, 7, 8 }
  };

  // Modify the color (in column 2) in row 0
  matrix[0][1] = 200;

  // Print the modified row from the matrix 2D array
  for (int j = 0; j < 3; j++) {
      Brain.Screen.print("%d, ", matrix[0][j]);
  }
}

Constant Variables#

const के साथ घोषित चर को आरंभ किए जाने के बाद संशोधित नहीं किया जा सकता है।

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  const int maxSpeed = 100;

  // maxSpeed can be used like a normal variable
  Drivetrain.setDriveVelocity(maxSpeed, percent);
}