+ 2

>> and %*d meaning in C

#include <stdio.h> int main() { int i=2048; while(i){ printf ("Sololearn\n"); i>>=1; } return 0; } This code prints 'Sololearn ' 12 times. Can someone tell me why this happens and what does >> do in C? #include <stdio.h> int main() { int x=4; printf ("%*d",x,42); return 0; } Another question is that why this code outputs 42 instead of 4 and what is the use of %*d in C?

20th Nov 2022, 1:45 PM
Atkiya
Atkiya - avatar
2 Antworten
+ 1
>> is bitwise right shift, there is also a left shift and you can see here what exactly does https://www.geeksforgeeks.org/left-shift-right-shift-operators-c-cpp/ about the "%*d" you can see here what it does https://www.quora.com/What-does-printf-*d-*d-a-b-mean-What-about-the-format-specifier
20th Nov 2022, 3:26 PM
Giannis
Giannis - avatar
0
">>" - This is a bitwise operator that shifts a binary number by a certain value to the right, that is, 4>>1 equals 2, (essentially divides the number in half): 4 2 100>>1 equals 010 100>>2 equals 001 In the second case, the indentation increases four times minus the number of characters of the printed value. 42 consists of two characters meaning 4-2 equals 2 indentations.
20th Nov 2022, 2:48 PM
Solo
Solo - avatar