EZ Notes Logo

EZNotes

🎯 Aim

To write a C/C++ program that displays a student's Name, School Name, and Address using the puts() function.

🛠️ What You Need

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

📖 Introduction

In this practical, you will use the puts() function to display student information. The puts() function is a simple way to output strings in C programming. It automatically adds a newline after each string, making it perfect for displaying multiple lines of text.

The puts() function takes a single argument — a string (character array) — and displays it on the screen. It is part of the stdio.h library.

💡 Tip: puts() automatically adds a newline at the end of each string, so you don't need to use \n.

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named student_info_puts.c.
  3. Write the following code:
/* Practical 14: Displaying name, school name, address using puts() */ #include <stdio.h> int main() { // Declare variables to store student information char name[] = "Ahmed Ali"; char school[] = "Government High School"; char address[] = "House # 12, Street 5, Lahore"; // Display student information using puts() puts("========== STUDENT INFORMATION =========="); puts("Name :"); puts(name); puts("School :"); puts(school); puts("Address :"); puts(address); puts("=========================================="); 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. char school[] = "Government High School"; — Declares a string for the school name.
  4. char address[] = "House # 12, Street 5, Lahore"; — Declares a string for the address.
  5. puts() — Displays each string on a separate line with automatic newline.

Step 3: Save, Compile, and Run

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

Run the program:

./student_info_puts

Expected Output:

========== STUDENT INFORMATION ========== Name : Ahmed Ali School : Government High School Address : House # 12, Street 5, Lahore ==========================================

Step 4: Modify the Program

  1. Change the values of the variables to your own name, school, and address.
  2. Save, compile, and run the program again.
  3. Notice how puts() automatically adds a newline after each string.

📊 Observations

  • The puts() function is used to display strings on the screen.
  • Each puts() statement displays one string and automatically moves to the next line.
  • The program successfully displays the student's Name, School Name, and Address.
  • puts() is simpler than printf() when you only need to display strings.
  • You don't need to use \n with puts().

✅ Result

You have successfully written a C program that displays a student's Name, School Name, and Address using the puts() function. You have learned how puts() can be used to display multiple lines of text easily.

📌 Important: puts() automatically adds a newline character after each string. This makes it perfect for displaying information line by line.
💡 Tip: You can use puts(""); to print a blank line. This is useful for adding spacing between lines of output.

🎯 مقصد

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

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

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

📖 تعارف

اس عملی میں، آپ طالب علم کی معلومات ظاہر کرنے کے لیے puts() فنکشن کا استعمال کریں گے۔ puts() فنکشن C پروگرامنگ میں سٹرنگز آؤٹ پٹ کرنے کا ایک آسان طریقہ ہے۔ یہ ہر سٹرنگ کے بعد خود بخود نئی لائن شامل کرتا ہے، جو متعدد لائنوں کا متن ظاہر کرنے کے لیے بہترین ہے۔

puts() فنکشن ایک ہی دلیل لیتا ہے — ایک سٹرنگ (کریکٹر اری) — اور اسے اسکرین پر ظاہر کرتا ہے۔ یہ stdio.h لائبریری کا حصہ ہے۔

💡 مشورہ: puts() ہر سٹرنگ کے آخر میں خود بخود نئی لائن شامل کرتا ہے، لہذا آپ کو \n استعمال کرنے کی ضرورت نہیں ہے۔

📝 اقدامات

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

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. student_info_puts.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 14: Displaying name, school name, address using puts() */ #include <stdio.h> int main() { // Declare variables to store student information char name[] = "Ahmed Ali"; char school[] = "Government High School"; char address[] = "House # 12, Street 5, Lahore"; // Display student information using puts() puts("========== STUDENT INFORMATION =========="); puts("Name :"); puts(name); puts("School :"); puts(school); puts("Address :"); puts(address); puts("=========================================="); return 0; }

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

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

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

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

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

./student_info_puts

متوقع آؤٹ پٹ:

========== STUDENT INFORMATION ========== Name : Ahmed Ali School : Government High School Address : House # 12, Street 5, Lahore ==========================================

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

  1. متغیرات کی ویلیوز کو اپنے نام، سکول اور پتے میں تبدیل کریں۔
  2. پروگرام کو محفوظ کریں، کمپائل کریں اور دوبارہ چلائیں۔
  3. غور کریں کہ puts() ہر سٹرنگ کے بعد خود بخود نئی لائن کیسے شامل کرتا ہے۔

📊 مشاہدات

  • puts() فنکشن اسکرین پر سٹرنگز ظاہر کرنے کے لیے استعمال ہوتا ہے۔
  • ہر puts() بیان ایک سٹرنگ ظاہر کرتا ہے اور خود بخود اگلی لائن پر چلا جاتا ہے۔
  • پروگرام کامیابی سے طالب علم کا نام، سکول کا نام اور پتہ ظاہر کرتا ہے۔
  • puts() printf() سے آسان ہے جب آپ کو صرف سٹرنگز ظاہر کرنی ہوں۔
  • آپ کو puts() کے ساتھ \n استعمال کرنے کی ضرورت نہیں ہے۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو puts() فنکشن کا استعمال کرتے ہوئے طالب علم کا نام، سکول کا نام اور پتہ ظاہر کرتا ہے۔ آپ نے سیکھ لیا ہے کہ puts() کو کس طرح متعدد لائنوں کا متن آسانی سے ظاہر کرنے کے لیے استعمال کیا جا سکتا ہے۔

📌 اہم: puts() ہر سٹرنگ کے بعد خود بخود نئی لائن کا کریکٹر شامل کرتا ہے۔ یہ معلومات کو لائن بہ لائن ظاہر کرنے کے لیے بہترین ہے۔
💡 مشورہ: آپ خالی لائن پرنٹ کرنے کے لیے puts(""); استعمال کر سکتے ہیں۔ یہ آؤٹ پٹ کی لائنوں کے درمیان فاصلہ شامل کرنے کے لیے مفید ہے۔
⬅ Back to Practical Notebooks