Output of C Program | Set 24
Last Updated :
22 Feb, 2022
Predict the output of following C programs:
Difficulty Level: Rookie
Question 1
C
#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;
}
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 integers 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 pointers. Since size of a character is one byte, we get 5*sizeof(int) (which is 20) as difference of two pointers.
As an exercise, predict the output of following program.
C
#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;
}
Question 2
C
#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;
}
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 (which can also be equal to 8 in case you are using a 64 bit machine).
Please write comments if you find above answer/explanation incorrect, or you want to share more information about the topic discussed above
Similar Reads
Output of C Program | Set 27 Predict the output of following C programs. Question 1 C #include <stdio.h> int main(void) { int i; int power_of_ten[5] = { 00001, 00010, 00100, 01000, 10000, }; for (i = 0; i < 5; ++i) printf("%d ", power_of_ten[i]); printf("\n"); return 0; } In the above example, we ha
3 min read
Output of C Program | Set 21 Predict the output of following C programs. Question 1 C #include<stdio.h> #define fun (x) (x)*10 int main() { int t = fun(5); int i; for(i = 0; i < t; i++) printf("GeeksforGeeks\n"); return 0; } Output: Compiler Error There is an extra space in macro declaration which causes fun
2 min read
Output of C Programs | Set 2 Predict the output of below programs. Question 1 c #include<stdio.h> char *getString() { char str[] = "Will I be printed?"; return str; } int main() { printf("%s", getString()); getchar(); } Output: Some garbage value The above program doesn't work because array variables a
2 min read
Output of C Program | Set 26 Predict the output of following C programs. Question 1 C #include <stdio.h> int main() { int arr[] = {}; printf("%d", sizeof(arr)); return 0; } Output: 0 C (or C++) allows arrays of size 0. When an array is declared with empty initialization list, size of the array becomes 0. Questio
2 min read
Output of C Program | Set 20 Predict the outputs of following C programs. Question 1 C int main() { int x = 10; static int y = x; if(x == y) printf("Equal"); else if(x > y) printf("Greater"); else printf("Less"); getchar(); return 0; } Output: Compiler Error In C, static variables can only be in
2 min read
Output of C Program | Set 29 Question 1 How many times main() will get called? C main() { printf("\n main Called Again"); main(); } Answer : Infinite loop Description : There is no condition in the main() to stop the recursive calling of the main() hence it will be called infinite no of times. Question 2 Guess the out
6 min read