EZ Notes Logo

EZNotes

🎯 Aim

To write a C program that demonstrates the use of the printf() function to display output on the screen.

📖 Introduction

In C programming, printf() is a built-in function used to display output on the screen. It stands for "print formatted". The printf() function is part of the stdio.h (Standard Input/Output) library. You need to include this library at the beginning of your program using #include <stdio.h>.

The printf() function can display:

  • Simple text or strings
  • Values of variables (integers, floats, characters)
  • Formatted output using format specifiers
💡 Tip: Format specifiers like %d (integer), %f (float), %c (character), and %s (string) are used with printf() to print different types of data.

📝 Steps

Step 1: Write the C Program

  1. Open VS Code or any C compiler.
  2. Create a new file named printf_demo.c.
  3. Write the following code:
/* Practical 10: Program showing use of printf() */ #include <stdio.h> int main() { // Printing simple text printf("Hello World!\n"); // Printing integers int age = 15; printf("My age is %d years.\n", age); // Printing floats float marks = 85.5; printf("I got %.2f marks in Math.\n", marks); // Printing characters char grade = 'A'; printf("My grade is %c.\n", grade); // Printing strings char name[] = "Ahmed"; printf("My name is %s.\n", name); // Multiple format specifiers printf("Name: %s, Age: %d, Grade: %c, Marks: %.2f\n", name, age, grade, marks); return 0; }

Step 2: Save and Compile

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

Step 3: Run the Program

  1. Run the program using the command:
./printf_demo

Expected Output:

Hello World! My age is 15 years. I got 85.50 marks in Math. My grade is A. My name is Ahmed. Name: Ahmed, Age: 15, Grade: A, Marks: 85.50

📊 Format Specifiers Used

Format Specifier Data Type Example Output
%d Integer printf("%d", 10); 10
%f Float printf("%f", 3.14); 3.140000
%.2f Float (2 decimals) printf("%.2f", 3.14); 3.14
%c Character printf("%c", 'A'); A
%s String printf("%s", "Hello"); Hello
\n Newline printf("Hello\nWorld"); Hello
World

📊 Observations

  • The printf() function is used to display output on the screen.
  • Format specifiers like %d, %f, %c, %s are used to print different data types.
  • \n is used to move to a new line.
  • %.2f limits float values to 2 decimal places.
  • Multiple variables can be printed in a single printf() statement using multiple format specifiers.

✅ Result

You have successfully written a C program that demonstrates the use of the printf() function. You can now display different types of data on the screen using format specifiers.

📌 Important: Always include #include <stdio.h> at the top of your program to use printf() and other I/O functions.
💡 Tip: Practice using printf() with different format specifiers to become comfortable with displaying output in C programs.

🎯 مقصد

ایک C پروگرام لکھنا جو اسکرین پر آؤٹ پٹ ظاہر کرنے کے لیے printf() فنکشن کا استعمال دکھاتا ہے۔

📖 تعارف

C پروگرامنگ میں، printf() ایک بلٹ ان فنکشن ہے جو اسکرین پر آؤٹ پٹ ظاہر کرنے کے لیے استعمال ہوتا ہے۔ یہ "print formatted" کا مخفف ہے۔ printf() فنکشن stdio.h (Standard Input/Output) لائبریری کا حصہ ہے۔ آپ کو اس لائبریری کو اپنے پروگرام کے شروع میں #include <stdio.h> استعمال کرکے شامل کرنا ہوگا۔

printf() فنکشن درج ذیل ظاہر کر سکتا ہے:

  • سادہ متن یا سٹرنگز
  • متغیرات کی ویلیوز (انٹیجرز، فلوٹس، کریکٹرز)
  • فارمیٹ اسپیسیفائرز کا استعمال کرتے ہوئے فارمیٹ شدہ آؤٹ پٹ
💡 مشورہ: فارمیٹ اسپیسیفائر جیسے %d (انٹیجر)، %f (فلوٹ)، %c (کریکٹر)، اور %s (سٹرنگ) مختلف اقسام کے ڈیٹا کو پرنٹ کرنے کے لیے printf() کے ساتھ استعمال ہوتے ہیں۔

📝 اقدامات

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

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. printf_demo.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 10: Program showing use of printf() */ #include <stdio.h> int main() { // Printing simple text printf("Hello World!\n"); // Printing integers int age = 15; printf("My age is %d years.\n", age); // Printing floats float marks = 85.5; printf("I got %.2f marks in Math.\n", marks); // Printing characters char grade = 'A'; printf("My grade is %c.\n", grade); // Printing strings char name[] = "Ahmed"; printf("My name is %s.\n", name); // Multiple format specifiers printf("Name: %s, Age: %d, Grade: %c, Marks: %.2f\n", name, age, grade, marks); return 0; }

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

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

تیسرا قدم: پروگرام چلائیں

  1. نیچے دی گئی کمانڈ استعمال کرتے ہوئے پروگرام چلائیں:
./printf_demo

متوقع آؤٹ پٹ:

Hello World! My age is 15 years. I got 85.50 marks in Math. My grade is A. My name is Ahmed. Name: Ahmed, Age: 15, Grade: A, Marks: 85.50

📊 استعمال شدہ فارمیٹ اسپیسیفائر

فارمیٹ اسپیسیفائر ڈیٹا کی قسم مثال آؤٹ پٹ
%d انٹیجر printf("%d", 10); 10
%f فلوٹ printf("%f", 3.14); 3.140000
%.2f فلوٹ (2 اعشاریہ) printf("%.2f", 3.14); 3.14
%c کریکٹر printf("%c", 'A'); A
%s سٹرنگ printf("%s", "Hello"); Hello
\n نئی لائن printf("Hello\nWorld"); Hello
World

📊 مشاہدات

  • printf() فنکشن اسکرین پر آؤٹ پٹ ظاہر کرنے کے لیے استعمال ہوتا ہے۔
  • فارمیٹ اسپیسیفائر جیسے %d، %f، %c، %s مختلف ڈیٹا ٹائپس کو پرنٹ کرنے کے لیے استعمال ہوتے ہیں۔
  • \n نئی لائن پر جانے کے لیے استعمال ہوتا ہے۔
  • %.2f فلوٹ ویلیوز کو 2 اعشاریہ جگہوں تک محدود کرتا ہے۔
  • ایک ہی printf() بیان میں متعدد فارمیٹ اسپیسیفائر استعمال کرتے ہوئے متعدد متغیرات کو پرنٹ کیا جا سکتا ہے۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو printf() فنکشن کا استعمال دکھاتا ہے۔ اب آپ فارمیٹ اسپیسیفائر کا استعمال کرتے ہوئے مختلف اقسام کا ڈیٹا اسکرین پر ظاہر کر سکتے ہیں۔

📌 اہم: printf() اور دیگر I/O فنکشنز استعمال کرنے کے لیے ہمیشہ اپنے پروگرام کے شروع میں #include <stdio.h> شامل کریں۔
💡 مشورہ: C پروگراموں میں آؤٹ پٹ ظاہر کرنے میں مہارت حاصل کرنے کے لیے مختلف فارمیٹ اسپیسیفائر کے ساتھ printf() استعمال کرنے کی مشق کریں۔
⬅ Back to Practical Notebooks