You are on page 1of 7
Chapter:3 Input / Output Statements & Bu functions 3. 1 Formatted I/O statements: 1, Formatted Output Statement (printf): = printf()'s used to display the output on monitor screen, = _Itallows progremmerto display the formatted output. - Htisapart of library file ‘stdio. > Syntax: printf("Control String”, variablet, variable2,..); = Control String includes following types of items: © Characters which we want to display on screen, © Format Specification of variables Type of data Format Single Character %e String % integer ea Foat 6, He, he Short int ‘eh Unsigned int. %u Long int id Lang double [iF © Escape Sequence: Description Escapee sequence Next line \n Horizontal Tab it Vertical Tab Ww Backspace 18 Backslash W Single quate V Double quate V Question mark v = Output of integer numbers: © Syntax: Sued (specifies minimum width) of Statement (ifa=123) ‘Output printf”, a); 7 Pp print "965d", a; 1 2 [3 print("962d", 2); 1 [2 473 print("36-6d", a); 1 Ms. Hiral A. Patel Page 1 Chapter:3 Input / Output Statements & Bu functions rinth(" 605d", a); o fo [i [2 [3 = Output of Real numbers: © Syntax Sow.pf —_(wspecifies total width, p specifies digits after decimal point.) ‘Output Print (56, a 2 [3 |- |é |7 |@ Jo jo fo printt("%67.2F%, a; 2 [3 [. [6 [8 printt("%68.4", a); 2 [3 @ [7 [8 (0 Printt("%8.4P", a); 2 13 é |? ]@ Jo printf("%608.44", a); o [2 |3 «7 |s 0 ~ Output of String: © Syntax: Yowsps —_ (wpetifes total width, p specifies numbers of characters from left side) Output rintt("%s" a; [e-em Pu te Te peint"96106" a); [ © [0 [mM elu [t Je [Re printf("%10.55",a); c [o [w |p fu printi("%105s",a); [¢ [0 [M [P [u peintt"%65s"2); [e_To Im [plu 17 le [R® 2. Formatted Input Statement (scanf): - _ scanf() is used to read character, string, and numbers from keyboard. = Wis apart of library file ‘stdio.n’. = syntax: scanf(“Control String” & variable1,& variable2,...); ~ Control String includes format specifier for given variables. {The total numbers of specifier and variables must be equal.) - Toenter numeric values: © Syntax = Integer: %wd_ (w specifies total numbers of digits to accept). + Float: %f * Double: sf o "Statements Input Result scanf{“%a” Ba); 123 seanf("%20", Ba); 123 scanf{%5d", fa} 323 23 Page 2 Chapter:3 Input / Output Statements & Bu functions scanf{’%6f" Ba); 12.45 | 912.45 = Tocenter character string: © Syntax = Single character: String: (With st L.%we: System will wait till w character (Fix length) 2. %ws:It terminates when user press “enter’, It also terminates reading when “Blank” is found. (Means only first word of strings stored in variable). 3. Ylcharacters] : Only specified characters are allowed asinput. 4, SélAcharacters: Mentioned characters are not allovied as input. o Bk Statements Result Remark scant\"%c",&a); ey Only single character scanf\"%6c" a); a="Hello” (need to enter one character) scanf("%15¢" a), ‘a="Hello World” | (Because of fixed length) scanfi"%155",a); a="Hello” After “Hello” theres a blank so only first wordis there with variable. scani("%la2l’,a);_| “Hello World” | a="Hello” Space is net allowed. scanfi"%[*a2]" a), | “123Hello” | a="123" [2-2] are not allowed. scanfi"%6[*H)",a); | *abcl24deP” | a="abcl2” isnot allowed. = Reading mixed data type values: © Multiple values ean be entered with one scanf{) by separating using “Space” © Be: scant "¥éd 96F %c %s", Ba, Bb, Be, dl; = Input: 12 135 y hello "Output: 912, b=13.5, c='y, d="hello” Problem Definition: ‘Write a program to enter rollno, name and percentage. Display values on screen.” Hincludecstdio.h> Hinclude void main() { int mo; float per; char, name[15]; reer); M1 Take input printf(“\n Enter roll number:"}; scanf("%ed”, Ben]; printf(“\n Enter percentage:”); scant(“%f", &2er) printf("\n Enter your name”); scanf("%s", name); 1/ 8 sign is not mentioned. ‘Ms. Hiral A. Patel Page 3 Chapter:3 Input / Output Statements & Bu functions 111. Display resuk— printf("\n Roll no: %d", rno); printf("\n Name: %s", name); print{{"\n Percentage: %f", per); 11 2. Display formatted output print{(“\n Rolino: 618d" no}; printf("\n Name: %203", name}; print{(“\n Percentage: 14.2", per); 113. Dispiay resuk with one printf printi(“\n Rolino: %d \n Name: %s \n Percentage: %f \n",rno, name, per); getch(); 3.2 Unformatted I/0 statements: Following functions are used for input and output. 1, getchar() and putchar(): ~_getchar) is used to read single character from standard input ~ Putchar()is used to display a single character on standard output = They are partof library file ‘stdout. > Syntaxs variable_name = getchar(); putchar(variable_name); - char choice; choice = getchari); (It stores a single character to ‘choice! variable which ig entered by the user.) putchar(choice); (It displays the value of ‘choice’ variable, 2. gets() and puts() = gets() is used to read a string fromstandard input and append ‘\0’ at the end of the string. ~ puts|) is used to display a string on standard output. = They are partof lisrary file ‘stdio.hY. = Syntax aets(variable_name}; puts(variable_name); - & char name[15}; gets{name); (It stores a string to ‘name’ variable and add \0' at the end.) puts(name); _(Itdisplays value of ‘name’ variable on screen.) 3. getch() and putch() Ms. Hiral A. Patel Page 4 Chapter:3 Input / Output Statements & Bu functions getch() is used to read a single character from standard input. The inputted character is not di putch() is used to display a single character on standard output. They are part of library file ‘conio.h’. Syntax: Bx variable_name=geteh(); putch{variable_name); char ch; chegetch(); (It stores a single characterin ‘ch’ variable.) pputch(ch); (It displays single character on screen.) 4, getche() layed on ‘getche() is used to reed a ingle character from standard input. The inputted characteris displayed on Iisa part of library file ‘conio.h’. Syntax: & variable_name = getche(); chegetche(); 3.3 Mathematical Functions: Mathematical functions are part ofthe library file ‘math’. Sr. | Syntax of Explanation Example | Output No | function T | abs(no}; Teretumns the absolute value of given number. abs), | 2 abs(i2y, | 2 2 [eeilinoy, ie retums the smallest integer number which isgreater | cel(2.3); | 3 than the given number. cei23); | 2 3 | floortnoy, Teretums the largest integer number which is lessthan | floor(23), | 2 given number. floor(-2.3); | 3 4 | cos(no}; Ie returns the cosine value of ‘ne’ cox(30); | 0.1542 5 | sinfoo}; Te returns the sine value of Ao sin(@0; | 0.988032 ® | tanjno); Te returns the tangent value of no" tan(a0;, | 6.405331 7 | explo), Tr returns the result of e™, exp(1Q}; | 2.178282 ® | logtno}, Teretums the result of natural log. loga). (ne > 0) Tog(io;, [2 3 | TogiO(no); | It retums the result of log base 10. (log) (no >0) Togio(io}; | 2 70 | powno, Teretums the result of no?™ pow2a), | 36 power); 11 | sart(no}; Te returns the square root of given number. (na>0) | sqrt(dy «| 2 Ms, Hiral A. Patel Page 5 Chapter:3 Input / Output Statements & Bu functions 3.4 String Functions: =] Syntax of function Explanation Example ‘Output ‘ a-"Hello”, b="bea” 1 | strepy(strings, string2); | It copies string? into string. strepylab); bea * | streat{stringt, string2), | It appends string2 in stringl. streat{a,b); Hellobea *Y stremp(stringt, string2), | It compares two strings character by strempib,a), 32 character. strempl"Ram", "Raj’) | 3 lf match > Returns 0 IFnot match > Returns difference between the fist non-matching character. =| strien(string); Iereturns the length of given string. strlenta); 5 >| strivw( string) Te-converts string into lower case. strlwe(a; hello © strupristring): econverts string into upper case. struprla); HELLO 7 | strrev(string): Itreverses the given string, strev(al collet! | stnepy(strt,str2,n); | It copies n numbers of characters from left | strnepy(a, “CPPM™2}; | CP hand side of str2 into str We have to add ‘\0'at the end, * | stinemp(strd, st, n), | It compares n numbers of characters from | stenemp("than’y"that” | 0 left hand side of str1 and str2. 3 lf match-> Returns 0 IFnot match > Returns difference between the first non-matching character. B | streat(strd, str2;n); | Itappends first n characters of str2 in stri. | strneatia,b2); Hellobe strstr ste st2), It finds stein str. strste(a"el"); ello I match-> Returns string from that strste(a"m"} NULL position. Ifnot match-> Returns NULL © | strebr(ste, char); Itfinds first occurrence of charin strd. | strehriae’} ello lf match-> Returns string from that strchela,'"; lo position. pf] If not match-> Returns NULL —_ | strrehr(str, char); Itfinds ast occurrence of charinstel. | strrchr(a, 7); lo lf match-> Returns string from that position, Ifnot match->Returns NULL ¥ | stmset(strd, charin); | Itset ntimes a given character into str | strnsetia,e.3); eeelo from left hand side. strnsetia,”f 10} rere 3.5 Conversion Functions: ‘These functions are part of library file “ctype.h”. Sr. | Syntax of Explanation Example Output No | function 1 | isalnum(char); | Itis used to check letter or number. isalnum('S), Non-zere Ifmateh-> nonzero _not match-> 0 isalnum(2’) Non-zere Z| isalpha(char); | Itis used to check the letter. ifalphat’s), Non-zere Ifmateh-> nonzero net match-> 0 isalpha(’2’) ° 3. [isasci(char), | Itis used to check the ASCII character ccde isascil‘a' Non-zere Ms. Hiral A. Patel Page 6 Chapter:3 Input / Output Statements & Built in functions Between Oto 0177, lfmatch-> nonzero _not match-> 0 [islower(char), | tis used to check the lower case character. ilowert), Nowzere lmatch-> non zero _not match-> 0 islower() ° 5 | isupper{char); | Itis used to check the uppercase character. isupper( M1); Non-1er0 lfmatch-> non zero__not match-> 0 isupper(im’) ° 6 (char, | Wis used to check number. isaigit('), Noneere lfmatch-> non zero _ not match-> 0 isa"); ° 7 [isspacelchar); | Itis used to check one ofthe following space. | isspece(‘n); Non-zero (space, new line \r, horizontal tab “t'vertical | isspace(’‘); Non-zero tab “\v, form feed 4P, carriage retum “\). isspece(W); Non-zero Ifmatch-> nonzero not match-> 0 isspece(’); 0 Problem Definition: ‘Write a program to check whether pressed character is alphabet, digit, space or special character.” Finelude- Finclude Hincludecctypeh> void main() { charch; rser); printf("\nEnter one character”); chegetchar(); if isalpha{ch]>0) print{("\n Pressed characteris alphabet.”; elseif (sdigit(ch)>0) printi("\n Pressed characteris digit”, elseif (sspace(ch)20) printf("\n Pressed characteris space else printf("\n Pressed character isa special character.”; getch(); Ms. Hiral A. Patel Page 7

You might also like