What is a string in C?
A
An integer array B
A character array ending with a null character '\0' C
A built-in string object D
A memory pointer only
Analysis & Theory
In C, a string is an array of characters terminated by a null character `\0`.
How do you declare a string in C?
A
char str = "Hello"; B
string str = "Hello"; C
char str[] = "Hello"; D
char str = {'H','e','l','l','o'};
Analysis & Theory
Correct declaration is `char str[] = "Hello";`
What is the length of the string `char str[] = "Hi";`?
A
1 B
2 C
3 D
Undefined
Analysis & Theory
It has 2 characters + 1 null character, so the array size is 3. `strlen(str)` returns 2.
What does the `strlen()` function do?
A
Returns size of array B
Returns length of string excluding null character C
Returns length including `\0` D
Compares two strings
Analysis & Theory
`strlen()` returns the number of characters in a string, not counting the null terminator.
What is the output?
```c
char str[] = "C Programming";
printf("%c", str[2]);
```
A
C B
P C
r D
Analysis & Theory
`str[2]` accesses the 3rd character which is a space (' ').
Which function is used to copy strings in C?
A
copy() B
strcpy() C
strcat() D
assign()
Analysis & Theory
`strcpy(destination, source)` copies the source string into the destination.
What does `strcmp(str1, str2)` return when strings are equal?
A
1 B
0 C
-1 D
true
Analysis & Theory
`strcmp()` returns 0 when the two strings are equal.
Which of the following is true about string literals in C?
A
They are modifiable B
They are stored in read-only memory C
They can be assigned to `char[]` directly D
They include the `#` symbol
Analysis & Theory
String literals are stored in read-only memory and modifying them causes undefined behavior.
What is the output?
```c
char str[10] = "abc";
printf("%s", str);
```
A
abc B
abc\0 C
a D
abcabc
Analysis & Theory
The string `abc` is printed. The null terminator is not shown in output.
Which function is used to concatenate two strings in C?
A
strmerge() B
strjoin() C
strcat() D
addstr()
Analysis & Theory
`strcat(dest, src)` appends the source string to the destination string.