EZ Notes Logo

EZNotes

🎯 Aim

To learn the rules for naming variables in C/C++ and to identify valid and invalid variable names.

📖 Introduction

In C/C++ programming, a variable is a name given to a memory location to store data. Variable names must follow certain rules. If you break these rules, the compiler will show an error. In this practical, you will learn the rules for naming variables and practice identifying valid and invalid variable names.

📝 Rules for Variable Names in C/C++

  • Rule 1: A variable name can contain letters (A-Z, a-z), digits (0-9), and underscore (_).
  • Rule 2: A variable name must start with a letter or an underscore (_). It cannot start with a digit.
  • Rule 3: Variable names are case-sensitive. age and Age are different.
  • Rule 4: No spaces or special characters are allowed (except underscore).
  • Rule 5: Variable names cannot be keywords (like int, float, return, if, etc.).
  • Rule 6: There is no maximum length limit, but keep them short and meaningful.
💡 Tip: Use meaningful variable names like studentAge instead of a. This makes your code easier to understand.

📊 Table: Valid and Invalid Variable Names

Variable Name Status Reason
age ✅ Valid Letters only, starts with a letter
_value ✅ Valid Starts with underscore, allowed
myAge ✅ Valid Letters only, camelCase format
student1 ✅ Valid Letters and digits, starts with letter
_score ✅ Valid Starts with underscore, allowed
total_marks ✅ Valid Letters and underscore, starts with letter
num1 ✅ Valid Letters and digits, starts with letter
Name ✅ Valid Starts with capital letter, valid but not common
_temp ✅ Valid Starts with underscore, allowed
long_variable_name_123 ✅ Valid Letters, digits, underscore; starts with letter
1st ❌ Invalid Starts with a digit (Rule 2)
my-age ❌ Invalid Contains hyphen (-) which is not allowed (Rule 4)
my age ❌ Invalid Contains space (Rule 4)
int ❌ Invalid Reserved keyword in C++ (Rule 5)
float ❌ Invalid Reserved keyword in C++ (Rule 5)
return ❌ Invalid Reserved keyword in C++ (Rule 5)
if ❌ Invalid Reserved keyword in C++ (Rule 5)
while ❌ Invalid Reserved keyword in C++ (Rule 5)
break ❌ Invalid Reserved keyword in C++ (Rule 5)
continue ❌ Invalid Reserved keyword in C++ (Rule 5)
@value ❌ Invalid Contains special character @ (Rule 4)
#num ❌ Invalid Contains special character # (Rule 4)
123abc ❌ Invalid Starts with a digit (Rule 2)
my$name ❌ Invalid Contains special character $ (Rule 4)
📌 Important: Always use meaningful and descriptive variable names. This makes your code easier to read and understand for others and for yourself.

📊 Observations

  • Variable names can only contain letters, digits, and underscores.
  • Variable names must start with a letter or underscore.
  • Keywords cannot be used as variable names.
  • Special characters (like @, #, $, -) and spaces are not allowed.

✅ Result

You have learned the rules for naming variables in C/C++. You can now identify valid and invalid variable names. This knowledge will help you avoid common errors when writing programs.

💡 Tip: Practice naming variables with different combinations to become familiar with the rules. For example, try making your own list of valid and invalid names.

🎯 مقصد

C/C++ میں متغیرات کے نام رکھنے کے اصول سیکھنا اور درست اور غلط متغیر ناموں کی شناخت کرنا۔

📖 تعارف

C/C++ پروگرامنگ میں، متغیر ایک میموری لوکیشن کو دیا جانے والا نام ہے تاکہ ڈیٹا کو ذخیرہ کیا جا سکے۔ متغیر کے ناموں کو بعض اصولوں پر عمل کرنا ہوتا ہے۔ اگر آپ ان اصولوں کو توڑیں گے تو کمپائلر غلطی دکھائے گا۔ اس عملی میں، آپ متغیرات کے نام رکھنے کے اصول سیکھیں گے اور درست اور غلط متغیر ناموں کی شناخت کی مشق کریں گے۔

📝 C/C++ میں متغیر کے ناموں کے اصول

  • اصول 1: متغیر کے نام میں حروف (A-Z, a-z)، ہندسے (0-9)، اور انڈر سکور (_) شامل ہو سکتے ہیں۔
  • اصول 2: متغیر کا نام حرف یا انڈر سکور (_) سے شروع ہونا چاہیے۔ یہ کسی ہندسے سے شروع نہیں ہو سکتا۔
  • اصول 3: متغیر کے نام کیس-سینسیٹو ہیں۔ age اور Age مختلف ہیں۔
  • اصول 4: متغیر کے نام میں خالی جگہ یا خاص حروف (انڈر سکور کے علاوہ) کی اجازت نہیں ہے۔
  • اصول 5: متغیر کے نام کلیدی الفاظ (جیسے int، float، return، if) نہیں ہو سکتے۔
  • اصول 6: زیادہ سے زیادہ لمبائی کی کوئی حد نہیں ہے، لیکن نام مختصر اور معنی خیز رکھیں۔
💡 مشورہ: a کی بجائے studentAge جیسے معنی خیز متغیر نام استعمال کریں۔ اس سے آپ کا کوڈ سمجھنے میں آسان ہوتا ہے۔

📊 جدول: درست اور غلط متغیر نام

متغیر کا نام حیثیت وجہ
age ✅ درست صرف حروف، حرف سے شروع
_value ✅ درست انڈر سکور سے شروع، جائز
myAge ✅ درست صرف حروف، کیمرل کیس
student1 ✅ درست حروف اور ہندسے، حرف سے شروع
_score ✅ درست انڈر سکور سے شروع، جائز
total_marks ✅ درست حروف اور انڈر سکور، حرف سے شروع
num1 ✅ درست حروف اور ہندسے، حرف سے شروع
Name ✅ درست بڑے حرف سے شروع، درست لیکن عام نہیں
_temp ✅ درست انڈر سکور سے شروع، جائز
long_variable_name_123 ✅ درست حروف، ہندسے، انڈر سکور؛ حرف سے شروع
1st ❌ غلط ہندسے سے شروع (اصول 2)
my-age ❌ غلط مائنس (-) ہے جو جائز نہیں (اصول 4)
my age ❌ غلط خالی جگہ (اصول 4)
int ❌ غلط C++ کا کلیدی لفظ (اصول 5)
float ❌ غلط C++ کا کلیدی لفظ (اصول 5)
return ❌ غلط C++ کا کلیدی لفظ (اصول 5)
if ❌ غلط C++ کا کلیدی لفظ (اصول 5)
while ❌ غلط C++ کا کلیدی لفظ (اصول 5)
break ❌ غلط C++ کا کلیدی لفظ (اصول 5)
continue ❌ غلط C++ کا کلیدی لفظ (اصول 5)
@value ❌ غلط خاص حرف @ (اصول 4)
#num ❌ غلط خاص حرف # (اصول 4)
123abc ❌ غلط ہندسے سے شروع (اصول 2)
my$name ❌ غلط خاص حرف $ (اصول 4)
📌 اہم: ہمیشہ معنی خیز اور وضاحتی متغیر نام استعمال کریں۔ اس سے آپ کا کوڈ دوسروں اور خود آپ کے لیے پڑھنے اور سمجھنے میں آسان ہوتا ہے۔

📊 مشاہدات

  • متغیر کے ناموں میں صرف حروف، ہندسے اور انڈر سکور ہو سکتے ہیں۔
  • متغیر کے نام کا آغاز حرف یا انڈر سکور سے ہونا چاہیے۔
  • کلیدی الفاظ کو متغیر کے نام کے طور پر استعمال نہیں کیا جا سکتا۔
  • خاص حروف (جیسے @, #, $, -) اور خالی جگہ کی اجازت نہیں ہے۔

✅ نتیجہ

آپ نے C/C++ میں متغیرات کے نام رکھنے کے اصول سیکھ لیے ہیں۔ اب آپ درست اور غلط متغیر ناموں کی شناخت کر سکتے ہیں۔ یہ علم آپ کو پروگرام لکھتے وقت عام غلطیوں سے بچنے میں مدد دے گا۔

💡 مشورہ: اصولوں سے واقف ہونے کے لیے مختلف مجموعوں کے ساتھ متغیر نام دینے کی مشق کریں۔ مثال کے طور پر، درست اور غلط ناموں کی اپنی فہرست بنائیں۔
⬅ Back to Practical Notebooks