c变量
(一) 一个程序将操作系统给其分配的内存分为4块
1.代码区。。存放程序的代码。各个函数的代码块
2.全局数据区,存放全局数据和静态变量
3.堆区:存放程序的动态数据
4.栈区:存放程序的局部数据,即各个函数的数据。
(二) 局部变量:在一个函数中说明的变量,他只在该函数中有效。函数外部可以使用该变量
1:主函数main()中定义的局部变量也只能在主函数中用
2:允许不同函数中定义相同名称变量,他们代表不同对象,分配不同单元,互不干扰
3:形参变量也是内部变量,属于被调用函数。实参变量,是调用该函数的内部变量
4:复合语句中也可以定义变
//原型:char *strcpy(char *strDst, const char *strSrc)
#include <iostream>
#include <assert.h>
using namespace std;
char *StrCpy(char *strDst,const char * strSrc)
{
assert((strDst!=NULL)&&(strSrc!=NULL));
char *addr = strDst;
while ((*strDst++=*strSrc++)!= '\0') NULL;
return addr;
}
//注意:char转换为int型是把char转换为对应的ASCII码
#include <iostream>
using namespace std;
int CharToInt(const char *str)
{
int iVal = *str++-'0';
while (*str!='\0')
{
iVal = iVal*10 +(*str++-'0');
}
return iVal;
}
void main()
{
char *str ='12345';
cout<<'str: '<<str<<endl;
int i = 0;
i = CharToInt(str);
cout<<' Int: '<<i<<endl;
}
1.
使用C编译器,可以这样指定: #define _STDC_
使用C++编译器,
#define _cplusplus
2.
出错提示:The program '[3084] CountSize.exe: Native' has exited with
code 0 (0x0).
解决: this is normal.it is stand for the program is exiting
normaly.
method1: run the program with CTRL+F5, rather than only
F5;
method2: add the sentence: system('pause'); at the
end of the program.
method3: add the sentences: | printf('press any key
to continue....');
| _getch();
3. 字节对齐
长度为取其最长字节的整数倍,
按最小字节对齐