The C array, character and string program example

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

To do: Read character and string from the standard input and print to standard output using array variables

To show: To show the relationship among array, character and string in C programming

 

 

#include <stdio.h>

 

int main(void)

{

/* normal and array variable */

char sex, name[11], name1[11];

 

printf("Enter your sex (M or F): ");

scanf("%c", &sex);

 

printf("Enter your first name (max 10 characters): ");

scanf("%s", &name);

 

printf("Enter your last name (max 10 characters): ");

scanf("%s", &name1);

 

/* test whether male or female */

if (sex == 'M')

/* array name without brackets is the pointer to the first array's element */

printf("\nHow are you, Mr. %s %s?\n", name, name1);

else

printf("\nHow are you, Ms/Mrs. %s %s?\n", name, name1);

 

return 0;

}

 

Output example:

 

Enter your sex (M or F): F

Enter your first name (max 10 characters): Jodie

Enter your last name (max 10 characters): Foster

How are you, Ms/Mrs. Jodie Foster?

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index