1 / 58

Introduction

Introduction. Data types Int, char, float, double …… Variables and constants Basic data objects manipulated in program Declarations Operators Arithmetic,Relational,Logical,Bitwise…… Expressions ,statements Combine variables and constants with operators Precedence and Order of Evaluation

amora
Download Presentation

Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction • Data types • Int, char, float, double …… • Variables and constants • Basic data objects manipulated in program • Declarations • Operators • Arithmetic,Relational,Logical,Bitwise…… • Expressions ,statements • Combine variables and constants with operators • Precedence and Order of Evaluation • Type conversions

  2. Data Types and Sizes • There are only a few basic data types in ANSI C: • char (1 byte) • int (2/4 bytes depend on hardware) • long [int] (4 bytes) • short [int] ( 2 bytes) • signed/unsigned [int] (2 bytes) • float (4 bytes) • single-precision floating point • double (8 bytes) • double-precision floating point

  3. Variables • Variable Name • Names are made up of letters 、digits and underscore. • The first character must be a letter or underscore. • Upper case and lower case letters are distinct. • You can’t use reserved keywords as variable names. • Such asif 、else、for…..

  4. Select all of the legal variable names shown as the following • INT k6 xy_0 _next_ • -bcd integer *p do • 2ky my_name long

  5. Variables • All variables must be declared before use. • for example int a=5,b,c; b=10; c=a+b; • A declaration specifies a type, and assigns certain space to the variables. • A variable may also be initialized in its declaration by an assign sign.

  6. Variables-integer • Int as data type declaration

  7. Variables-float point Float or double as data type declaration

  8. Variables-character • Char as data type declaration • Each character have one byte storage • for example char c, cc; c=‘a’; cc=‘9’;

  9. Constants-Integer • Integer constant • (Short) integer constant is an int ,as 1234. • Range: -32768 -- +32767 • Long integer constant • is written with a terminal l or L, as 1234567L; • If an iteger is too big to fit into an int will be taken as a long. • Range:-2147483648 -- 2147483647

  10. Constants-Integer • Value of an integer constant can be specified in three ways • Decimal • Octal • leading 0 • Hexadecimal • leading 0x or 0X; • Example 31(Dec) 037(Oct) 0x1f(Hex)

  11. Constants-floating point • Floating-point constant • Decimal • Contain a decimal point • As 0.123 .123 123.0 123. • Exponent • As 123e3 123E4 • illegal exponent: e3 2.1d3.5 .e3 e • for example float f; f = 123.45;

  12. Constants-character • Chatacter constant • Written as one character within single quotes, such as ‘x’,‘a’ etc. • The value of a character constant is the numeric value of the character in the character set (ASCII code) • for example: char c=‘A’; equals to char c=65; char b=‘0’; equals to char c=48;

  13. Constants-character • Certain characters can be represented in character and string constants by escape sequences (\),such as ‘\n’ • Look like two characters but represent only one • An arbitrary byte-sized b it pattern can also be specified by ‘\ooo’ or ‘\xhh’ • Ooo is one to three octal digits(0…7) • Xhh is one or more hexadecimal digits (0…9,a…f,A…F) • For example: char c=‘A’; equals to char c=65; also equals to char c=‘\101’; or char c=‘\x41’;

  14. Constants-character • Complete set of escape sequence

  15. Select all of the legal constants shown as the following • 7356 -0496 0x3A1 8b 6L • 3.92 0456 '\“’ E8 3e-5.6 • 8e-002 0.78e+004 '14' '101' '\55'

  16. Constants-string • String constant(literal) • A sequence of zero or more characters surrounded by double quotes • Such as “hello!” • A string constant is an array of characters,each character have one byte storage as in the following table • The internal representation of a string has a null character ‘\0’ at the end, so “x” is different to ‘x’

  17. Constants-symbolic • Symbolic constants is defined to be a particular string of characters by a #define line #define name replacement text #define PI 3.1415926 • For example: • Just only a replacement #define PRICE 35 #define NUM 10 #define SUM PRICE*NUM main ( ) { printf(“total=%d\n”,SUM);} result: total= 350

  18. Declarations • All variables must be declared before use • A declaration specifies a type, and contains a list of one or more variables of that type as in int lower, upper, step; char c, line[100]; • Variables can be distributed among declaration in any fashion • A variable may be initialized in its declaration as in int limit =100; char esc = ‘\\’;

  19. Operators • Operators specify what is to be done to the objects,such as variables,constants or expressions • Types • Arithmetic operators • Increment and Decrement operators • Relational operators • Logical operators • Bitwise operators • Assignment operators

  20. Arithmetic Operators • Five basic arithmetic opertators: • + - * / % • Integer division truncates any fractional part. • For example 4/3=1 3/4=0 But 3./4=0.75 • The % operator can only be applied to integer. • For example 10%6=4

  21. int n=10; n++; /*++n;*/ int n=10; n=n+1; Increment&Decrement Operator • Increment operator • The increment operator “++” adds 1 to its operand • Decrement operator • The decrement operator “--” subtracts 1 to its operand. • ++ and -- may be used either as prefix operators (++n/--n), or postfix operators (n++/n--). In both cases , the effect is to increment n or decrement n. • for example

  22. Increment&Decrement Operator • Surely, the prefix operator ++ are different from postfix operator ++ • The expression ++n increments n before its value is used, while n++ increments n after its value has been used. • especially some other operators and called for together as in x=y++; • Increment&Decrement operator can only be applied to integer variables; • expression like (i+j)++ is illegal

  23. Increment&Decrement Operator • for example • main() • { • int a,c; • a = 5; • c = + + a; • c = c + a + +; • printf (“ a = %d \tc = %d\n”,a,c); • } ? a=7 c=12

  24. Relational Operators • Six relational operators > >= < <= == != • The anterior four operators have the same precedence. == and != have lower precedence. • Relational expressions • means expressions connected by relational operators • It’s value can only be true(1) of false(0)

  25. Relational Operators • for example • Notes 1) If the operand is a character, it depends on it’s value of ASCII code, so 70<‘A’ is false while ‘0’>0 is true main( ) { int a=4,b=8; printf(“%d %d”,a>b,a<b); }

  26. Relational Operators • ‘=’ is different from ’==’ “a = b” is not equals to “a==b”; • Function fabs() is usually used to compare two floatpoints , for example, we often writes if(1/4.==0.25) as fabs(1/4.0-0.25)<1e-6

  27. Logical operators • Three logical operators • ! && || • Relational expressions • means expressions connected by logical operators a&&b a||b !b • It’s operand is regarded as non-zero/true or zero/false • It’s value can only be true(1) of false(0) too. • Notes 1) Expressions connected by && or || are evaluated left to right, and evaluation stops as soon as the truth or false hood of the result is known.

  28. Logical operators • 2) If judge a mathematical domain ,you should use logical expressions instead of relational expressions. • for example: • 1<x<=10 should be written as • 1<x&&x<=10 .but why? main() { int x, y, z, w; x = y = z = -1; w = (+ + x || + + y) && + + z; printf (“ %d, %d, %d, %d\n ”, x, y, z, w); } ? 0, 0, -1,0 1<x<=10 is always true

  29. Bitwise Operators • C provides six operators for bit manipulation; these may only be applied to integral operands, that is char,short,int,and long,whether signed or unsigned. • &: bitwise AND, is often used to mask off some set of bits. N=n&0177 • |: bitwise inclusive OR, is used to turn bits on. • ^: bitwise exclusive OR, sets a one in each bit position where its operanda have different bits, and zero where they are the same.

  30. Bitwise Operators 4. <<: left shift, filling vacated bits with zero. 5. >>: right shift, filling vacated bits with zero. 6. ~: yields the one’s complement of an integer, converts each 1-bit into a 0-bit and vice versa. • For example • Write a function to combine integer x and y to a new integer z, assume z’s high-byte is y’s low-byte and z’s low-byte is x’s low-byte

  31. Bitwise Operators • x=0x0532, y=0xabcd • x y • 00000101001100101010101111001101 • & & • 0000000011111111(0x00ff) 0000000011111111 • 0000000000110010 0000000011001101 • <<8 • 1100110100000000 • 11001101 00110010 • c d 3 2

  32. Bitwise Operators • main () • { unsigned x,y,z; • x = 0x0532; y = 0xabcd; • z = ( x&0x00ff ) | (y&0x00ff)<<8; • printf ( “z = %x \n”, z ); • } • Result: z = cd32

  33. Assignment Operators • Assignment operator “=“ • Such asa=a+1 • Combine operator • “op=“, as in i*=j+1, where op is one of the following binary operators: + - * / % << >> & ^ | • if expr1 and expr2 are expressions, then • expr1 op= expr2 is equivalent to • expr1=(expr1) op (expr2) • So i*=j+1 means i=i*(j+1) rather than i=i*j+1 • For example • if x=6, then what is the value of x+=x-=x*x?

  34. Expressions Combine the operands with operators or braces It’s operand can be variables,constants or even functions Statements Can be regarded as expressions ended with semicolon Be made up of expressions expression statement a++ a++; b=a+1 b=a+1; Therefore a = ( b = 4 ) + ( c = 6 ); right ,however a = ( b = 4;) + ( c = 6; ); wrong Expressions and Statements

  35. Precedence and Order of Evaluation Note: In Order2 “+ - * ” is different from order 3&4 ”+ -”

  36. Precedence and Order of Evaluation

  37. *Comma operator • Combine more than one expressions to statements • Compute from left to right • The value of the comma expression is the rightest expression’s value • example int a; if a=(a=3*5,a*4),a+10; then a=? else if a=((a=3*5,a*4),a+10); then a=?

  38. When an operator has operands of different types,they are converted to a common type according to rules Often automatically convert a “narrower” operand into a “wider” operand Otherwise ,results may be wrong Long double Double Float Unsigned long Long Unsigned int Int Char,short high low Type Conversions-automatic

  39. 10 + ‘ a ’ + I * f - d / e int double char float float int int double float float double Type Conversions-automatic • Illustration

  40. Type Conversions-coerced • Explicit type conversions can be forced(coerced) in any expressions, with a unary operator called a cast • ( type-name) expression • For example • ( double ) a • ( float ) ( x + y ) • ( int ) 4.5 / 3

  41. Type Conversions • Note • If conversions take place across assignments, the value of the right side is converted to the type of the left, which is the type of the result. • If the type of the right side is “wider” than the left side, value maybe be truncated in implementation. • example

  42. Type Conversions-example • main() • { • char c; • int i; • i=11361; /* 0x2c61*/ • c=i; /* 0010110001100001*/ • printf(“%c %d %x”, c, c, c); • }

  43. Putchar Int putchar(int) puts the character on the standard output, which is by default the screen. Returns the character written, or EOF if an error occurs. example #include “stdio.h” main ( ) { char a; a=‘b’; putchar(65); putchar(‘\n’); putchar(a); } Standard Input and Output

  44. Getchar int getchar (void) read one character at a time from the standard input. returns the next input character each time it is called, or EOF when it encounters end of file. The symbolic constant EOF is defined in <stdio.h>. example #include “stdio.h” main() { char c; c=getchar(); putchar(c-32); } Standard input and Output

  45. Formatted Output-Printf • Printf converts, formats,and prints its arguments on the standard output under control of the format. • int printf(char *format, arg1,arg2,…) • Returns the number of characters printed • The format string contains two types of objects: • Ordinary characters: are copied to the output stream. • Conversion specifications • Each conversion specification begins with a % and ends with a conversion character.

  46. Printf-basic conversions

  47. Formatted Output-Printf • Between the % and the conversion character there may be: • - specifies left adjustment of the converted argument, right adjustment default • m specifies the minimum field width. • m.n specifies the maxium number of characters to be printed from a string, or the number of digits after the decimal point of a floating-point value. • l specifies the integer to be printed as a long or h as a short

  48. Printf-example • main() • { • int i = -5; • float j = 98; • printf ( “i = %d, j = %-10.2f \ n ”, i, j ); • } • result: i = - 5, j =98.00

  49. Printf-example • main ( ) • { int a = 345, b = -1; • printf (“ a = %d, %x, %o \n ”, a, a, a ); • printf (“b = %u %o\n ”, b ); • } • result: • a = 345, 159, 531 • b = 65535 17777

More Related