On this section will be expalined how to define string variable value/string variable content on C programming language by using methode 3, please check it out!
A string constant is written by beginning and ending double quotes, example:
"ABCD"
The string value is saved in memory sequentially with composition as follows:
A character will occupy memory of 1 byte. Last byte will contain NULL (\0) character automatically. By knowing that a string is ended NULL value, then end of a string value will be detected. As a array of character, first character of string value have index-0, second character have index-1, and so on.
Example:
If value of a string is ABCDE, then:
char name_of_string [] = {'A','B','C','D','E','\0'};
Example of defining string variable value/string variable content on C programming language syntax:
https://lecturer.eepis-its.edu
Hopefully useful... : )
A string constant is written by beginning and ending double quotes, example:
"ABCD"
The string value is saved in memory sequentially with composition as follows:
A | B | C | D | E | \0 |
Example:
If value of a string is ABCDE, then:
- index-0 is A
- index-1 is B
- index-2 is C
- index-3 is D
- index-4 is E
- index-5 is 0
char name_of_string [] = {'A','B','C','D','E','\0'};
Example of defining string variable value/string variable content on C programming language syntax:
- Open Dev C++
- File menu > New > Source File, then will be created a new worksheet titled Untitled1
- File menu > Save As. On Save as type: choose C source files (*.c) and on File name, example we give name define string methode 3.c
- First, type minimal required syntax on C programming:
main()
{
} - Add getch() function in main() function to show program result while be Run:
main()
{
getch();
} - Define a name of string variable in which after the string variable is given mark [ ] and defined its string value directly
main()
{
char name_of_string [] = {'A','B','C','D','E','\0'};
getch();
} - To show string value has been defined on syntax program, use printf() function without using code %s:
main()
{
char name_of_string [] = {'A','B','C','D','E','\0'};
printf(name_of_string);
getch();
}
If we try to cut NULL character, so that program syntax becomes:
main()
{
char name_of_string [] = {'A','B','C','D','E'};
printf(name_of_string);
getch();
}
than its result will occur ERROR, please try!
- We can add other string variable:
main()
{
char name_of_string1 [] = {'A','B','C','D','E','\0'};
char name_of_string2 [] = {'L','e','a','r','n','\0'};
printf("%s\n", name_of_string1);
printf(name_of_string2);
getch();
}
https://lecturer.eepis-its.edu
Hopefully useful... : )
0 komentar:
Posting Komentar