C Input and Output
Learn how to take input from users and display output in C programs.
जानें कि C प्रोग्राम में उपयोगकर्ता से इनपुट कैसे लें और आउटपुट कैसे दिखाएं।
Input and Output in C is done using standard library functions like printf()
, scanf()
, getchar()
, putchar()
, gets()
, and puts()
. They are defined in stdio.h
.
C में इनपुट और आउटपुट printf()
, scanf()
, getchar()
, putchar()
, gets()
, और puts()
जैसी standard library functions के द्वारा किया जाता है। ये stdio.h
में परिभाषित हैं।
Example 1: Basic output using printf()
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
`printf()` displays text on the screen.
`printf()` स्क्रीन पर टेक्स्ट दिखाने के लिए उपयोग किया जाता है।
Output: Hello, World!
Example 2: Printing variables
#include <stdio.h>
int main() {
int age = 20;
printf("Age: %d", age);
return 0;
}
Use format specifiers like `%d`, `%f`, `%c` with printf.
printf में `%d`, `%f`, `%c` जैसे format specifiers का उपयोग करें।
Output: Age: 20
Example 3: Basic input using scanf()
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("You entered: %d", num);
return 0;
}
`scanf()` takes input from the user using format specifiers.
`scanf()` उपयोगकर्ता से इनपुट लेने के लिए format specifiers का उपयोग करता है।
Output: Enter a number: 5
You entered: 5
Example 4: Taking float input
#include <stdio.h>
int main() {
float price;
printf("Enter price: ");
scanf("%f", &price);
printf("Price: %.2f", price);
return 0;
}
`%f` reads and prints floating-point numbers.
`%f` floating-point numbers पढ़ने और प्रिंट करने के लिए उपयोग होता है।
Output: Enter price: 12.50
Price: 12.50
Example 5: Character input using getchar()
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
ch = getchar();
printf("You entered: %c", ch);
return 0;
}
`getchar()` reads a single character input.
`getchar()` एक अक्षर पढ़ने के लिए उपयोग होता है।
Output: Enter a character: A
You entered: A
Example 6: Character output using putchar()
#include <stdio.h>
int main() {
char ch = 'Z';
putchar(ch);
return 0;
}
`putchar()` prints a single character to the screen.
`putchar()` स्क्रीन पर एक अक्षर प्रिंट करने के लिए उपयोग होता है।
Output: Z
Example 7: Using puts() for strings
#include <stdio.h>
int main() {
char str[] = "Welcome to C!";
puts(str);
return 0;
}
`puts()` prints strings with an automatic newline.
`puts()` स्ट्रिंग को newline के साथ प्रिंट करता है।
Output: Welcome to C!
Example 8: Reading string with gets() (unsafe)
#include <stdio.h>
int main() {
char name[20];
printf("Enter name: ");
gets(name); // Avoid in real programs
printf("Hello %s", name);
return 0;
}
`gets()` reads a full line but is unsafe; prefer `fgets()`.
`gets()` पूरी लाइन पढ़ता है लेकिन असुरक्षित है; `fgets()` बेहतर है।
Output: Enter name: John
Hello John
Example 9: Formatted output
#include <stdio.h>
int main() {
int a = 10;
float b = 3.5;
printf("Int: %d, Float: %.1f", a, b);
return 0;
}
You can mix multiple variables in printf.
printf में एक से अधिक variables का प्रयोग किया जा सकता है।
Output: Int: 10, Float: 3.5
Example 10: Using escape sequences
#include <stdio.h>
int main() {
printf("Line1\nLine2\tTabbed");
return 0;
}
Escape sequences like `\n` (newline) and `\t` (tab) control formatting.
`\n` (newline) और `\t` (tab) जैसे escape sequences formatting को नियंत्रित करते हैं।
Output: Line1
Line2 Tabbed