Write, compile, and run a C program demonstrating printf() in Visual Studio Code.
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.
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.
Figure 1: Creating a new C file in VS Code
Figure 2: Writing the printf() program in VS Code
Explanation:
gcc — the C compiler
printf_demo.c — your source code file
-o printf_demo — the output executable file name
Figure 3: Compiling the printf() program
Expected Output:
Figure 4: Output of the printf() program
| 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 |
printf() function successfully displayed all types of data.%.2f specifier limited float values to 2 decimal places.printf().\n.
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.
#include <stdio.h> at the top of your program to use printf(). This header file contains the declaration of the printf() function.
printf() statements with various format specifiers. This will help you become comfortable with formatting output in C programs.
VS Code میں ایک C پروگرام لکھنا، کمپائل کرنا اور چلانا جو مختلف اقسام کا آؤٹ پٹ ظاہر کرنے کے لیے printf() فنکشن کا استعمال دکھاتا ہے۔
اس عملی میں، آپ ایک C پروگرام لکھیں گے جو مختلف اقسام کا ڈیٹا ظاہر کرنے کے لیے printf() فنکشن کا استعمال کرتا ہے۔ آپ آؤٹ پٹ کو فارمیٹ کرنے کے لیے فارمیٹ اسپیسیفائر کا استعمال کرنا بھی سیکھیں گے۔ پروگرام کو Visual Studio Code کا استعمال کرتے ہوئے لکھا، کمپائل اور چلایا جائے گا۔
printf() فنکشن C پروگرامنگ میں سب سے زیادہ استعمال ہونے والے فنکشنز میں سے ایک ہے۔ یہ آپ کو متن، اعداد اور دیگر ڈیٹا کو اسکرین پر ظاہر کرنے کی اجازت دیتا ہے۔ یہ فنکشن stdio.h لائبریری کا حصہ ہے۔
شکل 1: VS Code میں نئی C فائل بنانا
شکل 2: VS Code میں printf() پروگرام لکھنا
وضاحت:
gcc — C کمپائلر
printf_demo.c — آپ کی سورس کوڈ فائل
-o printf_demo — آؤٹ پٹ ایگزیکیوٹیبل فائل کا نام
شکل 3: 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 میں کامیابی سے ایک C پروگرام لکھا، کمپائل کیا اور چلایا جو printf() فنکشن کا استعمال دکھاتا ہے۔ آپ نے انٹیجرز، فلوٹس، کریکٹرز اور سٹرنگز ظاہر کرنے کے لیے مختلف فارمیٹ اسپیسیفائر استعمال کرنا سیکھ لیا ہے۔ آپ نے جگہوں اور پیشانی صفر کے ساتھ آؤٹ پٹ فارمیٹ کرنا بھی سیکھ لیا ہے۔
printf() استعمال کرنے کے لیے ہمیشہ اپنے پروگرام کے شروع میں #include <stdio.h> شامل کریں۔ اس ہیڈر فائل میں printf() فنکشن کا اعلان موجود ہے۔
printf() بیانات لکھنے کی مشق کریں۔ اس سے آپ کو C پروگراموں میں آؤٹ پٹ فارمیٹ کرنے میں مہارت حاصل ہوگی۔