EZ Notes Logo

EZNotes

🎯 Aim

To write a C/C++ program that displays the Name, Roll Number, School Name, Age, and Address of a student using the printf() function.

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Basic knowledge of printf() function

📖 Introduction

In this practical, you will write a simple C/C++ program that stores and displays student information. You will use variables of different data types:

  • String (char array): For Name, School Name, and Address
  • Integer (int): For Roll Number and Age

You will use the printf() function with format specifiers like %s (for strings) and %d (for integers) to display the information.

💡 Tip: In C, strings are stored as character arrays. You can initialize a string like this: char name[] = "Ahmed";

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named student_info.c.
  3. Write the following code:
/* Practical 12: Program to display student information */ #include <stdio.h> int main() { // Declare variables to store student information char name[] = "Ahmed Ali"; int rollNo = 101; char school[] = "Government High School"; int age = 16; char address[] = "House # 12, Street 5, Lahore"; // Display student information printf("========== STUDENT INFORMATION ==========\n"); printf("Name : %s\n", name); printf("Roll Number : %d\n", rollNo); printf("School Name : %s\n", school); printf("Age : %d years\n", age); printf("Address : %s\n", address); printf("==========================================\n"); return 0; }

Step 2: Explanation of the Code

  1. #include <stdio.h> — Includes the standard input/output library.
  2. char name[] = "Ahmed Ali"; — Declares a string variable for the name.
  3. int rollNo = 101; — Declares an integer variable for the roll number.
  4. char school[] = "Government High School"; — Declares a string for the school name.
  5. int age = 16; — Declares an integer variable for the age.
  6. char address[] = "House # 12, Street 5, Lahore"; — Declares a string for the address.
  7. printf() — Displays the information on the screen.

Step 3: Save, Compile, and Run

  1. Save the file (Ctrl + S).
  2. Open the terminal and compile using the command:
gcc student_info.c -o student_info

Run the program:

./student_info

Expected Output:

========== STUDENT INFORMATION ========== Name : Ahmed Ali Roll Number : 101 School Name : Government High School Age : 16 years Address : House # 12, Street 5, Lahore ==========================================

📊 Variables and Data Types Used

Variable Name Data Type Format Specifier Value
name String (char array) %s Ahmed Ali
rollNo Integer (int) %d 101
school String (char array) %s Government High School
age Integer (int) %d 16
address String (char array) %s House # 12, Street 5, Lahore

Step 4: Modify the Program (Optional)

  1. Change the values of the variables to your own information.
  2. Save, compile, and run the program again to see your information displayed.

📊 Observations

  • String variables (char arrays) store text like name, school, and address.
  • Integer variables store numeric values like roll number and age.
  • The %s format specifier is used to print strings.
  • The %d format specifier is used to print integers.
  • The program successfully displayed all student information.

✅ Result

You have successfully written a C/C++ program that displays the Name, Roll Number, School Name, Age, and Address of a student using the printf() function. You have learned how to use string and integer variables and display them using format specifiers.

📌 Important: In C, strings are stored as arrays of characters and end with a null character '\0'. You can declare a string as char name[] = "Your Name";
💡 Tip: You can add more details like Class, Section, or Date of Birth by adding more variables and printf() statements.

🎯 مقصد

ایک C/C++ پروگرام لکھنا جو printf() فنکشن کا استعمال کرتے ہوئے طالب علم کا نام، رول نمبر، سکول کا نام، عمر اور پتہ ظاہر کرتا ہے۔

🛠️ آپ کو کیا چاہیے

  • Visual Studio Code یا کوئی بھی C/C++ کمپائلر
  • printf() فنکشن کی بنیادی معلومات

📖 تعارف

اس عملی میں، آپ ایک سادہ C/C++ پروگرام لکھیں گے جو طالب علم کی معلومات کو ذخیرہ اور ظاہر کرتا ہے۔ آپ مختلف ڈیٹا ٹائپس کے متغیرات استعمال کریں گے:

  • سٹرنگ (char array): نام، سکول کا نام، اور پتے کے لیے
  • انٹیجر (int): رول نمبر اور عمر کے لیے

آپ معلومات ظاہر کرنے کے لیے printf() فنکشن کا استعمال کریں گے جس میں فارمیٹ اسپیسیفائر جیسے %s (سٹرنگز کے لیے) اور %d (انٹیجرز کے لیے) استعمال ہوں گے۔

💡 مشورہ: C میں، سٹرنگز کو کریکٹر اری کے طور پر ذخیرہ کیا جاتا ہے۔ آپ سٹرنگ کو اس طرح شروع کر سکتے ہیں: char name[] = "Ahmed";

📝 اقدامات

پہلا قدم: پروگرام لکھیں

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. student_info.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 12: Program to display student information */ #include <stdio.h> int main() { // Declare variables to store student information char name[] = "Ahmed Ali"; int rollNo = 101; char school[] = "Government High School"; int age = 16; char address[] = "House # 12, Street 5, Lahore"; // Display student information printf("========== STUDENT INFORMATION ==========\n"); printf("Name : %s\n", name); printf("Roll Number : %d\n", rollNo); printf("School Name : %s\n", school); printf("Age : %d years\n", age); printf("Address : %s\n", address); printf("==========================================\n"); return 0; }

دوسرا قدم: کوڈ کی وضاحت

  1. #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
  2. char name[] = "Ahmed Ali"; — نام کے لیے ایک سٹرنگ متغیر کا اعلان کرتا ہے۔
  3. int rollNo = 101; — رول نمبر کے لیے ایک انٹیجر متغیر کا اعلان کرتا ہے۔
  4. char school[] = "Government High School"; — سکول کے نام کے لیے سٹرنگ کا اعلان کرتا ہے۔
  5. int age = 16; — عمر کے لیے ایک انٹیجر متغیر کا اعلان کرتا ہے۔
  6. char address[] = "House # 12, Street 5, Lahore"; — پتے کے لیے سٹرنگ کا اعلان کرتا ہے۔
  7. printf() — معلومات کو اسکرین پر ظاہر کرتا ہے۔

تیسرا قدم: محفوظ کریں، کمپائل کریں اور چلائیں

  1. فائل کو محفوظ کریں (Ctrl + S
  2. ٹرمینل کھولیں اور نیچے دی گئی کمانڈ استعمال کرتے ہوئے کمپائل کریں:
gcc student_info.c -o student_info

پروگرام چلائیں:

./student_info

متوقع آؤٹ پٹ:

========== STUDENT INFORMATION ========== Name : Ahmed Ali Roll Number : 101 School Name : Government High School Age : 16 years Address : House # 12, Street 5, Lahore ==========================================

📊 استعمال شدہ متغیرات اور ڈیٹا ٹائپس

متغیر کا نام ڈیٹا کی قسم فارمیٹ اسپیسیفائر ویلیو
name سٹرنگ (char array) %s Ahmed Ali
rollNo انٹیجر (int) %d 101
school سٹرنگ (char array) %s Government High School
age انٹیجر (int) %d 16
address سٹرنگ (char array) %s House # 12, Street 5, Lahore

چوتھا قدم: پروگرام میں ترمیم کریں (اختیاری)

  1. متغیرات کی ویلیوز کو اپنی معلومات میں تبدیل کریں۔
  2. پروگرام کو محفوظ کریں، کمپائل کریں اور دوبارہ چلائیں تاکہ آپ کی معلومات ظاہر ہو سکے۔

📊 مشاہدات

  • سٹرنگ متغیرات (char arrays) متن کو ذخیرہ کرتے ہیں جیسے نام، سکول، اور پتہ۔
  • انٹیجر متغیرات عددی ویلیوز کو ذخیرہ کرتے ہیں جیسے رول نمبر اور عمر۔
  • %s فارمیٹ اسپیسیفائر سٹرنگز کو پرنٹ کرنے کے لیے استعمال ہوتا ہے۔
  • %d فارمیٹ اسپیسیفائر انٹیجرز کو پرنٹ کرنے کے لیے استعمال ہوتا ہے۔
  • پروگرام نے کامیابی سے تمام طالب علم کی معلومات ظاہر کیں۔

✅ نتیجہ

آپ نے کامیابی سے ایک C/C++ پروگرام لکھا ہے جو printf() فنکشن کا استعمال کرتے ہوئے طالب علم کا نام، رول نمبر، سکول کا نام، عمر اور پتہ ظاہر کرتا ہے۔ آپ نے سٹرنگ اور انٹیجر متغیرات استعمال کرنا اور انہیں فارمیٹ اسپیسیفائر کا استعمال کرتے ہوئے ظاہر کرنا سیکھ لیا ہے۔

📌 اہم: C میں، سٹرنگز کو کریکٹر اری کے طور پر ذخیرہ کیا جاتا ہے اور یہ ایک null کریکٹر '\0' پر ختم ہوتی ہیں۔ آپ سٹرنگ کو اس طرح شروع کر سکتے ہیں: char name[] = "Your Name";
💡 مشورہ: آپ مزید متغیرات اور printf() بیانات شامل کر کے کلاس، سیکشن، یا تاریخ پیدائش جیسی مزید تفصیلات شامل کر سکتے ہیں۔
⬅ Back to Practical Notebooks