how to define a string in c code example

Example 1: string in c

#include <stdio.h>

int main(void) {
  char name[] = "Harry Potter";

  printf("%c", *name);     // Output: H
  printf("%c", *(name+1));   // Output: a
  printf("%c", *(name+7));   // Output: o

  char *namePtr;

  namePtr = name;
  printf("%c", *namePtr);     // Output: H
  printf("%c", *(namePtr+1));   // Output: a
  printf("%c", *(namePtr+7));   // Output: o
}

Example 2: how to create a string in c

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
char greeting[] = "Hello";

Example 3: declare string in c

char string[20];

Example 4: string in c programming

#include <stdio.h>
int main(){
    char name[10];
    printf("What is your name: \n");
    scanf("%s", name);
    printf("Hello, %s!\n", name);
}

Example 5: how to store string in c

char str[4] = "GfG"; /*One extra for string terminator*/

Tags:

C Example