EZ Notes Logo

EZNotes

🎯 Aim

To write a C/C++ program that demonstrates the use of the puts() function to display strings on the screen.

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Basic knowledge of string handling in C

📖 Introduction

In C programming, puts() is a built-in function used to display a string on the screen. It stands for "put string". The puts() function is part of the stdio.h library. It automatically adds a newline character (\n) at the end of the string, so you don't need to include it yourself.

The puts() function is simpler than printf() because it only displays strings. It takes a single argument:

  • A string (character array) or a string literal in double quotes.
💡 Tip: puts() automatically adds a newline after the string. This is different from printf() where you need to add \n manually.

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named puts_demo.c.
  3. Write the following code:
/* Practical 13: Program showing the use of puts() */ #include <stdio.h> int main() { // Display string using puts() puts("Hello World!"); puts("Welcome to C Programming"); // Using puts() with variables char name[] = "Ahmed"; puts("My name is:"); puts(name); // Multiple strings puts("-----------------------"); puts("This is a line of text."); puts("puts() automatically adds a newline."); puts("You don't need to use \\n."); return 0; }

Step 2: Explanation of the Code

  1. #include <stdio.h> — Includes the standard input/output library.
  2. puts("Hello World!"); — Displays "Hello World!" and moves to a new line.
  3. puts(name); — Displays the value of the variable name.
  4. Each puts() statement automatically adds a newline after the string.

Step 3: Save, Compile, and Run

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

Run the program:

./puts_demo

Expected Output:

Hello World! Welcome to C Programming My name is: Ahmed ----------------------- This is a line of text. puts() automatically adds a newline. You don't need to use \n.

📊 Comparison: printf() vs puts()

Feature printf() puts()
Purpose Display formatted output Display only strings
Format Specifiers Supports %d, %f, %s, etc. Does NOT support format specifiers
Newline Requires \n manually Adds \n automatically
Arguments Multiple arguments possible Only one argument (string)
Header File stdio.h stdio.h
Example printf("Age: %d", age); puts("Hello");

📊 Observations

  • The puts() function is used to display strings on the screen.
  • It automatically adds a newline character at the end of the string.
  • puts() is simpler than printf() for displaying strings.
  • puts() does not support format specifiers like %d or %f.
  • You can use puts() with string literals or string variables.

✅ Result

You have successfully written a C program that demonstrates the use of the puts() function. You have learned how puts() differs from printf() and when to use each function.

📌 Important: puts() automatically adds a newline, while printf() does not. Use printf() when you need formatted output, and puts() when you only need to display a simple string.
💡 Tip: You can use fputs() to write strings to files, but puts() is specifically for displaying output on the screen.

🎯 مقصد

ایک C/C++ پروگرام لکھنا جو اسکرین پر سٹرنگز ظاہر کرنے کے لیے puts() فنکشن کا استعمال دکھاتا ہے۔

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

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

📖 تعارف

C پروگرامنگ میں، puts() ایک بلٹ ان فنکشن ہے جو اسکرین پر سٹرنگ ظاہر کرنے کے لیے استعمال ہوتا ہے۔ یہ "put string" کا مخفف ہے۔ puts() فنکشن stdio.h لائبریری کا حصہ ہے۔ یہ خود بخود سٹرنگ کے آخر میں ایک نئی لائن (\n) شامل کرتا ہے، لہذا آپ کو اسے خود شامل کرنے کی ضرورت نہیں ہے۔

puts() فنکشن printf() سے آسان ہے کیونکہ یہ صرف سٹرنگز ظاہر کرتا ہے۔ یہ ایک ہی دلیل لیتا ہے:

  • ایک سٹرنگ (کریکٹر اری) یا ڈبل کوٹس میں ایک سٹرنگ لٹرل۔
💡 مشورہ: puts() خود بخود سٹرنگ کے بعد نئی لائن شامل کرتا ہے۔ یہ printf() سے مختلف ہے جہاں آپ کو \n دستی طور پر شامل کرنا پڑتا ہے۔

📝 اقدامات

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

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. puts_demo.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 13: Program showing the use of puts() */ #include <stdio.h> int main() { // Display string using puts() puts("Hello World!"); puts("Welcome to C Programming"); // Using puts() with variables char name[] = "Ahmed"; puts("My name is:"); puts(name); // Multiple strings puts("-----------------------"); puts("This is a line of text."); puts("puts() automatically adds a newline."); puts("You don't need to use \\n."); return 0; }

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

  1. #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
  2. puts("Hello World!"); — "Hello World!" ظاہر کرتا ہے اور نئی لائن پر چلا جاتا ہے۔
  3. puts(name); — متغیر name کی ویلیو ظاہر کرتا ہے۔
  4. ہر puts() بیان خود بخود سٹرنگ کے بعد نئی لائن شامل کرتا ہے۔

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

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

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

./puts_demo

متوقع آؤٹ پٹ:

Hello World! Welcome to C Programming My name is: Ahmed ----------------------- This is a line of text. puts() automatically adds a newline. You don't need to use \n.

📊 موازنہ: printf() بمقابلہ puts()

خصوصیت printf() puts()
مقصد فارمیٹ شدہ آؤٹ پٹ ظاہر کرنا صرف سٹرنگز ظاہر کرنا
فارمیٹ اسپیسیفائر %d، %f، %s وغیرہ کو سپورٹ کرتا ہے فارمیٹ اسپیسیفائر کو سپورٹ نہیں کرتا
نئی لائن \n دستی طور پر درکار ہے خود بخود \n شامل کرتا ہے
دلائل متعدد دلائل ممکن ہیں صرف ایک دلیل (سٹرنگ)
ہیڈر فائل stdio.h stdio.h
مثال printf("Age: %d", age); puts("Hello");

📊 مشاہدات

  • puts() فنکشن اسکرین پر سٹرنگز ظاہر کرنے کے لیے استعمال ہوتا ہے۔
  • یہ خود بخود سٹرنگ کے آخر میں نئی لائن کا کریکٹر شامل کرتا ہے۔
  • puts() سٹرنگز ظاہر کرنے کے لیے printf() سے آسان ہے۔
  • puts() %d یا %f جیسے فارمیٹ اسپیسیفائر کو سپورٹ نہیں کرتا۔
  • آپ puts() کو سٹرنگ لٹرلز یا سٹرنگ متغیرات کے ساتھ استعمال کر سکتے ہیں۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو puts() فنکشن کا استعمال دکھاتا ہے۔ آپ نے سیکھ لیا ہے کہ puts() printf() سے کیسے مختلف ہے اور ہر فنکشن کو کب استعمال کرنا چاہیے۔

📌 اہم: puts() خود بخود نئی لائن شامل کرتا ہے، جبکہ printf() نہیں کرتا۔ جب آپ کو فارمیٹ شدہ آؤٹ پٹ کی ضرورت ہو تو printf() استعمال کریں، اور جب آپ کو صرف ایک سادہ سٹرنگ ظاہر کرنی ہو تو puts() استعمال کریں۔
💡 مشورہ: آپ فائلوں میں سٹرنگز لکھنے کے لیے fputs() استعمال کر سکتے ہیں، لیکن puts() خاص طور پر اسکرین پر آؤٹ پٹ ظاہر کرنے کے لیے ہے۔
⬅ Back to Practical Notebooks