🎯 Aim
To write a C/C++ program that uses the gets() function to input a student's Name, Father Name, and School Name, and then displays them on the screen.
🛠️ What You Need
- Visual Studio Code or any C/C++ compiler
- Basic knowledge of strings and character arrays
📖 Introduction
In this practical, you will learn about the gets() function. The gets() function is used to read a string from the user, including spaces. It continues reading until a newline character is encountered. However, note that gets() is deprecated and unsafe because it does not check buffer size. For modern programming, fgets() is recommended instead.
You will input:
- Student's Name
- Father's Name
- School Name
💡 Tip: gets() reads the entire line including spaces and stops at newline. It is simpler to use than scanf() for strings with spaces.
⚠️ Warning: gets() is deprecated and unsafe. It does not check buffer size and can cause buffer overflow. Use fgets() in modern programs.
📝 Steps
Step 1: Write the Program
- Open VS Code or any C compiler.
- Create a new file named student_info_gets.c.
- Write the following code:
#include <stdio.h>
int main()
{
char name[100];
char fatherName[100];
char schoolName[100];
printf("Enter Student Name: ");
gets(name);
printf("Enter Father Name: ");
gets(fatherName);
printf("Enter School Name: ");
gets(schoolName);
printf("\n========== STUDENT INFORMATION ==========\n");
printf("Student Name : %s\n", name);
printf("Father Name : %s\n", fatherName);
printf("School Name : %s\n", schoolName);
printf("==========================================\n");
return 0;
}
Step 2: Explanation of the Code
- #include <stdio.h> — Includes the standard input/output library.
- char name[100]; — Declares a character array of size 100 to store the student's name.
- char fatherName[100]; — Declares a character array to store the father's name.
- char schoolName[100]; — Declares a character array to store the school name.
- gets(name); — Reads the student's name from the user, including spaces.
- printf() — Displays all the information on the screen.
Step 3: Save, Compile, and Run
- Save the file (Ctrl + S).
- Open the terminal and compile using the command:
gcc student_info_gets.c -o student_info_gets
./student_info_gets
Sample Input & Output:
Enter Student Name: Ahmed Ali
Enter Father Name: Muhammad Ali
Enter School Name: Government High School
========== STUDENT INFORMATION ==========
Student Name : Ahmed Ali
Father Name : Muhammad Ali
School Name : Government High School
==========================================
Step 4: Test with Different Inputs
- Test 1: Enter different names with spaces
- Test 2: Enter names with special characters
- Test 3: Enter very long names (but within buffer size)
📊 gets() vs scanf() Comparison
| Feature |
gets() |
scanf("%s") |
| Reads spaces |
✅ Yes |
❌ No |
| Stops at |
Newline (\n) |
Space or newline |
| Buffer overflow protection |
❌ No (unsafe) |
⚠️ Limited |
| Status |
Deprecated |
Standard |
| Recommended |
❌ No (use fgets) |
✅ For words only |
📊 Observations
- The
gets() function reads the entire line including spaces.
- It stops reading when a newline character is encountered.
- Each input is stored in its respective character array.
- The program successfully displays all the entered information.
gets() is simpler than scanf() for reading strings with spaces.
- However,
gets() is unsafe because it does not check buffer size.
✅ Result
You have successfully written a C program that uses the gets() function to input a student's Name, Father Name, and School Name, and then displays them on the screen. You have learned how gets() works and its advantages and disadvantages.
📌 Important: gets() is deprecated in modern C (C11 and later). It is recommended to use fgets() instead for safer string input.
💡 Tip: To use fgets() instead of gets():
fgets(name, 100, stdin);
This reads up to 99 characters and includes the newline. You can remove the newline using strcspn().
#include <stdio.h>
#include <string.h>
int main()
{
char name[100], fatherName[100], schoolName[100];
printf("Enter Student Name: ");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = '\0';
printf("Enter Father Name: ");
fgets(fatherName, sizeof(fatherName), stdin);
fatherName[strcspn(fatherName, "\n")] = '\0';
printf("Enter School Name: ");
fgets(schoolName, sizeof(schoolName), stdin);
schoolName[strcspn(schoolName, "\n")] = '\0';
printf("\nStudent Name: %s\n", name);
printf("Father Name : %s\n", fatherName);
printf("School Name : %s\n", schoolName);
return 0;
}
🎯 مقصد
ایک C/C++ پروگرام لکھنا جو gets() فنکشن کا استعمال کرتے ہوئے طالب علم کا نام، والد کا نام اور سکول کا نام ان پٹ کے طور پر لے اور پھر انہیں اسکرین پر ظاہر کرے۔
🛠️ آپ کو کیا چاہیے
- Visual Studio Code یا کوئی بھی C/C++ کمپائلر
- سٹرنگز اور کریکٹر اریز کی بنیادی معلومات
📖 تعارف
اس عملی میں، آپ gets() فنکشن کے بارے میں جانیں گے۔ gets() فنکشن صارف سے خالی جگہوں سمیت سٹرنگ پڑھنے کے لیے استعمال ہوتا ہے۔ یہ نیو لائن کریکٹر ملنے تک پڑھتا رہتا ہے۔ تاہم، نوٹ کریں کہ gets() متروک اور غیر محفوظ ہے کیونکہ یہ بفر سائز کو چیک نہیں کرتا۔ جدید پروگرامنگ کے لیے fgets() استعمال کرنے کی سفارش کی جاتی ہے۔
آپ ان پٹ لیں گے:
- طالب علم کا نام
- والد کا نام
- سکول کا نام
💡 مشورہ: gets() خالی جگہوں سمیت پوری لائن پڑھتا ہے اور نیو لائن پر رک جاتا ہے۔ یہ خالی جگہوں والی سٹرنگز کے لیے scanf() سے استعمال میں آسان ہے۔
⚠️ انتباہ: gets() متروک اور غیر محفوظ ہے۔ یہ بفر سائز کو چیک نہیں کرتا اور بفر اوور فلو کا سبب بن سکتا ہے۔ جدید پروگراموں میں fgets() استعمال کریں۔
📝 اقدامات
پہلا قدم: پروگرام لکھیں
- VS Code یا کوئی بھی C کمپائلر کھولیں۔
- student_info_gets.c نام کی ایک نئی فائل بنائیں۔
- نیچے دیا گیا کوڈ لکھیں:
#include <stdio.h>
int main()
{
char name[100];
char fatherName[100];
char schoolName[100];
printf("Enter Student Name: ");
gets(name);
printf("Enter Father Name: ");
gets(fatherName);
printf("Enter School Name: ");
gets(schoolName);
printf("\n========== STUDENT INFORMATION ==========\n");
printf("Student Name : %s\n", name);
printf("Father Name : %s\n", fatherName);
printf("School Name : %s\n", schoolName);
printf("==========================================\n");
return 0;
}
دوسرا قدم: کوڈ کی وضاحت
- #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
- char name[100]; — طالب علم کا نام ذخیرہ کرنے کے لیے 100 سائز کی کریکٹر اری کا اعلان کرتا ہے۔
- char fatherName[100]; — والد کا نام ذخیرہ کرنے کے لیے کریکٹر اری کا اعلان کرتا ہے۔
- char schoolName[100]; — سکول کا نام ذخیرہ کرنے کے لیے کریکٹر اری کا اعلان کرتا ہے۔
- gets(name); — صارف سے طالب علم کا نام خالی جگہوں سمیت پڑھتا ہے۔
- printf() — تمام معلومات کو اسکرین پر ظاہر کرتا ہے۔
تیسرا قدم: محفوظ کریں، کمپائل کریں اور چلائیں
- فائل کو محفوظ کریں (Ctrl + S)۔
- ٹرمینل کھولیں اور نیچے دی گئی کمانڈ استعمال کرتے ہوئے کمپائل کریں:
gcc student_info_gets.c -o student_info_gets
./student_info_gets
نمونہ ان پٹ اور آؤٹ پٹ:
Enter Student Name: Ahmed Ali
Enter Father Name: Muhammad Ali
Enter School Name: Government High School
========== STUDENT INFORMATION ==========
Student Name : Ahmed Ali
Father Name : Muhammad Ali
School Name : Government High School
==========================================
📊 gets() بمقابلہ scanf() موازنہ
| خصوصیت |
gets() |
scanf("%s") |
| خالی جگہیں پڑھتا ہے |
✅ ہاں |
❌ نہیں |
| کس پر رکتا ہے |
نیو لائن (\n) |
خالی جگہ یا نیو لائن |
| بفر اوور فلو تحفظ |
❌ نہیں (غیر محفوظ) |
⚠️ محدود |
| حیثیت |
متروک |
معیاری |
| تجویز کردہ |
❌ نہیں (fgets استعمال کریں) |
✅ صرف الفاظ کے لیے |
📊 مشاہدات
gets() فنکشن خالی جگہوں سمیت پوری لائن پڑھتا ہے۔
- یہ نیو لائن کریکٹر ملنے پر پڑھنا بند کر دیتا ہے۔
- ہر ان پٹ اپنی متعلقہ کریکٹر اری میں محفوظ ہوتا ہے۔
- پروگرام کامیابی سے تمام درج کردہ معلومات ظاہر کرتا ہے۔
gets() خالی جگہوں والی سٹرنگز پڑھنے کے لیے scanf() سے آسان ہے۔
- تاہم،
gets() غیر محفوظ ہے کیونکہ یہ بفر سائز کو چیک نہیں کرتا۔
✅ نتیجہ
آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو gets() فنکشن کا استعمال کرتے ہوئے طالب علم کا نام، والد کا نام اور سکول کا نام ان پٹ کے طور پر لیتا ہے اور پھر انہیں اسکرین پر ظاہر کرتا ہے۔ آپ نے سیکھ لیا ہے کہ gets() کیسے کام کرتا ہے اور اس کے فوائد اور نقصانات کیا ہیں۔
📌 اہم: gets() جدید C (C11 اور بعد میں) میں متروک ہے۔ محفوظ سٹرنگ ان پٹ کے لیے fgets() استعمال کرنے کی سفارش کی جاتی ہے۔
💡 مشورہ: gets() کے بجائے fgets() استعمال کرنے کے لیے:
fgets(name, 100, stdin);
یہ 99 حروف تک پڑھتا ہے اور نیو لائن شامل کرتا ہے۔ آپ strcspn() کا استعمال کرتے ہوئے نیو لائن کو ہٹا سکتے ہیں۔