A string consists of an array of characters and is delimited by double quotes. - C String

C examples for String:char array

Introduction

There is no string type in C.

Strings are commonly assigned to a character array as shown here.

char myString[] = "12";

Strings in C are terminated with a null character \0.

The null character is added automatically by the compiler for quoted strings.

The above statement can be written using regular array initialization syntax as follows.

char myString[3] = { '1', '2', '\0' };

To print a string the format specifier %s is used with the printf function.

%s outputs the literals of the string until the null character is encountered.

Demo Code

#include <stdio.h>
int main(void) {
    char myString[] = "12";
    printf("%s", myString); /* "Hi" */
}

Result

A char pointer may be set to point to a string.

Demo Code

#include <stdio.h>
int main(void) {
    char* ptr = "Hi";
    printf("%s", ptr); /* "Hi" */
}

Result


Related Tutorials