|3. Input & Output in C
Chapter 3C Language Tutorial~1 min read

Input & Output in C

printf आणि scanf — Input/Output

C मध्ये screen वर print करायला printf() आणि user कडून input घ्यायला scanf() वापरतात. दोन्ही stdio.h मध्ये आहेत. Format specifiers सांगतात कोणता data type print/read करायचा आहे.

printf() — Output

printf with format specifiers

c
#include <stdio.h>

int main() {
    int age = 20;
    float marks = 85.5;
    char grade = 'A';

    // Format specifiers
    printf("Age: %d\n", age);       // %d = integer
    printf("Marks: %.2f\n", marks); // %.2f = float, 2 decimal places
    printf("Grade: %c\n", grade);   // %c = character

    // Multiple values एकाच printf मध्ये
    printf("Student: age=%d, marks=%.1f, grade=%c\n",
           age, marks, grade);

    // Escape sequences
    printf("Line 1\nLine 2\n");    // \n = नवी ओळ
    printf("Column1\tColumn2\n");  // \t = tab
    printf("He said \"Hello\"\n"); // \" = double quote

    return 0;
}

scanf() — Input

scanf — user कडून input घेणे

c
#include <stdio.h>

int main() {
    int age;
    float marks;
    char name[50];   // string साठी char array

    // Input घेणे
    printf("तुमचे नाव टाका: ");
    scanf("%s", name);          // string साठी & नको

    printf("तुमचे वय टाका: ");
    scanf("%d", &age);          // int साठी & लागतो

    printf("तुमचे marks टाका: ");
    scanf("%f", &marks);        // float साठी & लागतो

    // Output दाखवणे
    printf("\nनमस्कार %s!\n", name);
    printf("वय: %d वर्षे\n", age);
    printf("Marks: %.2f\n", marks);

    return 0;
}
📌

scanf() मध्ये integer/float साठी & (address-of operator) लागतो. String (char array) साठी & लागत नाही कारण array name आधीच address असतो.

Format Specifiers — Quick Reference

  • %d — int (integer)
  • %f — float (दशांश)
  • %lf — double (long float)
  • %c — char (एकच character)
  • %s — string (char array)
  • %ld — long int
  • %.2f — float 2 decimal places पर्यंत

Key Points — लक्षात ठेवा

  • printf() — screen वर output, scanf() — user input
  • Format specifiers: %d, %f, %lf, %c, %s
  • scanf मध्ये variables आधी & द्यायला हवे (strings सोडून)
  • \n = newline, \t = tab, \\ = backslash
  • stdio.h header असल्याशिवाय printf/scanf काम करणार नाही
0/12 chapters पूर्ण