Class 16: Strings Flashcards
A character array terminated by null character
String
Anything enclosed in double quotation marks. Null character is automatically provided by compiler and stored in memory as character string.
String literal/string constant
Initializing a character array
char string [] = “Hi my name is Tanvi”;
char string2[60] = “Hi my name is Tanvi”
char car[10] = “Tata”;
car == ?
&car[0]
scanf(“%d %d”, &a, &b)
read two integers from the standard input. The space in the format string %d %d tells scanf to expect whitespace (spaces, tabs, or newlines/enters) between the two numbers. When you press Enter after typing the first number, it’s interpreted as whitespace, and scanf proceeds to read the second integer. The values are then stored in the integer variables a and b, respectively.
scanf(“%[^\n]s”, name);
The scanf(“%[^\n]s”, name) function reads an entire line, including spaces, until a newline (\n)
Strings which are array of characters
Read-write strings
Compiler will insert NULL terminator when we define string with “”
TRUE
Compiler will insert NULL terminator when we define a string as a array of characters
FALSE
String *string1 = “GO Bucks”;
Char string2[10] “Go Bucks”;
Which string can change?
String 1 can change because it is a pointer to string literal. “GoBucks” CANNOT
String2 cannot change because it is a constant pointer to char (name or array is always a pointer to the first element) however “Go Bucks” CAN.
In all but one of these options, the compiler will place a null terminating character at the end of the string
1. Char label1[5] = {‘t’,’a’,’n’,’u’};
2. Char lablel2[] = {‘t’,’a’,’n’,’u’}
3. Char label3[10] = “container”
4. char label4[] = “container”;
All declarations will be null terminated expect the lablel2 array.