Data Types by Erol Seke For the course Introduction to Programming - - PowerPoint PPT Presentation

data types
SMART_READER_LITE
LIVE PREVIEW

Data Types by Erol Seke For the course Introduction to Programming - - PowerPoint PPT Presentation

Data Types by Erol Seke For the course Introduction to Programming OSMANGAZI UNIVERSITY MATLAB to C 1. Syntax is a little bit different. 2. You need to write a complete program to run. we could write scripts (a sequence of commands) in


slide-1
SLIDE 1

Data Types

by Erol Seke For the course “Introduction to Programming”

OSMANGAZI UNIVERSITY

slide-2
SLIDE 2

MATLAB to C

  • 1. Syntax is a little bit different.
  • 2. You need to write a complete program to run.

we could write scripts (a sequence of commands) in MATLAB. we had Command Window to run individual commands etc.

  • 3. Functions are important again with a little bit different syntax.
  • 4. You need to declare every variable.

in MATLAB they are automatically created when values assigned to them

  • 5. C programs start from a special function named as main().

MATLAB programs start from any function or script.

function return_val = main() fprintf('Hello MATLAB world'); return_val = 5; return; end #include <stdio.h> int main(void){ printf("Hello C world"); return 5; }

slide-3
SLIDE 3

Minimal Components of a C Program

#include <stdio.h> int main(void){ printf("Hello C world"); return 0; } This line gives information to compiler that it should take this file into consideration By ANSI-C standard, function main returns an int void type means ‘nothing’ here This line outputs (on the screen) Many compilers allow omitting of code shown in red Keep in mind that that is non-ANSI. Hello C world All C programs must have a function main where the program starts int main(void){ return 0; } A valid but useless C program A simple program which does something at least

slide-4
SLIDE 4

Another Simple C Program

#include <stdio.h> int a; int main(void){ int b=12, c; a=3; b=5; c=a+b; printf(“Result=%d”,c); return 0; } Declare three ints Assign values to a and b An arithmetic operation. The result is assigned to c Output the result Note that; 1. Variables are declared at the beginning of the function before any code 2. Variables declared outside of all functions are called global variables 3. = means assignment (from right to left) 4. All statements are ended with a semicolon ; 5. Multiple statements can be on a single line. 6. A statement can span multiple lines as long as they are split at valid points global local

slide-5
SLIDE 5

Functions (C means functions)

Think of a function as a service desk in goverment office where;

  • 1. You hand in some papers as input (sometimes you don’t)
  • 2. Goverment employee returns you some processed papers (sometimes they don’t)
  • 3. You just wait for them to do something for you

type function_name(arguments) { …code to do something… return the_result } stamped_papers Employee(papers_you_handed_in) { … photocopying, stamping, phone calls etc … return the resulting_papers }

slide-6
SLIDE 6

Creation of Basic Data Types in C

In order to use data types in C, we need to create them first. This is called declaration. type name <=initializer>;

  • ptional

examples: int a; /* creates an integer named a */ When not initialized it should be assumed that it has a random value (tbdl) This is a comment. Comments do not affect the flow of the program int a, A=3, counter=100; /* multiple declarations at once */ long Long; /* C distinguishes upper and lower cases */ float distance21=5.43; double x, y, xyz=11.45; ; ends declaration Naming Rules : 1. Names start with an English letter or _ (underscore) 2. Names consist of letters, numbers and/or underscore characters 3. Names must be unique within scope 4. C distinguishes upper and lower case 5. C-keywords can not be names 6. Length of a name can be 256 characters (but first 32 is significant in old compilers)

slide-7
SLIDE 7

Memory Map

Computers’ memory can be imagined as a stack of boxes each one of which can hold a number M-1 M M+1 M+2 M+3 K-1 K-2 each box has a unique number ranged from 0 to K-1 1 1 1 1 each box consists of 8 smaller box 1 each small box can assume one

  • f two possible

values So, possible 256 combinations for each box can range from bytes bits 1 1 1 1 1 1 1 1 to = 255d = 0d Therefore value of a byte can be 0 to 255 ( or -127 to 127) addresses

slide-8
SLIDE 8

Since the range (-127 to 127) is not enough for many applications, bytes are combined to carry bigger numbers 01001010 10010111

0100101010010111

M M+1 now we have 16 bits which can represent numbers from 0 to 65535 (or -32767 to +32767) 10010111 01001010 00111001 11010101 M M+1 M+2 M+3 similarly, we can have 32 bit numbers like 10010111010010100011100111010101 ranging from 0 to 4294967295 (or -2147483647 to +2147483647) Note that the numbers made up this way are all integers. That’s why we call them integers.

slide-9
SLIDE 9

Integer types in C

Type Range signed char

  • 127 to 127

unsigned char

0 to 255

signed short

  • 32767 to 32767

unsigned short

0 to 65535

signed int or int

  • 32767 to 32767

unsigned int

0 to 65535

signed long or long

  • 2147483647

2147483647

unsigned long

0 to 4294967295

signed long long

  • 9223372036854775807

9223372036854775807

unsigned long long 0

18446744073709551615

byte, char word, int double word, int, long 2 bytes 4 bytes 8 bytes long long (new)

slide-10
SLIDE 10

32 bit and 64 bit floating point data types are defined by ANSI/IEEE Standard 754-1985

Floating Point Numbers

s e f 1 31 8 9 s 0<e<255 f 255 ≠0

= NaN

) 1 ( 2 ) 1 (

127

f

e s

  

255

  

s

) 1 ( ) 1 (

s

 

s e f 1 63 11 12 s 0<e<2047 f 2047 ≠0

= NaN

) 1 ( 2 ) 1 (

1023

f

e s

  

2047

  

s

) 1 ( ) 1 (

s

 

≠0

) ( 2 ) 1 (

126

f

e s

  

) ( 2 ) 1 (

1022

f

e s

  

float double we also have long doubles (e:15 bits, f:64 bits)

slide-11
SLIDE 11

int L, int u; float int,a; int _15=15; int u='A'; E_l_e_c_t_r_i_c d-float Examples Determine if the following declarations are valid Determine if the following identifiers are valid A1= _16 Else 27December October.29 Terra___45 longint C++ long Long, Int; char integer=1; integer i=1; short S;long L; int i1,i2,i65540,_0_; This_is_one_damn_long_identifier This_one_is_an_invalid_identifier! ″invalid″ invâlid

  • ooöps

double__D; int i;

slide-12
SLIDE 12

Create your human readible code Compile (Convert to machine code) Link (connect/add ready-to-use code)

int foo(int x){ float a; ... }

Coding Compilation Linking Source code Libraries Executable Building

int fbb(int x){ int b; ... return y; }

Ready to use code Project

problem solution desig n plan Debug/Test

slide-13
SLIDE 13

Start MS Visual C++ Create a new console application

Visual C++

select type enter project name

slide-14
SLIDE 14

Select File-New menu again Select file type Enter file name MyHello.cpp is created and seen on an editor window workspace browser Finally enter your code in the editor window and save it

slide-15
SLIDE 15

Compile and Make (=Build) your code You should see ...0 error(s)... in the Output window If you see an error (or errors) in the output window, like You must correct them in order to be able to run your program Interpreting compiler errors can sometimes be very tricky. You should familiarize yourself to errors by doing more programming (exercise and learn)

Homework : Try these at home, but replace (void) by (void} accidentally . Correct the error, Run the program, see output

slide-16
SLIDE 16

int printf(char *format,...);

printf is one of the most complex standard library functions in C. Function prints the values of variable number of arguments, and their printing format can be specified Examples: int i=3; float x=4.75,y, long L=129000; ... printf("%d\n%f %ld\n",i,x,L); 3 4.75 129000 will print int scount; float avg=0; ... printf("The average GPA of %d students is %4.2f. Continue? ",scount, avg); Should print something like The average GPA of 34 students is 2.85. Continue? Formatting characters: %d : decimal (integer) %ld : decimal (long) %f : float %lf : double %n.mf (n, m are integers): m digits after radix, total n digits (if possible) %s, %c, %p : will be seen later Escape Characters: \n : next line \t : tab

Homework: Try to print an integer a=1 using %2.2f format 

slide-17
SLIDE 17

More Functions Functions can be called from other functions, even from themselves. int SumThem (int x, int y ){ int z; z =x+y;return z;} int SumThem(int x, int y){ int z; z=x+y; return x+y; } int SumThem(int x,int y){return x+y;}

≡ ≡

functions make programs more intelligible and manageable  #include <stdio.h> int main(void){ int a,b=3,c=5; a=SumThem(b,c); printf("%d",a); return 0; }

a = SumThem(b,c)

  • 1. Function SumThem is called with arguments b and c
  • 2. The return value of the function is assigned to a

returned value is assigned to a Notes: 1. Functions may return nothing (void) 2. The return value can be ignored if not needed. 3. printf is just like any other function and we ignore what it returns

slide-18
SLIDE 18

More Functions double PolyVal(double x){ /* given x find y and return */ double y; y=5+2.7*x+3.61*x*x; return y; }

2 2 1

x a x a a y   

A second order polynomial is given Assume that a0=5, a1=2.7 and a2=3.61 #include <stdio.h> double PolyVal(double x){ ... } int main(void){ float x1=4.7, x2=11.4, u; u=PolyVal(x1); printf("f(%f)=%f\n",x1,u); printf("f(%f)=%f\n",x1,PolyVal(x2)); return 0; } as long as valid syntax is used expressions can get as complex as you needed PolyVal accepts one double argument and returns another double. But here float is given and the returned double is assigned to a float variable. So there are two conversions. Problem here. Should use %lf

Homework: Try this example at home with third degree polynomial in PolyVal

slide-19
SLIDE 19

Type Conversions General rule : Arguments of an arithmetic expression (assignment too) are converted to bigger type Implicit/Automatic Conversions

int i; long L; float f; ... i = L + f;

since bigger type is float a temporary variable is created with the value of L before the + operation. The result is a float (in a temporary space) Type of i cannot be changed. So the float result is truncated to int and assigned to i. This triggers a compiler warning. Explicit Conversions These are the ones that we do in order to tell the compiler that “we know what we are doing” We use type cast operator for this.

int i; long L; float f; ... i = (int)(L + (long)f);

we forcibly create a temporary long variable with the truncated value of f. the result is converted to int before assignment and compiler does not warn us

slide-20
SLIDE 20

END