Anúncio
Anúncio

Mais conteúdo relacionado

Anúncio

Data Type in C Programming

  1. DATA TYPES in C Programming 1 Engr. Qazi Shahzad Ali Kyungpook National University, South Korea
  2. Four Types of Data 1- Fundamental Data Types 4- User Defined Data Types 2- Data Type Modifiers 3- Derived Data Types 2
  3. 1- Fundamental Data Types 1.1- Integer 1.2- Character 1.4- Double 1.5- Void 1.3- Float FUNDAMENTAL DATA TYPES ARE THOSE THAT ARE NOT COMPOSED OF OTHER DATA TYPES 3
  4. 2- Data type modifiers 2.1- Integer Type Modifiers 2.2- Character Type Modifiers 2.3- Floating-point Modifiers 1-They change some properties of the data type 2-The modifiers define the amount of storage allocated to the variable. Like short, double, long 4
  5. 3- Derived Data Types 3.1- Arrays 3.3- Pointers 3.4- References 3.5- Constants 3.2- Functions 5
  6. 4- User Defined Derived Data Types 4.1- Class 4.2- Structure 4.3- Union 4.4- Enumeration 6 Web link for study of Enumeration http://cplus.about.com/od/introductiontoprogramming/p/enumeration.htm
  7. The Decimal & Binary Number System Binary Decimal 0000 0 max number that can be represent by= 2n -1 0001 1 n= number of bits 0010 2 for Example if we use 1 bit then we can represent only 0 & 0011 3 1 decimal number, if 2 bit then 2 and 3, if 3 bit then max 0100 4 up to 7 (23-1=8-1=7), Similarly if we use 8 bits in case of 0101 5 “char data type” then max (28-1=256-1=255),similarly in 0110 6 case of signed 1 bit out of 8 bits are used for plus or minus 0111 7 (0 for +, & 1 for -) so (Max27-1=128-1=127) & (Min-27=-128), 1000 8 1001 9 7
  8. Name Description Size* Range* char Character or small integer. 1byte signed: -128 to 127 unsigned: 0 to 255 short int (short) Short Integer. 2bytes signed: -32768 to 32767 unsigned: 0 to 65535 int Integer. 2bytes signed: -32768 to 32767 unsigned: 0 to 65535 long int (long) Long integer. 4bytes signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 float Floating point number. 4bytes 10 -38 to 1038 (~7 digits) double Double precision floating point number. 8bytes 10 -308 to 10308 (~15 digits) long double Long double precision floating point number. 10bytes 10 -4932 to 104932 (~19 digits) 8
  9. Integer Modifier TYPE APPROXIMAT E SIZE MINIMAL RANGE SHORT 2 -32768 to 32767 UNSIGNED SHORT 2 0 to 65,535 SIGNED SHORT 2 Same as short INT 2 -32768 to 32767 UNSIGNED INT 2 0 to 65,535 SIGNED INT 2 Same as int LONG 4 -2,147,483,648 TO 2,147,483,647 UNSIGNED LONG 4 0 to 4,294,967,295 SIGNED LONG 4 Same as long Note: The prefix signed makes the integer type hold negative values also. Unsigned makes the integer not to hold negative values. 9
  10. Char Modifier TYPE APPROXIMATESIZE MINIMALRANGE CHAR 1 -128 to 127 UNSIGNED CHAR 1 0 to 255 SIGNED CHAR 1 Same as char Floating Point Modifier TYPE APPROXIMATE SIZE DIGITS OF PRECISION FLOAT 4 7 DOUBLE 8 15 LONG DOUBLE 10 19 10
  11. How to declare them { int Count; Count = 5; } { float Miles; Miles = 5.6; } { char Letter; Letter = 'x'; } Integer Declaration: Float Declaration: Character Declaration: 11
  12. Signedness • If signed, the most significant bit designates a positive or negative value leaving the remaining bits to be used to hold a designated value. Unsigned integers can only take non-negative values (positive or zero), while signed integers (default) can take both positive and negative values. • An unsigned character, depending on the code page, might access an extended range of characters from 0 → +255, instead of that accessible by a signed char from −128 → +127, or it might simply be used as a small integer. The standard requires char, signed char, unsigned char to be different types. Since most standardized string functions take pointers to plain char, many C compilers correctly complain if one of the other character types is used for strings passed to these functions  unsigned int x;  signed int y;  int z; /* Same as "signed int" */  unsigned char grey;  signed char white; 12
  13. INTEGER DATA TYPE Integers are whole numbers. Countable like # of rooms in a house C has 3 classes/Modifier of integer 1. short int 2. int 3. long int 13
  14. FLOAT DATA TYPE • Decimal/Exponential Number • Values that are measureable like length of room 145.25 inches • Store memory in two parts 1. Mantissa (The value of number takes 3 bytes) 2. Exponent (Power of number takes 1 byte) Exp: 12345 will be represented as 1.2345e4 (4 is exponent & 1.2345 is number) • They have two advantages 1: they can represent values between integers. 2: they can represent a much greater range of values. 14
  15. CHARACTER DATA TYPE • Includes every printable character on the standard English language keyboard • We can show ASCII code with the help of characters • Example of characters: – Numeric digits: 0 - 9 – Lowercase/uppercase letters: a - z and A - Z – Space (blank) – Special characters: , . ; ? “ / ( ) [ ] { } * & % ^ < > etc 15
  16. DOUBLE DATA TYPE • The data type double is also used for handling floating-point numbers. But it is treated as a distinct data type because, it occupies twice as much memory as type float, and stores floating-point numbers with much larger range and precision. It is slower that type float. 16
  17. VOID DATA TYPE • It specifies an empty set of values. It is used as the return type for functions that do not return a value. No object of type void may be declared. • It is used when program or calculation does not require any value but the syntax needs it. 17
  18. C-Software Tools In order to write a C program you will need: • An Editor - Something to write code in. • A Compiler - Converts the code into Machine Language (object files). • A Linker – Combines several object files into one executable (written in Machine Language). • A Debugger – Provides supervised execution of the code. 18
  19. • A token is a language element that can be used in forming higher level language constructs • Equivalent to a ‘word’ in English language • Several types of tokens can be used to build a higher level C language construct such as expressions and statements • There are 6 kinds of tokens in C: – Reserved words (keywords) – Identifiers – Constants – String literals (constant) – Punctuators – Operators Token 19
  20. Reserved Words/Keywords • Keywords that identify language entities such as statements, data types, language attributes, etc. • Have special meaning to the compiler • cannot be used as identifiers/variable in our program. • Should be typed in lowercase. • White in color • Total numbers=32 • Example: const, double, int, main, void, while, for, else (etc..) 20
  21. Identifiers • Words used to represent certain program entities (program variables, function names, etc). • Example: – int my_name; • my_name is an identifier used as a program variable – void CalculateTotal(int value) • CalculateTotal is an identifier used as a function name 21
  22. Punctuators (separators) • Symbols used to separate different parts of the C program. • These punctuators include: – [ ] ( ) { } , ; “: * # • Usage example: 22 void main (void) { int num=10; printf (“%d”,num); }
  23. C Constants Primary Constants Integer Constant, Real Constant, Character Constant Secondary Constants Array Pointer Structure Union Enum.etc 23
  24. Constants • Entities that appear in the program code as fixed values. • 3 types of Primary constants: – Integer constants • Positive or negative whole numbers with no fractional part • Example: – const int MAX_NUM = 10; – const int MIN_NUM = -90; – Floating-point constants • Positive or negative decimal numbers with an integer part, a decimal point and a fractional part • Example: – const double VAL = 0.5877e2; (stands for 0.5877 x 102) – Character constants • A single character enclosed in a single inverted commas • Example: – const char letter = ‘n’; – const char number = ‘1’; 24
  25. String Literal (Constant) • A sequence of Character consisting of Alphabets,didgits,and special character in double quotation mark is call String Constant Example “Pakistan” and “Lahore-58900” 25 Const Qualifier • The data that follow the keyword “const” cannot change its value during execution of program. This can be replaced by Define directive Example : const float p=3.14
  26. Alphabets Words Sentences Paragraphs Alphabets Digits Special Symbols Constants Variables Keywords Instructions Program Steps in learning English Language Steps in learning C Language 26
  27. C Development Environment DiskPhase 2 : Preprocessor program processes the code. DiskCompilerPhase 3 : Compiler creates object code and stores it on Disk. Preprocessor DiskLinkerPhase 4 : Linker links object code with libraries, Creates and stores it on Disk. EditorPhase 1 : Program is created in the Editor and stored on Disk. Disk 27
  28. LoaderPhase 5 : : . Primary Memory Loader puts Program in Memory C P UPhase 6 : : . Primary Memory CPU takes each instruction and executes it, storing new data values as the program executes. 28
  29. From code to executables Electrical.cpp Electrical.obj Electrical.exe Electrical.bkp Compiler Linker SaveSource File cs.LIB Library Files 29
  30. C Development system 1.IDE( Integrated Development Environment) 2.Command Line Development System C Directories • TC = The Install program puts all the subdirectories and files in this directory • BIN=Executable files store in this sub-directory • INCLUDE= Header Files (.h file extension) • LIB=Following Files a) Library Files b) Run-Time Object Files c) Math Libraries which are combines during Linking process, are stored in this sub-directory 30
  31. Making An Application Write the source code Compile the code into object files Link several object files into an executable Run the application We’ll focus on this part Don’t worry about these parts 31
  32. Variable names- Rules • Should not be a reserved word like int etc.. • Should start with a letter or an underscore(_) • Can contain letters, numbers or underscore. • No other special characters are allowed including space • Variable names are case sensitive – A and a are different. 32
  33. Brackets •< > Angle Brackets •{ } Curly Brackets •( ) Parenthesis •[ ] Square Brackets 33
  34. A Simple C Program Example #include <stdio.h> main() { printf("Programming in C is easy."); } Sample Program Output Programming in C is easy. Preprocessor Directive Header File Program Statement Function (NOTE: no semicolon with function) NOTE: All commands in C must be lowercase. 34
  35. 1. program execution begins at main() 2. keywords are written in lower-case 3. statements are terminated with a semi-colon 4. text strings are enclosed in double quotes 5. printf() is actually a function (procedure) in C that is used for printing variables and text 6. The text appears in double quotes "", it is printed without modification. There are some exceptions however. Summary of major points 35
  36. Preprocessor Directives • The first thing to be checked by the compiler. • Starts with ‘#’. • Tell the compiler about specific options that it needs to be aware of during compilation. • There are a few compiler directives. But only 2 of them will be discussed here. – #include <stdio.h> • Tell the compiler to include the file stdio.h during compilation • Anything in the header file is considered a part of the program – #define VALUE 10 • Tell the compiler to substitute the word VALUE with 10 during compilation 36
  37. Header Files • Messages to Compiler called compiler directives • “.h” file extension • Tell the compiler about definition of words or phrases used in program • Have prototype for library function in order to avoid program error. Examples: • stdio.h →(Standard Input/output) • conio.h →(Console Input/output) • iostream →(Input Output Stream) 37
  38. Header files (Cont’d) • The files that are specified in the include section is called as header file • These are precompiled files that has some functions defined in them • We can call those functions in our program by supplying parameters 38
  39. Four types of Header Files: stdio.h conio.h ctype.h math.h 39
  40. Stdio.h (Standard Input/output ) 40
  41. conio.h (console input/output) 41
  42. Math.h 42
  43. ctype .h 43
  44. main () Functions • A C program consists of one or more functions that contain a group of statements which perform a specific task. • C program must at least have one function: the function main. • The C programs starting point is identified by the word main() • This informs the computer as to where the program actually starts. The Parenthesis () that follow the keyword main indicate that there are no arguments supplied to this program (this will be examined later on). • The two braces, { and }, signify the begin and end segments of the program. 44
  45. main function (Cont’d) • This is the entry point of a program • When a file is executed, the start point is the main function • From main function the flow goes as per the programmers choice. • There may or may not be other functions written by user in a program • Main function is compulsory for any c program 45
  46. Statements • A specification of an action to be taken by the computer as the program executes. • In the previous example the line that terminate with semicolon ‘;’ is a statement. – printf ("Programming in C is easy."); • If there are more than one line then each line is a statement. 46
  47. Define & Declaration • Declaration: specifies the type of a variable. – Example: int local_var; • Definition: assigning a value to the declared variable. – Example: local_var = 5; • A variable can be declared globally or locally. • A globally declared variable can be accessed from all parts of the program. • A locally declared variable can only be accessed from inside the function in which the variable is declared. 47
  48. Example of Define & Declaration: #include <stdio.h> #define VALUE 10 int global_var; void main (void) { /* This is the beginning of the program */ int local_var; local_var = 5; global_var = local_var + VALUE; printf (“Total sum is: %dn”, global_var); // Print out the result } } Preprocessor directive Global variable declaration Comments Local variable declaration Variable definition 48
  49. Comments writing in C language ‘//’ & ‘/*’ ‘*/’ • Explanations or annotations that are included in a program for documentation and clarification purpose. • Completely ignored by the compiler during compilation and have no effect on program execution. • Starts with ‘/*’ and ends with ‘*/’ • Some compiler support comments starting with ‘//’ 49
  50. THANK YOU 50
Anúncio