Jumat, Mei 23, 2014

Define String Variable As Input

On this section will be pratciced, how to make string variable as input. When program is run, user of program will be asked to type input from PC keyboard. Make string variable as an input usually using gets() function or scanf() function

Example of using of string variable as input on C programming language:

  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 as input.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 string in which after the variable name is given mark [ ] and defined its maximum character, for example 5 character and 1 NULL character, so written [5], because its index begin from index-0, and last character (NULL) is recorded as index-5

    main()
    {
      char name_of_string[6];
      getch();
    }


    or can also added NULL character so that program syntax becomes:

    main()
    {
      char name_of_string[6] = {'\0'};
      getch();
    }


  7. Define string variable be an input by using gets() or scanf() function

    main()
    {
      char name_of_string[6];
      gets(name_of_string);
      getch();
    }


    Please try to be Compiled&Run, at this time we can already input/type string value, for example: we can type some character of letters, numbers, and others character, to exit from program Run window press Enter on PC keyboard.
  8. Give comment/information so that user knows when must he type input from PC keyboard when program is run.

    main()
    {
      char name_of_string[6];
      printf("Type string which maximum consist of 6 characters! ");
      gets(name_of_string);
      getch();
    }


  9. To show string value is inputted use printf() function

    main()
    {
      char name_of_string[6];
      printf("Type string which maximum consist of 6 characters! ");
      gets(name_of_string);
      printf("String is typed is: %s", name_of_string);
      getch();
    }


  10. We can add others string vriable

    main()
    {
      char name_of_string1[6];
      char name_of_string2[6];
      printf("Type first string which maximum consist of 6 characters! ");
      gets(name_of_string1);
      printf("Type second string which maximum consist of 6 characters! ");
      gets(name_of_string2);
      printf("\nFirst string is typed is: %s", name_of_string1);
      printf("\nSecond string is typed is: %s", name_of_string2);
      getch();
    }


    or with add NULL character and use scanf() function

    main()
    {
      char name_of_string1[6] = {'\0'};
      char name_of_string2[6] = {'\0'};
      printf("Type first string which maximum consist of 6 characters! ");
      scanf("%s", name_of_string1);
      printf("Type second string which maximum consist of 6 characters! ");
      scanf("%s", name_of_string2);
      printf("\nFirst string is typed is: %s", name_of_string1);
      printf("\nSecond string is typed is: %s", name_of_string2);
      getch();
    }


I still don't understand about amount of maximum of string character, for example like this:
  • If we have program syntax as follows:

    main()
    {
      char name_of_string[1];
      printf("Type string which maximum consist of 1 character! ");
      gets(name_of_string);
      printf("String is typed is: %s", name_of_string);
      getch();
    }


    If according to its explain, it means amount of maximum of string character is able to inputted is 1 character, and when program is run, when we input 2 to 4 characters (more than 1 character), program is still normal, please try. But if we type 5 characters, little ERROR has occur, moreover if we type 6 character or more, its ERROR is more severe
  • And if we change on program syntax so that amount of maximum of string character can be inputted becomes 4 characters, so that program syntax becomes:

    main()
    {
      char name_of_string[4];
      printf("ype string which maximum consist of 4 characters! ");
      gets(nama_string);
      printf("String is typed is: %s", name_of_string);
      getch();
    }


    If typed 1 to 7 characters, program is still normal, but if typed 8 characters, occur little ERROR, moreover if typed 9 charactersor more, its ERROR is more severe, please try

  • For who understand about this poin of amount of maximum of string variable, please share information, waited... thank you...

    Hopefully useful... : )

    0 komentar: