🌟 C Strings Explained with Examples

C Strings in C Programming – Full Tutorial with Examples

In C programming, strings are used to store and work with text. However, unlike languages like Python or Java, C doesn’t have a built-in string data type. Instead, we use arrays of characters, terminated with a null character '\0'.

📌 What Is a String in C?

A string in C is a sequence of characters ending with '\0':

char city[] = "Dhaka";

This will store: 'D' 'h' 'a' 'k' 'a' '\0'

📤 How to Print a String

#include <stdio.h>

int main() {
    char city[] = "Dhaka";
    printf("%s\n", city);
    return 0;
}

đŸŽ¯ Accessing Individual Characters

#include <stdio.h>

int main() {
    char country[] = "Bangladesh";
    printf("First: %c\n", country[0]);
    printf("Fifth: %c\n", country[4]);
    return 0;
}

✏️ Modifying a String

#include <stdio.h>

int main() {
    char food[] = "Mango";
    food[0] = 'B';
    printf("%s\n", food); // Bango
    return 0;
}

🔁 Looping Through a String

Simple loop:

#include <stdio.h>

int main() {
    char name[] = "Ahsan";
    for (int i = 0; i < 5; i++) {
        printf("%c\n", name[i]);
    }
    return 0;
}

Loop with sizeof (more sustainable):

#include <stdio.h>

int main() {
    char name[] = "Ahsan";
    int len = sizeof(name) / sizeof(name[0]);
    for (int i = 0; i < len - 1; i++) {
        printf("%c\n", name[i]);
    }
    return 0;
}

🧱 Manual String Creation

#include <stdio.h>

int main() {
    char country[] = {'B', 'a', 'n', 'g', 'l', 'a', '\0'};
    printf("%s\n", country);
    return 0;
}

Note: '\0' is required to terminate the string properly.

⚖️ Compare Two Methods

#include <stdio.h>

int main() {
    char s1[] = {'H','e','l','l','o','\0'};
    char s2[] = "Hello";

    printf("%zu\n", sizeof(s1)); // 6
    printf("%zu\n", sizeof(s2)); // 6
    return 0;
}

🌟 Real-Life Example: Welcome Message

#include <stdio.h>

int main() {
    char greeting[] = "Hello";
    char name[] = "Amina";

    printf("%s, %s!\n", greeting, name); // Hello, Amina!
    return 0;
}

✅ Summary Table

FeatureDescription
String typeNo built-in type; use char[]
Null terminatorUse '\0' to mark end
Print stringUse %s in printf()
Access charactersUse indexing like string[0]
Modify stringAssign new char to index
Loopfor loop + sizeof
Manual stringUse char array + '\0'
C strings tutorial, how to declare strings in C, string character array in C, printing string in C, loop through string in C, modify C string, beginner C programming string examples, null character in C, manual string creation in C

C Strings – Special Characters and Escape Sequences

In C programming, strings are written within double quotes. But what happens if you need to include a quote " or backslash \ inside the string?

If you try this directly, it causes an error:

// Invalid – will cause a compile error
char txt[] = "We are the so-called "Vikings" from the north.";

✅ The Solution: Escape Characters

C uses the backslash (\) as an escape character. It tells the compiler that the next character has a special meaning.

📋 Common Escape Sequences

Escape Sequence Result Description
\''Inserts a single quote
\""Inserts a double quote
\\\Inserts a backslash

đŸ§Ē Example: Double Quotes

char txt[] = "We are the so-called \"Vikings\" from the north.";
printf("%s\n", txt);

Output:

We are the so-called "Vikings" from the north.

đŸ§Ē Example: Single Quote

char txt[] = "It\'s alright.";
printf("%s\n", txt);

Output:

It's alright.

đŸ§Ē Example: Backslash

char txt[] = "The character \\ is called backslash.";
printf("%s\n", txt);

Output:

The character \ is called backslash.

✨ Other Popular Escape Characters

Escape Sequence Result Usage
\nNew LineMoves cursor to the next line
\tTabInserts a horizontal tab
\0NullMarks end of string (automatically added)

📚 Summary

  • Use \" to insert double quotes inside a string.
  • Use \' for single quotes.
  • Use \\ for backslash.
  • Use \n for new lines, and \t for tabs.
C string special characters, C escape sequences, C string double quote, backslash in string C, newline escape character, single quote in C string, C programming strings tutorial for beginners

C Programming – Getting User Input

In C, we use the scanf() function to accept input from the user. You’ve already seen how printf() displays output — now let's explore how to take input.

đŸ”ĸ Basic Input: Integer

This example takes a number from the user and prints it back:

#include <stdio.h>

int main() {
    int myNum;
    printf("Type a number: \n");
    scanf("%d", &myNum);
    printf("Your number is: %d", myNum);
    return 0;
}

Explanation:

  • %d is the format specifier for integers.
  • &myNum means "store the value at the address of myNum".

🧮 Multiple Inputs: Integer + Character

#include <stdio.h>

int main() {
    int myNum;
    char myChar;
    printf("Type a number AND a character: \n");
    scanf("%d %c", &myNum, &myChar);
    printf("Your number is: %d\n", myNum);
    printf("Your character is: %c\n", myChar);
    return 0;
}

This accepts two inputs in one line: an integer and a character.

đŸ“Ĩ String Input: Single Word

scanf() can read a single word (until a space or newline):

#include <stdio.h>

int main() {
    char firstName[30];
    printf("Enter your first name: \n");
    scanf("%s", firstName);
    printf("Hello %s", firstName);
    return 0;
}

Note: You do not need the reference operator & when scanning strings.

⚠️ Limitation of scanf() for Strings

scanf("%s", ...) only reads up to the first whitespace. So if the user types “John Doe”, only “John” is captured.

char fullName[30];
scanf("%s", fullName);  // Will only capture "John"

📋 Full Name Input: Using fgets()

To read a full line of text (including spaces), use fgets():

#include <stdio.h>

int main() {
    char fullName[30];
    printf("Type your full name: \n");
    fgets(fullName, sizeof(fullName), stdin);
    printf("Hello %s", fullName);
    return 0;
}

Explanation:

  • fgets() reads until newline or buffer limit.
  • sizeof(fullName) prevents buffer overflow.
  • It retains the newline \n, which you may want to remove using strcspn() or similar.

📌 Summary

Function Purpose Limitation
scanf() Reads formatted input (numbers, characters, single-word strings) Cannot read strings with spaces
fgets() Reads an entire line (including spaces) Includes newline character
C user input tutorial, scanf in C, fgets in C, how to take input in C, get string input in C, scanf string with spaces, beginner C programming input examples, input and output in C

āĻ•োāύ āĻŽāύ্āϤāĻŦ্āϝ āύেāχ:

āĻāĻ•āϟি āĻŽāύ্āϤāĻŦ্āϝ āĻĒোāϏ্āϟ āĻ•āϰুāύ