当前位置:网站首页>C language - characters and strings, arithmetic operators, type conversions
C language - characters and strings, arithmetic operators, type conversions
2022-07-27 02:22:00 【Lydialyy】
Catalog
3. Precedence and associativity of operators
One 、 Characters and strings
1. character
- Character variables can be used to store integers
The code for :
#include <stdio.h>
int main()
{
char a = 'C';
printf("%c = %d\n", a, a);
return 0;
}Running results :

2.ASCII Character table
ASClI(American Standard Code for Information Interchange, American standard information exchange code ) It's a computer coding system based on the Latin alphabet , Mainly used to show modern English and other western European languages . It's the most common single byte encoding system today , And equivalent to international standards ISO/IEC646.
standard ASCⅡ Character table usage 7 Bit binary number to represent all upper and lower case letters , Numbers 0 To 9、 Punctuation , And special control characters used in American English .
- ASCIl Numbers on the character table 0~31 as well as 127( common 33 individual ) Assigned to the control character , It's used to control peripheral devices like printers .
- ASCIl Numbers on the character table 32~126 Assigned to characters that can be found on the keyboard , Appears when you view or print a document
The code for :
#include <stdio.h>
int main()
{
char a = 70, b = 105, c = 115, d = 104, e = 67;
printf("%c%c%c%c%c\n", a, b, c, d, e);
return 0;
}Running results :

Conclusion : The character type is a special integer . But for integers , If don't write signed or unsigned, The system default is signed; But for character types ,C Language and There's no rule The default is signed still unsigned, At the discretion of the compiling system .
The code for :
#include <stdio.h>
int main()
{
unsigned char height;
height = 170;
printf(" His height is %d centimeter !\n", height);
return 0;
}Running results :

If not add “unsigned”, The result is :

so , If use char To store height , You must add “unsigned”.
3. character string
- Syntax for declaring strings :char Variable name [ Number ];
- Get the space of each variable through the index number : Variable name [ Reference no. ] = character ;
example :
- Declaration string :char name[2];
- Assign a value to a string :name[0] = 'F'; name[1] = 'i';
- Define string :char name[5] = {'F', 'I', 'S', 'H', 'c'};
The code for :
#include <stdio.h>
int main()
{
char a[4] = {'L', 'O', 'V', 'E'};
printf("%s\n",a);
return 0;
}Running results :

There seems to be no problem with this result , But if you print another ( such as hello), Then there will be confusion . This is because , To determine the end position of the string in memory ,C The language stipulates adding a... At the end of the string “\0” To indicate the end .
Modify the above code as follows :
char a[5] = {'L', 'O', 'V', 'E', '\0'};You can also write a pair directly “[]”, as follows :
char a[] = {'L', 'O', 'V', 'E', '\0'};Or write string constants directly , Use double quotation marks , There is no need to add “\0”. as follows :
char a[] = {"LOVE"};If you use string constants , You can also remove the braces . as follows :
char a[] = "LOVE";Two 、 Arithmetic operator
C Language supports us to process data by providing operators .

1. What is the purpose ?
The operands that operators act on are called operands , Whether an operator is binocular or unary depends on how many operands it has , How many operands work .

C Language has a unique ternary operator , It has 3 Operands , The following will be supplemented .
2. expression
An expression that connects operands with operators and parentheses , It's called an expression .
Such as :1 + 1; 'a' + 'b'; a + b; a + 'b' + pow(a,b) * 3 / 4 + 5
3. Precedence and associativity of operators
Plus operator 、 Negative operator > /、*、% > +、-
The code for :
#include <stdio.h>
#include <math.h>
int main()
{
int i,j,k;
i = 1 + 2;
j = 1 + 2 * 3;
k = i + j + -1 + pow(2,3);//3 + 7 + (-1) + 8
printf("i = %d\n",i);
printf("j = %d\n",j);
printf("k = %d\n",k);
return 0;
}Running results :

4. Type conversion
Type conversion is to ensure the accuracy of calculation .
The code for :
#include <stdio.h>
int main()
{
printf(" Integer output :%d\n", 1 + 2.0);
printf(" Floating point output :%f\n", 1 + 2.0);
}Running results :

so , Integer output is the wrong result , This is because ,1 + 2.0 The result of automatic conversion is 1.0 + 2.0, Is a floating-point number , If a floating-point number is forcibly changed to an integer number, the output will print the wrong result .
C Language allows us to cast data types of operands , Precede the operand with () Just enclose the target data type . as follows :
#include <stdio.h>
int main()
{
printf(" Integer output :%d\n", 1 + (int)2.0);
printf(" Floating point output :%f\n", 1 + 2.0);
}Running results :

If the 2.0 Change it to 1.8, The result is :

If the 2.0 Change it to (1 + 0.8), The result is :

边栏推荐
- 在有序数组找具体某个数字
- C语言——字符和字符串、算术运算符、类型转换
- TCP的三次握手与四次断开
- Static routing default routing VLAN experiment
- Codeforces Round #807 (Div. 2), problem: (C) Mark and His Unfinished Essay
- Timer interrupt experiment
- 光光光仔的CSDN之旅
- 7.16 written examination of Duoyi network
- First knowledge of C language (2)
- 微信小程序:用户微信登录流程(附:流程图+源码)
猜你喜欢

OSPF static experiment

【C语言程序设计】分支结构

ACM mode input and output exercise

Experiment exercise of two-layer packaging technology (HDLC, ppp--pap\chap, GRE)

RS-485总线通信应用

Tim output comparison - PWM

静态路由综合实验

最新C语言入门与进阶 -史上最全最详细的C语言教程!! 第一节-总览C语言概括

RISC-V工具链编译笔记

(title + detailed idea + annotated code) codeforces round 805 (Div. 3) F Equate Multisets
随机推荐
Lora communication application development
TCP的三次握手与四次断开
OSPF basic configuration application (comprehensive experiment: interference election default routing area summary authentication -- interface authentication)
C语言——第一个程序、打印、变量和常量
Self introduction and planning about programming
Educational Codeforces Round 132 (Rated for Div. 2), problem: (D) Rorororobot
JUC并发编程
Esp8266wi fi data communication
识时务者常用网址大全
Experiment exercise of two-layer packaging technology (HDLC, ppp--pap\chap, GRE)
Ogeek meetup phase I, together with cubefs, is hot
ensp中的简单静态路由
机械硬盘选购指南——从选购经历谈起
Lvs+keepalived project practice
求解整数的每一位
Republishing and routing strategy of OSPF
Use of golang - sync package (waitgroup, once, mutex, rwmutex, cond, pool, map)
HCIA基础知识(1)
About unsafe problems such as fopen and strError encountered in vs2022 or advanced version running environment
Lora gateway node converges sensor data