C 基础回顾 - 浮点数、字符和字符串
2018-07-23 本文已影响2人
289346a467da
//
// main.c
// CFloat
//
// Created by JakePrim on 2018/7/23.
// Copyright © 2018年 JakePrim. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <locale.h>
//浮点数结构体
typedef struct{
unsigned int nMant : 23; //尾数部分 最大存储23位
unsigned int nExp : 8;//指数部分 最大存储8位
unsigned int nSign : 1;//符号位 1位
} FP_SINGLE;
int main(int argc, const char * argv[]) {
// insert code here...
//float 始终占用了4个字节 double 始终占用了8个字节
float a = 0.302f;//小数的默认类型是double,加上后缀f / F 就不用转换了
float b = 128.11f;
double c = 123;
float d = 12.64E3f;
double e = 0.78e-2;
// printf("a = %f,b = %f,c = %lf,d = %E,e = %le",a,b,c,d,e);
printf("a = %g,b = %g,c = %g,d = %g,e = %g \n",a,b,c,d,e);
int g = 1.345; //将小数赋值给整数,直接丢掉小数部分,注意是直接丢掉。
printf("g = %d \n",g);
// char strBin[23] = {0};
float f = 19.625;
// FP_SINGLE *p = (FP_SINGLE*)&f;
// itoa(p->nSign,strBin,2);
// sprintf(p->nSign, strBin, 2)
printf("sign:%f \n",f);
//char 字符 只能是对应ASCII的字符
char a1 = 'a';
char b1 = '&';
char c1 = 'X';
char d1 = ' ';
putchar(a1);
putchar(b1);
putchar(c1);
putchar(d1);
putchar('\n');
printf("a1 = %c,b1 = %c,c1 = %c,d1 = %c \n",a1,b1,c1,d1);
printf("%c %c %-9d \n",'E',71,71);
//字符串 可以通过字符或指针来间接的存储字符串
char str[] = "这是数组存储的字符串";
char *strs = "这是指针存储的字符串";
puts(str);
puts(strs);
printf("%s %s \n",str,strs);
//L 表示的是宽字符 使用UTF-16 UTF-32, 如果不用L前缀 则表示使用ASCII表中的字符
wchar_t wa = L'中';
wchar_t wb = L'。';
setlocale(LC_ALL, "zh_CN");
putwchar(wa);
putwchar(wb);
wprintf(L"Wide char %lc %lc",wa,wb);//必须使用宽字符串
//宽字符串
wchar_t wc[] =L"我是宽字符串数组";
wchar_t *wd = L"我是宽字符串指针";
wprintf(L"\n 输出宽字符串:wc = %ls ; wd = %ls \n",wc,wd);
char aa1 = '\61';//字符1
char bb1 = '\141';//字符a
putchar(aa1);
putchar(bb1);
printf("C\tC++\tJava\n \"c\" frist apperaed \n");
int aaaas = 0,bbbbs = 0;
scanf("输入a:%d",&aaaas);
scanf("输入b:%d",&bbbbs);
printf("a+b = %d \n",aaaas+bbbbs);
return 0;
}