C

C Programming

Master the fundamentals of C programming with comprehensive tutorials, examples, and hands-on exercises

Strings in C

In C, strings are arrays of characters terminated by a null character (\0). C provides various functions to manipulate strings through the string.h library.

String Declaration and Initialization

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

int main() {
    // Different ways to declare and initialize strings
    char name1[20] = "John";
    char name2[] = "Alice";
    char name3[20];
    
    // Input string from user
    printf("Enter your name: ");
    scanf("%s", name3);  // Note: no & needed for strings
    
    // Display strings
    printf("Name 1: %s\n", name1);
    printf("Name 2: %s\n", name2);
    printf("Name 3: %s\n", name3);
    
    // String length
    printf("Length of name1: %lu\n", strlen(name1));
    
    return 0;
}

String Functions

Common String Operations
#include <stdio.h>
#include <string.h>

int main() {
    char str1[50] = "Hello";
    char str2[50] = "World";
    char str3[50];
    
    // String copy
    strcpy(str3, str1);
    printf("After strcpy: %s\n", str3);
    
    // String concatenation
    strcat(str1, " ");
    strcat(str1, str2);
    printf("After strcat: %s\n", str1);
    
    // String comparison
    if (strcmp(str2, "World") == 0) {
        printf("Strings are equal\n");
    }
    
    // String length
    printf("Length of str1: %lu\n", strlen(str1));
    
    return 0;
}

Common String Functions

Function Purpose Example
strlen()Get string lengthstrlen("Hello") returns 5
strcpy()Copy stringstrcpy(dest, src)
strcat()Concatenate stringsstrcat(str1, str2)
strcmp()Compare stringsstrcmp(str1, str2)
strchr()Find characterstrchr(str, 'a')
strstr()Find substringstrstr(str, "sub")
Important
  • Always ensure destination arrays are large enough
  • Remember the null terminator \0
  • Use fgets() instead of gets() for safety
  • Include <string.h> for string functions

Frequently Asked Questions

C is a powerful, general-purpose programming language that's been around since the 1970s. It's the foundation for many modern languages and is essential for system programming, embedded systems, and understanding how computers work at a low level. Learning C gives you a solid foundation in programming fundamentals.

No prior programming experience is required! Our C programming tutorial is designed for complete beginners. We start with the basics and gradually progress to more advanced topics. However, having some basic computer literacy and logical thinking skills will be helpful.

You need a C compiler to run C programs. For beginners, we recommend using our online C compiler which requires no installation. For local development, you can use GCC (GNU Compiler Collection) which is available on Windows (via MinGW), macOS (via Xcode), and Linux. IDEs like Code::Blocks, Dev-C++, or Visual Studio Code are also helpful.

The time to learn C depends on your dedication and prior experience. With consistent practice (1-2 hours daily), you can grasp the basics in 2-4 weeks and become proficient in 2-3 months. Mastering advanced concepts like pointers, memory management, and data structures may take 6-12 months of regular practice.

C programming skills open doors to various career paths including system programming, embedded systems development, operating system development, device driver programming, game development, and firmware development. Many companies in automotive, aerospace, telecommunications, and IoT sectors actively seek C programmers.

Absolutely! C remains highly relevant and is consistently ranked among the top programming languages. It's essential for system programming, embedded systems, IoT devices, and performance-critical applications. Many modern languages are built on C, and understanding C helps you become a better programmer overall.