1,下面分别gcc和VS上运行的是代码和分析区别
1,gcc代码
/*************************************************************************
* 本题考点是gcc编译器 i++ 在遇到 '=' 符号时也是会增的不一定在一个循环继续后再自增的
* VS编译器 是在整个循环结束后才会i++的
> File Name: 3strcpy的实现.c
> Author:
> Mail:
> Created Time: Tue 08 Aug 2017 03:42:06 AM PDT
************************************************************************/
#include<stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char s1[] = "Hello World!";
char s2[20];
int i = 0;
// while (s1[i/*++*/])// 和下面的第2种情况一样的
// {
// s2[i/*++*/] = s1[i/*++ */];//1, 这里s2中已经i++ 后 有= 所以s1的地址 s1指向s1【1】2, 了s1 是直接错误的rr
// i++;//ok
// }
// s2[i] = '\0';
//printf("while :%s\n", s2);
for (i = 0; i < strlen(s1) ; i++)//ok
{
s2[i/*++*/] = s1[i/*++*/];//这里s2[i++] 的情况和while中s2{i++]情况一样 都和上面一样的
//i++; //ok
}
//s2 = '\0'; // gcc 编译器不需要 再末尾加'\0' VS 要加 '\0'
printf("for : %s\n", s2);
strcpy(s2, s1);
printf("strcpy: %s\n", s2);
return 0;
}
2,VS上的代码分析
/*************************************************************************
> File Name:
> Author: songli
> Mail:
> Created Time:
************************************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(int argc, char *argv[])
{
char str[] = "chensong chenli";
char ch[30];
//int i = 0;
//while (str[i/*++*/])//err
//{
// ch[i/**++ ok*/] = str[i/*++ ok */];
// i++;//ok
//}
//ch[i] = '\0';
int i;
for (i = 0; i < strlen(str); /*i++ok*/)
{
ch[i/*++ ok*/] = str[i/*++ok*/];
i++;//ok
}
ch[i] = '\0';
printf("ch = %s\n", ch);
printf("\n");
system("pause");
return EXIT_SUCCESS;
}