Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

C Strings Made Easy: Declaration, Input, Output, and Library Functions

What is a String in C?

A string in C is a contiguous sequence of characters terminated by the null character (\0). Although the language does not have a dedicated string type, a string is usually represented as an array of char values. The compiler automatically appends the null terminator when you initialize a string literal.

"Welcome to the world of programming!"

The standard library header <string.h> offers a wealth of functions for manipulating strings efficiently.

Declaring and Initializing Strings

Because C treats a string as a character array, you must specify the array size. The size defines the maximum number of characters (excluding the null terminator) that can be stored.

char string_variable_name[array_size];

Typical declarations:

char first_name[15];    /* holds up to 14 characters + \0 */
char last_name[15];

When you provide an initializer, the compiler counts the characters and automatically adds the null terminator. The array size may be omitted if you give an initializer.

char first_name[15] = "ANTHONY";
char first_name[]   = "NATHAN";   /* size inferred */
char string3[6]     = {'h','e','l','l','o','\0'};  /* explicit terminator */

Reading Strings from the User

Input functions vary in safety and behaviour:

Example using fgets:

#include <stdio.h>
int main() {
    char name[50];
    printf("Enter your full name: ");
    fgets(name, sizeof(name), stdin);
    printf("Your name is %s", name);
    return 0;
}

Printing Strings

The classic way to display a string is printf("%s", name);. Two convenient alternatives are:

Example with puts:

#include <stdio.h>
int main() {
    char greeting[] = "Hello, world!";
    puts(greeting);
    return 0;
}

The String Library (<string.h>)

Commonly used functions:

FunctionDescription
strlenReturns the length of a string (excluding the null terminator).
strcatAppends one string to another.
strcmpCompares two strings lexicographically.
strcpyCopies a string.
strncmpCompares up to n characters.
strncpyCopies up to n characters.
strchrFinds the first occurrence of a character.
strrchrFinds the last occurrence of a character.
strstrFinds a substring.
strncatConcatenates up to n characters.
strlwrConverts to lower case (non‑standard).
struprConverts to upper case (non‑standard).
strrevReverses a string (non‑standard).

Illustrative program:

#include <stdio.h>
#include <string.h>
int main() {
    char str1[20] = "Hello";
    char str2[20] = " World!";
    char result[40];

    /* Comparison */
    if (strcmp(str1, str2) == 0)
        puts("Strings are equal");
    else
        puts("Strings are not equal");

    /* Concatenation */
    strcat(str1, str2);  // str1 now contains "Hello World!"
    printf("Concatenated: %s\n", str1);

    /* Length */
    printf("Length of str1: %zu\n", strlen(str1));

    /* Copy */
    strcpy(result, str1);
    printf("Copied string: %s\n", result);
    return 0;
}

Converting a String to a Number

Standard conversions:

Example with atoi:

#include <stdio.h>
#include <stdlib.h>
int main() {
    char number_str[20];
    printf("Enter a number: ");
    fgets(number_str, sizeof(number_str), stdin);
    int num = atoi(number_str);
    printf("You entered %d\n", num);
    return 0;
}

Key Takeaways

C Language

  1. C Programming Strings: Mastering Declaration, Initialization, and I/O
  2. Master Python Strings: Creation, Formatting, and Manipulation
  3. Mastering Java Strings: Creation, Methods, and Best Practices
  4. Java String length() Method: How to Get a String’s Size (Example)
  5. Mastering Java's split() Method: A Practical Guide with Code Examples
  6. Python Variables: Declaring and Managing String Types
  7. C Strings: How Null-Terminated Character Arrays Work
  8. Mastering C++ Strings: C-Style vs std::string
  9. Mastering Strings in C#: Best Practices & Tips
  10. Mastering Python Strings: Creation, Access, and Manipulation