C语言学习笔记 第三章

函数:

float–>%f 浮点类型
&变量名 相当于指针
scanf 输入 scanf(“类型”,&变量名)
%.(数字)(类型) %.2f 保留几位小数

数据类型关键字:
int 整数型 -32767——32767
long (int) 长整数型 -2147483647——2147483647
long long (int) 超长整数型 -9223372036854775807——922372036854775807
short (int) 短整数型
unsigned 无符号型(unsigned int,unsigned long,unsigned short,unsigned long long)
char 字符型
float 浮点型
double 双精度小数型(long double)
signed 有符号型
void 空虚
_bool 布尔型(逻辑型)
_complex 复数
_imaginary 虚数

方法:
1、二进制到十进制
0     0     0     0     0     1     1     1
|     |     |
2^2  2^1  2^0
|     |     |
4 + 2 + 1 = 7

0     0     0     0     0     1     0     1
|     |     |
2^2   0   2^0
|     |     |
4 + 0 + 1 = 5

2、浮点数计数法
3.16E7 表示 3.16 × 10^7
2.82E-8 表示 2.82 × 10^-8

3、声明变量
方法一: int dogs;
int cows;
方法二: int dogs, cows;

4、初始化变量:
方法一: int dogs = 21;
int cows = 36;
方法二: int dogs = 21, cows = 36;(int dogs, cows = 36 这样只有cows被赋予了值,这样写容易造成误解dogs也被赋予了值)

5、输出八进制数值:
%o 代替 %d(无前缀)
%#o 代替 %d(有前缀)

6、输出十六进制数值:
%x 代替 %d(无前缀)
%#x 代替 %d(有前缀)

7、当变量的数值达到最大值时,它将溢出到起始点

8、定义常量类型

后缀:
U表示Unsigned
L表示long

前缀:
0表示八进制
0x表示十六进制

eg:
020L 八进制数值20是长整数类型
0x10L 十六进制数值10是长整数类型
5ULL 十进制数值5是无符号超长整数型

9、Printf()格式说明符
%u 打印Unsigned int

long:
%ld 以十进制格式打印long
%lx 以十六进制格式打印long
%lo 以八进制格式打印long
%lu 以十进制格式打印无符号long

short:
%hd 以十进制格式打印short
%hx 以十六进制格式打印short
%ho 以八进制格式打印short
%hu 以十进制格式打印无符号short

long long:
%lld 以十进制格式打印long long
%llx 以十六进制格式打印long long
%llo 以八进制格式打印long long
%llu 以十进制格式打印无符号long long

10、字符常量赋值
char grade;
grade = ‘A';
grade = 65;(A在ASCII码中代码为65)
“A”这里把A看做字符串,是错误的

序  列 意  义
\a 报警
\b 退格
\f 走纸
\n 换行
\r 回车
\t 水平制表符
\v 垂直制表符
\\ 反斜杠(\)
\’ 单引号’
\” 双引号(“)
\? 问号(?)
\0oo 八进制值(o表示一个八进制数字)
\xhh 十六进制值(h表示一个十进制数字)

例子:

3-1
#include <stdio.h>
int main (void)
{
float weight;
float value;
printf ("Are you worth your weight in rhodium?\n");
printf ("Let's check it out.\n");
printf("Please enter your weight in pounds: ");
scanf ("%f",&weight);
value = 770 * weight * 14.5833;
printf("Your weight in rhodium is worth $%.2f.\n",value);
printf("You are easily worth that! If rhodium prices drop, \n");
printf("eat more to maintain your value.\n");
return 0;
}

3-2
#include <stdio.h>
int main (void)
{
int ten = 10;
int two = 2;
printf ("Doing it right: ");
printf ("%d minus %d is %d\n", ten, 2, ten - two);
printf ("Doing it wrong: ");
printf ("%d minus %d is %d\n", ten);
return 0;
}

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

3-4
#include <stdio.h>
int main(void)
{
unsigned int un = 3000000000;
short end = 200;
long big = 65537; long long verybig = 12345678908642;
printf ("un = %u and not %d\n", un, un);
printf ("end = %hd and %d\n", end, end);
printf ("big = %ld and not %hd\n", verybig, verybig);
printf ("verybig = %lld and not %ld\n", verybig, verybig);
return 0;
}

3-5
#include <stdio.h>
int main(void)
{
char ch;
printf ("Please enter a character.\n");
scanf ("%c",&ch);
printf ("The code for %c is %d.\n", ch, ch);
return 0;
}

3-7
#include <stdio.h>
int main(void)
{
float aboat = 32000.0;
double abet = 2.14e9;
long double dip = 5.32e-5;
printf ("%f can be written %e\n", aboat, aboat);
printf ("%f can be written %e\n", abet, abet);
printf ("%f can be written %e\n", dip, dip);
return 0;
}

3-8
#include <stdio.h>
int main (void)
{
printf ("Type int has a size of %u bytes.\n", sizeof (int));
printf ("Type char has a size of %u bytes.\n", sizeof (char));
printf ("Type long has a size of %u bytes.\n", sizeof (long));
printf ("Type long long has a size of %u bytes.\n", sizeof (long long));
printf ("Type double has a size of %u bytes.\n", sizeof (double));
printf ("Type short has a size of %u bytes.\n", sizeof (short));
return 0;
}

作业:
3-11-2
#include <stdio.h>
int main (void)
{
char c_char;
printf ("Tell me an ASCII int:");
scanf ("%d", &c_char);
printf ("ASCII int %d is %c.\n", c_char, c_char);
return 0;
}

3-11-3
#include <stdio.h>
int main (void)
{
printf ("\aStartled by the sudden sound, Sally shouted, \"By the Great Pumpkin, what was that!\"");
return 0;
}

3-11-4
#include <stdio.h>
int main (void)
{
float f_a;
printf ("Please input a floata:");
scanf ("%fd.", &f_a);
printf ("The input is %f or %e.\n", f_a, f_a);
return 0;
}

3-11-5
#include <stdio.h>
int main (void)
{
long L_a;
printf ("How old are you in years?\n");
scanf ("%LD",&L_a);
printf ("So, you have been living for %lld seconds!\n", L_a * 365 * 24 * 60 * 60);
return 0;
}

3-11-6
#include <stdio.h>
int main (void)
{
int i_water;
printf ("How much water do you have in Quart:");
scanf ("%d",&i_water);
printf ("Oh! You have %lld water molecules!\n", i_water * 950 / 3.0e-23);
getchar();
getchar();
return 0;
}

3-11-7
#include <stdio.h>
int main (void)
{
int i_tall;
printf ("How tall are you in centimeter:");
scanf ("%d",&i_tall);
printf ("Oh! You are %.1f inch tall!\n", i_tall / 2.54);
return 0;
}

原创文章,转载请注明: 转载自YXZ’s Blog

本文链接地址: C语言学习笔记 第三章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>