C का printf()
In this page:
printf("format string");
printf("text %specifier", variable_name);
printf() क्या है?
printf ("print formatted") screen पर text और variable values लिखने के लिए standard C library function है -- यह stdio.h का हिस्सा है और वह primary तरीका है जिससे एक C program इसे चलाने वाले व्यक्ति को कुछ भी वापस बताता है।
उदाहरण: What is printf()?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("Hello from printf");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Variables Print करना
एक printf string के अंदर %d या %s जैसे format specifiers placeholders की तरह काम करते हैं जो order में, format string के बाद आपके pass किए गए arguments की values से replace हो जाते हैं -- specifier और variable के type को mismatch करना garbage output produce करता है।
उदाहरण: Printing Variables
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare age and set it to 25
int age = 25;
// Print formatted text to the screen
printf("%d", age);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
कई Values Print करना
जब printf को कई format specifiers के साथ call किया जाता है, हर एक list में left-to-right order में अगला argument consume करता है, इसलिए printf("%d and %d", a, b) पहले a की value print करता है और दूसरी b की, उनकी position से match करते हुए।
उदाहरण: Printing Multiple Values
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int a = 1, b = 2;
// Print formatted text to the screen
printf("%d and %d", a, b);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Output में Newlines इस्तेमाल करना
क्योंकि printf खुद कभी line break add नहीं करता, consecutive printf calls same line पर एक-दूसरे के तुरंत बाद print होते हैं जब तक आप explicitly \n शामिल न करें जहाँ भी आप चाहते हैं कि output एक नई line पर जाए।
उदाहरण: Using Newlines in Output
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("First\n");
// Print formatted text to the screen
printf("Second");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Floating-Point Precision Format करना
%f की बजाय %.2f लिखना print होने पर एक floating-point value को exactly 2 decimal places तक round करता है -- currency जैसी चीज़ें decimal point के बाद C के default six digits की बजाय cleanly दिखाने के लिए essential।
उदाहरण: Formatting Floating-Point Precision
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare price and set it to 19.9999
double price = 19.9999;
// Print formatted text to the screen
printf("%.2f", price);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- गलत format specifier इस्तेमाल करना जैसे एक
floatके लिए%d, जो garbage print करता है। printfको format specifiers से कम arguments देना, जो undefined behavior है।- एक user-supplied string को format की तरह pass करना, जैसे
printf(input);, जो एक security risk है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: