L03 data input and output

Page 1

emeronTI3 ³

bBa©Úl nig beBa©jTinñn½y DATA Input & Output

I. GnuKmn_ printf ( ) and putchar ( ) :

Ca Function sMrab; Print Tinñn½yEdlenAkñúgsBaØa “ … ” mkelI Screen output . Binitü]TahrN_xageRkam ³ + printf ( ) :

# include < stdio.h > # include < conio.h > main ( ) { int n; float x; char ch; n = 5; x = 8.2; ch = ‘A’ ; clrscr (); printf (“ print values : %d getch ( ); return (0); }

%f

%c

“ , n, x, ch );

Edl % Ca Format code nig d Ca Conversion code . + ebIcg; print Expression (Variable ) enaHeyIgRtUvkMNt; eGaytMélénkenSamenaH . Note : + %d

tarag Format Code :

Format


Format Code

Gtßn½y

%c

Print

bNþacMnYnCa characters short, int Print bNþacMnYn int , short Print bNþacMnYn KμansBØaa/ unsigned int, Print bNþacMnYn long Print bNþacMnYnKt;xageRkamrag hexa Print bNþacMnYnKt;xageRkamrag Octab ( eKal 8)

%d %u %ld %X, %x %o %f %s

Ex:

%lf

Print float, double Print string

+ printf (“ %3d”,n);

n = 28 n=1

enaH print)anCa ^28 enaH print )anCa ^^1

x = 1.3245 Æ 1.324500 x = 12.3456789 Æ 1.23456789 + E01 x = 1.2345 Æ ^^^^ 1.234 x = 1.2345E3 Æ ^^ 1234.500

+ printf (“ %f ”, x);

+ printf (“ %10.3f”,x );

+ printf (“ %d = %c”, ‘A’, ‘A’) ; Æ 65 = A *

ebIcg; print % RtUvsresr printf (“ 50 %%” );

+ print

Æ 50%

sMelg Beep : printf (“ \a\a Enter Value :”);

+putchar ( c) :

Ca Function sMrab; print character mYymkelI screen .

ch = ‘a’; putchar (ch); Æ a + puts :

sMrab; Print string mkelI screen, ehIy Cursor nigcuHbnÞat;/ puts (“this is a character”) ;

II. Print Data into printer :

56


fprintf (stdprn, “ Print Data on printer!”);

nig print Tinñn½yeTAkñúg printer .

sMrab;bBa¢akMuBüÚT½reGay print eTA printer .

stdprn :

III. scanf( ) function :

Ca Function sMrab; Read data from keyboard Ex1: int n,p; scanf (“%d%d”, &n, &p); Ex2: char c; int n; scanf (“%c%d”,&c,&n);

ebI ³ EtebI ³

Enter

Result

a25 a^^25

c = ‘a’ , n = 25 c = ‘a’ , n = 25

Ex3: char c; int n; scanf (“ %d %c”, &n, &c); Enter Result 12^a

Æ

n = 12; c = ‘^’; (

minEmn = ‘a’ eT )

Ex4: char c; int n; scanf (“ %d %c”, &n, &c); Enter Result 12^a Æ n = 12; c = ‘a’; 12^^a Æ n = 12; c = ‘a’; * Macro Ex: Ex:

getchar( );

sMrab; Read character mYyBI Keyboard .

c = getchar ( ); # include <stdio.h> # include <conio.h> main ( ) { clrscr (); int n,p; printf (“ Enter n = ”); scanf (“ %d”, &n) ; while (getchar ( )! = ‘\n’); printf (“ thank you for your Enter Value %d \n”, n) ; printf (“ Enter P =”); scanf (“ %d”, &p) ; while (getchar ( ) ! = ‘\n’) ;

57


printf (“thank you for your Enter Value %d \n”, p) ; getch(); } Statement : while (getchar ( ) != ‘n\’); Note :

mann½yfa ³

scanf (“%d”,&n);

Read character

Readln (n)

<=> while (getchar( ) ! = ‘\n’);

(Read

rhUtdl; Enter eTIbQb;

kúñg Pascal .

tMélehIycuHbnÞat;Etmþg )

eyIgGacCMnYs while (getchar ( ) ! = ‘\n’); eday gets (ss); scanf (“%d”, &n); gets (ss);

edIm,I chear stdin eyIgeRbI Statemenut fflush(stdin);

etIman character Read bBa©Úl b¤Gt; ³ kbhit ( ): Function enHnwgRtYtBinitüemIl/man Read character NaBI Keyboard b¤Gt;/ ehIyEdl Character kMBugsßitenAkñúg memory . kbhit ( ) smmUl nig keypressed kñúg Pascal . vapÞúkkñúg <conio.h>

* Control

Ex:

# include <conio.h> # include <stdio.h> int main (void ) { clrscr () ; cprintf (“\r\npress any key to continue :”); while(!kbhit ( ) ); /* No action */ cprintf (“\r\n you have press a key ! \r\n”); getch ( ); return (0); }

Note :

Ca Function <=> printf, kñúg cprintf => Gt;sÁal; \n CasBaØacuHedImbnÞat;eT/ RtUveRbI\r\n eTIbsÁal;fa cuHedImbnÞat; . -cprintf() :

smmUlnwg Readkey kñúg Pascal ) getche ( ) Function RbePT Macro Read character nImYy²BI keyboard . character Read BI keyboard RtUv)ansMEdgmkBI screen . cMENk Function getch ( ) k¾CaRbePT Macro Read character BI keyboard Edr Et ,Character Edl Read BI keyboard enaHminsMEdgmkelI screen eT . ( smmUl nig Readkey kúñg Pascal ) * getch ( ) Function (

58


. dUecñHeyIgGnuvtþn¾ Function getch( ) kñúgkmμviFI Password . mü:ageToteyIgeRbI Function enHenA cugénRKb;kmμviFITaMg Gs;edIm,ITb; screen output . + Function getche( ) nig getch( ) dUcKñaRtg; Read mYy character BI keyboard edaymincaM Enter, EtxusKñaRtg; Function getche ( )nigsMEdg character Edl Read BI keyboard mkelI screen

.

+ Function getche( ) stdin

eT .

nig getche( ) : Read Rtg;BI keyboard Etmþg/ edayminqøgkat;

edIm,I Read password, sUmBinitü Example xageRkamEdlmaneRbI Function get pass( ): Ex: Read password : # include < stdio.h> # include <conio.h> int main (void) { char *password; clrscr (); password = getpass (“Enter password :”); cprintf (“ password is : %s \r\n”, password); getch( ); return (0); } Readln(st) Pascal ) * gets ( ) Function : (

smmUl

kñúg

sMrab; Read string BI keyboard rhUtdl; Enter eTIbQb; . bnÞab;BI Read rYcvaEfm sBaØa bBa©b; bnÞat; (\0) enAxagcug String . gets ( )

Ex: # include < stdio.h> # include < conio.h> main ( ) { clrscr (): char name[80]; printf (“\n Enter your name :”); gets (name); printf (“\n your name is %s”, name ); getch ( ); return (0); }

59


Note : Programmer

ebIcg;P¢ab;kargarkñúgkar

Readnumber

dMNak; kar xageRkam ³ + Read cMnYnKt; ³ bnÞab;BI Enter;

nig

String

Kb,IeRbIR)as;

gets (str ); i = atoi (str); /* a to int */ + Read

cMnYnBit ³ bnÞab;BIcuc

Enter;

j = atof (str); /* a to float */

kñúg language : + getchar ( )and putchar( ): (macro) Read nig write character mYyBI keyboard . + gets ( ) and puts ( ): Read nig write string ( KitTaMgsBaØa space ) . + scanf ( ): Read tam format Code ( %), tam Conversion code (d) + printf ( ): write Data tam format code, Conversion code . + getche() nig getch( ): Read character Rtg;BI Keyboard edaymincaM Enter, minqøgkat; stdin . Total Function input / output Data

* gotoxy (int x, int y );

naM Cursor eTATItaMg x, y én screen output . x = 1 dl; 80 , y = 1 dl; 25 . Ex:

# include < stdio.h> # include <conio.h> int main (void) { clrsrcr( ) gotoxy (35,12); cprintf (“Hello world !!!”); getch ( ); return (0); } clrscr ( ); (clear screen )

Ca Function sMrab;lubsMGat screen output, bnÞab;mkvanaM Cursor eTATItaMg (1, 1) . clreol ( ): (Clear end of line )

Ca Function lubbNþa character BIcMnuc Current cursor eTAcugbnÞat; . textcolor (n) nig textbackground (n): dak;BN¾ text nigBN¾ Background 60


tarag Colors : Const

Value 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 128

BLACK BLUE GREEN CYAN RED MAGENTA BROWN LIGHT GRAY DARKGRAY LIGHT BLUE LIGHT GREEN LIGHT CYAN LIGHT RED LIGHT MAGENTA YELLOW WHITE BLINK

cMeBaH textbackground tMél n KitBI ( 0Æ 8 ) . Function lowvideo ( ) nig normvideo( ) : bBa¢aBnøW Text, lowvideo( ) eFVIeGayBnøWrbs; text exSay²eTA normvideo( ) eFVIeGayBnøWrbs; text FmμtaeLIgvij . Ex: sresrBakü WELCOME ( enaHGkSr W E manBnøW PøW ) Note:

# include < stdio.h> # include <conio.h> main( ); { clrscr () ; highvideo( ); cprintf (“W”); lowvideo( ); cprintf (“ ELCOM”); highvideo( ); cprintf (“E”); getch( ); return(0); } wherex Ex:

nig

wherey :

bBa¢ak;R)ab; kUGredaenrbs; Current cursor .

# include < stdio.h> # include <conio.h>

61


int main (void) { clrscr ( ); gotoxy (10,10); printf (“coordinate x :%d y :%d \n”, wherex, wherey); getch( ); return(0); } window (int x1, int y1, int x2, int y2 );

Ca Function begáIt text window EdlmankUGredaen mMuEkgxagelIEpñkxageqVg CacMnuc (x1,y1) nig mMuEkgxageRkamEpñkxagsþaM (x2, y2) ehIy statement print data on screen nig print data mkelI Text window EdleTIbnwgbegáItenH . Ex:

# include <stdio.h> # include <conio.h> int main(void) { window (10, 10, 40, 11); textcolor (YELLOW); textbackground (BLUE); clrscr (); cprintf(“This is a test \n”); getch( ); return(0); }

=====*=====

62


lMhat;Gnuvtþn_ 1.

cUrsresr Screen output én program xageRkam ³ # include <stdio.h> # include <conio.h> main ( ) { int n = 345; int p = 5; float x = 34.5678 ; printf (“A :%d %f \n”, n,x ); printf (“B :%4d %10f \n”, n,x ); printf (“C :%2d %3f \n”, n,x ); printf (“D :%10.3f %10.3ef \n”, x,x ); printf (“E :%*d \n”, p,n ); printf (“F :%*.*f \n”, 12,5,x ); getch ( ); return (0); }

2.

cUrR)ab;BItMélén Variable RbePT int KW n nig p eRkayBIGnuvtþn¾ Statement xageRkam³ scanf (“%4d %2d”, &n, &p ); CamYyTinñn½yEdlvaybBa©ÚldUcxageRkam ³ ¬sBaØa ^ CasBaØadkXøa ¦ a) b) c) d) e)

3.

12^ 45 123456 123456^7 1^458 ^^^4567^^891

cUrsresr screen output énkmμviFIxageRkam ³ # include <stdio.h> # include <conio.h> main ( ) { char firstname [ ]= “ Chan ” ; char lastname [ ] = “ Dara” ; float money = 5000 ; clrscr ( ) ; gotoxy(10,10); printf (“ %s %s, you will get $ %6.2f \n ”, firstname, lastname, money) ; getch ( ) ;

63


return (0 ) ; } 4.

cUrsresr screen output énkmμviFIxageRkam ³ # include <stdio.h> # include <conio.h> main ( ) { int letter ; /* print Value of code ASCII of characters */ letter = ‘e’ ; printf (“Code ASCII of %c is %d \n” , letter , letter ); getch ( ); return (0); }

ehtuGVIeKniymeRbI Function gets ( ) Cag Function scanf ( )? 6. cUrrkkEnøgxusén kmμviFIxageRkam ³

5.

main ( ) { char str [ ] = {‘H’ , ‘e’ , ‘l’ , ‘l’ , ‘o’ }; puts (str); getch ( ); return (0); }

64


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.