1、Programming In C Programming In C The movie “Roaring Across the Horizon” Programming In C in mathematics you use x for multiplication, in C you use *. The * is used rather than to avoid any possible confusion with a variable called x. Note also that /, rather than , is used for division. Programming
2、 In C #include stdio.h void main() char s=7; int n=4; printf(7/4=%dn, s / n); printf(7.0/4=%fn,(float) s / n); output: 7/4=1 not 1.75 7.0/4=1.750000 Programming In C The remainder operation is very interesting in C. The remainder is also called the modulus. Each time the hour hand reaches the 12 ocl
3、ock, and it indicates that it has rotated once and the modulus becomes 0. Starting from 0, the modulus will increase by 1 in turn. Therefore, the modulus of the hour hand ranges from 0 to 11; the modulus of the minute hand ranges from 0 to 59. Programming In C #include stdio.h #include math.h void m
4、ain() char s=A, t; / generate the random capital letter t=s+rand() % 26; printf(s=%c,t=%cn,s,t); Programming In C #include stdio.h #include math.h void main() char s=a, t; /generate the random lowercase letter t=s+rand() % 26; printf(s=%c,t=%cn,s,t); Programming In C #include stdio.h #include math.h
5、 void main() char s=0,t; / generate the numeric symbols t=s+rand() % 10; printf(s=%c,t=%cn,s,t); Programming In C How to simulate and generate a license plate number in Kunming? The modulus operation has not only the special operators, but also the restrictions on the two operands involved in the op
6、eration. It can only operate on integers, that is, integer and data of character type can find the modulus. The floating numbers cannot find the modulus. Find the modulus for a negative number, and the sign of the result is the same as the number before the percent. Programming In C -7%2 : -1 15%-4
7、: 3 With the operations of division and modulus, how to take out the number of each digit of an integer? 518/100 5 518/10%10 1 518%10 8 Type conversions and casts When doing calculations involving mixed data types, C ranks the data types in this order: promotion or widening of the data For calculati
8、ons involving mixed data types, C automatically converts the value in the lower data type to a higher type. Promotion will cause no loss of data, because the higher data types occupy more memory than the lower types and can therefore hold the data precisely. Type conversions and casts Sometimes the
9、automatic type conversion does not conform to our calculation intention. It requires the use of cast operations. (dataType) expression (float)(s/4) (float)s /4 1.000000 1.750000 Note: The operand of the cast operation is the value of the expression, not the variable itself. Operator precedence Consi
10、der the following statement: 2 + 7 * 8 = 72 var 2 + 7 * 8 = 58 var Operator precedence 1) *, / have a higher priority than + 、 - 2) Expressions containing operators of the same precedence are evaluated according to their associativity 3) use () to change the order of evaluation Programming In C #inc
11、lude stdio.h void main() char d=0; int a=9,b=-5,c=23; float x; x=(a*a+c/b)%d-3.5f/(d-20); printf(x=%fn, x); x=(a*a+c/b)%d-3.5f/(d-20); (9*9+23/-5)%48-3.5/(48-20) (81+23/-5)%48-3.5/(48-20) (81-4)%48-3.5/(48-20) 77%48-3.5/(48-20) 29-3.5/(48-20) 29-3.5/28 29-0.125 28.875 Programming In C The common mat
12、hematical operations, such as square root, exponent, logarithm and trigonometric function, there are standard library functions in C to complete the corresponding calculations. We only need to call library functions correctly in expressions during programming. When calling a library function, the fu
13、nction name must be correct, and the data type, number, and order of the arguments must be consistent with the system regulations. The function name is provided by the system. Programming In C Before calling a standard library function, you usually use precompiled commands to include the header file
14、 of the function in the source codes. sqrt(n) pow(a, b) fabs(x) sin(x) rand() #include “math.h” Programming In C Function Description Example Returned value Comments sqrt(x) Square root of x sqrt(16.000) 4.000000 An integer values of x results in a complier error pow(x,y) x raised to the y power (xy
15、) pow(2,3) pow(81,.5) 8.000000 9.000000 Integer values of x and y are permitted exp(x) e raised to the x power (ex) exp(-3.2) 0.040762 An integer value of x results in a complier error Programming In C Function Description Example Returned value Comments log(x) Natural log of x base e log(18.697) 2.
16、928363 An integer value of x results in a complier error log10(x) Common log of x base 10 log(18.697) 1.271772 An integer value of x results in a complier error fabs(x) Absolute value of x fabs(-3.5) 3.5000000 An integer value of x results in a complier error abs(x) Absolute value of x fabs(-2) 2 An
17、 floating-point value of x return a value of 0 Programming In C #define PI 3.14159 ps=2*cos( 2*PI/3*(a+b) * sin(PI/2*(a-b) ); x=( -b+sqrt( b*b-4*a*c ) )/(2*a); The symbols in the C expressions are written on one line, without subscripts, exponents, fractional line and other forms. The multiplication sign in algebra can be omitted, but * must be written in C expressions. Only parentheses can be used to change the precedence, not brackets or braces. Programming In C