Sabtu, Mei 03, 2014

Define Value/Content of Char Variable

Char derived from the word character, there are many characters that we know, begin from types of letters, numeral, or others symbol such as punctuation mark, etc.
  • a is valued as character consists of 1 character
  • 7 is valued as character consists of 1 character
  • abcd is valued as character consists of 4 characters
  • 6789 is valued as character consists of 4 characters
  • ab87 is valued as character consists of 4 characters
  • cumi1569 is valued as character consists of 8 characters

Syntax of char variableis:

char nama_char; //Define a variable as char variable
name_of_char = ' ' //Define content one character filled between single quotes mark


Example of using of char variable in which char variable value is defined on program 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 introduction to char and string variable part 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, so that program syntax be:

    main()
    {
      getch();
    }


  6. Define name of a variable as a char variable:

    main()
    {
      char name_of_char;
      getch();
    }


  7. Define content one character of char variable, example we fill with S letter character

    main()
    {
      char name_of_char;
      name_of_char = 'S';
      getch();
    }


  8. If we want to show character defined on program syntas, can be used printf() function with use code %c
    Code %c is code to show value of a char variable (gotten from initial of char, i.e. c) continued with type its char variable name

    main()
    {
      char name_of_char;
      name_of_char = 'S';
      printf("%c", name_of_char);
      getch();
    }


  9. For more interesting can be added senetece

    main()
    {
      char name_of_char;
      name_of_char = 'S';
      printf("Character from program syntax: %c", name_of_char);
      getch();
    }


  10. We can also add other char vriable

    main()
    {
      char name_of_char1;
      char name_of_char2;
      name_of_char1 = 'S';
      name_of_char2 = '2';
      printf("First character from program syntax: %c \n", name_of_char1);
      printf("Second character from program syntax: %c", name_of_char2);
      getch();
    }




Hopefully useful... :)

0 komentar: