Hi Praveen, I suggest you join "Progav" at
http://groups.google.com/group/progav?lnk=li However, please read till bottom --- praveen kumar <[EMAIL PROTECTED]> wrote: > why the output of this program is 256 > > main() > { > unsigned char i=0x80; > printf("\n%d",i<,1); > } This program didn't compile. It is the problem with the printf statement. My compiler tells me that there is a syntax error befor ','. On removeing the ',', I got the output 0 and then, removing the '<' gave me the output as 128. Well, that means 0x80 is 128 in decimal form. In your printf statement you are printing the value of i in integer form, which is 128. > > > plz anyone explain the logic of the following > programs This program is not as difficult as it looks. In simple terms, you are creating an array buff with selected elemets from the string '01..." and returning the string to main(). You pass 2 numbers 128 and 2 to your funcion fun() received by formal parameters num and base respectively. You use a pointer ptr to handle the string buff. The next steps in the function enter data into the string, but in your case, it is in the reverse order. Look in your program now. > main() > { > char *s; > s=fun(128,2); The returned address is received by the pointer s. > printf("\n%s",s); This prints the data pointed by s in string mode, i.e. it keeps on printing from the address pointed by s in consecutive locations the forward direction till '\0' os encountered. > } > fun(unsigned int num,intbase) > { > static char buff[33]; > char *ptr; > ptr=&buff[sizeof(buff)-1]; Here, ptr is made to point to buff[32], which is the last element in the array(string in this case). > *ptr='\0'; Here, you are setting the last element of the string as '\0'. which is conventional as all strings must end by '\0'. > do > { > *--ptr="0123456789abcdef"[num%base]; > num/=base; > }while(num!=0); *--ptr means that ptr is pre-incremented, i.e. the address pointed by ptr now shifts backwards by a word, i.e. ptr now points to the previous element in your string. num % base in all passes except last = 0. So, 0th element of the string "0123..." is taken in all the passes except the last one and assigned to the locations pointed by ptr. In the last pass, num%base would be 1%2 = 1, so 1th elemet i.e. 1 is assigned to the first element in your string. Thus, you get the output as 1000000. > return ptr; ptr now points to the string i.e. the first element of the string and this address is retuened to main. > } > Hope you got the idea. One more thing. Do you follow C99?? If not, the function declarion must be included at top or else the program won't be compiled. > This is very easy program. Do you know that '<<' is the left shift operator and '>>' is the right shift operator. > main() > { > unsigned int num; > int i; > scanf("%u",&num); > for(i=0;i<16;i++) > printf("%d",(num<<i & 1<<15)?1:0); > } Here, num is being leftshifed sequentially by 0,1,2,3...16. At the same time, in the ternary construct, it is anded bitwise i.e. each bit is considered seperately, with 1 left shifted 15 times. In all passes where num <<i and 1 << 15 are equal, then we get the output as 1 and in other cases, we get 0. Due to time constraints, I have to leave you here. So, if you don't know about the bitwise operators in C, please refer your text books or simply google it up or the best way is to join "Progav" like I told you before and ask the people there. If nobody is replying to your post, atleast I can as I am there. Why I leave you here is because, to tell you about these low-level operator, takes quite sometime and I don't have that right now. If you join and post this in progav, I can atleast remember about your doubt and reply to you some other time, when I get free. Regards AK __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com >-----------------------------------------~-~> CHECK THE ARCHIVE BEFORE POSTING!!!! Archive is available at http://www.eScribe.com/software/C-Paradise/ >------------------------------------------_-> Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/C-Paradise/ <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/
