how subtraction of two strlen() function works [duplicate] - c

This question already has answers here:
sizeof() operator in if-statement
(5 answers)
What will be the value of strlen(str)- 1 in a 'for' loop condition when str is empty?
(2 answers)
why is -1>strlen(t) true in C? [duplicate]
(3 answers)
Closed 4 years ago.
Why does the shorter string ("paid") get printed by this program?
#include <stdio.h>
int main()
{
char s[] = "paid", t[] = "paviDboss";
if ((strlen(s) - strlen(t)) > 0)
printf("%s\n", s);
else
printf("%s\n", t);
}

Return type of strlen is size_t which is an unsigned type. The result of the subtraction is also size_t and can therefore only be positive.
Just use
if(strlen(s) > strlen(t))

Related

Why is the printed result 5452853 [duplicate]

This question already has answers here:
Multi-character constant warnings
(6 answers)
Closed 11 months ago.
#include <stdio.h>
int main()
{
int a = '\12345';
printf("%d",a);
return 0;
}
Why is the printed result 5452853? I wonder what happened?
I combined phuclv's and Mark Dickinson's answers and I seem to understand that first of all '\12345' is equivalent to three characters '\123' = 1*8*8+2*8+3*1 = 83; '4' = 52 ; '5' = 53. Then the result is equal to 83*256*256+52*256+53 = 5452853
– HuangGuojun

Why is "sizeof(double) > -1" false? [duplicate]

This question already has answers here:
sizeof() operator in if-statement
(5 answers)
Closed 1 year ago.
int main()
{
if(sizeof(double) > -1)
printf("M");
else
printf("m");
return 0;
}
I expected the output to be M but it is m. Can anybody please explain me the reason for the output?
That's because sizeof returns a size_t value, that is, a unsigned integer type, so, -1 will overflow.

Please explain the output of the following C code [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 2 years ago.
#include<stdio.h>
int main()
{
char a[] = { 'A', 'B', 'C', 'D' };
char* ppp = &a[0];
*ppp++;
printf("%c %c ", *++ppp, --*ppp);
return 0;
}
My expected output was C B. But Output is C A
Use parenthesis in those cases so we understand what you want to do.
Do you increase the pointer ?
ppp++;
Do you increase the value inside ?
(*ppp)++;

operations on character pointers [duplicate]

This question already has answers here:
What does 1[d=b] mean?
(3 answers)
Closed 5 years ago.
I came across a code as in below
#include <stdlib.h>
int main(){
char a[]="0123456789";
printf("%s\n",a+6[a]-2[a]);
return 0;
}
Output
456789
How does the calculation of a+6[a]-2[a] happens in printf?
Why giving just 6[a] in printf doesn't work?
printf("%s\n",6[a]);
Well, a statement like
a+6[a]-2[a]
can be re-written as
&(a[ a[6] - a[2] ])
which is simply,
use the value of a[6] (type, int) as the index in the first case
use the value of a[2] (type int) as the RHS.
The result, is a pointer, is passed to printf() as an argument to %s conversion specification.

Printf with unfixed length [duplicate]

This question already has answers here:
printf string, variable length item
(2 answers)
Closed 7 years ago.
I need to
printf(%?d)
Where '?' is some int. How can I do it?
I'm using pure c.
I've tried to work with const char* array. But there was no result.
See the printf man page:
int width = 16;
int value = 42;
printf("%*d\n", width, value);
Output:
42
LIVE DEMO

Resources