Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why this loop is running infinite times in c?

Tags:

c

char

for-loop

I was just experimenting a code in C programming. and came to know a strange behavior. Well... Since i am not an expert on C, so i don't know whether its strange or normal.

Basically my question is all about the difference between the following two lines of code:-

char a = 'h'; // here variable a is not an array of "char"

and

char a = 'hi'; //here variable a is not an array of "char" as well (i don't know if compiler assumes it as an array or not but , at least i didn't declared it that way )

I used the following codes

first:-

char a =0;
for(;a<'hi';a++)
{
    printf("%d= hello world \n",a);
}

second:-

char a;
for(a='h';a<'hi';a++)
{
    printf("%d= hello world \n",a);
}

both of the above mentioned loops keep running forever,

Can somebody tell me why so ?

I might be missing a very basic concept of programing. please help me guys

like image 568
Pawan Joshi Avatar asked Jan 28 '15 09:01

Pawan Joshi


People also ask

How do you stop an infinite loop in C?

There is no way to stop an infinite loop. However, you can add a condition inside of the loop that causes it to break, or you can call the exit() function inside of the loop, which will terminate your program. Highly active question.

Why does my loop go to infinite?

An infinite loop occurs when a condition always evaluates to true. Usually, this is an error.

How do you stop an infinite loop from running?

IMPORTANT - Stopping An Infinite Loop In response to this message, type HI for halt interpretation and press the Enter key. If that doesn't stop the loop, you can press the attention interrupt key again, type HE for halt execution, and press the Enter key.

Why does an infinite loop run forever?

An infinite loop is a sequence of instructions in a computer program which loops endlessly, either due to the loop having no terminating condition, having one that can never be met, or one that causes the loop to start over.


2 Answers

That is because 'hi' has type int not a char. It also resolves to value 26729. But loop variable most likely (assuming char is 1-byte type and byte has 8 bits) is limited by 127 and after that overflows.

Note that this:

char a =0;
char m = 'hi';
for(; a < m; a++)
{
    printf("%d= hello world \n",a);
}

will work because 'hi' is will be coerced to char (105).

'hi' is a multi-character literal. It is not common practice in programming, it is "less known" C feature which became part of C99 standard. More information about them: http://zipcon.net/~swhite/docs/computers/languages/c_multi-char_const.html

like image 75
myaut Avatar answered Oct 18 '22 07:10

myaut


In C (as opposed to C++, as cited in some comments), character literals, always have type int. It doesn't matter if it's an ordinary one-character literal, such as 'c', or a multi-character literal, such as 'hi'. It always has type int, which is required to hold at least 16 bit. A char holds exactly one byte.

When comparing integer values of different type, integer-promotion rules kick in and the integer value of smaller size gets promoted to the larger one. That is why a < 'hi' can only be 1 ("true"). Even if promoted to type int, the variable a can never hold anything larger than MAX_CHAR. But the multi-character literal 'hi' is an int with a larger value than that in your complier's implementation.

The reason that a < m can succeed is that when declaring m, you initialise it with 'hi' which gets converted to type char, which indeed has a chance to compare not-less-than an other char.

like image 34
bitmask Avatar answered Oct 18 '22 07:10

bitmask