02dayC language data types

A data type

 Second, input and output statements

Example:

scanf("%d",&a);
printf("%d",a);

 Different types of data have their corresponding format type:

. 1, int % D takes an integer value and it is represented as a signed decimal integer   

long% ld is a long integer

2 , % O unsigned octal integer (no output prefix 0)       

3,% u unsigned decimal integer      

4,% x / X unsigned hexadecimal integer  Example:    

printf("%x\n", i);   printf("%X\n", i);   printf("%#x\n", i);   printf("%#X\n", i);

 Respectively output 2f 2F 0x2f 0X2F

. 5, a float , and double  single-precision floating-point ( % F  ) and double precision floating point ( % LF)     

. 6 , % .mf output real decimal places of m bits, m has a front attention points.     

. 7 , % E / E number, represented here scientific notation "e" is represented by a case when the output "e" capitalization    

8,% c character. Can according to the digital input ASCII code into corresponding respective character     

Example:

#include <stdio.h>
int main()
{
    char i='A';
    printf("%c",i);
    return 0;
}

 

     A character output

     If the output is a control character '% d', the output value of the ASCII code corresponding to 'A'

9,% s string. Output string of characters until a null character string (string '\ 0' ends, this '\ 0' i.e. null character)     

Example:

#include <stdio.h>
int main()
{
    char i[]="ABC";
    printf("%s",i);
    return 0;
}

 

      Output string of ABC

10,% p to the output pointer, output variable address in hexadecimal notation    

Example:

#include <stdio.h>
 int main () 
{ 
    int I = . 5 ; 
    the printf ( " % D \ n- " , I); // output decimal integer 
    the printf ( " % P " , I);    // hexadecimal output pointer form 
    the printf ( " % P " , & I);   // output variable memory address 
    return  0 ; 
}

 

11,% n number of characters before this character until the total output, no output text    

12 %% without conversion, outputting the character '%' (percent) itself   

13,% m print error errno value corresponding to the content, (Example: printf ( "% m \ n ");   

Third, the escape character

Escape character
significance
ASCII value (decimal)
\a
Bell (BEL)
007
\b
Backspace (BS), the current position to the previous column
008
\f
Feed (FF), the current position to the beginning of the page
012
\n
Line feed (LF), the current position to the beginning of the next line
010
\r
A carriage return (CR), the current position to the beginning of the line
013
\t
Horizontal tab (the HT) (TAB jumps to the next position)
009
\ v
Vertical tabulation (VT)
011
\\
Represent a backslash character '\'
092
\'
On behalf of a single quote (apostrophe) character
039
\"
It represents a double quote character
034
\? On behalf of a question mark 063
\0
Null character (NUL)
000
\ooo
Any character 1-3 octal number represented
Three octal
\ socialization
Hexadecimal character represents any
Hex

String needs to be used to represent \\ \ first \ represents the escape, the second \ character represents the

Example:

#include <stdio.h>

main()
{
    printf("hello world");
    printf("\n");
    printf("hello world");
    printf("\a"); //响铃
    printf("\b"); //退格
    printf("\n"); 
    printf("\f"); //翻页
    printf("\n");
    printf("hello world");
    printf("\r"); //回车 
    printf("\t"); //水平制表,跳到下一个TAB位置
    printf("hello world");
    system("\"C:\\Program Files\\Tencent\\QQ\\Bin\\QQScLauncher.exe\""); // ‘\\’和‘"""’表示不出来用\来转义 
    getchar();    //等待响应 
    return 0;
}

 

四、常量与变量

1、变量的命名规则

• 标识符:

程序中用于标识常量、变量、函数的字符序列组成

• 只能由字母、数字、下划线组成,第一个字母必须是字母 或下划线
• 大小写有区别
• 不能使用C语言的关键字
• 命名规则:
• 见名知意
• 不宜混淆
例:

变量使用之前一定要初始化,如果变量不初始化,就会默认读取垃圾数据, 有些垃圾数据

会导致程序崩溃。所以,变量使用之前,必须初始化。

2、定义常量

• 定义常量PI的两种方式:

 #define PI 3.14159;
  const float PI=3.14159; //const定义的常量不能修改,是只读数据

 例:

#include <stdio.h>
int main()
{
    #define NAME "朱健康"
    const int AGE=22;
    printf("我的名字: %s",NAME); 
    printf("\n"); 
    printf("我的年龄:%d",AGE); 
    return 0;
}
#include <stdio.h>

//自由落体运动 g=9.8
//输入时间,输出自由落体运动的距离 
int main()
{
    #define g 9.8
    float t,s;
    printf("自由落体时间:\n");
    scanf("%f",&t);
    s=1/2.0*g*t*t;
    printf("自由落体的距离: %f",s);
    return 0;
}

 

五、进制的计算

1、二进制、八进制转换

•  二进制转换成八进制:
从右向左,每3位一组(不足3位左补
0),转换成八进制
八进制转换成二进制:
用3位二进制数代替每一位八进制数

• 例 (1101001)2=(001,101,001)2=(151)8

• 例 (246)8=(010,100,110)2=(10100110)2

2、十进制、二进制转换

• 十进制整数转换为二进制:方法是除以2取余,逆序排列
• 二进制转换为十进制:
(1101) 2 = 1 * 2^3 + 1 * 2^2 + 0 * 2^1 + 1 * 2^0 = 8 + 4 + 0 + 1 = (13) 10
十进制小数的转换为二进制:
              方法是乘以2取整,顺以(0.625) 10为例
  1.  0.625 * 2 = 1.25 取整1
  2.      0.25 * 2 = 0.5 取整0
  3.          0.5 * 2 = 1 取整1
                   (0.625) 10 = (0.101) 2
                     (0.25) 10 = (0.01) 2

 3、位、字节、字

一个字节(byte)由8位(B)构成,而一个字(word)由32位(B)构成。位就是0、1  。
字节的单位(bits)
 

六、整型常量

• 十进制整数:

由数字0~9和正负号表示。 如123,-456,0

• 八进制整数:

由数字0开头,后跟数字0~7表示。 如0123,011

• 十六进制整数:

由0x或0X开头,后跟0~9,a~f,A~如0x123,0Xff

• 长整型常量:

789L、017L、0x12aL(用l或L引出)

七、整型变量

• 整型变量用于存放整型数据。根据数值的表示范围整型可以 为整型(int)、短整型(short)、长整型(long)三种。
• 这三种整型都默认为有符号型(signed),有符号型即可以 是正数、负数和0。也可以根据需要,将整型指定为无符号 型(unsigned),此时整型变量只能存放非负数。
• 上面提到的short、long、signed、unsigned都是一些类型 修饰符,用于补充说明变量的特性。

八、浮点型常量

• 十进制小数形式:

必须带小数点 • 如0.123, .123, 123.0, 0.0, 123.

• 指数形式:

e或E之前必须有数字;指数必须为整数 • 如12.3e3, 123E2, 1.23e4, e-5 ,1.2E-3.5
12.3e3相当于12.3的10的3次方

• 实型常量的类型细分:

• 默认为double型,例如3.14就是double类型
• 后面加f或F认为是float型,例如3.14f就是float类型 浮点型常量

九、浮点型变量

浮点型分为单精度型(float)和双精度型(double)两种。
• float型数据占用4个字节(32bits)存储单元,提供的有 效数字是6~7位。
• double型数据占用8个字节(64bits)存储单元,提供的 有效数字是15~16位。
 
#include <stdio.h>
#include <math.h>

//给出三角形的三边长,求三角形面积。
int main()
{
    double a,b,c;  
    double p,s;
    scanf("%lf %lf %lf",&a,&b,&c);  //double的格式化类型为%lf
    if(a+b>c && a+c>b && b+c>a)
    {
        p=(a+b+c)/2.0;
        s=sqrt(p*(p-a)*(p-b)*(p-c));
        printf("a=%lf\tb=%lf\tc=%lf\n",a,b,c);
        printf("三角形面积:%f",s);    
    }
    else
    {
        printf("三边不能构成三角形");    
    }
    return 0;
}

 

 

Guess you like

Origin www.cnblogs.com/zhujiankang/p/11203865.html