Names, addresses and messages are text, not numbers. C has no separate string type; it stores text in a character array. This topic covers strings and their library functions, and then completes the study of operators with the rules that decide the order in which an expression is evaluated.
Strings in C
Definition: String. A sequence of characters stored in a character array and terminated by the null character '\0'.
The null character, written '\0', has the code 0 and marks the end of the string. The declaration char name[6] = "ASHA"; stores four letters followed by the null character An array meant to hold a string of n characters must have at least n + 1 elements.
A string is displayed with printf("%s", name);. It is read with scanf("%s", name);, which reads one word and needs no &, because the name of an array already stands for its address.
String Functions
The header file <string.h> declares functions for common operations on strings. Table 4.11 lists the four most used.
Table 4.11: Common string functions
| Function | Purpose | Example and result |
|---|---|---|
strlen(s) |
Length of s, not counting the null character | strlen("ASHA") gives 4 |
strcpy(d, s) |
Copies s into d | d becomes a copy of s |
strcat(d, s) |
Joins s to the end of d | "AB" and "CD" give "ABCD" |
strcmp(a, b) |
Compares a and b | 0 when the two are equal |
Strings cannot be copied with = or compared with ==. The assignment d = s is not allowed for arrays, and a == b compares addresses, not contents. strcmp returns 0 for equal strings, a negative value when the first comes earlier in dictionary order, and a positive value when it comes later. For strcpy and strcat, the destination array must be large enough to hold the result and its null character.
Other Operators
Besides the arithmetic, relational, logical, assignment, increment and decrement operators already studied, C has:
- the conditional operator
? :, as inmax = (a > b) ? a : b;, which chooses one of two values; - the sizeof operator, which gives the number of bytes occupied by a type or variable;
- the comma operator, which separates expressions, as in
for (i = 0, j = 9; i < j; i++, j--); - the compound assignment operators
+=,-=,*=,/=and%=.
Precedence and Associativity
Definition: Precedence. The rule that determines which of two different operators in an expression is applied first.
Definition: Associativity. The rule that determines the direction, left to right or right to left, in which operators of equal precedence are applied.
In 2 + 3 * 4, multiplication has higher precedence than addition, so the result is 14, not 20. In 20 / 5 * 2, the two operators have equal precedence and associate from left to right, so the result is (20 / 5) × 2 = 8. Table 4.12 lists the common operators from highest to lowest precedence.
Table 4.12: Precedence and associativity of common operators (highest first)
| Level | Operators | Associativity |
|---|---|---|
| 1 | () |
Left to right |
| 2 | !, ++, --, unary -, sizeof |
Right to left |
| 3 | *, /, % |
Left to right |
| 4 | +, - |
Left to right |
| 5 | <, <=, >, >= |
Left to right |
| 6 | ==, != |
Left to right |
| 7 | && |
Left to right |
| 8 | || |
Left to right |
| 9 | ? : |
Right to left |
| 10 | =, +=, -=, *=, /=, %= |
Right to left |
Parentheses have the highest precedence. They should be used freely, both to change the order of evaluation and to make the intended order plain to the reader.
Example 4.11: String functions and expression evaluation
Problem: Use the string functions on the names "Ravi" and "Kumar", and evaluate three expressions to show precedence and associativity.
Solution:
- Find the length of the first name, join the two names, and compare two strings.
- Evaluate
2 + 3 * 4,(2 + 3) * 4and20 / 5 * 2.
#include <stdio.h>
#include <string.h>
int main(void)
{
char first[20] = "Ravi";
char last[10] = "Kumar";
char full[30];
printf("Length of %s = %d\n", first, (int)strlen(first));
strcpy(full, first);
strcat(full, " ");
strcat(full, last);
printf("Full name = %s\n", full);
printf("Compare with Ravi = %d\n", strcmp(first, "Ravi"));
printf("2 + 3 * 4 = %d\n", 2 + 3 * 4);
printf("(2 + 3) * 4 = %d\n", (2 + 3) * 4);
printf("20 / 5 * 2 = %d\n", 20 / 5 * 2);
return 0;
}
Output:
Length of Ravi = 4
Full name = Ravi Kumar
Compare with Ravi = 0
2 + 3 * 4 = 14
(2 + 3) * 4 = 20
20 / 5 * 2 = 8
Result: The array full has 30 elements, enough for "Ravi Kumar" and its null character. The three expressions give 14, 20 and 8, as the rules of precedence and associativity predict.
Common mistakes
- Declaring a character array with no room for the null character.
- Comparing two strings with
==instead ofstrcmp(). - Relying on memory for precedence in a complicated expression instead of using parentheses.
Key takeaways
- A string is a character array ending with the null character '\0'.
strlen,strcpy,strcatandstrcmpare declared in<string.h>.- Precedence decides which operator is applied first; associativity decides the direction among equals.
- Parentheses override precedence and make expressions clearer.
Practice questions
- How many array elements are needed to store the string "INDIA"?
- What does strcmp() return when two strings are equal?
- Evaluate 10 - 4 - 3 and state the rule used.
Answers to the practice questions
- Six: five letters and the null character.
-
- The value is 3. The two operators have equal precedence and associate from left to right: (10 − 4) − 3.
Sources
- Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit IV
