Chapter 2 - Data Types and Storage Classes

Slides:



Advertisements
Similar presentations
1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
Advertisements

Types and Variables. Computer Programming 2 C++ in one page!
Structure of a C program
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
Basic Elements of C++ Chapter 2.
Objectives You should be able to describe: Data Types
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
Copyright © – Curt Hill Types What they do.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 1.
Variables Symbol representing a place to store information
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
7. BASIC TYPES. Systems of numeration Numeric Types C’s basic types include integer types and floating types. Integer types can be either signed or unsigned.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Numbers in ‘C’ Two general categories: Integers Floats
C++ Lesson 1.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Chapter Topics The Basics of a C++ Program Data Types
The Machine Model Memory
Data Representation Binary Numbers Binary Addition
Tokens in C Keywords Identifiers Constants
7. BASIC TYPES.
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
Variable Symbol represents a place to store information
C Short Overview Lembit Jürimägi.
Instructor: Ioannis A. Vetsikas
Plan of the Day: More on type conversions scanf printf format strings
Fundamental Data Types
Data Type.
Basic Elements of C++ Chapter 2.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Chapter 08- printf and scanf
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Data Type.
פרטים נוספים בסילבוס של הקורס
Variables in C Topics Naming Variables Declaring Variables
Basics of ‘C’.
Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei
C AS A LANGUAGE Interface between computer & human being. ALPHABETS
Lectures on Numerical Methods
Basic Types Chapter 7 Copyright © 2008 W. W. Norton & Company.
Homework Starting Chapter 2 K&R. Read ahead. Questions?
WEEK-2.
Fundamental Data Types
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
The C Language: Intro.
Chapter 11 Programming in C
Data Type.
Module 2 Variables, Data Types and Arithmetic
Variables in C Topics Naming Variables Declaring Variables
DATA TYPES There are four basic data types associated with variables:
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Variables and Constants
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Chapter 2 - Data Types and Storage Classes We will now take a closer look at the details of C’s data types, learn about the various types of constants, and learn about the scope and privacy of the different classes of data storage. C has four fundamental data types... (you have already worked with three )

Fundamental data types Type Usage int Integral numbers char Text and control characters; small integers float Low/medium precision real numbers double High precision real numbers

Modified data types Type Usage short int small - medium integers long int large integers long double very large reals integers can also be unsigned, which means that there are no negative values and the most significant bit is used to double the positive range.

Other data type expressions You will often see *_t data types. For example, look in <stdint.h> Generally used to show intent: uint8_t  Unsigned short integer (8bits) size_t  Unsigned integer (at least 16 bits) time_t  Arithmetic type for storing times

Data type sizes and ranges The storage size (and hence ranges) of the various character types are often machine dependent. Typical values for 32-bit words: type #bytes range char 1 -128 to 127 short int 2 -32768 to 32767 long int 4 -2147483648 to 2147483647

real variable sizes and ranges type #bytes exponent precision float 4 ±38 7 digits double 8 ±308 15 digits long double 10 ±4932 19 digits

sizeof operator sizeof is an operator, like +, -, *, AND / it is NOT a function sizeof can be used to find the (sometimes device-specific) lengths of data types, variables, and arrays. with data types use sizeof(...) with variables the parentheses are optional sizeof has many applications, particularly dynamic memory allocation (ENEE 150)

printf format indicators %d char, short, int decimal %x char, short, int hexadecimal %o char, short, int octal %u char, short, int unsigned decimal %ld, %lx... long integer decimal, hex, ... %f float, double floating point %e float, double scientific notation %g float, double shortest of %f & %e %Lf, Le... long double floating, scientific,...

scanf format indicators integer %d %i %u %x %o short int %hd %hi %hu %hx %ho long integer %ld %li %lu %lx %lo for decimal, decimal, unsigned dec, hex, octal float %f %e %g double %lf %le %lg long double %Lf %Le %Lg for floating point, scientific notation, or shortest of %f & %e

Program 2.1 - Size of Data Types /* #2.1 determine size of data types in Pelles on my laptop*/ #include <stdio.h> int main(void) { char name1[5], prompt[]="Enter the second name:"; int test=20; printf("Size of character:\t%zu\n",sizeof(char)); printf("Size of short int:\t%zu\n",sizeof(short int)); printf("Size of integer:\t%zu\n",sizeof(int));

Program 2.1 - part 2 printf("Size of long int:\t%zu\n",sizeof(long int)); printf("Size of real:\t\t%zu\n",sizeof(float)); printf("Size of double:\t\t%zu\n",sizeof(double)); printf("Size of long double:\t%zu\n",sizeof(long double)); printf("Size of name1:\t\t%zu\n",sizeof(name1)); printf("Size of prompt:\t\t%u\n",sizeof prompt); printf("Size of test:\t\t%d\n",sizeof test); return 0; }

Program 2.1 - Integrated testing Let’s see what the default values are on the our compiler from the Pelles IDE...on my laptop

constants symbolic explicit implicit #define PI 3.141592 (pre-processor statement) explicit const float pi = 3.141592; implicit area = 3.141592654*radius*radius;

explicit constants add “const” before type attempting to change an explicit constant is illegal.

implicit constants defaults decimal integers (no decimal point) double real (decimal point) integers that are too large are converted to long int (or unsigned long int) reals that are too large are NOT converted to long double - an overflow occurs identifiers are used to override defaults

Implicit constant examples 42 integer 42U unsigned int 42L long int 42UL unsigned long int 42000 signed long int 062 int (octal) 0x2A int (hexadecimal) 42. double 42.0 double 42.0f float 4.2e1 double 0.42e2l long double 42000.e-3L long double (upper or lower case l, f, and u are o.k.)

Character data - ASCII table Letters and digits are sequential in ASCII: ‘0’ is 0x30 ‘9’ is 0x39 ‘a’ is 0x61 ‘A’ is 0x41 (hexadecimal)

Character functions We can use these facts to make simple versions of character functions which are defined in <ctype.h> and <stdlib.h> isdigit checks to see if character is 0 - 9 atoi converts a string to a number

ISDIGIT function /* true if character is 0 - 9; false otherwise */ int ISDIGIT(char c) { return ((c>=0x30)&&(c<0x3A)); }

ATOI function /* converts numeric string to an integer */ int ATOI (char s[]) { int i, j=0, ISDIGIT(char); for (i=0;ISDIGIT(s[i]);i++) j=10*j+(s[i]-'0'); return j; }

main - convert string to integer /* read a string and convert it to a character */ #include <stdio.h> int main (void) { int ATOI(char[]); char test[BUFSIZ]; puts("enter a number"); gets(test); printf("\n%s = %d\n",test,ATOI(test)); return 0; }

Program 2.2 - Integrated testing Let’s test the program now in the Pelles IDE...

Variable storage classes global automatic register static

Scope (privacy), duration, and linkage region of program in which a variable is known storage duration: the time period when a variable exists in memory linkage: in multiple-source-file programs (more than one *.c file), indicates the connection of variables in one file to other files. (we won’t discuss this attribute further in this class).

Global variables a global variable is defined outside (or between) functions. The scope of a global variable extends from the point of definition up to the end of the file (but not before the definition). global variables can be initialized only by a constant (not an expression). global variables will be set to zero if not explicitly initialized. initialization of all globals occurs before main starts. globals exist for the life of the program

Automatic variables (auto) an automatic variable is defined inside a function or a statement block {}. they must be defined immediately after the opening {. The scope of an automatic variable extends from the point of definition up to the end of the function or block. No code outside this block can access the variable. automatic variables can be initialized with an expression. automatic variables will contain garbage if not explicitly initialized (they are NOT set to zero). initialization occurs at “run time”.

Automatic variables - continued duration: automatic variables are created when the execution of their statement block begins and the variables cease to exist when the block execution concludes. This means that the next time the block is run, the value from the previous execution is GONE! If a global variable and an automatic variable have the same name, the global variable is “hidden” from the program for the duration of the automatic variable’s existence. The global variable becomes accessible to the program as soon as the automatic variable is gone and retains its original value.

Global versus automatic whenever practical, automatic variables should be used because: minimizes storage and program size. reduces the chance of accidently modifying a variable (when inadvertently used for two different purposes). improves the portability and reusability of the code to have functions as self-contained units.

Register variables variables are normally stored in memory. The time it takes to acquire data from memory is usually much longer than the time it takes to perform an operation in the CPU/ALU. C allows you to request that frequently used variables be stored in CPU registers rather than memory.

Register variables - continued place register in front of the variable type to request a register variable. There are a limited number of registers, so the compiler may choose to ignore request, in which case the variable is stored in memory. global variables can not be register variables. arrays can not be register variables. function arguments can be register variables same initialization rules as auto variables

Static variables keyword static precedes data type. has the scope of an auto variable. duration is extended beyond the execution of the statement block so that static variables retain their values between function calls. can be initialized only with a constant. Set to zero if not explicitly initialized.

Program 2.3 - concept demonstration #include <stdio.h> /* demonstration of storage classes */ int i=10, j=5, k; // global variables int main(int argc, char *argv[]) { int l, func(int); printf("main: i=%d\t j=%d\t k=%d\t l=%d\n",i,j,k,l); func(i); l=func(j); return 0; }

Program 2.3 - continuation int func (register int i) { auto int j=i+4, l; static int k; printf("func: i=%d\t j=%d\t k=%d\t l=%d\n",i++,j++,k++,l++); return k; }