Is it possible to do multiple operations in increment part of for loop in C/C++? Something like this:
int a = 0, b = 0, c = 5;
for(; a < c; increase a by 1 and increase b by 2)
Is it possible to do multiple operations in increment part of for loop in C/C++? Something like this:
int a = 0, b = 0, c = 5;
for(; a < c; increase a by 1 and increase b by 2)
Use the comma operator:
for (; a < c; ++a, b += 2)
Yes it is possible. You can also declare multiple variables inside the loop and don't need to do it before.
for (int a = 0, b = 0, c = 5; a < c; ++a, b += 2)
for(int a=0, char b='A';;)
doesn't work, as it would be expecting char
to be a variable name. for(int a=0; char b='A';;)
wouldn't work either, as that places the char declaration in the condition section of the for loop. –
Translucid {}
to create a new scope if you need some variables that should only be visible in a limited scope/destroyed automatically afterwards: Extra brace brackets in C++ code –
Quartz ()
which can be used in for loops, but perhaps it was in another c-like language. –
Translucid What about declaring multiple variables of different types?
multiple declarations in a for loop only works if they are all of the same type.
This might be close to heresy, but you could use a struct
to contain multiple variables of different types like this:
#define BENCHMARK(elasped) \
for ( \
struct { uint8_t done; uint32_t start; } __metadata = \
{ 0, systick_get() }; \
!__metadata.done; \
((elasped) = elasped_time(__metadata.start)), __metadata.done = 1 \
)
EDIT: Here's a more complete example
#include <stdint.h>
#include <stdio.h>
volatile uint32_t g_systick = 0; //< Incremented by the system clock somewhere else in the code
uint32_t systick_get(void)
{
return g_systick;
}
uint32_t elasped_time(uint32_t const start)
{
uint32_t const now = systick_get();
return now - start;
}
#define BENCHMARK(elasped) \
for ( \
struct { \
uint8_t done; \
uint32_t start; \
} __metadata = {0, systick_get()}; \
!__metadata.done; \
((elasped) = elasped_time(__metadata.start)), __metadata.done = 1)
int main(void)
{
uint32_t time = 0;
BENCHMARK(time)
{
int i = 0; //< Something you want to benchmark
}
printf("Time = %d\n", time);
return 0;
}
Here's how the BENCHMARK()
macro looks like when expended (using gcc -E
)
int main(void) {
uint32_t time = 0;
for (
struct {
uint8_t done;
uint32_t start;
} __metadata = {0, systick_get()};
!__metadata.done;
((time) = elasped_time(__metadata.start)), __metadata.done = 1)
{
int i = 0; //< Something you want to benchmark
}
printf("Time = %d\n", time);
return 0;
}
May Dennis Ritchie forgive us.
struct
. Can you also post a full, runnable example which includes proper headers and calls this BENCHMARK()
macro? –
Stacistacia systick_get()
and elapsed_time()
functions defined? Is this on a microcontroller? With an RTOS? I think I'm going to add something like this to my Linux timinglib. –
Stacistacia there's a systick timer built it
built-in* –
Valuation © 2022 - 2024 — McMap. All rights reserved.