c - what will be the output of printf("%d","Hello"+1);? -


i running c program includes statement :

#include <stdio.h> #include <string.h> main() {   printf("%d","hello"+1);   } 

it gives 4196445 output.is correct..please explain logic

although "it undefined behavior according standard", here happens de-facto. please note it's general description, may different depending on platform (compiler, cpu architecture, operating system, mmu, standard-output controller, etc):

  • the compiler generates null-terminated string of characters ("hello"), , places in code-section (ro-data section more accurate) of program.

  • every time process created , executable image loaded memory (i.e., whenever run program), string containing characters 'h','e','l','l','o','\0' resides @ logical memory address 4196444. physical memory address of string can calculated adding value value of base-address register (although should of no concern you, program oblivious that).

  • since the logical address of string remain 4196444 throughout every execution of program, compiler can replace calculation of "hello"+1 constant value 4196445.

  • so can imagine instead of compiling printf("%d","hello"+1), compiler has compiled printf("%d",4196445). in fact, since "%d" string constant string located in code-section of program, replaced constant value.

  • btw, if using variable pointing "hello" string, compiler, unable determine value during compilation, generate code compute during runtime instead. computation performed using either stack or general-purpose registers (or possibly combination of both). here typical example of how value calculated through stack (which section of program - similar code-section, write-permission):

    • the value of variable pushed stack.

    • the value 1 pushed stack.

    • the first 2 elements popped stack , added.

    • the result pushed stack.

  • in case, when printf("%d","hello"+1) invoked:

    • the address of string "%d" pushed stack.

    • the address of string "hello" plus 1 pushed stack.

    • the program counter (or call - instruction pointer) jumps address of function printf in memory, , execution continues there.

    • for every % character in string pointed first argument passed function printf, function loads corresponding argument stack, , - based on type specified after % character - computes data printed.

    • finally, outcome sent screen (to more accurate, every character in outcome, standard-output interrupt generated, causing pc (program counter) / ip (instruction pointer) jump iv (interrupt vector), designated isr (interrupt service routine), function other function in code, invoked, , in turn writes input character fifo queue of standard-output controller).


as implied @hvd in 1 of comments below:

on 64-bit system, %d% truncate result of "hello"+1 64-bit value 32-bit value. using %lld fix that, of course - correct solution use %p.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -