#include <stdio.h>
#include <stdlib.h>
#include <string.h> // string.h adds a lot of pre-defined functions
int main() {
// There are 2 ways to define a basic string in C:
char string1[] = "This is a string!";
// Notice that here, I use the brackets [] to tell C that this is a
// char array.
// I can also define a string this way:
char* string2 = "This is another string!";
// If I didn't want to ititialize the string yet:
char string3[10];
// This creates a string 10 bytes long, with no value initialized yet.
// Another way to do this is by using malloc/calloc:
char* string4 = malloc(10);
// However, with this method, I would have to free the string later,
// because it is stored on the heap:
free(string4);
}
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MAX_LENGTH 100
#define NUM_STRINGS 10
int main(){
char arr[NUM_STRINGS][MAX_LENGTH] = {""};
arr2[0] = "string literal"; // Not permitted
strcpy(arr[0], "hello world");
printf("%s
", arr[0]);
printf("%s
", strcpy(arr[0], "hello world"));
exit(EXIT_SUCCESS);
}
1. char str[] = "GeeksforGeeks";
2. char str[50] = "GeeksforGeeks";
3. char str[] = {'G','e','e','k','s','f','o','r','G','e','e','k','s',' '};
4. char str[14] = {'G','e','e','k','s','f','o','r','G','e','e','k','s',' '};
// C program to illustrate strings
#include <stdio.h>
#include <string.h>
int main()
{
// declare and initialize string
char str[] = "Geeks";
// print string
printf("%s
", str);
int length = 0;
length = strlen(str);
// displaying the length of string
printf("Length of string str is %d", length);
return 0;
}