2015.1.21
Constants: Is the most basic element in C, character constants, integer constants, floating-point constants, string constants, enumeration constants
Character constant: ' A ' F ' \ n ' ' 0 ' \f '
' 0 ' 48
' A ' 65
' A ' 97
1. Enclose with single quotation marks
2. There is only one character
3. Print character%c placeholder
4. Character constants occupy 4 bytes in memory
5. Character variable takes up 1 bytes of space
' \ n ' \f ' \ r ' \ ' \ ' \ ' \ ' \ '
/* #include <stdio.h>
int main (int argc, const char * argv[]) {
Char ch;
scanf ("%c", &ch);
printf ("%d\n", ch);
printf ("%c\n", ch);
printf ("%lu\n", sizeof (' a '));
Char CH2 = ' A ';
printf ("CH2 =%lu\n", sizeof (CH2));
printf ("%%\n");//two% output of one%
return 0;
}*/
String constants
"HelloWorld"
1. Enclose with double quotation marks
2. Print the string with%s
3. ' ' Is the string end flag
4. Each character in a string occupies one byte space
//
#include <stdio.h>
//
int main (int argc,const char *argv[])
//{
Char *str = "HelloWorld";
//
printf ("%s\n", str);
printf ("%lu\n", sizeof ("HelloWorld"));
return 0;
//}
' 0 ' 0 "0" "+"
//
#include <stdio.h>
//
int main (int argc,const char *argv[])
//{
printf ("%lu\n", sizeof ("n"));
return 0;
//}
#include <stdio.h>
Integral type constant: 456 98%d%ld%lld%u%lu%llu
Floating-point constants: 3.14 7899.6789%f%LF%lf
int main (int argc, const char *argv[])
//{
printf ("%ld\n", sizeof (786789876567890));
printf ("%ld\n", sizeof (3.1445678765434));
//
return 0;
//}
Variable: A named memory space in memory
Variable definition: int A; float F;
Data type + identifier
Variable declaration: extern int A;
Tells the compiler that a corresponding variable exists
Variable definition differs from variable declaration: A variable is defined as a variable that opens a memory space of the specified data type size, and the variable declaration tells the compiler that the variable is defined, looked up from another location, and no more space is given to the variable;
#include <stdio.h>
extern float F;
int main (int argc,const char *argv[])
{
int A;
A = 10;
printf ("%d\n", a);
printf ("%.2f\n", f);
return 0;
}
float f=3.14;
Identifier specification: A letter, or an underscore, followed by a number of alphanumeric underscores, cannot be a keyword in the C language
ABC _abc
1. Must start with a letter or underscore
2. You cannot use keywords in the C language
3. Keywords that usually start with an underscore are used by the compiler
Operator
Minus sign Operator:-
Self-increment decrement operator: + +--
Arithmetic operators: */% +-
Relational operators: > >= < <= = = =
Logical operator:! && | |
Trinocular operator:?:
Assignment operator: = *=/=%= + = =
Comma operator:,
Expression: A formula consisting of variable constants and operators
4+5 a+b (a+b) *c
Expressions have deterministic values, with data types
#include <stdio.h>
//
int main (int argc,const char *argv[])
//{
int a=3;
int b = 5;
printf ("%d\n", a+b);
//
return 0;
//}
-Number operator is a single-mesh operator
#include <stdio.h>
int main (int argc, const char *argv[])
//{
int a=-10;
printf ("%d\n",-a);
return 0;
//}
//++ --
/*int Main (int argc,const char *argv[])
{
int a=11, b=21;
printf ("%d\n", a++);//a=a+1;
printf ("%d\n", a);
printf ("%d\n", ++a);
printf ("%d\n", a);
printf ("%d\n", b--);
printf ("%d\n", b);
printf ("%d\n",--b);
printf ("%d\n", b);
printf ("%d", ++a++);//illegal
printf ("%d%d\n", ++a,--a);//The same variable cannot be modified multiple times in the same C language statement, the result is undefined
printf ("%d%d\n", A, a--);
return 0;
}*/
//* / % + -
int main (int argc,const char *argv[])
//{
int a=3, b=5;
printf ("a*b =%d\n", a*b);
printf ("A/b =%d\n", A/b);
printf ("a%%b =%d\n", a%b);
printf ("a+b =%d\n", a+b);
printf ("A-B =%d\n", A-a);
printf ("%d\n", a+b*a);
return 0;
//}
> >= < <= = = =
A>b
int main (int argc,const char *argv[])
//{
int a=30, b=20;
printf ("%d\n", a>b);//Expression is set, value is!0 expression is not true, value is 0
printf ("%d\n", a>=b);
printf ("%d\n", a<b);
printf ("%d\n", a<=b);
printf ("%d\n", a!=b);
printf ("%d\n", a==b);
return 0;
//}
&& (logical AND logical multiplication) | | (Logical OR logical plus)! (Logical non)
&&
Expression 1-expression 2 ... Value of an expression
It's true.
False and True
True and False
Fake Fake fake
Conclusion: expression 1 to expression N, one expression is false, then the entire expression is false
// ||
Expression 1-expression 2 ... Value of an expression
It's true.
It's fake.
True and False
Fake Fake fake
Conclusion: expression 1 to expression N, one expression is true, then the entire expression is true
//!
Expression-expression value
True and False
False truth
int main (int argc,const char *argv[])
//{
int a=10, b=0;
printf ("%d\n", a&&b);
//
printf ("%d\n", a| | b);
//
printf ("%d\n",!a);
20>a>10
//
printf ("%d\n", (a>5) && (a<20));
//
return 0;
//}
Trinocular operator:?:
int main (int argc,const char *argv[])
//{
int a=30, b=20;
printf ("max =%d\n", a>b?a:b);//ask for a maximum of two numbers
printf ("min =%d\n", a<b?a:b);//ask for the minimum value of two numbers
return 0;
//}
Maximum minimum value for three numbers
int main (int argc, const char *argv[])
//{
int A, b, C;
scanf ("%d%d%d", &a, &b, &c);
printf ("max =%d\n", (A>B?A:B) >c? ( A>B?A:B): c);
printf ("min =%d\n", (A<B?A:B) <c? ( A<B?A:B): c);
//
return 0;
//}
int main (int argc, const char *argv[])
//{
int a=10;
int b=5;
int c=8;
A= 20;
//
printf ("A =%d\n", a);
printf ("A =%d\n", a+=5);//<==> a = a+5;
//
printf ("A =%d\n", a-=10);//<==> a = a-10;
//
printf ("A =%d\n", a*=3);//<==> a = a*3;
//
printf ("A =%d\n", a/=9);//<==> a = A/9;
//
printf ("A =%d\n", a%=3);//<==> a = a%3;
//
++a; <==> a=a+1;
--a; <==> A=a-1;
//
printf ("%d%d", a+=2, a-=3);//illegal
//
printf ("%d\n", a=a+b);
printf ("%d\n", c= (a=a+b));
return 0;
//}
Comma operator
//,
(expression 1, expression 2, expression 3, ...) Expression N)
The value of an expression is the value of an expression n
int main (int argc,const char *argv[])
//{
int a=5,b=6,c=7,d=8;
printf ("%d\n", (a,b,d,c));
printf ("%d\n", (a+b,b-c,c+d,a*d));
printf ("%d\n", (A=a+b,b=b+c,c=c+d,a));
//
return 0;
//}
Priority order: parentheses > Monocular operators > Arithmetic operators (*/% > +-) > Relational operators > Logical operators (&& > | |) > Three mesh operator > assignment operator > Comma operator
Type conversions
int main (int argc,const char *argv[])
//{
int a=10;
Long B = 6789;
float f=3.99;
//
printf ("%ld\n", a+b);//a bit extension//implicit conversion int---long
printf ("%f\n", a+f);//int---float
//
printf ("%d\n", (int) f+a);//display conversion, also called coercion type conversion
//
return 0;
//}
Char short is aligned to int
int to Long
Integral type is aligned to floating-point type
Single-precision to double-progress
Only char is calculated with the short data to the INT type
Signed to no sign
int main (int argc,const char *argv[])
//{
Char a=-6;
Short d = 23;
unsigned int n=78;
//
int B = 12;
Long C = 40;
float f1 = 3.14;
Double F2 = 678.423;
printf ("%d\n", a+b),//char--and int
printf ("%ld\n", b+c);//INT---long
printf ("%ld\n", a+c);//char--long
printf ("%f\n", a+f1);//char---float
printf ("%f\n", b+f1);//int---float
printf ("%f\n", c+f1);//long---float
printf ("%f\n", F1+F2);//float--double
//
printf ("%d\n", a+d);
printf ("%d\n", a+n);
//
//
return 0;
//}
Bit extension: For positive number, bit extension high 0, for negative, bit expansion, high 1;
0000 0000 0000 0000 0000 0000 0000 0110 A=6
0000 0000 0000 0000 0000 0000 0000 1100 b=12
//----------------------------------------------
0000 0000 0001 0010
1111 1111 1111 1111 1111 1111 1111 1010 a=-6
0000 0000 0000 0000 0000 0000 0000 1100 b=12
//----------------------------------------------
0000 0000 0000 0000 0000 0000 0000 0110 6
int main (int argc,const char *argv[])
//{
Char A;
int b=0x1256;
float f=311.4567;
A = b;//bit truncation Discard high data
printf ("A = 0x%x\n", a);
A = f;
printf ("%d\n", a);
return 0;
//}
Signed to no sign
Char A =-5
unsigned int b=4;
1111 1111 1111 1111 1111 1111 1111 1011
0000 0000 0000 0000 0000 0000 0000 0100
//------------------------------------------
0000 0000 0000 0000 0000 0000 0000 1111 = 1
int main (int argc,const char *argv[])
//{
Char a=-5;
unsigned int b=4;
printf ("%d\n", a+b);
return 0;
//}
All equipment Documents
int main (int argc,const char *argv[])
{
int A, B;
scanf ("%d%d", &a, &b);//read data from a standard input file, parse the data into a variable stdin
printf ("a =%d B =%d\n", A, b);//write data to the standard output file stdout
return 0;
}
ios-c_day3___ Operators and expressions