Input and Output के दो मुख्य in-built functions है |
- scanf (Input)
- printf (Output)
1. scanf (Input Function)
- scanf ये एक standard library function है |
- scanf के जरिये numeric, characters और string कोई भी value input किया जाता है |
- scanf को program में इस्तेमाल करने के लिए stdio.h ये header file include करनी पडती है |
- scanf ये Keyboard से data read करने के लिए इस्तेमाल करते है |
Syntax for scanf() function
scanf("format_specifier(s)", &variable_list(s) );निचे दिए हुए example में दो format specifiers और दो variables & (address operator or ampersand) के साथ लिए है | हर data types के अलग-अलग format specifiers होते है | यहाँ पर %d ये integer data type का format specifier है | data को Keyboard से read करने के लिए Variable के साथ '&' देना पड़ता है | अगर input देना हो तो Compiler को variable का address बताना पड़ता है |
for eg.
scanf( "%d %d", &a, &b );
2. printf (Output Function)
- printf ये एक standard library function है |
- printf data को screen पर write करने के लिए इस्तेमाल किया जाता है |
- printf को program में इस्तेमाल करने के लिए stdio.h ये header file include करनी पडती है |
Syntax for scanf() function
printf("String / format_specifier(s) / escape sequence(s)", variable_list(s) );for string
यहाँ पर सिर्फ string को इस्तेमाल किया गया है | String को Double quotes(" ") में लिखा जाता है और बाद में semi-colon देते है |printf("Hello World !");
for format specifier and variable
ये scanf से input किये गए numeric (%d), character(%c), float(%f) या string(%s) output में print करता है |यहाँ पर variable का address(&) नहीं दिया जाता |
printf("%d", a);
for escape sequence
यहाँ पर printf में \n(newline) ये escape sequence लिया है |printf("\n");
और भी कुछ Input/Output Library Functions
- getchar() and putchar() (for single character)
- gets() and puts() (for string)
1. getchar(Input) and putchar(Output) (for single character)
getchar और putchar ये दोनों standard input/output (stdio.h) इस header file के library functions है,इसीलिए #include <stdio.h> ये preprocessor देना जरुरी है |getchar का इस्तेमाल user से input मतलब एक character लेने की अनुमति ली जाती है |
अगर user ने एक से ज्यादा character input में दे दिए तो putchar पहले एक ही character को output में print करता है |
For Example
Source Code :
#include <stdio.h>
int main( ) {
int i;
printf( "Enter a character :");
i = getchar();
printf( "Entered character is : ");
putchar(i);
return 0;
}
Output :
Enter a character :hello Entered character is : h
2. gets() and puts() (for string)
gets और puts ये दोनों standard input/output (stdio.h) इस header file के library functions है,इसीलिए #include <stdio.h> ये preprocessor देना जरुरी है |gets का इस्तेमाल user से input मतलब string लेने की अनुमति ली जाती है | puts input लिए string को output में print करता है |
For Example
Source Code :
#include <stdio.h>
int main(){
char name[20];
printf( "Enter your name : \n");
gets(name);
printf( "You name is : ");
puts(name);
return 0;
}
Output :
Enter your name : Rakesh Your name is : Rakesh







No comments: