This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 24 days ago.
while reading about the c preprocessor I got something like array[x=y,x+1]. I haven't seen this kind of syntax in c before and after searching for many hours I didn't find anything useful.
#include <stdio.h>
int main() {
int arr[] = {5,10,15};
printf("%d %d %d",arr[0,1]);
return 0;
}
outputs:
10 1762365112 1769491896
Can someone elaborate on this?
Because of how the comma operator (,) works in C, the effect of the (questionable and woefully hard to read) array[x=y,x+1] is to first copy the value of y into x and then use x+1 as the normal (single dimensional, non-ranged, or whatever you were expecting) index in to the array.
(I do not see the need to discuss your experimental code.)
See the link to documentation of comma operator, as kindly provided in comments by Pignotto:
https://en.cppreference.com/w/c/language/operator_other#Comma_operator
Related
This question already has answers here:
How do I use the conditional (ternary) operator?
(10 answers)
Closed 10 months ago.
Can someone explain this kind of statement(s)? I didn't get it.
data[i] = ((data[i] == div[i]) ? '0' : '1');
It's an if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.
So , your expression can be visualized into if-else statement as:
#include <stdio.h>
int main(){
// Code ...
if(data[i]==div[i])
data[i]='0';
else
data[i]='1';
}
** ‘?:’ takes three operands to work, hence they are also called ternary operators.
This question already has answers here:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 4 years ago.
#include<stdio.h>
void main()
{
int x=3,y=2,z=0,m;
m=++x || ++y && ++z;
printf("\n %d %d %d %d\n",x,y,z,m); // 4 2 0 1
}
The output of the following code is mentioned as comment in program and I am trying to evaluate how this answer came but I am not able to understand.
I just wanted to know how the program calculates the relative value.
thanks to pmg, i have corrected my original answer (i had an error)
Because the left side of the OR operator (||) is not zero, it doesn't evaluate anything else on that line. This is called a "short circuit operator". In this example you gave, the programmer basically is tricking the compiler. If the argument on the right of the operator does not affect the outcome, it will not execute that code. However in this case, there are increments going on there and they won't be evaluated either.
This will assign "1" to m.
your output should be 3, 2, 0, 1.
This question already has answers here:
Square of a number being defined using #define
(11 answers)
Closed 5 years ago.
I was given a variation of this C code in an interview recently, and asked what would be returned by the function.
#define fun(a) a*a
int main() {
return(fun(4+5));
}
I've run it with a printf("%d"...) in place of the return and it prints 29. No other types than "%d" showed a result. Can someone explain what is going on here?
a is expanded, not evaluated as a. So you get:
4+5*4+5
and with operator priorities; you get 4 + 20 + 5 => 29
better way (with extra outside parenthesis for protecting against outside operators too thanks to Thomas comment):
#define fun(a) ((a)*(a))
but all parenthesis of the world still don't protect against i++, function calls (with side effects, preferably)... so argument reusing in macros is always a problem (there's no syntax to declare & assign a temp variable either). Prefer inlined functions in that case.
This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 6 years ago.
I tried executing the following c code:
#include <stdio.h>
int main()
{
int i=2000,j=100;
printf("%d",(j,i));
}
I gave different values to i and j and found the output. I always get the output as the value contained in the second variable. Does the expression always give the last variable as a result or does it have any other meaning?
You are using the comma operator.
The output of your code is 2000, because the comma operator evaluates the two expressions and returns the value of the second operand. More details can be found in this SO answer.
This question already has answers here:
two strings separated by blank being concatenated automatically
(3 answers)
Closed 8 years ago.
I've doubt regarding the basics of C language while using printf statement.
well this is how my code looks like.
#include<stdio.h>
int main(){
printf("%s %s",("senthil""kumar"),("hello""world"),("stack""overflow");
return 0;}
I've got an output like,
senthilkumar helloworld
but i don't how does this code works.
could u pls help me to figure out how it works...
thanx in advance.
Two consecutive string literals are merged by the compiler.
I.e. the following examples are equivalent:
// 1:
"foo" "bar"
// 2:
"foobar"
// 3:
#define FOO "foo"
FOO "bar"
You do not need all those inner parentheses (also, there's a ) missing at the end).