Use of comma separated variables in foor loop [C] [duplicate] - c

This question already has answers here:
How does the Comma Operator work
(9 answers)
What does the comma operator , do?
(8 answers)
Closed 3 years ago.
In the linux kernel (ASOP) code I came across the following macro code:
#define for_each_cpu_and(cpu, mask, and) \
for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and)
I have never come across a for loop like the one above where there are several comma separated variables along with the increment part. Using the following code I tried to check how the aforementioned macro actually behaves:
#include <stdio.h>
#include <stdbool.h>
#define for_each_cpu_and(cpu, mask, and) \
for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and)
int main()
{
int i;
for_each_cpu_and(i, false, 3){
printf("i value: %d\n", i);
}
return 0;
}
The answer in the console is as follows:
i value: 0
If I tweek the code to the following:
#include <stdio.h>
#include <stdbool.h>
#define for_each_cpu_and(cpu, mask, and) \
for ((cpu) = 0; (cpu) < 3; (cpu)++, (void)mask, (cpu)+2)
int main()
{
int i;
for_each_cpu_and(i, 4, 3){
printf("i value: %d\n", i);
}
return 0;
}
The answer in the console is as follows:
i value: 0
i value: 1
i value: 2
So from the aformentioned code it seems like in the increment part only the first increment option i.e. (cpu)++ is given precedence and others are not being used.
Can someone please explain with example(s) the usage of additional comma separated variables in the increment part of the for loop?
Note: I am aware what a comma separated variable in C does in general and based on the rule the first varaible in the code should be given precedence. However, in the aforementioned code the case is not true. So explanation on the working of the comma separated variables in the increment part of the for-loop in the aforementioned code would be much appreciated.
Comma separated variables in C: How does the Comma Operator work

Related

what is this kind of usage, ``braces inside parentheses'', called in C? [duplicate]

I have this piece of code and i don't know how it works
#include <stdio.h>
int main(void)
{
int numero = ({const int i = 10; i+10;});
printf("%d\n", numero); // Prints 20
return 0;
}
Why if i delete the second part (i+10;), the compiler gets an error?
Why are the brackets necessary?
Thank you ^^!
It's a GCC statement expression. It executes the statements in it, and returns the value evaluated in the last statement. Thus numero is initialized to 20. If you delete the second part, there is no expression as the last statement, so it can't get a value from the statement expression.
The braces are necessary to disambiguate it from ordinary C parenthesized expressions.

How ## operator in macro works?

I was studying some macro operations, and I got this Code and I was unable to figure out how this code is actually working and generates the output? and is there any (i-+) operator that exists or not?
Here is the code
#include<stdio.h>
#define p(a,b) a##b
#define call(x) #x
int main()
{
do{ int i=14,j=3;
printf("%d",p(i-+,+j));
}while(*(call(625)+3));
return 0;
}
Output is 10.
It will be very helpful if you explain it with some examples.
The ## in the macro is the concatenation operator, it glues its operands together. So after the preprocessor is done, that expression will be
i-++j
which of course just means i - (++j), i.e. 14 - 4 which of course is 10.

Is it possible to pass two arguments in c? [duplicate]

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 3 years ago.
I have written a C program where I declared a function reverse(int i). When I compile and run the program, it runs fine despite passing two arguments like this reverse((i++, i)). Why doesn't this cause a syntax error? reverse expects one argument.
#include <stdio.h>
void reverse(int i);
int main()
{
reverse(1);
}
void reverse(int i)
{
if (i > 5)
return ;
printf("%d ", i);
return reverse((i++, i));
}
You're not passing two arguments - that would be reverse(i++, i) (which incidentally would invoke undefined behaviour because of the lack of a sequence point between (i++ and i).
You're passing (i++, i) as a single argument. Since it is inside an additional pair of parentheses, the comma here does not separate the arguments of the function, but rather acts as the comma operator.
(i++, i) seems to execute i++, then evaluate to i, the last operand to ,. You can see that here:
// Notice the ( , )
int i = (puts("Inside\n"), 2); // Prints "Inside"
printf("%d\n", i); // Prints 2
It didn't cause an error because you only passed one argument. That one argument though was a sequence of effects that evaluated to i.

C macro with expression unwanted result

I am running the following program and getting a result as 9 7, I understood why 9 is the output but I can't figure out why I'm getting 7 as output.
#include<stdio.h>
#define sqr(i) (i*i)
int main()
{
printf("%d %d", sqr(3), sqr(3+1));
return 0;
}
For the second function that is sqrt(3+1) how the micro is getting expanded and how Im getting 7 output?
You can have the compiler or IDE preprocess the file and show you how the macro expanded.
In your case sqr(3+1) expands to (3+1*3+1). Now the precedence of C operators means that the multiplication is done before the addition. So (3+1*3+1) -> (3+3+1) -> (7).
You can fix this by defining your macro this way, with parentheses around the argument:
#define sqr(i) ((i)*(i))

Why does the simple c program I've written to compare two strings count an extra same character? [duplicate]

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 6 years ago.
I'm using turbo C++ to compile the program. The program is
main()
{
char inp1[21],inp2[21];
int nsame=0,i=0,l1,l2;
clrscr();
gets(inp1);
gets(inp2);
l1=strlen(inp1);
l2=strlen(inp2);
if(l1==l2)
{
for(;inp1[i]!='\0',inp2[i]!='\0',inp1[i]==inp2[i];i++)
{
nsame++;
}
}
if(nsame==l1)
{
puts("Same");
}
else
{
puts("Not the same");
}
getch();
}
The for loop above runs an extra time so the nsame is greater than the correct value by 1. So the program's output is correct if an extra nsame--; is added.
The conditional in the for statement is not right. You have:
for(; inp1[i]!='\0', inp2[i]!='\0', inp1[i]==inp2[i]; i++)
You have three comma separated expressions. The first two are evaluated and discarded. Their values are not used in the test. Only the value of the last expression is used to determine when to terminate the loop.
You need to use:
for(; inp1[i]!='\0' && inp2[i]!='\0' && inp1[i]==inp2[i]; i++)
Suggestion for improvement:
You can remove one of the first two expressions and your program will work.
for(; inp1[i]!='\0' && inp1[i]==inp2[i]; i++)

Resources