+19 votes
2.9k views

Consider the following C program.

#include<stdio.h>
#include<string.h>

void printlength(char *s, char *t) {
unsigned int c=0;
int len = ((strlen(s) - strlen(t)) > c) ? strlen(s) : strlen(t);
printf("%d\n", len);
}

void main() {
char *x = "abc";
char *y = "defgh";
printlength(x,y);
}

Recall that strlen is defined in string.h as returning a value of type size_t, which is an unsigned int. The output of the program is __________ .

asked
retagged | 2.9k views

Let us assume our int  is of 3 bits then

value of binary 110  in unsigned int will be 6.( which is > 0)

value of binary 110  in signed int (or int ) will be -2. (In C int is represented in  2's compliment  representation)

One more thing -->

As you can see, (unsigned)0 - (unsigned)1 equals -1 modulo UINT_MAX+1, or in other words, UINT_MAX.

## 4 Answers

+21 votes
Best answer
(strlen(s)- strlen(t)) = 3-5 =-2

But in C, when we do operation with two unsigned integers, result is also unsigned. (strlen returns size_t which is unsigned in most systems). So, this result "-2" is treated as unsigned and its value is INT_MAX - 2 (not sure about all systems, but at least on systems using 2's complement representation). Now, the comparison is between this large number and another unsigned number c which is 0. So, the comparison return TRUE here.

Even if 'c' is declared as "int", while doing an operation with an unsigned int, it gets promoted to unsigned int and we get the same result.

Hence (strlen(s)- strlen(t)) >0 will return 1 on execution thus the conditional operator will return the true statement which is strlen(abc) =3.

Ans should be 3.
answered by Boss (8.2k points)
edited by
If anyone know the reason behind -2>0=true(1)pls let me know..
Unsigned int..... Therefore -ve value will be wrapped around and will be greater than zero...... Therefore...answer is 3....But didn think like this in exam.... :(
Try to print -2 as unsigned integer.
@Arnabi Due to representation. Even though number is "-2" we are telling compiler and eventually hardware to treat it as an unsigned number which would be MAX_INT - 2.
arjun sir pls check the 1-43 question
okk got it now.Thanks arjun sir and all..:)
#include<stdio.h>
int main(){
if(-2>0){
printf("true");
}
else{
printf("flase\n");
}
return 0;
}

Output:

[email protected]:~/socket_programming$gcc compare.c [email protected]:~/socket_programming$ ./a.out
flase

so -2>0 is flase not true
Try (-2 > (unsigned int) 0) . You will get True

-2 in unsigned int is the same as 232-2 =  4294967294

Hence -2 > 0 is true for unsigned int.

but why -2 should be converted in unsigned int?

It is not told that s, t are in unsigned int.

And it stores in len which is 'int' only. It can be signed int.rt?
If we remove unsigned then from variable "c" then will it affect this program?????????plz explain

Well if you have a variable of type int, and it contains a negative value such as -9 then how do you convert this to an unsigned data type and what exactly happens if you perform a cast as shown above? Well the basic answer is – nothing. No bits are changed, the compiler just treats the bit representation as unsigned. For example, let us assume that the compiler represents signed integers using 2’s complement notation (this is the norm – but is *not* mandated by the C language). If our signed integer is a 16 bit value, and has the value -9, then its binary representation will be 1111111111110111. If you now cast this to an unsigned integer, then the unsigned integer will have the value 0xFFF7 or 6552710. Note however that you cannot rely upon the fact that casting -9 to an unsigned type will result in the value 0xFFF7. Whether it does or not depends entirely on how the compiler chooses to represent negative numbers.

@sushmita

len is int, not an unsigned int. rt?

and as u said it is compiler dependent, then how could u sure the answer?
Sretha because in c if we are having unsigned int operands first by typecasting other operands are also promoted to unsigned int and hence -2 will no longer be -2. It will be a positive number only.
ok means len here promoted to unsigned int.
The Answer is 3 but I found somewhat a different explanation.
Even if 'unsigned int c' is not given, ((strlen (s) - strlen (t)) > c) would return true(and print 3 as output) since strlen() function always returns unsigned value. So, strlen(s) - strlen(t) will yield unsigned-unsigned= unsigned value which, upon being compared with c( even if it is signed), would return TRUE( and print 3 ) since c will be converted to unsigned.

So, having unsigned/siged int c doesn't make much of a dfference.
You are correct.

the definition of strlen function is given in string.h header file and the output of the strlen function retrun a value of type size_t form and size_t is a unsigned integer . so in each case  ((strlen(s) - strlen(t)) > c) return a unsigned value, it is not matter that whether c is given unsigned or not.

if c is not given unsgined then answer also will be same .

Binary operations between different integral types are performed within a "common" type defined by so called usual arithmetic conversions (see the language specification, 6.3.1.8)

C Language Specification URL : www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf

So here (strlen(s)-strlen(t)) which is coming as -2 is converted to first unsigned int (which evaluates to INT_MAX-2) and then it is compared with c(c being unsigned int) which is 0.

The result of the boolean expression in program goes true and strlen(s) is selected for output which prints output as 3.

IIn above case it returns -2 .I think it should return 2 .Do not understand why this is happening .We discussed that arithmetic calcuation between unsigned is always positive.

+3 votes

Suppose  numbes are represented in 2's complement.

Then ,(strlen(s)- strlen(t)) gives -2.

Because it is unsigned ,its binary format will be in 2's complement

+2n-1-2 on n bit machine.

Just care here about that it will be positive and  condition become true,and length of first string gets printed which is 3.

answered by Loyal (2.9k points)
0 votes
5 is the answer because ((3-5)>0 ) return 0 so len=strlen(t).

#include<stdio.h>
int main(){
printf("%d\n",((3-5)>0));
return 0;
}

[email protected]:~/socket_programming$gcc compare.c [email protected]:~/socket_programming$ ./a.out
0
answered by (269 points)
Good, but what you just answered showed a different thing - integer constants by default is signed in C.
0 votes

answer is 3
http://codepad.org/Yx20F3LK

answered by (71 points)

This is because of the unsigned int property which causes negative values to overflow into a large positive number
see this code snippet for better understanding

#include<stdio.h>
#include<string.h>

void printlength(char *s, char *t) {
unsigned int c = 0;
int len = ((strlen(s) - strlen(t)) > c )? strlen(s) : strlen(t);
printf("using standard int = %d\n", (strlen(s) - strlen(t)));
printf("using unsigned int = %u\n", (strlen(s) - strlen(t)));
printf("%d\n", len);
}

void main() {
char *x = "abc";
char *y = "defgh";
printlength(x,y);
}
Answer:

+12 votes
2 answers
1
+8 votes
3 answers
2
+4 votes
3 answers
3