data types
play

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


  1. Data Types by Erol Seke For the course “ Introduction to Programming ” OSMANGAZI UNIVERSITY

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

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

  4. Another Simple C Program Declare three int s #include <stdio.h> global int a; Assign values to a and b int main(void){ local int b=12, c; a=3; b=5; An arithmetic operation. c=a+b; The result is assigned to c printf(“Result=%d”,c); return 0; 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 statement s 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

  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 stamped_papers Employee(papers_you_handed_in) type function_name(arguments) { { ≡ … photocopying, stamping, phone calls etc … …code to do something… return the resulting_papers return the_result } }

  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>; optional When not initialized it should be assumed that it has a random value (tbdl) examples: int a; /* creates an integer named a */ ; ends declaration 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; 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 )

  7. Memory Map Computers’ memory can be imagined as a stack of boxes each one of which can hold a number bytes 0 each box has a unique number ranged from 0 to K-1 each box consists of 8 smaller box each small box 0 1 1 0 0 1 0 1 can assume one of two possible 0 values M-1 1 bits M M+1 M+2 So, possible 256 combinations for each box can range M+3 from 0 0 0 0 0 0 0 0 = 0 d to K-2 1 1 1 1 1 1 1 1 = 255 d K-1 Therefore value of a byte can be 0 to 255 ( or -127 to 127) addresses

  8. Since the range (-127 to 127) is not enough for many applications, bytes are combined to carry bigger numbers M 01001010 0100101010010111 now we have 16 bits which can represent numbers from 0 to 65535 (or -32767 to +32767) M+1 10010111 similarly, we can have 32 bit numbers like M 10010111 10010111010010100011100111010101 M+1 01001010 ranging from 0 to 4294967295 (or -2147483647 to +2147483647) M+2 00111001 M+3 11010101 Note that the numbers made up this way are all integers. That’s why we call them integers.

  9. Integer types in C byte, char Type Range 2 bytes word, int 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 double word, 4 bytes unsigned int int , long 0 to 65535 -2147483647 signed long or long 2147483647 unsigned long 0 to 4294967295 -9223372036854775807 signed long long 9223372036854775807 unsigned long long 0 18446744073709551615 long long (new) 8 bytes

  10. Floating Point Numbers 32 bit and 64 bit floating point data types are defined by ANSI/IEEE Standard 754-1985 0 1 8 9 31 s e f    s e 127  ( 1 ) 2 ( 1 f ) s 0<e<255 f = NaN 255 ≠0    s 255 0 ( 1 ) float   s 0 0 ( 1 ) 0    s e 126  0 ≠0 ( 1 ) 2 ( 0 f ) 0 1 11 12 63 s e f     s e 1023 ( 1 ) 2 ( 1 f ) s 0<e<2047 f = NaN 2047 ≠0 double   s  2047 0 ( 1 )   s 0 ( 1 ) 0 0    s e 1022  0 0 ( 1 ) 2 ( 0 f ) we also have long double s (e:15 bits, f:64 bits)

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

  12. desig solution plan problem n Create your human readible code Coding Ready to use code int foo(int x){ Source code float a; ... int fbb(int x){ } int b; ... return y; } Compile (Convert to machine code) Compilation Libraries Project Building Link (connect/add ready-to-use code) Linking Executable Debug/Test

  13. Visual C++ Start MS Visual C++ enter project name select type Create a new console application

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

  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

  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; 3 ... 4.75 129000 will print printf("%d\n%f %ld\n",i,x,L); 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 

  17. More Functions Functions can be called from other functions, even from themselves. int SumThem(int x, int y){ functions make programs int int z; more intelligible and SumThem (int z=x+y; ≡ manageable  x, int y return x+y; ){ int z; } z =x+y;return ≡ z;} int SumThem(int x,int y){return x+y;} returned value is assigned to a #include <stdio.h> a = SumThem(b,c) int main(void){ int a,b=3,c=5; 1. Function SumThem is called with arguments b and c a=SumThem(b,c); 2. The return value of the function is assigned to a printf("%d",a); return 0; } 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend