Exploring Smartphone Influence on the Brain: Neurological Perspectives
Angular vs. React: Which One Should You Choose?
Discover Serenity: Your Guide to Mindfulness Apps
Discover how smartphone apps can enhance your mindfulness practice: 5 Senses Exercise with Apps
Discover How Smartwatches Measure Blood Pressure: Explained Simply
13 Exciting Games Launching in April 2024: From Freedom Planet 2 to TopSpin 2K25!
Discover the 5 Amazon Big Spring Deals on Tablets from Samsung, Lenovo
Big Savings Alert: Amazfit Smart Watches Now on Sale on Amazon!
Amazon's Big Spring Sale: Top 6 Anker Souncore Headphones and Earbuds Deals
Affordable VR Adventures: The Best Budget VR Headsets
Fly in Peace: Discover the Ultimate Noise-Cancelling Headphones for Flying
Bringing AI to Life: NVIDIA's Digital Human Technolgies in Healthcare, Gaming, and More
Discover Exciting New Games on NVIDIA GeForce NOW!
Steam Spring Sale 2024 is here: Explore the 10 Best FPS Games
The Future of iPhones: Apple's Bold Step into AI with DarwinAI
Discover the Magic of Sonos Soundbar: Transform Your Home Entertainment Experience!
Enhance Your Home Fun: 5 Best Sonos Soundbars to Amp Up Your Entertainment!
Pinterest Introduces AI-Powered Body Type Ranges for Inclusive Searches
Embrace the Next Wave: 35+ AI Tools for Enhanced Productivity in 2024
Xbox Gaming Bonanza: Lots of New Games with Xbox Game Pass!
Sony Paves the Way for Gaming Evolution with 'Super-Fungible Tokens' Patent
Smart Printing Choices: 10 Key Factors to Consider When Buying an HP Printer or Any Printer
Projector Picks: Explore the Best Mini Projectors for Big Screen Fun!
JavaScript Essentials: Your Quick Start to Web Coding
Gaming Laptop Guide 2024: 10 Crucial Checks Before You Buy + Top 5 Picks for you!
Gaming Joy Awaits: Explore the Best PS5 Games of 2024
Epic Games Special: Dive into Astro Duel 2 for FREE this week. See What’s Coming Next Week!
Fitbit Fitness Tracker Guide 2024: Choose Your Perfect Fit
Feel the Beat: Exploring Top Over-Ear Headphones
Explore the Web Development Strategies in 2024: A Developers Handbook
Explore Must-Play Nintendo Switch Games in 2024!
Eclipse Ready: CE and ISO Certified Solar Eclipse Glasses for a Safe Sky Spectacle
Disney and Pixar’s Inside Out 2 Introduces New Emotions to Riley's World
Discover Waze's cool new features for safer and happier drives!
Discover the Top Picks: Best Smartwatches for Your Lifestyle
Discover the Best Smartphones Trending Now: Your Easy Guide to the Best Picks!
Sound Revolution: Discover the Best Bluetooth Speakers of 2024!
Discover the 10 Best Productivity Apps to Supercharge Your Daily Tasks
Discover,Install and Enjoy: The Best Chrome Extensions for Developers in 2024
Crack the Code: Your Guide to Computer Programming Magic in 2024
Boost Your Content Creation: 10 ChatGPT Prompts to Supercharge Content Creation Productivity
10 Best Tech Companies in Silicon Valley
Top 10 Web Development Interview Questions you can...
Learn how to Answer Tell me about Yourself
5 Books You Need to Read Right Now
25 Practical Ways to Earn Money Online
Translate this page in your preferred language:
Article by: Manish Methani
Last Updated: November 7, 2021 at 2:04pm IST
In C programming, the printf() function is used to output values to the console. The function uses format specifiers to determine the type of data that is being output. The % format specifiers are used to format the output and can be used with various flags and modifiers to achieve different effects.
There are several types of format specifiers in C programming, including:
The % format specifiers are used in conjunction with the printf() function to format the output. Here are some examples of how to use the % format specifiers with the printf() function:
int num = -123; printf("The value of num is %d", num);
Output:
The value of num is -123
unsigned int num = 123; printf("The value of num is %u", num);
Output:
The value of num is 123
float num = 3.14159; printf("The value of num with 2 decimal places is %.2f", num);
Output:
The value of num with 2 decimal places is 3.14
char ch = 'A'; printf("The character is %c", ch);
Output:
The character is A
char str[] = "Hello, world!"; printf("The string is %s", str);
Output:
The string is Hello, world!
float a = 3.122222223; printf(“a = %.2f ”, a);
Output:
a = 3.12
float a = 3.122222223; printf(“a = %.3f ”, a);
Output:
a = 3.122
%.3f will restrict the values upto three decimal values.
float a = 1.289999; printf(“ a = %6.2f ”, a);
Output:
_ _1.29
where _ _ are spaces
%6.2f :- means output will be in 6 columns
a = 1.28999;
While printing float values using %6.2f format specifier, the final output should be of length 6. As 6.2f indicates, the total length of output should be 6 and 2 values after "." The rest part is covered with spaces. As you can see in the output image below, 2 decimal values after dot and before that we need three more values so that total length becomes 6.
float a = 1.289999; printf(“ a = %5.3f ”, a);
Output:
In this case, the final output length should be of length 5 and 3 decimal values after "." dot and 2 values before "." dot
float a = 1.289999; printf(“ a = %5.2f ”, a);
In this case, the final output length should be of length 5 and 2 decimal values after "." dot and 3 values before "." dot
float a = 1111.289999; printf(“ a = %5.3f ”, a);
Output:
a = 1111.290
In this case, before dot it contains enough digits than 5 so it will store 1111 in one block and rest in another.
Space will only be added when there are not enough digits.
Question
printf(“ %5.2f “, a);
Answer
If a is 1111.2344444, it prints 1111.23 If a is 11.2344444, it prints 11.23 If a is 1.2344444 , it prints _1.234 , where _ is one leading whitespace character.
Example 1:
int a = 1234; printf(“a = %3d”, a);
Output:
a = 1234
Example 2:
int a = 123; printf(“a = %3d”, a);
Output:
a = 123
Example 3:
int a = 12; printf(“a = %3d”, a);
Output:
a = _12 where, _ is space
Example 4:
int a = 1; printf(“a = %3d”, a);
Output:
a = _ _1 where, _ are spaces.
In conclusion, the % format specifiers are a powerful tool in C programming that allows you to format output in various ways. By using the examples provided in this tutorial, you can master the art of formatting output and improve your C programming skills.
A: Format specifiers are placeholders used in C language to print the value of a variable in a specific format. They are used with printf() and scanf() functions to format input and output.
A: The syntax of a format specifier starts with the percentage sign (%) followed by optional flags, width, precision, length modifier and conversion specifier.
A: Width specifies the minimum number of characters to be printed while printing a variable. Precision specifies the number of digits to be printed after the decimal point while printing floating-point numbers.
A: The most commonly used format specifiers in C are %d for integers, %f for floating-point numbers, %c for characters, %s for strings, and %p for pointers.
A: Yes, you can use multiple format specifiers in a printf() or scanf() statement to format the output or input of multiple variables.
A: Some advanced format specifiers in C include %e and %E for scientific notation, %u for unsigned integers, %x and %X for hexadecimal numbers, and %n for the number of characters printed so far.
A: Using the wrong format specifier for a variable can result in undefined behavior and may cause the program to crash or produce unexpected results. It is important to always use the correct format specifier for the type of variable being printed or scanned.
Test your C Programming skills with this comprehensive mock test on C Programming.
Take TestSolve most asked Interview Questions on Flutter and Test your foundational skills in flutter.
Take TestSolve most asked GATE Questions in Operating Systems and test your Gate Score.
Take TestThis is a mock test designed to help you assess your knowledge and skills in HTML and CSS.
Take TestSolve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.
Take Test