@Nicks *Problem 1* %d is used to take a signed integer as input. To take a short integer as input, use %hi. That way, you would get the correct answer as 2.
*Problem 2:* a:1 means that variable a is of width *1 bit* Similarly, b:2 means that b is of width *2 bits* b = 6 sets the two bits as 10, (last two bits of 110 considered), which is equal to -2 a = 2 sets the only bit as 0, (last bit of 10 considered), which is nothing but zero. Bit-fields like these however tend to be implementation-dependent and in the interest of portability should be avoided. On Thu, Jul 14, 2011 at 12:16 PM, sameer.mut...@gmail.com < sameer.mut...@gmail.com> wrote: > 1st problem > > format specifier is %d whereas memory is allocated only for short int > memory allocated-2bytes. but being stored in 4bytes. > so a is overwritten by b. > In dis case where u give a=1, b=1, a becomes become 0. > n so it gives:0+1=1 > in d scanf if u use %hd instead u ll get d right output!! > > > > On Thu, Jul 14, 2011 at 8:01 AM, nicks <crazy.logic.k...@gmail.com> wrote: > >> Hey Guys, plz help me in getting these 2 C output problems >> >> *PROBLEM 1>.* >> * >> * >> *#*include<stdio.h> >> int main() >> { >> short int a,b,c; >> scanf("%d%d",&a,&b); >> c=a+b; >> printf("%d",c); >> return 0; >> } >> INPUT- >> 1 1 >> >> OUTPUT >> 1 >> >> i am not getting why 1 is coming in the output.....what difference is >> using short making in the code ??? >> >> >> *PROBLEM 2>.* >> * >> * >> * >> * >> #include<stdio.h> >> main() >> { >> struct >> { >> int a:1; >> int b:2; >> }t; >> t.b=6; >> t.a=2; >> printf("%d %d",t.a,t.b); >> } >> >> OUTPUT >> 0 -2 >> >> What does the statement a:1 and b:1 mean and what are they doing.....i am >> seeing them first time ever...hence not able to get the output....if someone >> has any idea plz help !! >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algogeeks@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- Gaurav Jain Associate Software Engineer VxVM Escalations Symantec Software India Pvt. Ltd. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.