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

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)++;

Related

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

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))

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.

what will be output of the following code and why ? can anyone explain? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 6 years ago.
#include <stdio.h>
int main()
{
int i = 6;
printf("%d %d", ++i, i++);//printing
return 0;
}
What will be output of the following code and why?
printf("%d %d",++i ,i++);//printing
Is undefined behavior. The order of argument processing is not specifically defined in the C standard, it is not possible to predict exactly what the output will be. It could be anything at all according to this.

not able to understand how compiler(gcc) is interpreting the command(c) and giving output of the statement [duplicate]

This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 8 years ago.
Can anyone help me out, I am not getting how gcc is compiling the below statement and printing its output.:-
printf("%d",7["sunderban"]);
C allows to access array's elements in two ways (see Accessing arrays by index[array] in C and C++ and the answers) :
int v[5];
// 1)
v[2] = 33;
// 2)
2[v] = 44;
So, what happens in your case is that you access the 8th element of the string, and it is interpret as int by the printf.

Behaviour of macros and increment operator in C [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
When I compile the following code
#include<stdio.h>
#define CUBE(x) (x*x*x)
int main()
{
int a, b=3;
a = CUBE(b++);
printf("%d, %d\n", a, b);
return 0;
}
It gives 27 , 6
But shouldn't the expression a=b++*b++*b++; be calculated as a=3*4*5 and should give 60?
Your expression causes undefined behaviour, so you could get any answer. Trying to modify the same value more than twice between sequence points is bad news.

Resources