EZ Notes Logo

EZNotes

🎯 Aim

To write a C/C++ program that takes the name of a school as input from the user and then displays it on the screen.

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Knowledge of strings (character arrays) in C

📖 Introduction

In this practical, you will learn how to take string input from the user in C. Strings are sequences of characters stored in character arrays. You will use:

  • char array — to store the school name
  • scanf() or gets() — to read the input
  • printf() or puts() — to display the output

Note: scanf() with %s only reads words without spaces. To read full sentences with spaces, you need gets() or fgets().

💡 Tip: Use scanf("%[^\n]", school); to read a string with spaces, or use gets() (though it is unsafe and deprecated in modern C).

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named school_name.c.
  3. Write the following code:
/* Practical 17: Program to take school name as input and display it */ #include <stdio.h> int main() { // Declare a character array to store the school name char school[100]; // Ask user to enter the school name printf("Enter your school name: "); // Read the input with spaces using scanf with %[^\n] scanf(" %[^\n]", school); // Display the school name printf("\n========== SCHOOL INFORMATION ==========\n"); printf("School Name: %s\n", school); printf("==========================================\n"); return 0; }

Step 2: Explanation of the Code

  1. #include <stdio.h> — Includes the standard input/output library.
  2. char school[100]; — Declares a character array of size 100 to store the school name.
  3. printf("Enter your school name: "); — Prompts the user to enter the school name.
  4. scanf(" %[^\n]", school); — Reads a string with spaces from the user.
  5. printf() — Displays the school name 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 school_name.c -o school_name

Run the program:

./school_name

Sample Input & Output:

Enter your school name: Government High School ========== SCHOOL INFORMATION ========== School Name: Government High School ==========================================

Step 4: Test with Different Inputs

  1. Test 1: "Army Public School" → Displays "Army Public School"
  2. Test 2: "Lahore Grammar School" → Displays "Lahore Grammar School"
  3. Test 3: "The City School" → Displays "The City School"

📊 Alternative Ways to Read Strings

Method Code Reads Spaces? Notes
scanf() scanf("%s", school); ❌ No Only reads until space
scanf() with [^\n] scanf(" %[^\n]", school); ✅ Yes Reads until newline (recommended)
gets() gets(school); ✅ Yes Unsafe, deprecated
fgets() fgets(school, 100, stdin); ✅ Yes Safe, includes newline
/* Alternative using fgets() - safer method */ #include <stdio.h> int main() { char school[100]; printf("Enter your school name: "); fgets(school, sizeof(school), stdin); // Remove the newline character if present school[strcspn(school, "\n")] = '\0'; printf("School Name: %s\n", school); return 0; }

📊 Observations

  • The program successfully takes the school name as input from the user.
  • The %[^\n] format specifier reads the entire line including spaces.
  • The school name is stored in a character array.
  • The program displays the school name on the screen.
  • Using fgets() is safer and recommended for modern C programming.

✅ Result

You have successfully written a C program that takes the name of a school as input from the user and displays it on the screen. You have learned how to:

  • Declare character arrays to store strings
  • Read string input with spaces using %[^\n]
  • Display strings using printf()
  • Use alternative safer methods like fgets()
📌 Important: %[^\n] reads everything until a newline character. The space before %[^\n] (" %[^\n]") consumes any leftover newline from previous input.
💡 Tip: For safer string input, use fgets() instead of gets(). gets() is deprecated and unsafe because it does not check buffer size.

🎯 مقصد

ایک C/C++ پروگرام لکھنا جو صارف سے سکول کا نام ان پٹ کے طور پر لے اور اسے اسکرین پر ظاہر کرے۔

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

  • Visual Studio Code یا کوئی بھی C/C++ کمپائلر
  • C میں سٹرنگز (کریکٹر اریز) کی معلومات

📖 تعارف

اس عملی میں، آپ C میں صارف سے سٹرنگ ان پٹ لینا سیکھیں گے۔ سٹرنگز کریکٹر اریز میں محفوظ کردہ حروف کا ایک سلسلہ ہے۔ آپ استعمال کریں گے:

  • char array — سکول کا نام ذخیرہ کرنے کے لیے
  • scanf() یا gets() — ان پٹ پڑھنے کے لیے
  • printf() یا puts() — آؤٹ پٹ ظاہر کرنے کے لیے

نوٹ: %s کے ساتھ scanf() صرف خالی جگہ کے بغیر الفاظ پڑھتا ہے۔ خالی جگہوں کے ساتھ پورے جملے پڑھنے کے لیے gets() یا fgets() کی ضرورت ہے۔

💡 مشورہ: خالی جگہوں کے ساتھ سٹرنگ پڑھنے کے لیے scanf("%[^\n]", school); استعمال کریں، یا gets() استعمال کریں (حالانکہ یہ جدید C میں غیر محفوظ اور متروک ہے)۔

📝 اقدامات

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

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. school_name.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 17: Program to take school name as input and display it */ #include <stdio.h> int main() { // Declare a character array to store the school name char school[100]; // Ask user to enter the school name printf("Enter your school name: "); // Read the input with spaces using scanf with %[^\n] scanf(" %[^\n]", school); // Display the school name printf("\n========== SCHOOL INFORMATION ==========\n"); printf("School Name: %s\n", school); printf("==========================================\n"); return 0; }

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

  1. #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
  2. char school[100]; — سکول کا نام ذخیرہ کرنے کے لیے 100 سائز کی کریکٹر اری کا اعلان کرتا ہے۔
  3. printf("Enter your school name: "); — صارف کو سکول کا نام درج کرنے کا کہتا ہے۔
  4. scanf(" %[^\n]", school); — صارف سے خالی جگہوں کے ساتھ سٹرنگ پڑھتا ہے۔
  5. printf() — سکول کا نام اسکرین پر ظاہر کرتا ہے۔

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

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

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

./school_name

نمونہ ان پٹ اور آؤٹ پٹ:

Enter your school name: Government High School ========== SCHOOL INFORMATION ========== School Name: Government High School ==========================================

📊 سٹرنگ پڑھنے کے متبادل طریقے

طریقہ کوڈ خالی جگہیں پڑھتا ہے؟ نوٹس
scanf() scanf("%s", school); ❌ نہیں صرف خالی جگہ تک پڑھتا ہے
scanf() [^\n] کے ساتھ scanf(" %[^\n]", school); ✅ ہاں نئی لائن تک پڑھتا ہے (تجویز کردہ)
gets() gets(school); ✅ ہاں غیر محفوظ، متروک
fgets() fgets(school, 100, stdin); ✅ ہاں محفوظ، نئی لائن شامل کرتا ہے

📊 مشاہدات

  • پروگرام کامیابی سے صارف سے سکول کا نام ان پٹ کے طور پر لیتا ہے۔
  • %[^\n] فارمیٹ اسپیسیفائر خالی جگہوں سمیت پوری لائن پڑھتا ہے۔
  • سکول کا نام کریکٹر اری میں محفوظ ہوتا ہے۔
  • پروگرام سکول کا نام اسکرین پر ظاہر کرتا ہے۔
  • جدید C پروگرامنگ کے لیے fgets() کا استعمال زیادہ محفوظ اور تجویز کردہ ہے۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو صارف سے سکول کا نام ان پٹ کے طور پر لیتا ہے اور اسے اسکرین پر ظاہر کرتا ہے۔ آپ نے سیکھ لیا ہے:

  • سٹرنگز کو ذخیرہ کرنے کے لیے کریکٹر اریز کا اعلان کرنا
  • %[^\n] کا استعمال کرتے ہوئے خالی جگہوں کے ساتھ سٹرنگ ان پٹ پڑھنا
  • printf() کا استعمال کرتے ہوئے سٹرنگز ظاہر کرنا
  • fgets() جیسے محفوظ متبادل طریقے استعمال کرنا
📌 اہم: %[^\n] نئی لائن کریکٹر تک سب کچھ پڑھتا ہے۔ %[^\n] سے پہلے کی خالی جگہ (" %[^\n]") پچھلے ان پٹ سے بچی ہوئی نئی لائن کو ہٹا دیتی ہے۔
💡 مشورہ: محفوظ سٹرنگ ان پٹ کے لیے gets() کے بجائے fgets() استعمال کریں۔ gets() متروک اور غیر محفوظ ہے کیونکہ یہ بفر سائز کو چیک نہیں کرتا۔
⬅ Back to Practical Notebooks