strings1.c

来自「C源码集」· C语言 代码 · 共 45 行

C
45
字号
#include <stdio.h>#include <string.h>int main (){	char fastcar[15] = "Super Groover";	char slowcar[6]  = {'R', 'u', 's', 't', 'y', '\0'};	char winner[] = "Big Head";  // size = 8 + 1 chars	char daily[20];	int i;	printf("What is the name of your everyday car? ");	scanf("%s", daily);	printf("%s, that's a nice name for a car\n\n", daily);	/* scanf only reads to the first white space and other characters	can be a problem. Try changing to gets() */	printf("The slowcar's name backwards is ");	for (i = 2; i <= sizeof(slowcar); i++)		printf("%c", slowcar[sizeof(slowcar) - i]);	// slowcar [0], [1], [2], [3], [4], contain letters, [5] is '/0'	printf("\n\nWhat would you like to call the fast car? ");	gets(fastcar);	strcpy(slowcar, "Sam");	// OK, no overflow problem	printf("\n%s goes faster than %s\n\n", fastcar, slowcar);	strncpy(winner, "Small Gear Shift", sizeof(winner));	 // size 16 + 1 chars!! Won't all fit, need to add null character	winner[sizeof(winner) - 1] = '\0';	// comment out this line and see what happens with the printf!	printf("And the winner is ...  %s!!! \n", winner);	return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?