Learn to Code via Tutorials on Repl.it!

← Back to all posts
How Strings in C are made
Jakman (449)

Alright so lots of languages are interpreted from C such as Python or C# or Javascript and that peasant PHP. Here is how you make a string in C.

int main(){
   char mystring[] = "this is a string";
   return 0;
}

ok so lets explain this. the char keyword means character. It initializes character text data like "a" or "b" the name mystring is the variable. Since more than one character is not a character you have to make a collection or array of these characters. This is made by putting two square brackets next to the variable name. then you call the string name. it has to be called with more than one character or it will not work remember that it is now an array of characters.
that is how a whole string is made in C.
you can print it out in this format.

#include <stdio.h>
int main(){
   char mystring[] = "this is a string";
   printf("hello %s",mystring);
   return 0;
}

ok this prints it out.
Thanks for reading my ted talk. long live the C languages except objective C.

Comments
hotnewtop
Modestkit (0)

Thanks for teaching us, much appreciated.~ I am having a hard time with arrays here, myself. Can you tell me, however, why you don't have to specify the array's length in this case (such as mystring[10])? How come you don't have to specify it in this case?

Jakman (449)

@Modestkit thats just how the language works. In the example "mystring[10]" however you specify the amount to make sure that only ten chars are used. Specification is only used for memory reasons like if i wanted to only target arrays with x amount of storage for whatever you build. But the nonspecification is used because you dont have to specify. You can put as many chars as you want bro.

Modestkit (0)

@Jakman Ah, alright. Thanks for clarifying things.~

Jakman (449)

@Modestkit no problem bro. Never be afraid to ask anything. Confusion is natural but doesn't have to last.

geberel01 (0)

Like that! thanks!

theangryepicbanana (1697)

What's wrong with Objective C?

Jakman (449)

@theangryepicbanana Its abondened and left behind for swift. It also requires More lines than C to do the same as C (at times.). Havent seen an Objective C programmer since back in them days.

theangryepicbanana (1697)

@Jakman
1) It's dead not abandoned. Its github repo still gets updates on occasion.
2) Objective-C usually takes less code than normal C (as long as you are using the Foundation library) because it has automatic memory management.
3) Objective-C is still being used by companies that don't want to migrate to Swift, so it's not like the devs don't exist

Jakman (449)

@theangryepicbanana you do have a point. I just do not think it will survive long. I think it will die down like Java in web development. It will have been used and then a fragment.