zoukankan      html  css  js  c++  java
  • 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)。
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	int a1 = 10;
    	printf("a1 = %d
    ", a1);
    	int a2 = 20;
    	printf("a2 = %d
    ", a2);
    	
    	return 0;
    }
    

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

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

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

    #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("
    ");
    	
    	return 0;
    }
    

    2.3 支持单行注释one-line comments

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

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

    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	//this a one line comment 
    	
    	return 0;
    }

    2.4 for语句内的变量声明

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

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

    #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("
    ");
    	
    	return 0;
    }
    
    2.5 内联函数inline functions
    --------------------------------
    内联函数的主要作用就是提高可执行程序的性能。
    #include <stdio.h>
    
    inline int add(int a, int b)
    {
    	return (a + b);
    }
    
    int main(int argc, char *argv[])
    {
    	printf("%d + %d = %d
    ", 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暂时不支持。

    #include <stdio.h>
    #include <stdbool.h>
    #include <complex.h>
    
    int main(int argc, char *argv[])
    {
    	long long ll = 10;
    	printf("ll = %d
    ", ll);
    	
    	_Bool b1 = true;
    	printf("b1 = %d
    ", b1);
    	bool b2 = true;
    	printf("b2 = %d
    ", 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
  • 相关阅读:
    Knockout应用开发指南 第八章:简单应用举例(2)
    微软ASP.NET站点部署指南(7):生产环境部署
    Knockout应用开发指南 第七章:Mapping插件
    《Microsoft Sql server 2008 Internals》读书笔记第九章Plan Caching and Recompilation(6)
    《Microsoft Sql server 2008 Internals》读书笔记第九章Plan Caching and Recompilation(5)
    《Microsoft Sql server 2008 Internals》读书笔记第九章Plan Caching and Recompilation(3)
    《Microsoft Sql server 2008 Internals》读书笔记第九章Plan Caching and Recompilation(9)
    《Microsoft Sql server 2008 Internals》读书笔记第九章Plan Caching and Recompilation(8)
    Microsoft Visual Studio .NET 2003 引导程序插件下载地址(非官方)
    Vs2010在没有安装SQL Server 2005/2008 Express时如何连接MDF数据文件?
  • 原文地址:https://www.cnblogs.com/zhangzl419/p/6964288.html
Copyright © 2011-2022 走看看