Senin, Mei 12, 2014

Define Value/Content of String Variable Methode 1 On C Programming Language

Based on information from http://technohitech.blogspot.com/, on c language, string is array of char variable. It means, if char consist of more than one digits then that array will be rated as string variable. So for define string variable still use char variable, but to show value of a char variable used code %c, as while to show value of a string variable used code %s which initial of string word.

On this section will be explained how to define string value/string content on program syntax using methode 1, please check it out!

Syntax of string variable on C programming language:

char *name_of_char = " ";

Use asterisk mark before name of char variable. Fill string is a character or array of character between double quotes mark

Example of using of string variable in which its value is defined on program syntax (Methode 1):

  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 1.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 name of a variable as a string variable in which before the string variable name is given asterisk mark, and then define string value directly:

    main()
    {
      char *name_of_string = "Thariq bin Ziyad";
      getch();
    }


  7. Please Compile&Run and program has been OK
  8. If we want to show string content, then can be used printf() function with using code %s::

    main()
    {
      char *name_of_string = "Thariq bin Ziyad";
      printf("%s", name_of_string);
      getch();
    }


  9. For more interesting can be added sentence:

    main()
    {
      char *name_of_string = "Thariq bin Ziyad";
      printf("String is defined on program syntax is: %s", name_of_string);
      getch();
    }
  10. We can also add others string variable which its value is defined directly on program syntax:

    main()
    {
      char *name_of_string = "Thariq bin Ziyad";
      char *name_of_string2 = "Learning C Programming Language";
      printf("First String content is defined on program syntax is: %s\n\n", name_of_string);
      printf("Second String content is defined on program syntax is: %s", name_of_string2);
      getch();
    }


If there is something not understood please contact us... thank you : )

0 komentar: