c - After first printf it does nothing (successful compile) -


When I run this code, it does not even reach the loop. It comes lengthening at scanf . Any ideas why?

  contains # lt; Stdio.h & gt; Int main (int argc, char * argv []) {int hex_num; Int Mask = 9; Int test_int = 0; Int i = 0; Int res_mask; Printf ("Enter input as hexdynamic number"); Scanf ("% x", & amp; hex_num); Res_mask = Mask & amp; Hex_num; While ((res_mask! = Mask) || i <32) {hex_num = hex_num> & Gt; 1; Res_mask = Mask & amp; Hex_num; I ++; } If (res_mask == mask) {test_int = 1; } (Test_int = 1)? (Printf ("yes")): (printf ("no")); Return 0; }   

While you are reaching the loop, which will not end because I & lt; 32 condition continues to hex_num == 0, which makes res_mask == 0, which is! = Mask.

You have to fix the logic of your time loop (I help with that, but I do not know what you are trying to do) and you want to flush the output The printfile needs to be finished with a Newline or CallFluff (standout) (Other possibilities are to print instead of stdout, because pre is unbroken, or to call setbuff or setweb.)

Farewell Does test_int set to 1 (test_int = 1) and is always right; Better

  printf ("% s \ n", test_int? "Yes": "no");  

Update: If the number entered is 1001 and "No" if it does not, then a program "Yes" prints here:

  #include & lt; Stdio.h & gt; Int main (int argc, char ** argv) {int mask = 9; Unsigned int hex_num; For (;;) {fprintf (stderr, "Enter input as a hexadecimal number:"); Int nscanned = scanf ("% x", & amp; hex_num); If breaking (nscanned == 1); Else if (nscanned == EOF) 0; } While (hex_num & amp; (~ hex_num and mask)) hex_num & gt; & Gt; = 1; Printf ("% s \ n", hex_num? "Yes": "no"); Return 0; }  

And another way to do this is:

 for  (;; hex_num> gt;> = 1) if (hex_num & Lt; mask) {printf ("no"); Return 0; } And if ((~ hex_num and mask) == 0) {printf ("yes"); Return 0; }  

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

java - Algorithm negotiation fail SSH in Jenkins -

java - Messages from .properties file do not display UTF-8 characters -