Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

C string

#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);
}
 
PREVIOUS NEXT
Tagged: #C #string
ADD COMMENT
Topic
Name
8+8 =