/*以十进制、八进制、十六进制打印十进制数100*/
#include <stdio.h>
int main(void)
{
int x = 100;
printf("dec = %d; octal = %o; hex = %x\n",x,x,x);
printf("dec = %#d; octal = %#o; hex = %#x\n",x,x,x);//打印前缀加#
return 0;
}
还有
/*scanf函数*/
#include <stdio.h>
int main(void)
{
char ch;
printf("Please enter a character.\n");
scanf("%c",&ch);
printf("The coad for %c is %d.\n",ch,ch);
return 0;
}
还有
#include <stdio.h>
int main(void)
{
float weight;
float value;
printf("Are you worth your weight in platinum\n");
printf("let's check it now!\n");
printf("Please enter your weight in pounds:\n");
scanf("%f",&weight);
value=1700.0*weight*14.5833;
printf("Your weight in platinum is worth $%.2f.\n",value);
printf("You are easily worth that! If platinum prise drop,\n");
printf("eat more to maintain your value.\n");
return 0;
}
问题来了,关于可移植类型:stdio.h和inttypes.h。下面这个程序里,为什么我的编译器对第三行红字代码报错了呢???要是有大佬看到了这个问题,麻烦你教教我。。。
/*altnames.c--可移植证数 类型名*/
#include <stdio.h>
#include <inttypes.h> // 支持可移植类型
int main(void)
{
int32_t me32;
me32 = 45933945;
printf("First,assume int32_t is int :");
printf("me32 = %d\n", me32);
printf("Next, let's not make any assumptions.\n");
printf("Instead,use a \"macor\" from inttypes.h");
printf("me32 =%" PRId32 "\n", me32);
return 0;
}
#include <inttypes.h> // 支持可移植类型
int main(void)
{
int32_t me32;
me32 = 45933945;
printf("First,assume int32_t is int :");
printf("me32 = %d\n", me32);
printf("Next, let's not make any assumptions.\n");
printf("Instead,use a \"macor\" from inttypes.h");
printf("me32 =%" PRId32 "\n", me32);
return 0;
}
打印浮点值。这居然有两种打印方法,之前只知道一种。。。涨姿势了
/* 以两种方式显示float类型的值*/
#include <stdio.h>
int main(void)
{
float about =32000.0;
double abet = 2.14e9;
long double dip = 5.32e-5;
printf("%f can be written %e\n",about,about);
printf("And it's %a in hexadecimal, powers of 2 notation\n",about);
printf("%f can be written %e\n",abet,abet);
printf("%Lf can be written %Le\n",dip,dip);
return 0;
}
小结:基本数据类型
关键字:
基本数据类型由11个关键字组成:int , long , short , unsigned , char , float , double , signed , _Bool , _Complex和_Imaginary。
符号有整型:
符号有整型可用于表示正整数和负整数。
无符号整形:
无符号整形只能表达零或正整数,故可表示的正整数比有符号整型大。在整型前加unsigned 即可表示无符号整型。
字符型:
可打印出来的符号都是字符。
布尔类型:
布尔类型表示true OR false。
实浮点类型:
实浮点类型可表示正浮点数和负浮点数。
负数和虚数浮点数:
虚数类型是可选类型。复数的实部和虚部类型都基于实浮点类型来构成。
例如:float_Complex , double_Imaginary..........
明天继续,拜拜!