C Strings

1. What is a String in C?

  • In C, a string is an array of characters terminated by a null character \0.

  • The null character \0 marks the end of the string.

Example of a string:

char name[10] = "Alice";

Here, the memory contains: A l i c e \0.


2. Declaring Strings

1D Character Array

char name[20]; // declaration only
char name[20] = "Bob"; // declaration with initialization

Pointer to String

char *name = "Charlie";

Using a pointer is simpler but cannot modify string literal.


3. Input and Output Strings

Printing Strings

#include <stdio.h>

int main() {
char name[] = "Alice";
printf("Name: %s\n", name);
return 0;
}

Output:

Name: Alice
  • Use %s format specifier in printf to print strings.


Reading Strings

#include <stdio.h>

int main() {
char name[50];
printf("Enter your name: ");
scanf("%s", name); // stops at space
printf("Hello, %s!\n", name);
return 0;
}

Input/Output:

Enter your name: Alice
Hello, Alice!

Note: scanf("%s") cannot read strings with spaces.


Reading Strings with Spaces Using fgets

#include <stdio.h>

int main() {
char name[50];
printf("Enter your full name: ");
fgets(name, sizeof(name), stdin); // reads line including spaces
printf("Hello, %s", name); // fgets keeps newline
return 0;
}

Input/Output:

Enter your full name: Alice Johnson
Hello, Alice Johnson

4. Common String Functions (<string.h>)

Function Description
strlen(str) Returns length of string (without \0)
strcpy(dest, src) Copies src into dest
strcat(dest, src) Appends src to the end of dest
strcmp(str1, str2) Compares two strings (0 if equal)
strncpy(dest, src, n) Copies first n characters from src to dest
strncat(dest, src, n) Appends first n characters of src to dest

Example:

#include <stdio.h>
#include <string.h>

int main() {
char str1[20] = "Hello";
char str2[20] = "World";

strcat(str1, str2); // str1 = "HelloWorld"
printf("%s\n", str1);

printf("Length: %lu\n", strlen(str1));

return 0;
}

Output:

HelloWorld
Length: 10

5. Key Points About Strings in C

  1. Strings are character arrays ending with \0.

  2. Use %s for printing and scanf/fgets for input.

  3. Include <string.h> to use built-in string functions.

  4. Avoid exceeding array size → buffer overflow.

  5. C strings are mutable if stored in a character array, but string literals via pointer cannot be modified safely.


6. Real-Life Example: Greeting Program

#include <stdio.h>
#include <string.h>

int main() {
char name[50];
printf("Enter your name: ");
fgets(name, sizeof(name), stdin);

// Remove newline from fgets
name[strcspn(name, "\n")] = '\0';

printf("Welcome, %s!\n", name);
return 0;
}

Sample Input/Output:

Enter your name: Alice Johnson
Welcome, Alice Johnson!

Real-life use: Reading user input, names, addresses, or text messages.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *