[source code] C Programming Tutorial - 8 - String Terminator

+4 Bucky Roberts · August 4, 2014



  • Behind the scenes, C adds a zero to the end of Strings so it knows where they end

  • Called the "String terminator"

  • Length of strings = characters + whitespaces + string terminator

  • "bucky roberts" uses 14 bytes on your computer




#include <stdio.h>
#include <stdlib.h>

int main()
{
    char name[14] = "Bucky Roberts";
    printf("My name is %s \n", name);

    //you can access individual elements by using subscripts
    //in programming, all array start with 0, not 1
    name[2] = 'z';
    printf("My name is %s \n", name);

    //when you define an array and set its value on the same line, you don't need to say how many elements
    char food[] = "tuna";
    printf("I love to eat %s \n", food);

    //if you want to assign a new string to an array, you have to use strcpy()
    strcpy(food, "bacon");
    printf("I love to eat %s \n", food);

    return 0;
}


Post a Reply

Replies

Oldest  Newest  Rating
+2 Franz Schmidt · September 17, 2014
example:
strcpy("Johnny", "Smith") => "Smith"
strcat("Johnny", "Smith") => "JohnnySmith"

Forgot to change strcpy to strcat.
+2 Mathias Frits Rørvik · September 17, 2014
strcpy(x,y) puts whatever is in y into x
strcat(x, y) extends x with y

example:
    strcpy("Johnny", "Smith") => "Smith"
    strcat("Johnny", "Smith") => "JohnnySmith"

They do two completely different things.

edit: fixed.
+2 Amie . · September 17, 2014
Hey Bucky i have a doubt here u use 

char food[] = "tuna";
printf("I love to eat %s \n", food);


strcpy(food, "bacon");
printf("I love to eat %s \n", food);

on later on video u use strcat 

so can u tell me what is the difference between these(  strcpy & strcat ) two(i noe this one is a silly question then also i want to know ???

Thanx in advance :-)
  • 1

C

107,222 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator