Tricky C code involving the use of unary increment/decrement operators
I found this interesting piece of code online (can't remember where, it was on some Facebook group). The output is 28 but I don't get it. The unary operators have higher precedence so they are executed first. Then the local copies of variable 'a' are added.
#include <stdio.h>
int main()
{
int a = 5;
a = (++a) + (a) + (++a) + (a++);
printf("%d", a); // a = 28
getchar();
return 0;
}
See also questions close to this topic

How to configure CppCheck to discourage usage of a function
Reading CppCheck's List of Checks and the checkfunctions.h I noticed the feature:
Warn if a function is called whose usage is discouraged
I did not understand how to configure this, though. More specifically I want
 One of my own implemented functions to be discouraged
 A 3rdparty function (e.g. OpenCV's
cv::imwrite()
) to be discouraged. I am linking the prebuilts of this library so it would be hard (but not impossible) to change the source code to achieve it
How can I annotate these functions or how can I add them to CppCheck's list of "functions non grata"?

c++ Convert 4bytes to UTC time
I want to know how to convert 4bytes value to UTC Time. The language is C ++.
ex) Suppose 4 bytes of UTC is 0xACE62100 then corresponding split time would be 26th Jan 2000,17:09:00

recursive C++ program to display all the ways that an integer X between 1 and 100 can be expressed as the Nth power of natural numbers
recursive C++ program to determine all the ways that an integer X between 1 and 100 (both inclusive) can be expressed as the Nth power of natural numbers between 2 and 10 (both inclusive). The user should be able to type in the value of N and X. The program should also display all the different ways in which the number X can be expressed as the Nth power of natural numbers. For instance, if N = 2 and X = 13, your programâ€™s output should be:
13 = 2^2 + 3^2

RGB pixelation using c language
I'm trying to write a pure c code to implement mean filter pixelation.
size denotes the size of filter. p denotes padding, which will be added automatically when image is brought to memory to align width of memory to multiple of 4 bytes. My code below hasn't yet taken into account that it will be used to R, G, B. It's written to be applied to the first 8 bits of each 24 bits RGB block.
I have classified the case into 3, where the image pointer is at the end of each row / is at the last row / and all the other normal cases. I'm moving the image pointer along to move the filter to next pixel position and changing the value of original image array to average of the values inside the filter.
It gives me a segmentation fault error consistently,
problem arising from
val+=*(imgptr + 3 * l + (3 * width + p)*k)
.Can't think of solutions, need your ideas... Thanks
void pixelate(unsigned char *imgptr, long long width, long long height, long long size){ long long last_w; long long last_h; long long m = width / size; if ((width%size) == 0) last_w = size; else { m++; last_w = width % size; } long long n = height / size; if (height%size == 0) last_h = size; else { n++; last_h = height % size; } int p = 4  ((3 * width) % 4); //padding size for (long long i = 0; i < m*n; i++) { long long val = 0; if (i >= m * (n  1)) { if (i == m * n  1) { for (long long k = 0; k < last_h; k++) { for (long long l = 0; l < last_w; l++) { val += *(imgptr + 3 * l + k * (3 * width + p)); } } val /= (last_h*last_w); //getting average for (long long k = 0; k < last_h; k++) { for (long long l = 0; l < last_w; l++) { *(imgptr + 3 * l + k * (3 * width + p)) = val; } } return; } else { for (long long k = 0; k < last_h; k++) { for (long long l = 0; l < size; l++) { val += *(imgptr + 3 * l + k * (3 * width + p)); } } val /= (last_h*size); for (long long k = 0; k < last_h; k++) { for (long long l = 0; l < size; l++) *(imgptr + 3 * l + k * (3 * width + p)) = val; } imgptr += 3 * size; continue; } } else if (i%(m1) == 0) { for (long long k = 0; k < size; k++) { for (long long l = 0; l < last_w; l++) { val+=*(imgptr + 3 * l + (3 * width + p)*k); } } val /= (size*last_w); for (long long k = 0; k < size; k++) { for (long long l = 0; l < last_w; l++) *(imgptr + 3 * l + (3 * width + p)*k) = val; } imgptr += p; continue; } else { for (long long k = 0; k < size; k++) { for (long long l = 0; l < size; l++) val+=imgptr[3 * l + (3 * width + p)*k]; } val /= (size * size); for (long long k = 0; k < size; k++) { for (long long l = 0; l < size; l++) *(imgptr + 3 * l + (3 * width + p)*k)=val; } imgptr = imgptr  (3*width+p)*(size1) + 3; continue; } }
}

How to convert a C string literal to small letters?
I am looking for the way to convert c string literal to small letters. I know the
tolower
function could convert a single letter. But I need do something like:str_to_lower(FOO) // return "foo"
or
str_to_lower("FOO") // return "foo"
function
str_to_lower
could be a macro or a static function. And the input parameter of function could be a token or string literal. Because it's the same for me,FOO
could convert to"FOO"
by#define STRF(s) #s
STRF(FOO)

issue in swapping without pointers
When i type in the following function ,
void swap(int a, int b) { int t; t = a; a = b; b = t; }
This does not work upon calling the function, but it does when the parameters are set as pointers rather than integers. What is the reason for this?
void swap(int* a, int* b) { int t; t = *a; *a = *b; *b = t; }
(the working version)