C program Strings Example how come the result is 98? -
c program strings example how come result 98?
#include <stdio.h> int main() { char s[]="%d%d%d"; int a=9,b=8,c=5; printf(s+2,a,b,c); return 0; }
string + x operation called pointer arithmetic. way providing reference mathematically calculated memory area , semantics equivalent &string[x] happens behind calculation: (&string + (x * sizeof(*string))) why specific notion when applied pointers. stands arrays decay pointer first element after all.
as code, have following string:
char s[]="%d%d%d"; and passed format string printf, 2 bytes afterwards, explicitly provides reference "%d%d"
therefore this:
printf(s+2,a,b,c); is later parsed as:
printf("%d%d",a,b,c); printf except 2 integers read , 3rd 1 - ignored.
Comments
Post a Comment