Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- void string_function(){
- int i;
- char my_string[12]="hello world";
- //this character array (my_string) is 12 characters long
- //"hello" is 5 characters, " " (the space) is 1 character
- //"world" is 5 characters. 5+1+5=11, not 12, so where is the last character?
- //the last character is a 0 (or "\0") automatically added at the end of the character array
- //I will now print out the string with it's corresponding ascii value http://www.asciitable.com/index/asciifull.gif
- printf("%s \n\n",my_string);
- //this is what the string looks like normally
- for (i=0; i<12; i++) {
- printf("[%c] [%d] \n",my_string[i],my_string[i]);
- }
- printf("\n");
- //because of the fact that there is a 0 at the end of every string, you don't have to worry about the length of the string
- //this time I will print out characters until I hit the "\0" at the end, (because that is the end of the string)
- i=0;
- printf("\n");
- while (my_string[i]!='\0') {
- printf("%c",my_string[i]);
- i++;
- }
- printf("\n");
- }
- int main () {
- string_function();
- }
Advertisement
Add Comment
Please, Sign In to add comment