SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Strings And Vectors
Exercise:
Self-test Exercises
Question:1 | ISBN:9780321531346 | Edition: 7

Question

Which of the following declarations are equivalent?

char string_var[10] = "Hello";

char string_var[10] = {'H', 'e', 'l', 'l', 'o', '\0'};

char string_var[10] = {'H', 'e', 'l', 'l', 'o'};

char string_var[6] = "Hello";

char string_var[] = "Hello";

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Consider the declarations:

char string_var[10] = "Hello";:  Initializes the string "the easy way."

char string_var[10] = {'H', 'e', 'l', 'l', 'o', '\0'};: It is equivalent to the first string,

char string_var[10] = {'H', 'e', 'l', 'l', 'o'};: It is an array of characters, not a string. It is unique.

char string_var[6] = "Hello"; It creates a string with room for exactly the 5 characters in "Hello" and the null terminator

char string_var[] = "Hello";: It same as creates a string with room for exactly the 5 characters in "Hello" and the null terminator

0 0

Discussions

Post the discussion to improve the above solution.