Morteza Noferesti Have the same meaning as variables in algebra - - PowerPoint PPT Presentation

morteza noferesti
SMART_READER_LITE
LIVE PREVIEW

Morteza Noferesti Have the same meaning as variables in algebra - - PowerPoint PPT Presentation

Morteza Noferesti Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a = 5 x = a + b valid identifier in C may be given


slide-1
SLIDE 1

Morteza Noferesti

slide-2
SLIDE 2

 Have the same meaning as variables in algebra

  • Single alphabetic character
  • Each variable needs an identifier that distinguishes it from the others

 a = 5  x = a + b

 valid identifier in C may be given representations

containing multiple characters

  • A-Z, a-z, 0-9, and _ (underscore character)
  • First character must be a letter or underscore (no, _no 9no)
  • Usually only the first 32 characters are significant
  • There can be no embedded blanks (student no)
  • Identifiers are case sensitive (area, Area, AREA, ArEa)
  • Keywords cannot be used as identifiers
slide-3
SLIDE 3

 Before using a variable, you must declare it

 All variables must be defined with a name and a data type.

  • Data_Type Identifier;

 int width; // width of rectangle  float area; // result of calculating area stored in it  char separator; // word separator

  • Data_Type Identifier = Initial_Value;

 int width = 10; // width of rectangle  float area = 255; // result of calculating area stored in it  char seperator = ‘,’; // word separator

  • Data_Type Identifier, Identifier, Identifier;
  • int width, length, temporary;
  • float radius, area = 0;
slide-4
SLIDE 4

 When we declare a variable

  • Space is set aside in memory to hold a value of the specified

data type

  • That space is associated with the variable name
  • That space is associated with a unique address

 Visualization of the declaration

  • int width = 95; // get width form user
  • // &width is 22ff40
  • // *&width is 95
  • // sizeof width is 4
slide-5
SLIDE 5

 Minimal set of basic data types

  • primitive data types

 int  float  double  char  Void

 The size and range of these data types may vary among

processor types and compilers

slide-6
SLIDE 6

 Modify the behavior of data type to which they are

applied:

  • Size qualifiers: alter the size of the basic data types:

 short: multiply by 0.5  long: multiply by 2  short can be applied to: int  long can be applied to: int and double

  • Sign qualifiers: can hold both positive and negative numbers,
  • r only positive numbers.:

 signed: + and -  unsigned: +  they can be applied to : int and char

slide-7
SLIDE 7
slide-8
SLIDE 8

 A floating-point value contains a decimal point

  • 33.5 0.0 -657.983 .2 6.

 For example, the value 150.4582 is represented in

scientific notation as

  • 1.504582 X 102

 and is represented in exponential notation (by the

computer) as

  • 1.504582E+02

 This notation indicates that 1.504582 is multiplied by

10 raised to the second power (E+02)

  • The E stands for “exponent”
slide-9
SLIDE 9

 Char

  • char c;
  • c = 'A'; // d = 65;

 String

  • printf("string is array of char!!!");
  • printf("example of escape sequence is \n");
slide-10
SLIDE 10

 The sizeof keyword returns the number of bytes of the

given expression or type

 returns an unsigned integer result

  • sizeof variable_Identifier;
  • sizeof (variable_Identifier);
  • sizeof (Data_Taype);

 Example:

  • int x;
  • printf("size of x = %d", sizeof x);
  • printf("size of long long = %d", sizeof(long long));
  • printf("size of x = %d", sizeof (x))
slide-11
SLIDE 11

 Type Casting In C Language. Type casting is a

way to convert a variable from one data type to another data type. For example, if you want to store a long value into a simple integer then you can type cast long to int.

slide-12
SLIDE 12

 If an operand is long double, the other

  • perand is converted to long double!

 Else if an operand is double, the other operand

is converted to double!

 Else if an operand is float, the other operand

is converted to float!

 Else if an operand is unsigned long, the other

  • perand is converted to unsigned long!

 Else if an operand is long, the other operand is

converted to long!

 Else if an operand is unsigned int, the other

  • perand is converted to unsigned int!
slide-13
SLIDE 13

char ch; int i; float f; double d; result= (ch/i) + (f*d)

  • (f+i);

int float double float double int double double double

slide-14
SLIDE 14

 May lose the data! char ch; int i; float f; double d; … ch=i; i=f; f=ch; f=i;

slide-15
SLIDE 15

sourc rce destin inatio tion The lost data char signed char

If char>127, destination will be negative

short int char

8 most significant bits

int char

8 most significant bits (16 bits system) 24 most significant bits(32bits system)

long int char 24 most significant bits int short int No lost data! (16 bits system) 16 most significant bits(32 bits system) long int int 16 most significant bits No lost data! (16 bits system) float int The mantissa part double float Precision lost & the data is rounded long double double Precision lost & the data is rounded

slide-16
SLIDE 16

sizeof

  • f(int)

sizeof eof(char)

slide-17
SLIDE 17

#include <stdio.h> int main() { int num; int a=10,b=3; float c; c=a/b; printf("the result without casting %f\n",c); c=(float)a/b; printf("the result with casting %f\n",c); return 0; } #include <stdio.h> int main() { float a=10; int b=3; float c=a/b; printf("%f",c); printf("%d", a/b); }

slide-18
SLIDE 18

 This function provides for formatted output to the screen. The

syntax is:

  • printf("format", var1, var2, … ) ;

 The format includes, some text and conversion specifications  A conversion specifier begins with the % character. After the

% character come the following in this order:

  • [flags]:control the conversion(optional)
  • [width]:the number of characters to print(optional)
  • [.precision]:the amount of precision to print for a number type(optional)
  • [modifier]:overrides the size (type) of the argument(optional)
  • [type]:the type of conversion to be applied(required)

 Example:%[flags][width][.precision][modifier]type

slide-19
SLIDE 19
slide-20
SLIDE 20
slide-21
SLIDE 21
slide-22
SLIDE 22

 These functions take input in a manner that is specified by

the format argument and store each input field into the following arguments in a left to right fashion

 Each input field is specified in the format string with a

conversion specified which specifies how the input is to be stored in the appropriate variable

 Other characters in the format string specify characters that

must be matched from the input, but are not stored in any of the following arguments

 If the input does not match then the function stops scanning

and returns

 A white space character may match with any white space

character (space, tab, carriage return, newline, vertical tab, or form feed) or the next incompatible character

slide-23
SLIDE 23
slide-24
SLIDE 24
slide-25
SLIDE 25

 The conditional operator (?:) is used to simplify

an if/else statement

  • Condition ? Expression1 : Expression2;

 The statement above is equivalent to:

if (Condition) Expression1; else Expression2;

 Which are more readable?

slide-26
SLIDE 26

 Example:

if/else statement: if (total > 12) grade = ‘P’; else grade = ‘F’; conditional statement: (total > 12) ? grade = ‘P’: grade = ‘F’; OR grade =( total > 12) ? ‘P’: ‘F’;

slide-27
SLIDE 27

 Example:

if/else statement: if (total > 12) printf(“Passed!!\n”); else printf(“Failed!!\n”); Conditional Statement: printf(“%s!!\n”, total > 12 ? “Passed”: “Failed”);

slide-28
SLIDE 28

 The rules specify which of the operators will be

evaluated first

  • For example: x = 3 * a - ++b%3;

Precedence Operator Associativity Level 1 (highest) () left to right 2 unary right to left 3 * / % left to right 4 + - left to right 5 (lowest) = += -= *= /= %= right to left

slide-29
SLIDE 29

 how would this statement be evaluated?

  • : x = 3 * a - ++b % 3;
  • What is the value for X, for: a = 2, b = 4?

x = 3 * a - ++b % 3; x = 3 * a - 5 % 3; x = 6 - 5 % 3; x = 6 – 2; x = 4;

slide-30
SLIDE 30

 If we intend to have a statement evaluated differently

from the way specified by the precedence rules, we need to specify it using parentheses ( )

  • x = 3 * a - ++b % 3;

 Consider having the following statement:

  • x = 3 * ((a - ++b)%3);
  • the expression inside a parentheses will be evaluated first

 The

inner parentheses will be evaluated earlier compared to the outer parentheses

slide-31
SLIDE 31

 how would this statement be evaluated?

  • x = 3 * ((a - ++b)%3);
  • What is the value for X, for: a = 2, b = 4?

x = 3 * ((a - ++b) % 3); x = 3 * ((a - 5) % 3); x = 3 * ((-3) % 3); x = 3 * 0; x = 0;