← Back to C++ Course | Chapter 1: Introduction & Basics | Lesson 8 of 15

C++ Keywords और Identifiers

Keywords वे words हैं जो C++ ने अपने काम के लिए reserve किए हैं, जैसे reserved parking spots। Identifiers वे नाम हैं जो आप naming rules follow करते हुए अपनी चीज़ों को देते हैं।
Syntax
cpp
type identifier = value;   // identifier: letters, digits, _ (no leading digit)
int age = 25;              // int is a reserved keyword

Keywords

Keywords वे words हैं जिन्हें खुद C++ language specific purposes के लिए reserve करती है, जैसे int, return, और class, और compiler इन्हें एक fixed meaning देता है जिसे repurpose नहीं किया जा सकता।

एक variable को int या class नाम देने की कोशिश compile नहीं होगी, क्योंकि compiler उन words को सिर्फ उनके reserved role में appear होने की उम्मीद करता है।

उदाहरण: Keywords

cpp
#include <iostream>

int main() {
	// 'int', 'return' are keywords -- reserved, fixed meaning
	int age = 25;
	return 0;
}

Identifiers

Identifiers वे नाम हैं जो आप variables, functions, और classes के लिए चुनते हैं, और clear नाम चुनना code को पढ़ना आसान बनाने के सबसे सस्ते तरीकों में से एक है।

totalPrice नाम का एक variable इसका purpose instantly communicate करता है, जबकि x जैसा एक नाम code पढ़ने वाले किसी को भी इसे समझने के लिए logic में वापस track करने पर मजबूर करता है।

उदाहरण: Identifiers

cpp
#include <iostream>

int main() {
	int totalPrice = 100; // clear identifier vs an unclear name like x
	std::cout << totalPrice << std::endl;
	return 0;
}

Naming Rules

C++ को identifiers का एक letter या underscore (कभी digit नहीं) से शुरू होना ज़रूरी है, और उसके बाद सिर्फ letters, digits, और underscores होने चाहिए — कोई spaces, hyphens, या $ या # जैसे symbols नहीं।

ये rules इसलिए exist करते हैं क्योंकि compiler को एक unambiguous तरीका चाहिए यह बताने का कि एक identifier कहाँ खत्म होता है और अगला token कहाँ शुरू होता है।

उदाहरण: Naming Rules

cpp
#include <iostream>

int main() {
	int _count = 0;   // starts with underscore: valid
	int count2 = 1;   // letters/digits only: valid
	std::cout << _count << " " << count2 << std::endl;
	return 0;
}

नामों में Case Sensitivity

क्योंकि C++ case-sensitive है, count और Count compiler के लिहाज़ से दो पूरी तरह distinct identifiers हैं, भले ही वे एक human को लगभग identical दिखें।

यह 'undefined reference' errors का एक common source है जब एक variable एक casing से declared होता है और गलती से दूसरी से इस्तेमाल होता है।

उदाहरण: Case Sensitivity in Names

cpp
#include <iostream>

int main() {
	int count = 1;
	int Count = 2; // a distinct identifier from 'count'
	std::cout << count << " " << Count << std::endl;
	return 0;
}

नामों के लिए Best Practices

Abbreviations या single letters के मुकाबले descriptive, specific names पसंद करें, i जैसे बहुत short-lived loop counters को छोड़कर जहाँ convention अच्छे से समझी जाती है।

एक codebase में consistent naming conventions — जैसे variables के लिए हमेशा camelCase इस्तेमाल करना — unfamiliar code को जल्दी skim और समझना कहीं आसान बनाते हैं।

उदाहरण: Best Practices for Names

cpp
#include <iostream>

int main() {
	for (int i = 0; i < 3; i++) { // short name OK for a loop counter
		int orderTotal = i * 10;    // descriptive name elsewhere
		std::cout << orderTotal << std::endl;
	}
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. int class = 5; या new जैसे keyword इस्तेमाल करना, जो reserved है और एक compile error cause करता है।
  2. 2ndPlace जैसे किसी digit से identifier शुरू करना, जो एक legal name नहीं है।
  3. count और Count को मिला देना, क्योंकि identifiers case-sensitive हैं और ये दो अलग नाम हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.