-1

why is the output coming as 1 and not the address of j i tried with every alphabets still it's giving 1 as output

how is this program not giving address of j and why is it giving 1 as output i tried with every letters as input still it's giving 1?

#include "stdio.h"
int main()
{
    char arr[100];
    printf("%d", scanf("%s", arr));
    /* Suppose that input value given
         for above scanf is "jje" */
    return 2;
}

shouldn't it give the address of 1st letter ie j

Blaze
  • 16,736
  • 2
  • 25
  • 44
  • 1
    Check a reference for [`scanf`](https://en.cppreference.com/w/c/io/fscanf) : its return value is the number of arguments assigned (ie. just 1 in your case assuming a successful read). – Sander De Dycker Sep 05 '19 at 10:09
  • *NEVER* print what you think to be addresses with `%d`. `%p` is better (at least for real pointers). – S.S. Anne Sep 05 '19 at 11:14

2 Answers2

2

No. You're printing the return value of scanf. This isn't any address, but (emphasis mine):

On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.

Since that scanf result successfully matches the %d, you get 1.

If you wanted to print that address, which is the address of arr, you could try

printf("%p", arr);
Blaze
  • 16,736
  • 2
  • 25
  • 44
1

This is because you are printing the return value of scanf() which is the number of successful matches scanf makes which in your case is 1 as you're capturing a single string value.

Suraj
  • 602
  • 3
  • 16