【C++ 学习】04 C & C++ 记录

【C++ 学习】04 C & C++ 记录

说明

本文是C++的学习记录

语法记录

stdio.h 和 cstdio

总结:C++中要尽量避免C风格的出现,所以使用cstdio。

#include <stdio.h>//C标准库头文件<stdio.h> 全称standard input&output
#Icndlue <cstdio>//<stdio.h>的C++版本

C++为了兼容纯C的语法所有.h可以使用。
包含C++提供的头文件时,应该使用尖括号,不带.h后缀。当然如果你使用的是C的头文件则:

#include <stdio.h>//写法正确,C提供的C风格的stdio
#include <stdio.h>//写法错误,stdio非C++提供头文件
#Icndlue <cstdio>//正确,<stdio.h>的C++版本

cstdio.h文件具体内容

// cstdio standard header
#pragma once
#ifndef _CSTDIO_
#define _CSTDIO_
#include <yvals.h>

#ifdef _STD_USING
 #undef _STD_USING
  #include <stdio.h>
 #define _STD_USING

#else /* _STD_USING */
 #include <stdio.h>
#endif /* _STD_USING */

 #define _HAS_CONVENTIONAL_CLIB    1

 #define _IOBASE    _base
 #define _IOPTR    _ptr
 #define _IOCNT    _cnt

#ifndef RC_INVOKED
 #if _GLOBAL_USING
_STD_BEGIN
using ::size_t; using ::fpos_t; using ::FILE;
using ::clearerr; using ::fclose; using ::feof;
using ::ferror; using ::fflush; using ::fgetc;
using ::fgetpos; using ::fgets; using ::fopen;
using ::fprintf; using ::fputc; using ::fputs;
using ::fread; using ::freopen; using ::fscanf;
using ::fseek; using ::fsetpos; using ::ftell;
using ::fwrite; using ::getc; using ::getchar;
using ::gets; using ::perror;
using ::putc; using ::putchar;
using ::printf; using ::puts; using ::remove;
using ::rename; using ::rewind; using ::scanf;
using ::setbuf; using ::setvbuf; using ::sprintf;
using ::sscanf; using ::tmpfile; using ::tmpnam;
using ::ungetc; using ::vfprintf; using ::vprintf;
using ::vsprintf;
_STD_END
 #endif /* _GLOBAL_USING */
#endif /* RC_INVOKED */

#ifndef _Filet
 #define _Filet    FILE
#endif /* _Filet */

#ifndef _FPOSOFF
  #define _FPOSOFF(fp)  ((long)(fp))
#endif /* _FPOSOFF */

#endif /* _CSTDIO_ */

/*
 * Copyright (c) 1992-2005 by P.J. Plauger.  ALL RIGHTS RESERVED.
 * Consult your license regarding permissions and restrictions.
 V4.05:0009 */

typedef struct

总结
定义一个struct类型,C中要用typedef,而C++中不需要写typedef。
在C++中的typedef是定义类型别名。

  • 在C中定义一个结构体类型要用typedef:
typedef struct Student
{
int a;
}Stu;

于是在声明变量的时候就可:Stu stu1;
如果没有typedef就必须用struct Student stu1;来声明
这里的 Stu 实际上就是 struct Student 的别名。
另外这里可以省略 Student(于是也不能struct Student stu1;了)

typedef struct
{
int a;
}Stu;//C style

在c++里定义struct直接

struct Student
{
int a;
};//C++style:定义了结构体类型Student

声明变量时直接Student stu2。

  • 在c++中如果用typedef的话,又会造成区别:
struct Student 
{ 
int a; 
}stu1;//stu1 是一个 Student 类型的变量 
typedef struct Student2 
{ 
int a; 
}stu2;
//stu2 是一个结构体类型,它是struct Student2 的别名

使用时可以直接访问stu1.a
但是stu2则必须先 stu2 s2;
然后 s2.a=10;

参考资料

C/C++语法知识:typedef struct 用法详解

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值