Minggu, Mei 18, 2014

Define Value/Content of String Variable Mothode 3

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 B C D E \0
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:
  • index-0 is A
  • index-1 is B
  • index-2 is C
  • index-3 is D
  • index-4 is E
  • index-5 is 0
Other syntax code to define string value ABCDE is:

char name_of_string [] = {'A','B','C','D','E','\0'};

Example of defining string variable value/string variable content on C programming language syntax:
  1. Open Dev C++
  2. File menu > New > Source File, then will be created a new worksheet titled Untitled1
  3. 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
  4. First, type minimal required syntax on C programming:

    main()
    {
    }


  5. Add getch() function in main() function to show program result while be Run:

    main()
    {
      getch();
    }


  6. 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();
    }


  7. 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!
  8. 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();
    }

Reference:
https://lecturer.eepis-its.edu

Hopefully useful... : )

0 komentar: