1 votes 1 votes int main() { char ch=1; while(ch<256) ch++; printf("loop end"); return 0; } My doubt here is , will it give undefined behaviour? As we are storing integer value inside character variable and also is incrementation of character pointer will give some error? Programming in C programming-in-c programming + – srestha asked Oct 17, 2017 srestha 417 views answer comment Share Follow See all 7 Comments See all 7 7 Comments reply Rishabh Gupta 2 commented Oct 17, 2017 reply Follow Share I think it won't give any error, but the loop will never terminate. Since ch will never exceed 256. 0 votes 0 votes srestha commented Oct 17, 2017 reply Follow Share but here integer is storing under char variable 0 votes 0 votes Rishabh Gupta 2 commented Oct 17, 2017 reply Follow Share considering char is stored in 1 byte, so it's maximum value can be 127. when ch = 127 and we increment it, the value of ch will round back to -128. So, ch will never reach 256, and the loop will run forever. Modify your program to this and run it, you will see: int main() { char ch=1; while(ch<256){ ch++; printf("%d\n", ch); } printf("loop end"); return 0; } Regarding saving of integer in char is valid. 1 votes 1 votes srestha commented Oct 17, 2017 reply Follow Share that is correct But here 1 is an integer . And for 32 bit machine it store inside 2 B. Char takes 1 Byte for 32 bit machine. So, will it not give any error? may give some alignment problem too. 0 votes 0 votes saipriyab commented Oct 17, 2017 reply Follow Share but unsigned character range is 0 to 255 so wrapping takes place here 256 will be converted to 0 so condition in while loop wll fail 0 votes 0 votes Angkit commented Oct 17, 2017 reply Follow Share It is signed here, For unsigned it will be exclusively defined 0 votes 0 votes sid1221 commented Nov 2, 2017 reply Follow Share https://gateoverflow.in/61844/program 0 votes 0 votes Please log in or register to add a comment.
1 votes 1 votes Range of char variable is (-128,127) so it will never be greater than 256 it will loop inside the while loop forever teja lobo answered Oct 17, 2017 teja lobo comment Share Follow See all 0 reply Please log in or register to add a comment.