Strings in C Language – BKNMU Junagadh | BCA Sem 1 Notes



BCA Sem 1  |  Unit 1  |  Computer Concepts & C Programming

Strings in C Language — BKNMU Junagadh

Declaration, Initialization, Memory Allocation, Null Character '\0', and String Functions

BKNMU Junagadh NEP 2020 C Strings string.h Library Null Character
In simple words: Unlike languages like Java or Python, C does not have a native primitive string data type. In C, a String is represented as a one-dimensional array of characters terminated by a special Null Character ('\0').
📖 Fundamental Concepts of C Strings

To master strings in C, you must understand three core aspects:

char str[10];

1. Character Array

A sequence of continuous memory locations storing single char values in order.

'\0' (ASCII 0)

2. Null Terminator

Signals the end of the string to the C compiler so functions know where to stop reading.

<string.h>

3. String Library

Header file offering built-in functions like strlen(), strcpy(), and strcat().

🎨 String Memory Representation in C

Memory Allocation for string "BKNMU" in char str[6]:

'B'
str[0]
'K'
str[1]
'N'
str[2]
'M'
str[3]
'U'
str[4]
'\0'
str[5]

Notice that a string of 5 characters requires an array size of at least 6 bytes to store the terminating null character '\0'!

1️⃣ Ways to Declare and Initialize Strings

Here are the two primary ways to initialize a character array in C:

Initialization MethodSyntax ExampleNull Character Handled ByDescription
String Literal Methodchar str[] = "BKNMU";Compiler (Automatically)Easiest and most common method. Compiler appends '\0'.
Character List Methodchar str[] = {'B','K','N','M','U','\0'};Programmer (Manually)Explicit character array. Must manually append '\0' at the end!
Fixed Size Allocationchar str[20] = "Junagadh";CompilerReserves 20 bytes; stores "Junagadh" and fills remaining with '\0'.
ℹ️ Why '\0' is Essential: Functions like printf("%s", str) keep printing characters starting from index 0 until they encounter '\0'. Without '\0', the program will print garbage characters from adjacent memory!
🔍 Essential string.h Library Functions
strlen(str)
  • Returns string length (excluding '\0')
  • Example: strlen("BKNMU")5
strcpy(dest, src)
  • Copies source string into destination array
  • Overwrites target contents
strcat(dest, src)
  • Appends (concatenates) source string onto destination
  • Modifies destination string
💻 Working Code Example — String Manipulation

Here is a complete C program demonstrating string creation, input/output, and built-in library functions:

#include <stdio.h> #include <string.h> // Required for string functions int main() { char college[30] = "BKNMU"; char city[] = " Junagadh"; // 1. Finding String Length int len = strlen(college); printf("College Name Length: %d\n", len); // 2. Concatenating Strings strcat(college, city); printf("Full Location: %s\n", college); // Output: BKNMU Junagadh return 0; }
💡 Exam Tip for Input: Standard scanf("%s", str) stops reading input as soon as it hits a space! To read strings containing spaces (like full names), use fgets(str, sizeof(str), stdin) instead.
🔄 Reading Strings with Spaces Safely
1

Avoid Unsafe scanf() For Multi-Word Input

Using scanf("%s", str) for "BKNMU Junagadh" will only store "BKNMU".

2

Use fgets() Function

Use fgets(name, 30, stdin); — it safely reads spaces and prevents buffer overflow.

3

Avoid Obsolete gets() Function

Never use gets() in modern C as it causes critical memory security vulnerabilities.

⚠️ Memory Rule: Always size your character array to be at least 1 byte larger than the longest text you intend to store to leave room for '\0'!
📝 Quick Revision — Key Exam Points
  • Definition: A string in C is a sequence of characters terminated by a null character ('\0').
  • Null Terminator: '\0' has an ASCII value of 0 and signals the end of string data.
  • Header File: Must include #include <string.h> to use functions like strlen, strcpy, strcat, strcmp.
  • Memory Size: Always allocate length + 1 bytes of storage.
  • Format Specifier: Use %s in printf() and scanf() for string I/O.
  • strcmp(s1, s2): Returns 0 if both strings are identical.

Post a Comment

Thanks for comment.

Previous Post Next Post