Output of C Program | Set 24

Predict the output of following C programs:

Difficulty Level: Rookie

Question 1



filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
int main()
{
    int arr[] = {10, 20, 30, 40, 50, 60};
    int *ptr1 = arr;
    int *ptr2 = arr + 5;
    printf ("ptr2 - ptr1 = %d\n", ptr2 - ptr1);
    printf ("(char*)ptr2 - (char*) ptr1 = %d",  (char*)ptr2 - (char*)ptr1);
    getchar();
    return 0;
}

chevron_right


Output:

  ptr2 - ptr1 = 5
  (char*)ptr2 - (char*) ptr1 = 20

In C, array name gives address of the first element in the array. So when we do ptr1 = arr, ptr1 starts pointing to address of first element of arr. Since array elements are accessed using pointer arithmetic, arr + 5 is a valid expression and gives the address of 6th element. Predicting value ptr2 – ptr1 is easy, it gives 5 as there are 5 inetegers between these two addresses. When we do (char *)ptr2, ptr2 is typecasted to char pointer. In expression “(int*)ptr2 – (int*)ptr1”, pointer arithmetic happens considering character poitners. Since size of a character is one byte, we get 5*sizeof(int) (which is 20) as difference of two pointers.
As an excercise, predict the output of following program.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
int main()
{
    char arr[] = "geeksforgeeks";
    char *ptr1 = arr;
    char *ptr2 = ptr1 + 3;
    printf ("ptr2 - ptr1 = %d\n", ptr2 - ptr1);
    printf ("(int*)ptr2 - (int*) ptr1 = %d",  (int*)ptr2 - (int*)ptr1);
    getchar();
    return 0;
}

chevron_right




Question 2

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
  
int main()
{
  char arr[] = "geeks\0 for geeks";
  char *str = "geeks\0 for geeks";
  printf ("arr = %s, sizeof(arr) = %d \n", arr, sizeof(arr));
  printf ("str = %s, sizeof(str) = %d", str, sizeof(str));
  getchar();
  return 0;
}

chevron_right


Output:

  arr = geeks, sizeof(arr) = 17
  str = geeks, sizeof(str) = 4

Let us first talk about first output “arr = geeks”. When %s is used to print a string, printf starts from the first character at given address and keeps printing characters until it sees a string termination character, so we get “arr = geeks” as there is a \0 after geeks in arr[].
Now let us talk about output “sizeof(arr) = 17”. When a character array is initialized with a double quoted string and array size is not specified, compiler automatically allocates one extra space for string terminator ‘\0′ (See this Gfact), that is why size of arr is 17.
Explanation for printing “str = geeks” is same as printing “arr = geeks”. Talking about value of sizeof(str), str is just a pointer (not array), so we get size of a pointer as output.

Please write comments if you find above answer/explanation incorrect, or you want to share more information about the topic discussed above



My Personal Notes arrow_drop_up


Article Tags :

3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.