Senin, Mei 26, 2014

Show Some Variable Value In One printf() Function

Example we have program syntax:

main()
{
char name_of_char;
  name_of_char = 'S';
  char *name_of_string1 = "Thariq bin Ziyad";
  char name_of_string2 [] = "Learn C programming language";
  char name_of_string3 [] = {'A','B','C','D','E','\0'};
  getch();
}


There are 4 variable on program syntx above, are 1 char variable (name_of_char) and 3 string variable (name_of_string1, name_of_string2, name_of_string3)

  • Show value of char variable name_of_char

    main()
    {
    char name_of_char;
      name_of_char = 'S';
      char *name_of_string1 = "Thariq bin Ziyad";
      char name_of_string2 [] = "Learn C programming language";
      char name_of_string3 [] = {'A','B','C','D','E','\0'};
      printf("%c", name_of_char);
      getch();
    }


  • Show value of char variable name_of_char and string variable name_of_string by using two printf() functions

    main()
    {
    char name_of_char;
      name_of_char = 'S';
      char *name_of_string1 = "Thariq bin Ziyad";
      char name_of_string2 [] = "Learn C programming language";
      char name_of_string3 [] = {'A','B','C','D','E','\0'};
      printf("%c", name_of_char); //first printf() function
      printf("%s", name_of_string1); //second printf() function
      getch();
    }


    Show value pf char variable name_of_char and string variable name_of_string1 can also only by using one printf() function

    main()
    {
    char name_of_char;
      name_of_char = 'S';
      char *name_of_string1 = "Thariq bin Ziyad";
      char name_of_string2 [] = "Learn C programming language";
      char name_of_string3 [] = {'A','B','C','D','E','\0'};
      printf("%c %s", name_of_char, name_of_string1);
      getch();
    }


  • Show value of four variable in one printf() function

    main()
    {
    char name_of_char;
      name_of_char = 'S';
      char *name_of_string1 = "Thariq bin Ziyad";
      char name_of_string2 [] = "Learn C programming language";
      char name_of_string3 [] = {'A','B','C','D','E','\0'};
      printf("%c %s %s %s", name_of_char, name_of_string1, name_of_string2, name_of_string3);
      getch();
    }


  • If want to separate its value becomes every line, enought added \n:

    main()
    {
    char name_of_char;
      name_of_char = 'S';
      char *name_of_string1 = "Thariq bin Ziyad";
      char name_of_string2 [] = "Learn C programming language";
      char name_of_string3 [] = {'A','B','C','D','E','\0'};
      printf("%c \n%s \n%s \n%s", name_of_char, name_of_string1, name_of_string2, name_of_string3);
      getch();
    }


As much as this, hopefully useful... ^_^

0 komentar: