The C array, pointer and string program example

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

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)

Other info:

To do: Displaying a line of text on standard output which was read from the standard input using array data type

To show: An array, pointer and string type for C programming

 

 

// Array, pointer and string

#include <stdio.h>

 

void main(void)

{

// an array variable of type char, sized 79

char sentence[80];

 

// prompt for user input...

printf("Enter a line of text:\n");

// read the user input...

// gets(sentence);, the following is a secure version

gets_s(sentence, 80);

// display what has been read by gets_s()

printf("Line of text entered: \n%s\n", sentence);

getchar();

}

 

Output example:

 

Enter a line of text:

This is a line of text

Line of text entered:

This is a line of text

This is another line of text

Press any key to continue . . .

 

 

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