introduction n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Introduction PowerPoint Presentation
Download Presentation
Introduction

Loading in 2 Seconds...

play fullscreen
1 / 58

Introduction - PowerPoint PPT Presentation


  • 133 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Introduction' - amora


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
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
  • Type conversions
data types and sizes
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
variables
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…..
select all of the legal variable names shown as the following
Select all of the legal variable names shown as the following
  • INT k6 xy_0 _next_
  • -bcd integer *p do
  • 2ky my_name long
variables1
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.
variables integer
Variables-integer
  • Int as data type declaration
variables float point
Variables-float point

Float or double as data type declaration

variables character
Variables-character
  • Char as data type declaration
  • Each character have one byte storage
  • for example

char c, cc;

c=‘a’; cc=‘9’;

constants integer
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
constants integer1
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)

constants floating point
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;

constants character
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;

constants character1
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’;

constants character2
Constants-character
  • Complete set of escape sequence
select all of the legal constants shown as the following
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'
constants string
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’
constants symbolic
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

declarations
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 = ‘\\’;

operators
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
arithmetic operators
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
increment decrement operator

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
increment decrement operator1
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
increment decrement operator2
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

relational operators
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)
relational operators1
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);

}

relational operators2
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

logical operators
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.

logical operators1
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

bitwise operators
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.
bitwise operators1
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
bitwise operators2
Bitwise Operators
  • x=0x0532, y=0xabcd
  • x y
  • 00000101001100101010101111001101
  • & &
  • 0000000011111111(0x00ff) 0000000011111111
  • 0000000000110010 0000000011001101
  • <<8
  • 1100110100000000
  • 11001101 00110010
  • c d 3 2
bitwise operators3
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
assignment operators
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?
expressions and statements
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
precedence and order of evaluation
Precedence and Order of Evaluation

Note: In Order2 “+ - * ” is different from order 3&4 ”+ -”

comma operator
*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=?

type conversions automatic
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
type conversions automatic1

10 + ‘ a ’ + I * f - d / e

int

double

char

float

float

int

int

double

float

float

double

Type Conversions-automatic
  • Illustration
type conversions coerced
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
type conversions
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
type conversions example
Type Conversions-example
  • main()
  • {
  • char c;
  • int i;
  • i=11361; /* 0x2c61*/
  • c=i; /* 0010110001100001*/
  • printf(“%c %d %x”, c, c, c);
  • }
standard input and output
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
standard input and output1
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
formatted output printf
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.
formatted output printf1
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
printf example
Printf-example
  • main()
  • {
  • int i = -5;
  • float j = 98;
  • printf ( “i = %d, j = %-10.2f \ n ”, i, j );
  • }
  • result: i = - 5, j =98.00
printf example1
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
printf example2
Printf-example
  • main ( )
  • { float x = 345.678;
  • printf (“ x = %f %e %g \ n ”, x, x, x );
  • printf ( “ %f %% \ n ”, 1.0/3 );
  • }
  • Result:
  • x = 345.678009 3.45678e+02 345.678
  • 0.333333%
printf example3
Printf-example
  • printf(“format”, “hello, world”);, format as:
printf exercise
Printf-exercise
  • main()
  • {
  • char x,y,z,w;
  • x=‘e’;
  • y=x+2;
  • z=x-3;
  • w=x-’a’+’A’;
  • printf(“x=%c y=%c z=%c w=%c”,x,y,z,w);
  • } ?
formatted input scanf
Formatted Input-Scanf
  • Scanf reads characters from the standard input, interprets them according to the specification in format, and stores the results.
  • int scanf(char *format, arg1,arg2,…)
    • Basic scanf conversions(*format) is similar to the format in “printf”
    • Each of the argument must be a pointer or variable address
    • Stops when it exhausts its format string,or when some input fails to match the control specification
scanf example
Scanf-example
  • int a,b;
  • scanf(“%d %d”,&a,&b) input: 3 4
  • int a,b;
  • scanf(“%d%d”,&a,&b) input: 3 4
  • int a,b;
  • scanf(“%d,%d”,&a,&b) input: 3,4
  • int a,b;
  • scanf(“a=%d b=%d”,&a,&b) input: a=3 b= 4
  • char c1,c2,c3;
  • scanf(“%c%c%c ”,&c1,&c2,&c3) input: abc not a b c
  • float f;
  • scanf(“%f “,&f) input: 2.56

scanf(“%7.2f”,&f) is wrong

scanf
Scanf
  • If input 12345,abc, then what is the result of the following program

main()

{

int a;

char ch;

scanf(“%3d%3c”, &a,&ch);

printf(“%d,%c”,a,ch);

} ?

scanf1
Scanf
  • there is the statement

scanf(“%d%c%f”,&a,&b,&c);

If the list entries are 2223a123o.12, then the value of a,b,c are ?

  • Note: if use scanf to input data, inputs end when one of the following cases occur:
  • Input space, enter or tab key
  • Width limits
  • Illegal input
  • Illegal input can cause incorrect results
scanf2
Scanf
  • main()
  • {
  • int a,b,c;
  • scanf(“%2d%4d%2d”,&a,&b,&c);
  • printf(“a=%d,b=%4d,c=%2d”,a,b,c);
  • }
  • If input 19902548 then output is ?
  • If input 87236dw6 then output is ?