By : Nanda Kishor K N Mail ID : nandakishorkn@rediffmail.com _________________________________________________________________________________________________ C For Swimmers ______________ If u don't know how to swim, learn it now. Otherwise participate in the competition. _________________________________________________________________________________________________ Topic : C Fundamentals Data Types, Constants & Variables (First 5 queries, Ref: PROGRAMMING WITH C - Schaum's Outline Series) Playing with scanf function Operators & Expressions _____ _____________________________________ ************************************************************************************************* * NOTE : All the programs are tested under Turbo C/C++ compilers. * * It is assumed that, * * *=> Programs run under DOS environment, * * *=> The underlying machine is an x86 system, * * *=> Necessary header files are included. * * *=> Program is compiled using Turbo C/C++ compiler. * * The program output may depend on the information based on this assumptions. * * (For example sizeof(int) = 2 bytes may be assumed). * ************************************************************************************************* [Q001]. Determine which of the following are VALID identifiers. If invalid, state the reason. (a) sample1 (b) 5sample (c) data_7 (d) return (e) #fine (f) variable (g) 91-080-100 (h) name & age (i) _val (j) name_and_age Ans. (a) VALID (b) Invalid, since an identifier must begin with a letter or an underscore (c) VALID (d) Invalid, since return is a reserved word (e) Invalid, since an identifier must begin with a letter or an underscore (f) VALID (g) Invalid, since an identifier must begin with a letter or an underscore (h) Invalid, since blank spaces are not allowed (i) VALID (j) VALID _________________________________________________________________________________________________ [Q002]. Determine which of the following are VALID character constants. If invalid, state the reason. (a) 'y' (b) '\r' (c) 'Y' (d) '@' (e) '/r' (f) 'word' (g) '\0' (h) '\?' (i) '\065' (j) '\'' (k) ' ' Ans. (a) VALID (b) VALID (c) VALID (d) VALID (e) Invalid, since escape sequences must be written with a backward slash (i.e. \) (f) Invalid, since a character constant cannot consist of multiple characters (g) VALID (null-character escape sequence) (h) VALID (i) VALID (Octal escape sequence) (j) VALID (k) VALID _________________________________________________________________________________________________ [Q003]. Determine which of the following are VALID string constants. If invalid, state the reason. (a) 'Hi Friends' (b) "abc,def,ghi" (c) "Qualification (d) "4325.76e-8" (e) "Don\'t sleep" (f) "He said, "You\'re great" (g) "" (h) " " (i) "Rs.100/-" Ans. (a) Invalid, since a string constant must be enclosed in double quotation marks (b) VALID (c) Invalid, since trailing quotation mark is missing (d) VALID (e) VALID (single-quote escape sequence) (f) Invalid, since the quotation marks and (optionally) apostrophe within the string cannot be expressed without the escape sequences. (g) VALID (h) VALID (i) VALID _________________________________________________________________________________________________ [Q004]. Determine which of the following numerical values are valid constants. If a constant is valid, specify whether it is integer or real. Also, specify the base for each valid integer constant. (a) 10,500 (b) 080 (c) 0.007 (d) 5.6e7 (e) 5.6e-7 (f) 0.2e-0.3 (g) 0.2e 0.3 (h) 0xaf9s82 (i) 0XABCDEFL (j) 0369CF (k) 87654321l (l) 87654321 Ans. (a) Invalid, since illegal character(,) (b) VALID (c) VALID (d) VALID (e) VALID (f) VALID (g) Invalid, since illegal character(blank space) (h) Invalid, since illegal character(s) (i) VALID (j) Invalid, since illegal characters (9, C, F), if intended as an octal constant. (k) VALID (l) VALID _________________________________________________________________________________________________ [Q005]. Determine which of the following floating-point constants are VALID for the quantity (5 * 100000). (a) 500000 (b) 0.5e6 (c) 5E5 (d) 5e5 (e) 5e+5 (f) 500E3 (g) .5E6 (h) 50e4 (i) 50.E+4 (j) 5.0E+5 (k) All of the above (l) None of these Ans. (k) _________________________________________________________________________________________________ [Q006]. What will be the output of the following program : void main() { printf("%f",123.); } (a)123 (b)Compile-Time Error (c)123.00 (d)123.000000 Ans. (d) _________________________________________________________________________________________________ [Q007]. What will be the output of the following program : void main() { printf("%d",sizeof(integer)); } (a)2 (b)Compile-Time Error (c)4 (d)None of these Ans. (b) since there is no such data type called 'integer'. _________________________________________________________________________________________________ [Q008]. What will be the output of the following program : void main() { char str[]="C For Swimmers"; printf("%d",sizeof str); } (a)14 (b)Compile-Time Error (c)15 (d)None of these Ans. (a) _________________________________________________________________________________________________ [Q009]. What will be the output of the following program : void main() { char str[]="C For Swimmers"; printf("%d",++(sizeof(str))); } (a)14 (b)Compile-Time Error (c)15 (d)None of these Ans. (b) _________________________________________________________________________________________________ [Q010]. What will be the output of the following program : void main() { char str[]="C For Swimmers"; printf("%d",-sizeof(str)); } (a)14 (b)Compile-Time Error (c)-15 (d)-14 Ans. (d) _________________________________________________________________________________________________ [Q011]. What will be the output of the following program : void main() { printf("%d",!(100==100)+1); } (a)100 (b)0 (c)1 (d)2 Ans. (c) _________________________________________________________________________________________________ [Q012]. What will be the output of the following program : void main() { int x=5,y=6,z=2; z/=y/z==3?y/z:x*y; printf("%d",z); } (a)Compile-Time Error (b)2 (c)0 (d)1 Ans. (c) _________________________________________________________________________________________________ [Q013]. What will be the output of the following program : void main() { printf("%d %d %d",5,!5,25-!25); } (a)5 10 22 (b)5 5 25 (c)5 0 25 (d)5 1 24 Ans. (c) _________________________________________________________________________________________________ [Q014]. What will be the output of the following program : int main() { int a=500,b=100,c=30,d=40,e=19; a+=b-=c*=d/=e%=5; printf("%d %d %d %d %d",a,b,c,d,e); } (a)500 100 30 40 4 (b)Run-Time Error (c)700 200 300 10 4 (d)300 -200 300 10 4 Ans. (d) _________________________________________________________________________________________________ [Q015]. What will be the output of the following program : void main() { int a=500,b=100,c=30,d=40,e=19; if ((((a > b) ? c : d) >= e) && !((e <= d) ? ((a / 5) == b) : (c == d))) printf("Success"); else printf("Failure"); } (a)VALID : Success (b)VALID : Failure (c)INVALID (d)None of these Ans. (b) _________________________________________________________________________________________________ [Q016]. What will be the output of the following program : void main() { int a=1,b=2,c=3,d=4; printf("%d",!a?b?!c:!d:a); } (a)1 (b)2 (c)3 (d)4 Ans. (a) _________________________________________________________________________________________________ [Q017]. What will be the output of the following program : void main() { int i=12345,j=-13579,k=-24680; long ix=123456789; short sx=-2222; unsigned ux=5555; printf("\n%d %d %d %ld %d %u",i,j,k,ix,sx,ux); printf("\n\n%3d %3d %3d\n%3ld %3d %3u",i,j,k,ix,sx,ux); printf("\n\n%8d %8d %8d\n%15ld %8d %8u",i,j,k,ix,sx,ux); printf("\n\n%-8d %-8d\n%-8d %-15ld\n%-8d %-8u",i,j,k,ix,sx,ux); printf("\n\n%+8d %+8d\n%+8d %+15ld\n%+8d %8u",i,j,k,ix,sx,ux); printf("\n\n%08d %08d\n%08d %015ld\n%08d %08u",i,j,k,ix,sx,ux); } Ans. 12345 -13579 -24680 123456789 -2222 5555 12345 -13579 -24680 123456789 -2222 5555 12345 -13579 -24680 123456789 -2222 5555 12345 -13579 -24680 123456789 -2222 5555 +12345 -13579 -24680 +123456789 -2222 5555 00012345 -0013579 -0024680 000000123456789 -0002222 00005555 _________________________________________________________________________________________________ [Q018]. What will be the output of the following program : void main() { int i=12345,j=0xabcd9,k=077777; printf("%d %x %o",i,j,k); printf("\n%3d %3x %3o",i,j,k); printf("\n%8d %8x %8o"i,j,k); printf("\n%-8d %-8x %-8o",i,j,k); printf("\n%+8d %+8x %+8o",i,j,k); printf("\n%08d %#8x %#8o",i,j,k); } Ans. 12345 abcd9 77777 12345 abcd9 77777 12345 abcd9 77777 12345 abcd9 77777 +12345 abcd9 77777 00012345 0xabcd9 077777 _________________________________________________________________________________________________ [Q019]. What will be the output of the following program : void main() { char c1='A', c2='B', c3='C'; printf("%c %c %c",c1,c2,c3); printf("\n%c%c%c",c1,c2,c3); printf("\n%3c %3c %3c",c1,c2,c3); printf("\n%3c%3c%3c",c1,c2,c3); printf("\nc1=%c c2=%c c3=%c",c1,c2,c3); } Ans. A B C ABC A B C A B C c1=A c2=B c3=C _________________________________________________________________________________________________ [Q020]. What will be the output of the following program : void main() { float a=2.5, b=0.0005, c=3000.; printf("%f %f %f",a,b,c); printf("\n%3f %3f %3f",a,b,c); printf("\n%8f %8f %8f",a,b,c); printf("\n%8.4f %8.4f %8.4f",a,b,c); printf("\n%8.3f %8.3f %8.3f",a,b,c); printf("\n%e %e %e",a,b,c); printf("\n%3e %3e %3e",a,b,c); printf("\n%12e %12e %12e",a,b,c); printf("\n%8.2e %8.2e %8.2e",a,b,c); printf("\n%-8f %-8f %-8f",a,b,c); printf("\n%+8f %+8f %+8f",a,b,c); printf("\n%08f %08f %08f",a,b,c); printf("\n%#8f %#8f %#8f",a,b,c); printf("\n%g %g %g",a,b,c); printf("\n%#g %#g %#g"a,b,c); } Ans. 2.500000 0.000500 3000.000000 2.500000 0.000500 3000.000000 2.500000 0.000500 3000.000000 2.5000 0.0005 3000.0000 2.500 0.001 3000.000 2.500000e+000 5.000000e-004 3.000000e+003 2.500000e+000 5.000000e-004 3.000000e+003 2.500000e+000 5.000000e-004 3.000000e+003 2.5000e+000 5.0000e-004 3.0000e+003 2.50e+000 5.00e-004 3.00e+003 2.500000 0.000500 3000.000000 +2.500000 +0.000500 +3000.000000 2.500000 0.000500 3000.000000 2.500000 0.000500 3000.000000 2.5 0.0005 3000 2.500000 0.000500 3000.000000 _________________________________________________________________________________________________ [Q021]. What will be the output of the following program : void main() { char str[]="C For Swimmers"; printf("%s",str); printf("\n%.5s",str); printf("\n%8.*s",5,str); printf("\n%-10s %.1s",str+6,str); } Ans. C For Swimmers C For C For Swimmers C _________________________________________________________________________________________________ [Q022]. What will be the output of the following program : void main() { int a=1,b=2,c=3; scanf("%d %*d %d",&a,&b,&c); printf("a=%d b=%d c=%d",a,b,c); } [NOTE : 3 values entered by the user are:100 200 300] (a)1 2 3 (b)100 200 300 (c)100 200 3 (d)100 300 3 Ans. (d) _________________________________________________________________________________________________ [Q023]. What will be the output of the following program : void main() { char line[80]; // Max. length=80 Chars scanf("%[^,]s",line); printf("\n%s",line); } [NOTE : THE USER INPUT IS:Dear Friends, What is the output?] (a)Compile-Time Error (b)Dear Friends (c)What is the output? (d)None of these Ans. (b) _________________________________________________________________________________________________ [Q024]. What will be the output of the following program : void main() { char a,b,c; scanf("%c%c%c",&a,&b,&c); printf("a=%c b=%c c=%c",a,b,c); } [NOTE : THE USER INPUT IS :A B C] (a)a=A b=B c=C (b)a=A b= c=B (c)a=A b= c=C (d)None of these Ans. (b) _________________________________________________________________________________________________ [Q025]. What will be the output of the following program : void main() { int i=1; float f=2.25; scanf("%d a %f",&i,&f); printf("%d %.2f",i,f); } [NOTE : THE USER INPUT IS:5 5.75] (a)1 2.25 (b)5 5.75 (c)5 2.25 (d)None of these Ans. (c) _________________________________________________________________________________________________ [Q026]. What will be the output of the following program : void main() { char a,b,c; scanf("%c %c %c",&a,&b,&c); printf("a=%c b=%c c=%c",a,b,c); } [NOTE : THE USER INPUT IS :ABC DEF GHI] (a)a=ABC b=DEF c=GHI (b)a=A b=B c=C (c)a=A b=D c=G (d)None of these Ans. (b) _________________________________________________________________________________________________ [Q027]. What will be the output of the following program : void main() { char a[80],b[80],c[80]; scanf("%1s %5s %3s",a,b,c); printf("%s %s %s",a,b,c); } [NOTE : THE USER INPUT IS:CMeansSea Ocean Vast] (a)C O V (b)C Means Sea (c)C Ocean Vas (d)None of these Ans. (b) _________________________________________________________________________________________________ [Q028]. What will be the output of the following program : void main() { int a,b,c; scanf("%1d %2d %3d",&a,&b,&c); printf("Sum=%d",a+b+c); } [NOTE : THE USER INPUT IS :123456 44 544] (a)Sum=480 (b)Sum=594 (c)Sum=589 (d)None of these Ans. (a) _________________________________________________________________________________________________ [Q029]. What happens when the following program is executed : void main() { char line[80]; scanf("%[^1234567890\n]",line); } (a)Accepts the string that contains DIGITS only. (b)Accepts the string that contains DIGITS and NEWLINE characters. (c)Accepts the string that contains anything other than the DIGITS and NEWLINE characters. (d)None of these Ans. (c) _________________________________________________________________________________________________ [Q030]. What happens when the following program is executed : void main() { char line[80]; scanf("%[^*]",line); } (a)Accepts the string that contains DIGITS & ALPHABETS only. (b)Accepts the string that contains * or asterisk characters only. (c)Accepts the string that contains anything other than the * or asterisk character. (d)None of these Ans. (c) _________________________________________________________________________________________________ Thanx for using "C For Swimmers". Regarding this material, you can send Bug Reports, Suggestions, Comments,etc. to nandakishorkn@rediffmail.com