首页 > 编程语言 > 详细

c++中的数据类型

时间:2016-02-02 23:14:40      阅读:423      评论:0      收藏:0      [点我收藏+]

1.数据类型简介

c++中数据类型分为两种:预定义类型和自定义数据类型。

预定义类型:整型(int 4个字节)

                   字符型

                   布尔型 (bool)

                   浮点型(4个字节)

                  空类型:关键字void,不能用于普通变量的声明和普通的操作,只能用于指针型变量,函数返回值和函数参数。

                  指针类型

自定义类型:数组、结构体(struct)、联合体(union)、枚举(enum).具体在下边介绍

2.类型修饰符:

 signed,unsigned,short,long。

 signed int a;//有符号整数

 short int a;//短整型,2个字节

 long int a;//长整型,4个字节

 另外这几个符号只能修饰int。

3.占位符总结:

 %d:int

 %ld:long

 %f:float

 %lf:double

 %p:输出变量地址

 %x/%X:输出十六进制数

 %s:输出字符串

 %o:无符号八进制整数

 %u:无符号十进制整数

 %e/%E:用科学记数法输出浮点数

 

4.基本数据类型代码示例:

#include"stdio.h"
int a;
float b;
double c;
void ceshi();
int main(void)
{
    int d;
    int num=2147483646;//和最大值差1  最大值:2147483647
    float f=1.234567;
    char arr[]="abc";

    printf("num=%d\n",num+21);//-2147483648;
    printf("f=%f\n",f/4);//0.308642
    printf("arr的地址:%p\n",arr);//0028FEFC
    printf("arr的字符串:%s\n",arr);//abc
    long int lnum;
    printf("长整型占用字节数:%d\n",sizeof(lnum));//4
    printf("整型占用字节数:%d\n",sizeof(num));//4
    printf("默认值:%d\n",lnum);//38 随机的
    printf("main默认值:%d\n",d); //随机数
    printf("默认值:%f\n",b);  //0.000000
    printf("默认值:%f\n",c);  //0.000000
    printf("默认值:%lf\n",c);  //0.000000

    ceshi();
    return 0;

}
void ceshi(){
   int a=2,b=3,c=5,num;

   num=a+b%c;
   printf("jisuan: %d\n",num);//5
   char cc[64];
   scanf("%s",cc);
   printf("输入的值为:%s aa\n",cc);
}

  技术分享

 5.结构体数据类型

    5.1结构体是由一系列具有相同类型或者不同类型的数据构成的数据集合。

 定义:

  struct 名称{

    数据类型 变量名1;

     数据类型 变量名2;

     。。。。 

     数据类型 变量名n;

   }结构体变量名;

   5.2结构体数组:

    struct Info{

      int age;

      char name[32];

    }info[SIZE];

  5.3结构体嵌套:

    struct BookInfo{

     char name[SIZE];
    char author[SIZE];
   int status;
  };
  struct ReadreInfo{
   char Name[SIZE];
   char Date[SIZE];
  };
//结构体嵌套
 struct LibraryInfo{
   int ID;
   struct BookInfo book;
   struct ReadreInfo reader;
  }LibraryInfo;

代码示例:

#include <stdio.h>
#define SIZE 32

int main(void){

 struct BookInfo{
 char name[SIZE];
  char author[SIZE];
  int status;
};
struct ReadreInfo{
 char Name[SIZE];
 char Date[SIZE];
};
//结构体嵌套
struct LibraryInfo{
    int ID;
    struct BookInfo book;
    struct ReadreInfo reader;
    }LibraryInfo;
printf("input the book id:\n");
scanf("%d",&LibraryInfo.ID);
printf("input the book name:\n");
scanf("%s",LibraryInfo.book.name);
printf("input the book author:\n");
scanf("%s",LibraryInfo.book.author);
printf("input the book status:\n");
scanf("%d",&LibraryInfo.book.status);

printf("input the reader name:\n");
scanf("%s",LibraryInfo.reader.Name);
printf("input the reader BorrowDate:\n");
scanf("%s",LibraryInfo.reader.Date);

printf("图书信息如下:\n");
printf("图书编号:%d\n",LibraryInfo.ID);
printf("书名:%s\n",LibraryInfo.book.name);
printf("图书作者:%s\n",LibraryInfo.book.author);
printf("图书当前状态:%d\n",LibraryInfo.book.status);
printf("读者姓名:%s",LibraryInfo.reader.Name);
printf("借阅日期:%s",LibraryInfo.reader.Date);

return 0;

}

 

  技术分享

 6.联合体

   联合体的使用方式和结构体及普通变量类似。

  union 名称{

  类型变量符 变量名;

   ......

    类型变量符 变量名;

  };

 

c++中的数据类型

原文:http://www.cnblogs.com/jycboy/p/5178641.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!