EZ Notes Logo

EZNotes

🎯 Aim

To write, compile, and run a C program in VS Code that demonstrates the use of the printf() function to display various types of output.

🛠️ What You Need

  • Visual Studio Code installed on your computer
  • C/C++ extension installed in VS Code
  • MinGW (gcc) or any C compiler installed

📖 Introduction

In this practical, you will write a C program that uses the printf() function to display different types of data. You will also learn how to use format specifiers to format the output. The program will be written, compiled, and run using Visual Studio Code.

The printf() function is one of the most commonly used functions in C programming. It allows you to display text, numbers, and other data on the screen. The function is part of the stdio.h library.

💡 Tip: Make sure you have the C/C++ extension installed in VS Code. You can install it from the Extensions tab (square icon on the left).

📝 Steps

Step 1: Open VS Code

  1. Click the VS Code icon on your desktop or Start Menu.
  2. Open your folder where you save your C programs.

Step 2: Create a New File

  1. Click the New File button in the Explorer.
  2. Type the file name: printf_demo.c and press Enter.
Creating a new C file in VS Code Figure 1: Creating a new C file in VS Code

Step 3: Write the Program

  1. Copy and paste the following code into your file:
/* Practical 11: printf() Demonstration Program */ #include <stdio.h> int main() { // Variables of different types int age = 16; float height = 5.8; char grade = 'A'; char name[] = "Ali"; int num1 = 10, num2 = 20; int sum = num1 + num2; // Printing text printf("========== STUDENT REPORT ==========\n"); // Printing variables printf("Name: %s\n", name); printf("Age: %d years\n", age); printf("Height: %.1f feet\n", height); printf("Grade: %c\n", grade); // Arithmetic operations printf("\n========== ARITHMETIC ==========\n"); printf("%d + %d = %d\n", num1, num2, sum); printf("%d - %d = %d\n", num1, num2, num1 - num2); printf("%d x %d = %d\n", num1, num2, num1 * num2); printf("%d / %d = %d\n", num2, num1, num2 / num1); // Printing in different formats printf("\n========== DIFFERENT FORMATS ==========\n"); printf("Number: %d\n", num1); printf("Number with 5 spaces: %5d\n", num1); printf("Number with leading zeros: %05d\n", num1); printf("Float: %f\n", height); printf("Float with 2 decimals: %.2f\n", height); printf("\n====================================\n"); return 0; }

Step 4: Save the File

  1. Press Ctrl + S to save the file.
  2. You will see the file name in the Explorer.
printf() program code in VS Code Figure 2: Writing the printf() program in VS Code

Step 5: Compile the Program

  1. Open the terminal (Ctrl + `).
  2. Type the following command and press Enter:
gcc printf_demo.c -o printf_demo

Explanation:
gcc — the C compiler
printf_demo.c — your source code file
-o printf_demo — the output executable file name

Compiling printf program in VS Code Figure 3: Compiling the printf() program

Step 6: Run the Program

  1. Type the following command to run your program:
./printf_demo

Expected Output:

========== STUDENT REPORT ========== Name: Ali Age: 16 years Height: 5.8 feet Grade: A ========== ARITHMETIC ========== 10 + 20 = 30 10 - 20 = -10 10 x 20 = 200 20 / 10 = 2 ========== DIFFERENT FORMATS ========== Number: 10 Number with 5 spaces: 10 Number with leading zeros: 00010 Float: 5.800000 Float with 2 decimals: 5.80 ====================================
printf program output in VS Code Figure 4: Output of the printf() program

📊 Format Specifiers Summary

Format Specifier Description Example Output
%d Integer printf("%d", 42); 42
%f Float (default 6 decimals) printf("%f", 3.14); 3.140000
%.2f Float with 2 decimals printf("%.2f", 3.14); 3.14
%c Character printf("%c", 'A'); A
%s String printf("%s", "Hello"); Hello
%5d Integer with 5 spaces printf("%5d", 42); 42
%05d Integer with leading zeros printf("%05d", 42); 00042
\n Newline printf("Hello\nWorld"); Hello
World

📊 Observations

  • The printf() function successfully displayed all types of data.
  • Different format specifiers were used for different data types.
  • The %.2f specifier limited float values to 2 decimal places.
  • Arithmetic operations were performed and displayed using printf().
  • Text formatting and line breaks were achieved using \n.
  • The program compiled and ran without errors in VS Code.

✅ Result

You have successfully written, compiled, and run a C program in VS Code that demonstrates the use of the printf() function. You have learned how to use different format specifiers to display integers, floats, characters, and strings. You have also learned how to format output with spaces and leading zeros.

📌 Important: Always include #include <stdio.h> at the top of your program to use printf(). This header file contains the declaration of the printf() function.
💡 Tip: Practice writing different printf() statements with various format specifiers. This will help you become comfortable with formatting output in C programs.

🎯 مقصد

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

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

  • آپ کے کمپیوٹر پر Visual Studio Code انسٹال ہو
  • VS Code میں C/C++ ایکسٹینشن انسٹال ہو
  • MinGW (gcc) یا کوئی بھی C کمپائلر انسٹال ہو

📖 تعارف

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

printf() فنکشن C پروگرامنگ میں سب سے زیادہ استعمال ہونے والے فنکشنز میں سے ایک ہے۔ یہ آپ کو متن، اعداد اور دیگر ڈیٹا کو اسکرین پر ظاہر کرنے کی اجازت دیتا ہے۔ یہ فنکشن stdio.h لائبریری کا حصہ ہے۔

💡 مشورہ: یقینی بنائیں کہ آپ نے VS Code میں C/C++ ایکسٹینشن انسٹال کی ہے۔ آپ اسے Extensions ٹیب (بائیں طرف مربع آئیکن) سے انسٹال کر سکتے ہیں۔

📝 اقدامات

پہلا قدم: VS Code کھولیں

  1. اپنے ڈیسک ٹاپ یا اسٹارٹ مینو پر VS Code آئیکن پر کلک کریں۔
  2. اپنا وہ فولڈر کھولیں جہاں آپ اپنے C پروگرام محفوظ کرتے ہیں۔

دوسرا قدم: ایک نئی فائل بنائیں

  1. Explorer میں New File بٹن پر کلک کریں۔
  2. فائل کا نام ٹائپ کریں: printf_demo.c اور Enter دبائیں۔
VS Code میں نئی C فائل بنانا شکل 1: VS Code میں نئی C فائل بنانا

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

  1. نیچے دیے گئے کوڈ کو اپنی فائل میں کاپی اور پیسٹ کریں:
/* Practical 11: printf() Demonstration Program */ #include <stdio.h> int main() { // Variables of different types int age = 16; float height = 5.8; char grade = 'A'; char name[] = "Ali"; int num1 = 10, num2 = 20; int sum = num1 + num2; // Printing text printf("========== STUDENT REPORT ==========\n"); // Printing variables printf("Name: %s\n", name); printf("Age: %d years\n", age); printf("Height: %.1f feet\n", height); printf("Grade: %c\n", grade); // Arithmetic operations printf("\n========== ARITHMETIC ==========\n"); printf("%d + %d = %d\n", num1, num2, sum); printf("%d - %d = %d\n", num1, num2, num1 - num2); printf("%d x %d = %d\n", num1, num2, num1 * num2); printf("%d / %d = %d\n", num2, num1, num2 / num1); // Printing in different formats printf("\n========== DIFFERENT FORMATS ==========\n"); printf("Number: %d\n", num1); printf("Number with 5 spaces: %5d\n", num1); printf("Number with leading zeros: %05d\n", num1); printf("Float: %f\n", height); printf("Float with 2 decimals: %.2f\n", height); printf("\n====================================\n"); return 0; }

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

  1. فائل کو محفوظ کرنے کے لیے Ctrl + S دبائیں۔
  2. آپ کو Explorer میں فائل کا نام نظر آئے گا۔
VS Code میں printf() پروگرام کا کوڈ شکل 2: VS Code میں printf() پروگرام لکھنا

پانچواں قدم: پروگرام کمپائل کریں

  1. ٹرمینل کھولیں (Ctrl + `
  2. نیچے دی گئی کمانڈ ٹائپ کریں اور Enter دبائیں:
gcc printf_demo.c -o printf_demo

وضاحت:
gcc — C کمپائلر
printf_demo.c — آپ کی سورس کوڈ فائل
-o printf_demo — آؤٹ پٹ ایگزیکیوٹیبل فائل کا نام

VS Code میں printf پروگرام کمپائل کرنا شکل 3: printf() پروگرام کمپائل کرنا

چھٹا قدم: پروگرام چلائیں

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

متوقع آؤٹ پٹ:

========== STUDENT REPORT ========== Name: Ali Age: 16 years Height: 5.8 feet Grade: A ========== ARITHMETIC ========== 10 + 20 = 30 10 - 20 = -10 10 x 20 = 200 20 / 10 = 2 ========== DIFFERENT FORMATS ========== Number: 10 Number with 5 spaces: 10 Number with leading zeros: 00010 Float: 5.800000 Float with 2 decimals: 5.80 ====================================
printf پروگرام کا آؤٹ پٹ شکل 4: printf() پروگرام کا آؤٹ پٹ

📊 فارمیٹ اسپیسیفائر کا خلاصہ

فارمیٹ اسپیسیفائر وضاحت مثال آؤٹ پٹ
%d انٹیجر printf("%d", 42); 42
%f فلوٹ (6 اعشاریہ) printf("%f", 3.14); 3.140000
%.2f فلوٹ (2 اعشاریہ) printf("%.2f", 3.14); 3.14
%c کریکٹر printf("%c", 'A'); A
%s سٹرنگ printf("%s", "Hello"); Hello
%5d 5 جگہوں والا انٹیجر printf("%5d", 42); 42
%05d پیشانی صفر والا انٹیجر printf("%05d", 42); 00042
\n نئی لائن printf("Hello\nWorld"); Hello
World

📊 مشاہدات

  • printf() فنکشن نے تمام اقسام کا ڈیٹا کامیابی سے ظاہر کیا۔
  • مختلف ڈیٹا ٹائپس کے لیے مختلف فارمیٹ اسپیسیفائر استعمال کیے گئے۔
  • %.2f اسپیسیفائر نے فلوٹ ویلیوز کو 2 اعشاریہ جگہوں تک محدود کیا۔
  • ریاضی کے عمل کیے گئے اور printf() کا استعمال کرتے ہوئے ظاہر کیے گئے۔
  • متن کی فارمیٹنگ اور لائن بریک \n کا استعمال کرتے ہوئے حاصل کیے گئے۔
  • پروگرام VS Code میں بغیر کسی غلطی کے کمپائل اور چل گیا۔

✅ نتیجہ

آپ نے VS Code میں کامیابی سے ایک C پروگرام لکھا، کمپائل کیا اور چلایا جو printf() فنکشن کا استعمال دکھاتا ہے۔ آپ نے انٹیجرز، فلوٹس، کریکٹرز اور سٹرنگز ظاہر کرنے کے لیے مختلف فارمیٹ اسپیسیفائر استعمال کرنا سیکھ لیا ہے۔ آپ نے جگہوں اور پیشانی صفر کے ساتھ آؤٹ پٹ فارمیٹ کرنا بھی سیکھ لیا ہے۔

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