We will make syntax in which while program is run then program will ask user to input a character from keyboard PC. Example of using of char variable as input:
- Open Dev C++
- File menu > New > Source File, then will be created a new worksheet titled Untitled1
- 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 2.c
- First, type minimal required syntax on C programming:
main()
{
} - Add getch() function in main() function to show program result while be Run, so that program syntax be:
main()
{
getch();
} - Define name of a variable as a char variable:
main()
{
char name_of_char;
getch();
} - Define name_of_char variable as input with add syntax:
name_of_char = getchar();
so that program syntax becomes:
main()
{
char name_of_char;
name_of_char = getchar();
getch();
}
So user know when must type input from PC keyboard, should given comment/instruction in printf() function,
main()
{
char name_of_char;
printf("Type a character from PC keyboard! ");
name_of_char = getchar();
getch();
} - To show character typed by user can be used printf() function with use code %c
main()
{
char name_of_char;
printf("Type a character from PC keyboard! ");
name_of_char = getchar();
printf("%c", name_of_char);
getch();
}
Can also added sentence:
main()
{
char name_of_char;
printf("Type a character from PC keyboard! ");
name_of_char = getchar();
printf("Character is typed by user is: %c", name_of_char);
getch();
} - Example we want to add 2 other char variables as input:
main()
{
char name_of_char;
printf("Type a first character! ");
name_of_char = getchar();
printf("First character typed: %c \n \n", name_of_char);
char name_of_char2;
printf("Type a second character! ");
getchar();
name_of_char2 = getchar();
printf("Second character typed: %c \n \n", name_of_char2);
char name_of_char3;
printf("Type a third character! ");
getchar();
name_of_char3 = getchar();
printf("Third Character typed: %c \n \n", name_of_char3);
getch();
}
To get same display of program result, program syntax can be witten as follows:
main()
{
char name_of_char;
char name_of_char2;
char name_of_char3;
printf("Type a first character! ");
name_of_char = getchar();
printf("First character typed: %c \n \n", name_of_char);
printf("Type a second character! ");
getchar();
name_of_char2 = getchar();
printf("Second character typed: %c \n \n", name_of_char2);
printf("Type a third character! ");
getchar();
name_of_char3 = getchar();
printf("Third Character typed: %c \n \n", name_of_char3);
getch();
}
To get different display of program result, program syntax can be written as follows:
main()
{
char name_of_char;
char name_of_char2;
char name_of_char3;
printf("Type a first character! ");
name_of_char = getchar();
printf("Type a second character! ");
getchar();
name_of_char2 = getchar();
printf("Type a third character! ");
getchar();
name_of_char3 = getchar();
printf("\nFirst character typed: %c \n", name_of_char);
printf("Second character typed: %c \n", name_of_char2);
printf("Third Character typed: %c \n \n", name_of_char3);
getch();
}
As much as this, hopefully useful... : )
0 komentar:
Posting Komentar