C Programming - Strings

Exercise :: Strings - Yes / No Questions

1. 

Will the program compile successfully?

#include<stdio.h>

int main()
{
    char a[] = "India";
    char *p = "BIX";
    a = "BIX";
    p = "India";
    printf("%s %s\n", a, p);
    return 0;
}

A. Yes
B. No

Answer: Option B

Explanation:

Because we can assign a new string to a pointer but not to an array a.

2. 

For the following statements will arr[3] and ptr[3] fetch the same character?
char arr[] = "IndiaBIX";
char *ptr = "IndiaBIX";

A. Yes
B. No

Answer: Option A

Explanation:

Yes, both the statements prints the same character 'i'.


3. 

Is there any difference between the two statements?
char *ch = "IndiaBIX";
char ch[] = "IndiaBIX";

A. Yes
B. No

Answer: Option A

Explanation:

In first statement the character pointer ch stores the address of the string "IndiaBIX".
The second statement specifies the space for 7 characters be allocated and that the name of location is ch.