C Language में Printf और Scanf का Complete Guide in Hindi

अगर आप C Programming सीख रहे हैं तो printf and scanf in C in Hindi में समझना बेहद जरूरी है क्योंकि ये दोनों functions किसी भी C program के सबसे महत्वपूर्ण हिस्से हैं। Printf और scanf functions के बिना आप user से input नहीं ले सकते और ना ही output को screen पर display कर सकते हैं। ये दोनों standard input-output functions हैं जो stdio.h header file में defined होते हैं।

इस comprehensive guide में आप printf और scanf functions के बारे में विस्तार से सीखेंगे। हम इन functions के syntax, working mechanism, format specifiers, और practical examples के साथ समझेंगे। चाहे आप beginner हों या intermediate level के programmer, यह article आपको इन दोनों functions को पूरी तरह से समझने में मदद करेगा।

इस article में हम printf और scanf के अलग-अलग use cases, common errors, best practices और real-world examples भी देखेंगे। आप सीखेंगे कि कैसे different data types के साथ इन functions का उपयोग करें और अपने C programs को ज्यादा interactive और user-friendly बनाएं।

Printf Function क्या है?

Printf function C programming की सबसे commonly used output function है जो stdio.h header file में defined होती है। “Printf” का मतलब है “print formatted” यानी formatted तरीके से data को screen पर display करना। यह function आपको text, numbers, variables की values और special characters को console पर print करने की सुविधा देता है।

Printf का Basic Syntax

printf(“format string”, argument_list);

यहाँ format string में वह text होता है जो आप display करना चाहते हैं, और argument_list में variables होते हैं जिनकी values आप print करना चाहते हैं।

Printf के Simple Examples

Example 1: Simple Text Print करना

#include <stdio.h>
int main() {
    printf("Welcome to Anwarcodes.com!");
    return 0;
}

Output:

Welcome to Anwarcodes.com!

Example 2: Variables की Values Print करना

#include <stdio.h>
int main() {
    int age = 25;
    printf("You are %d year old", age);
    return 0;
}

Output:

You are 25 year old

Scanf Function क्या है?

Scanf function का उपयोग user से input लेने के लिए किया जाता है। यह भी stdio.h header file का हिस्सा है और “scan formatted” के लिए stand करता है। जब भी आपको अपने program में user से कोई data input करवाना हो, तब scanf function का use होता है।

Scanf का Basic Syntax

scanf(“format specifier”, &variable_name);

यहाँ & symbol address operator है जो variable के memory address को indicate करता है। यह बहुत जरूरी है क्योंकि scanf को यह बताना पड़ता है कि input value को memory में कहाँ store करना है।

Scanf के Simple Examples

Example 1: Integer Input लेना

#include <stdio.h>
int main() {
    int number;
    printf("Enter a number : ");
    scanf("%d", &number);
    printf("You have entered %d.", number);
    return 0;
}

Output:

Enter a number : 5
You have entered 5

printf and scanf in C in Hindi

Format Specifiers की Complete List

Format specifiers वे special characters होते हैं जो printf और scanf में data type को specify करते हैं। Printf and scanf in C in Hindi को समझने के लिए format specifiers की जानकारी बहोत जरुरी है।

मुख्य Format Specifiers

Format Specifiersकाम
%d या %iInteger values के लिए
%fFloat values के लिए
%lfDouble values के लिए
%cSingle character के लिए
%sString (character array) के लिए
%uUnsigned integer के लिए
%ldLong integer के लिए
%xHexadecimal format में
%oOctal format में
%%% symbol print करने के लिए

Format Specifiers का Practical Use

#include <stdio.h>
int main() {
    int roll = 101;
    float marks = 85.5;
    char grade = 'A';
    
    printf("Roll number : %d\n", roll);
    printf("Marks : %.2f\n", marks);
    printf("Grade : %c\n", grade);
    
    return 0;
}

Output:

Roll number : 101
Marks : 85.50
Grade : A

Printf में Advanced Features

1. Width और Precision Control

Printf में आप output की width और decimal precision को control कर सकते हैं:

#include <stdio.h>
int main() {
    float pi = 3.14159;
    
    printf("%.2f\n", pi);    // Output: 3.14
    printf("%.4f\n", pi);    // Output: 3.1416
    printf("%10.2f\n", pi);  // Output:       3.14 (10 spaces में)
    
    return 0;
}

Output:

3.14
3.1416
3.14

2. Escape Sequences का उपयोग

Escape Sequencesकाम
\nNew line
\tTab space
\Backslash print करने के लिए
Double quotes print करने के लिए
\rCarriage return

Example:

printf(“First line \nSecond line\n”);

printf(“Name:\tRahul\nAge:\t25\n”);

Scanf में Important Points

1. Address Operator (&) का महत्व

Scanf में & symbol न भूलना बहुत जरूरी है (strings को छोड़कर):

int num;

scanf(“%d”, &num);  // Correct

scanf(“%d”, num);   // Wrong – error

2. Multiple Inputs एक साथ लेना

#include <stdio.h>
int main() {
    int age;
    float height;
    
    printf("Enter age and height: ");
    scanf("%d %f", &age, &height);
    
    printf("Age: %d, Height: %.2f\n", age, height);
    return 0;
}

Output:

Enter age and height: 25
5.6
Age: 25, Height: 5.60

3. String Input में Special Case

Strings के लिए & operator की जरूरत नहीं होती:

char name[50];

scanf(“%s”, name);  // & Not need

Important: scanf(“%s”) spaces को छोड़ देता है। पूरी line पढ़ने के लिए fgets() या scanf(“%[^\n]”) use करें।

Printf और Scanf का Combined Use

एक complete program जो user input लेकर calculations करता है:

#include <stdio.h>
int main() {
    char name[50];
    int sub1, sub2, sub3;
    float average;
    
    printf("Enter student name: ");
    scanf("%s", name);
    
    printf("Enter marks of 3 subject: ");
    scanf("%d %d %d", &sub1, &sub2, &sub3);
    
    average = (sub1 + sub2 + sub3) / 3.0;
    
    printf("\n--- Result ---\n");
    printf("Name: %s\n", name);
    printf("Subject 1: %d\n", sub1);
    printf("Subject 2: %d\n", sub2);
    printf("Subject 3: %d\n", sub3);
    printf("Average marks: %.2f\n", average);
    
    return 0;
}

Output:

Enter student name: Rahul
Enter marks of 3 subject: 66
80
70

— Result —
Name: Rahul
Subject 1: 66
Subject 2: 80
Subject 3: 70
Average marks: 72.00

Common Errors और उनके Solutions

Error 1: Format Specifier Mismatch

int num = 10;

printf(“%f”, num);  // गलत – integer के लिए %d use करें

Solution: हमेशा सही format specifier use करें।

Error 2: & Operator भूलना

int value;

scanf(“%d”, value);  // गलत

scanf(“%d”, &value); // सही

Error 3: Buffer Overflow (String Input में)

char name[10];

// User अगर 15 characters enter करे तो problem

scanf(“%s”, name);

Solution: Size limit use करें:

scanf(“%9s”, name);  // Maximum 9 characters + 1 null character

Real-World Applications

Example 1: Simple Calculator

#include <stdio.h>
int main() {
    float num1, num2, result;
    char operator;
    
    printf("Enter first number: ");
    scanf("%f", &num1);
    
    printf("Enter operator (+, -, *, /): ");
    scanf(" %c", &operator);
    
    printf("Enter second number: ");
    scanf("%f", &num2);
    
    switch(operator) {
        case '+':
            result = num1 + num2;
            break;
        case '-':
            result = num1 - num2;
            break;
        case '*':
            result = num1 * num2;
            break;
        case '/':
            result = num1 / num2;
            break;
        default:
            printf("Wrong operator!\n");
            return 1;
    }
    
    printf("Result: %.2f\n", result);
    return 0;
}

Output;

Enter first number: 30
Enter operator (+, -, *, /): +
Enter second number: 20
Result: 50.00

Example 2: Temperature Converter

#include <stdio.h>
int main() {
    float celsius, fahrenheit;
    
    printf("Enter temperature in Celsius: ");
    scanf("%f", &celsius);
    
    fahrenheit = (celsius * 9/5) + 32;
    
    printf("%.2f°C = %.2f°F\n", celsius, fahrenheit);
    return 0;
}

Output;

Enter temperature in Celsius: 40
40.00 celsius = 104.00 fahrenheit

Best Practices

जब आप printf and scanf in C in Hindi में काम करें, तो इन best practices को follow करें:

  1. हमेशा stdio.h include करें: यह mandatory है
  2. Correct format specifiers use करें: Data type के according
  3. & operator याद रखें: Scanf में (strings को छोड़कर)
  4. Input validation करें: Invalid inputs को handle करें
  5. Clear prompts दें: User को क्या input देना है यह बताएं
  6. Code को readable रखें: Proper formatting और comments use करें

Performance Tips

  1. Printf calls को minimize करें: Multiple printf की जगह एक ही printf में सब print करें
  2. scanf return value check करें: यह बताता है कि कितने items successfully read हुए
int result = scanf("%d", &num);

   if(result != 1) {

       printf("Invalid input!\n");

   }
  1. Buffer को clear करें: Multiple inputs लेते समय

  while(getchar() != ‘\n’);  // Input buffer clear करने के लिए

Conclusion (निष्कर्ष)

Printf and scanf in C in Hindi को master करना आपकी C programming journey में एक crucial step है। इन दोनों functions के बिना आप user के साथ effective communication नहीं कर सकते। हमने इस article में printf और scanf के सभी important aspects को cover किया है – basic syntax से लेकर advanced features, common errors और उनके solutions तक। Format specifiers की समझ, escape sequences का सही उपयोग, और best practices को follow करके आप professional-quality programs लिख सकते हैं।

अब समय है practice का! इस article में दिए गए सभी examples को अपने computer पर run करें, अलग-अलग variations try करें, और अपने खुद के programs बनाएं। Programming में perfection केवल regular practice से ही आती है। अगर आपको कोई doubt हो तो code को छोटे parts में break करके समझने की कोशिश करें या फिर comment करके मुझसे पूछ सकते हैं। C programming में strong foundation बनाने के लिए printf और scanf का proper knowledge अत्यंत आवश्यक है। Happy Coding!

Frequently Asked Questions

Q1. Printf और Scanf में सबसे बड़ा अंतर क्या है?

Ans: Printf output function है जो data को screen पर display करता है, जबकि scanf input function है जो user से data लेती है। Printf में & operator की जरूरत नहीं होती, लेकिन scanf में (strings को छोड़कर) & operator mandatory है क्योंकि scanf को variable का address चाहिए होता है।

Q2. Scanf में & symbol क्यों जरूरी है?

Ans: & symbol एक address operator है जो variable के memory location को point करता है। Scanf को यह बताना जरूरी होता है कि input value को memory में कहाँ store करना है। बिना & के scanf को variable का address नहीं मिलेगा और program crash हो सकता है। String के case में array name खुद ही address होता है इसलिए & की जरूरत नहीं होती।

Q3. Float values को कितने decimal places तक print कर सकते हैं?

Ans: आप precision specifier का use करके decimal places control कर सकते हैं। उदाहरण के लिए, %.2f दो decimal places, %.4f चार decimal places print करेगा। Default रूप से printf float values को 6 decimal places तक print करता है। आप %f के बजाय %.nf use करें जहाँ n decimal places की संख्या है।

Q4. Scanf से पूरी line (spaces के साथ) कैसे read करें?

Ans: Scanf(“%s”) spaces पर रुक जाता है। पूरी line पढ़ने के लिए आप scanf(“%[^\n]”, str) या fgets(str, size, stdin) का use कर सकते हैं। Fgets ज्यादा safe है क्योंकि यह buffer overflow से बचाता है। Example: fgets(name, 50, stdin);

Q5. एक ही scanf statement में कई variables को input कैसे लें?

Ans: एक ही scanf में multiple format specifiers और variables use करें, सभी को space या comma से separate करते हुए। उदाहरण: scanf(“%d %f %c”, &num, &price, &grade); User को values space से separate करके enter करनी होंगी। यह तरीका efficient है लेकिन user को clear instructions देना जरूरी है।

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top