What does this line of code mean, I found this in this piece of code:
void simple_print_buffer(char *buffer, unsigned int size)
{
unsigned int i;
i = 0;
while (i < size)
{
if (i % 10)
{
printf(" ");
}
if (!(i % 10) && i)
{
printf("\n");
}
printf("0xx", buffer[i]);
i ;
}
printf("\n");
}
CodePudding user response:
i % 10
means i modulo 10
or calculates the last digit of the integer i
. Then operator precedence tells you the !
is applied next which means not
, and in this case, if the last digit is not 0
. &&
means and
so the if statement is executed if both left (already discussed) and right side are true
. Right side i
means that i
is not 0
.
You can refactor the code by using a for
loop, as both branches of the if
statement does printf
you can use a conditional value instead of a conditional statement:
void simple_print_buffer(char *buffer, unsigned size) {
for(unsigned i = 0; i < size; i ) {
if(i) printf(i % 10 ? " " : "\n");
printf("0xx", buffer[i]);
}
printf("\n");
}
CodePudding user response:
!condition
is true when condition
is false or 0.
First condition is equal to (i % 10) == 0
. Second condition is equal to i != 0
.
&&
means that the if
clause is executed when both conditions are true.
CodePudding user response:
I think the other answers already explain it, but this code might help understanding:
#include <stdio.h>
int main()
{
unsigned int i;
for(i = 0; i<50; i )
{
printf("i: %d i mod 10: %d !(i mod 10): %d !(i mod 10) && i: %d \n",
i, i, !(i), !(i)&& i);
}
return 0;
}
output:
i: 0 i mod 10: 0 !(i mod 10): 1 !(i mod 10) && i: 0
i: 1 i mod 10: 1 !(i mod 10): 0 !(i mod 10) && i: 0
i: 2 i mod 10: 2 !(i mod 10): 0 !(i mod 10) && i: 0
i: 3 i mod 10: 3 !(i mod 10): 0 !(i mod 10) && i: 0
i: 4 i mod 10: 4 !(i mod 10): 0 !(i mod 10) && i: 0
i: 5 i mod 10: 5 !(i mod 10): 0 !(i mod 10) && i: 0
i: 6 i mod 10: 6 !(i mod 10): 0 !(i mod 10) && i: 0
i: 7 i mod 10: 7 !(i mod 10): 0 !(i mod 10) && i: 0
i: 8 i mod 10: 8 !(i mod 10): 0 !(i mod 10) && i: 0
i: 9 i mod 10: 9 !(i mod 10): 0 !(i mod 10) && i: 0
i: 10 i mod 10: 0 !(i mod 10): 1 !(i mod 10) && i: 1
i: 11 i mod 10: 1 !(i mod 10): 0 !(i mod 10) && i: 0
i: 12 i mod 10: 2 !(i mod 10): 0 !(i mod 10) && i: 0
i: 13 i mod 10: 3 !(i mod 10): 0 !(i mod 10) && i: 0
i: 14 i mod 10: 4 !(i mod 10): 0 !(i mod 10) && i: 0
i: 15 i mod 10: 5 !(i mod 10): 0 !(i mod 10) && i: 0
i: 16 i mod 10: 6 !(i mod 10): 0 !(i mod 10) && i: 0
i: 17 i mod 10: 7 !(i mod 10): 0 !(i mod 10) && i: 0
i: 18 i mod 10: 8 !(i mod 10): 0 !(i mod 10) && i: 0
i: 19 i mod 10: 9 !(i mod 10): 0 !(i mod 10) && i: 0
i: 20 i mod 10: 0 !(i mod 10): 1 !(i mod 10) && i: 1
i: 21 i mod 10: 1 !(i mod 10): 0 !(i mod 10) && i: 0
i: 22 i mod 10: 2 !(i mod 10): 0 !(i mod 10) && i: 0
i: 23 i mod 10: 3 !(i mod 10): 0 !(i mod 10) && i: 0
i: 24 i mod 10: 4 !(i mod 10): 0 !(i mod 10) && i: 0
i: 25 i mod 10: 5 !(i mod 10): 0 !(i mod 10) && i: 0
i: 26 i mod 10: 6 !(i mod 10): 0 !(i mod 10) && i: 0
i: 27 i mod 10: 7 !(i mod 10): 0 !(i mod 10) && i: 0
i: 28 i mod 10: 8 !(i mod 10): 0 !(i mod 10) && i: 0
i: 29 i mod 10: 9 !(i mod 10): 0 !(i mod 10) && i: 0
i: 30 i mod 10: 0 !(i mod 10): 1 !(i mod 10) && i: 1
i: 31 i mod 10: 1 !(i mod 10): 0 !(i mod 10) && i: 0
i: 32 i mod 10: 2 !(i mod 10): 0 !(i mod 10) && i: 0
i: 33 i mod 10: 3 !(i mod 10): 0 !(i mod 10) && i: 0
i: 34 i mod 10: 4 !(i mod 10): 0 !(i mod 10) && i: 0
i: 35 i mod 10: 5 !(i mod 10): 0 !(i mod 10) && i: 0
i: 36 i mod 10: 6 !(i mod 10): 0 !(i mod 10) && i: 0
i: 37 i mod 10: 7 !(i mod 10): 0 !(i mod 10) && i: 0
i: 38 i mod 10: 8 !(i mod 10): 0 !(i mod 10) && i: 0
i: 39 i mod 10: 9 !(i mod 10): 0 !(i mod 10) && i: 0
i: 40 i mod 10: 0 !(i mod 10): 1 !(i mod 10) && i: 1
i: 41 i mod 10: 1 !(i mod 10): 0 !(i mod 10) && i: 0
i: 42 i mod 10: 2 !(i mod 10): 0 !(i mod 10) && i: 0
i: 43 i mod 10: 3 !(i mod 10): 0 !(i mod 10) && i: 0
i: 44 i mod 10: 4 !(i mod 10): 0 !(i mod 10) && i: 0
i: 45 i mod 10: 5 !(i mod 10): 0 !(i mod 10) && i: 0
i: 46 i mod 10: 6 !(i mod 10): 0 !(i mod 10) && i: 0
i: 47 i mod 10: 7 !(i mod 10): 0 !(i mod 10) && i: 0
i: 48 i mod 10: 8 !(i mod 10): 0 !(i mod 10) && i: 0
i: 49 i mod 10: 9 !(i mod 10): 0 !(i mod 10) && i: 0
CodePudding user response:
The purpose of the two if
statements is to print the separator before the next number. The loop outputs 10 bytes per line in hex format, there should be no initial separator, hence no output if !i
, and the separator should be a space between numbers except every 10 numbers where the separator is a newline.
%
is the modulo operator: i % 10
computes the remainder of the division of i
by 10
.
The test if (i % 10)
means if i
is not a multiple of 10 and the next one if (!(i % 10) && i)
means if i
is a multiple of 10 and i
is not zero.
The code can be simplified as:
void simple_print_buffer(const char *buffer, unsigned int size) {
for (unsigned int i = 0; i < size; i ) {
if (i % 10) {
printf(" ");
} else
if (i) {
printf("\n");
}
printf("0xx", buffer[i]);
}
printf("\n");
}
Or even simpler:
void simple_print_buffer(const char *buffer, unsigned int size) {
if (size) {
printf("0xx", buffer[0]);
for (unsigned int i = 1; i < size; i ) {
static char const separator[] = { ' ', '\n' };
printf("