C99标准新特性的说明

C99标准新特性的说明

 
一、说明
======
这里的讨论的是C语言的国际标准,即国际标准化组织ISO,制定的C语言标准。历史上ISO制定过4个版本的C语言标准,他们分别是:C90(ISO/IEC9899:1990)、C95(ISO/IEC 9899/AMD1:1995)、C99(ISO/IEC9899:1999)、C11(ISO/IEC 9899:2011)
 
这里主要介绍一下C99相对C90而增加的新特性,并且在Gcc编译器中对这些新特性的进行测试。
 
二、C99新特性介绍
=============
C99引入了很多个新特性,有些是非常常用的,已经在其他语言中大量普遍使用了,非常的方便,有些是不太常用的。下面逐个介绍。
 
2.1 混合代码和声明intermingled declarations and code
----------------------------------------------------------------
variable declaration is no longer restricted to file scope or the start of a compound statement (block)。
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
 
int  main( int  argc,  char  *argv[])
{
     int  a1 = 10;
     printf ( "a1 = %d\n" , a1);
     int  a2 = 20;
     printf ( "a2 = %d\n" , a2);
     
     return  0;
}

 

2.2 长度可变的数组variable-length arrays

-------------------------------------------------

数组在C语言中定义时,只能用常量表达式来指定其长度,可变长度的数组在定义时可以使用变量来指定数组的长度,这样以来,就可以定义必须等到运行时才能知道大小的数组。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdbool.h>
 
int  main( int  argc,  char  *argv[])
{
     int  size;
     scanf ( "%d" , &size);
     int  arr[size];  //使用运行时才能确定其值的变量作为数组长度来定义数组
     for  ( int  i = 0; i < size; i++)
     {
         scanf ( "%d" , &arr[i]);
     }
     
     for  ( int  i = 0; i < size; i++)
     {
         printf ( "%d " , arr[i]);
     }
     printf ( "\n" );
     
     return  0;
}

 

2.3 支持单行注释one-line comments

------------------------------------------

support for one-line comments beginning with //, as in BCPLC++ and Java.

1
2
3
4
5
6
7
8
#include <stdio.h>
 
int  main( int  argc,  char  *argv[])
{
     //this a one line comment
     
     return  0;
}

 

2.4 for语句内的变量声明

----------------------------  

C99中,可以在for语句的初始化部分定义一个或多个变量,这些变量的作用域仅于本for语句所控制的循环体内。

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
 
int  main( int  argc,  char  *argv[])
{
     int  arr[5] = {1, 2, 3, 4, 5};
     for  ( int  i = 0; i <  sizeof (arr)/ sizeof (arr[0]); i++)
     {
         printf ( "%d " , arr[i]);
     }
     printf ( "\n" );
     
     return  0;
}

 

2.5 内联函数inline functions
--------------------------------
内联函数的主要作用就是提高可执行程序的性能。
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
 
inline  int  add( int  a,  int  b)
{
     return  (a + b);
}
 
int  main( int  argc,  char  *argv[])
{
     printf ( "%d + %d = %d\n" , 10, 20, add(10, 20));
     
     return  0;
}

 

2.6 引入了新的数据类型,包括:long long int、_Bool、_Complex、_Imaginary
------------------------------------------------------------------------------------------

several new data types, including long long int, optional extended integer types, an explicit boolean data type, and a complex type to represent complex numbers

1 long long int   

C99标准中引进了long long int(-(2e63 - 1)至2e63 - 1)和unsigned long long int(0 - 2e64 - 1)。long long int能够支持的整数长度为64位。

 

2 _Bool

_Bool是作为关键字引入的。_Bool类型的变量的值是0或1。

C99中增加了头文件夹<stdbool.h>,在其中定义bool、true以及false宏的, 以便程序员能够编写同时兼容于C与C++的应用程序。在编写新的应用程序时,应该包含<stdbool.h>头文件,并且使用bool宏来定义_Bool类型的变量,通过用true和false这两个宏来为_Bool类型的变量赋值。

 

3 _Complex和_Imaginary

_Complex关键字用来代表复数类型,_Imaginary关键字用来代表虚数类型。虚数单位可以是i、I、j、J。

这两个关键字和float、double以及long double组合,可以形成如下的六种类型:

float _Complex : 实部和虚部都是float类型的复数

float _Imaginary 

double _Complex : 实部和虚部都是double类型的复数

double _Imaginary

long double _Complex : 实部和虚部都是long double类型的复数

long double _Imaginary

同时,C99新增了<complex.h>头文件,在其中定义了complex和imaginary宏,它们可以扩展为_Complex和_Imaginary。因此在编写新的应用程序时,应该在程序中包含<complex.h>头文件,并且使用中的complex和imaginary来定义复数变量和虚数变量。

_Imaginary关键字gcc暂时不支持。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <stdbool.h>
#include <complex.h>
 
int  main( int  argc,  char  *argv[])
{
     long  long  ll = 10;
     printf ( "ll = %d\n" , ll);
     
     _Bool b1 =  true ;
     printf ( "b1 = %d\n" , b1);
     bool  b2 =  true ;
     printf ( "b2 = %d\n" , b2);
     
     float  complex fc = 1.0f + 2.0j;
     double  complex dc = 3.0 + 4.0j;
     long  double  complex ldc = 5.0 + 6.0i;
     
     //下面的代码在gcc无法编译通过
     //float imaginary fi = 2.0j;
     //double imaginary di = 3.0j;
     //long double imaginary = 4.0i;
     
     return  0;
}

 

以上六个特性是非常常用,而且有用的特性。

 

2.7 其余用到再写

--------------- 

 
 
参考资料:
1、https://en.wikipedia.org/wiki/C99
2、http://www.cnblogs.com/fly1988happy/archive/2012/04/13/2445465.html
3、http://blog.csdn.net/syrchina/article/details/6662243
4、http://www.cnblogs.com/wuyudong/p/c99-new-feature.html


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值