Basic Data Types & Memory & Representation

Slides:



Advertisements
Similar presentations
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 Fundamental Data Types. 2 Declaration All variables must be declared before being used. –Tells the compiler to set aside an appropriate amount of space.
Plab - administration TA’s:TA’s: –Yoseph Barash –Liad Blumrosen –Michael Okun Contact ONLY ONLY website:
Please pick up an attendance question and submit in 5 minutes CS 1003 Lecture #3 Sept 12, 2006 Knarig Arabshian.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
How Create a C++ Program. #include using namespace std; void main() { cout
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
1 Review of Chapter 6: The Fundamental Data Types.
String Escape Sequences
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Copyright © – Curt Hill Types What they do.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
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.
1 Building a program in C: Preprocessor, Compilation and Linkage.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
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.
C++ Lesson 1.
Basic Data Types & Memory & Representation
Chapter 2 Variables.
The Machine Model Memory
CSE 303 Lecture 9 C programming: types, functions, and arrays
Building Java Programs
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Object Oriented Programming
INC 161 , CPE 100 Computer Programming
Multiple variables can be created in one declaration
Intro to C Tutorial 4: Arithmetic and Logical expressions
Java Programming: From Problem Analysis to Program Design, 4e
Fundamental Data Types
C Programming Variables.
Introduction to Java, and DrJava part 1
Building Java Programs
Chapter 2 Variables.
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Building Java Programs
Basic Types Chapter 7 Copyright © 2008 W. W. Norton & Company.
Building Java Programs Chapter 2
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Fundamental Data Types
Building Java Programs
Operations and Arithmetic
Building Java Programs Chapter 2
Names of variables, functions, classes
Introduction to Java, and DrJava
Chapter 2 Variables.
Agenda Packages and classes Types and identifiers Operators
DATA TYPES There are four basic data types associated with variables:
Assistant Professor Rabia Koser Dept. of Computer Applications
Presentation transcript:

Basic Data Types & Memory & Representation

Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA, All can be signed/unsigned Default: signed Unlike in JAVA, the types sizes are machine dependant!

Memory – few definitions Bit – a binary digit - zero or one Byte – 8 bits Not C specific 0/1 0/1

Basic data types The types sizes must obey the following rules. Size of char = 1 byte Size of short ≤ size of int ≤ size of long Undetermined type sizes Advantage: hardware support for arithmetic operations Disadvantage: problems with porting code from one machine to another

signed VS. unsigned Each type could be signed or unsigned int negNum = -3; int posNum = 3; unsinged int posNum = 3; unsinged int posNum = -3;

Integers binary representation

Integers binary representation unsigned integers types are represented with the binary representation of the number they stand for. The representation range therefore is… Where x is the size in bits of the type The exact representation of signed integers types is machine dependant. The most popular representation is, the two- complement representation The representation range is: [-2x-1,2x-1-1] [0,2x-1]

Integers binary representation short num = -1; printf("%u",num); The output on some machines is: 65535 What happened? -1 -> 11111111 11111111(signed short representation). 1111111...1->65535 when interpreted as unsigned short.

Integers binary representation

The sizeof() operator The operator sizeof (type) returns the size of the type: printf("%zu",sizeof(char)); //charSize == 1 lu is unsigned long

Primitive types & sizeof int main() { //Basic primitive types printf("sizeof(char) = %zu\n",sizeof(char)); printf("sizeof(int) = %zu\n",sizeof(int)); printf("sizeof(float) = %zu\n",sizeof(float)); printf("sizeof(double) = %zu\n",sizeof(double)); //Other types: printf("sizeof(void*) = %zu\n",sizeof(void*)); printf("sizeof(long double)= %zu\n",sizeof(long double)); return 0; }

Integral types - characters chars can represent small integers or a character code. Examples: char c = ‘A’; char c = 65;

Arithmetic with character variables char ch = 'A'; printf("The character %c has the ASCII code %u.\n", ch, ch);

Arithmetic with character variables for (char ch= 'A'; ch <= 'Z'; ++ch) { printf("%c", ch); }

& "%d %f %lf General Input/Output #include <stdio.h> int main() {    int n;    float q;    double w;    printf("Please enter an int, a float and a double\n");    scanf("%d %f %lf",&n,&q,&w);    printf("ok, I got: n=%d, q=%f, w=%lf",n,q,w);    return 0; } & "%d %f %lf

Characters input and output #include <stdio.h> int main () {    char c;    printf("Enter character:");    c=getchar();    printf("You entered:");    putchar(c);    return 0; }

Printing MAX_LINES #include <stdio.h> #define MAX_LINES 10 int main() {    int n = 0;    int c;    while(((c=getchar())!=EOF) && (n<MAX_LINES) )    {       putchar(c);       if( c == '\n' )        n++;    }    return 0; } 18

Boolean types Boolean type doesn’t exist in C! Use char/int instead (there is also a possibility to work on bits) zero = false non-zero = true Examples: while (1) { } infinite loop if (-1974) { } true statement i = (3==4); i equals zero

Boolean types Boolean type doesn’t exist in C! Use char/int instead (there is also a possibility to work on bits) zero = false non-zero = true Examples: #define TRUE 1 while (TRUE) { } infinite loop while (1) { } infinite loop if (-1974) { } true statement i = (3==4); i equals zero

Casting and Type Conversion Operands with different types get converted when you do arithmetic. Everything is converted to the type of the floatiest, longest operand, signed if possible without losing bits Casting possible between all primitive types. Casting up - usually automatic: float => double short => int => long etc. int i; short s; long l; i=s; // no problem l=i; // no problem s=l; // might lose info, // warning not guaranteed

Casting and Type Conversion Operands with different types get converted when you do arithmetic. Everything is converted to the type of the floatiest, longest operand, signed if possible without losing bits Casting possible between all primitive types. Casting up - usually automatic: float => double short => int => long etc. Casting down – warning ! int i; short s; long l; i=s; // no problem l=i; // no problem s=l; // might lose info, // warning not guaranteed

Integer division Mathematical operators on only int operands The result is int Integer numbers are treated as “int” float f=1/3; //0 float f=(float)1/3; //0.3333.. float f=1/3.0; //0.3333..

Expressions as Values && - logical “and” || - logical “or” & - bitwise “and” || - logical “or” | - bitwise “or” bitwise – end of the year Evaluation: int i=0; if (i==1 && x=isValid()) {     … } Might not be evaluated

Functions declarations and definitions

Declarations and definitions Function declaration: Specification of the function prototype. No specification of the function operations. Function definition Specification of the exact operations the function performs.

Declarations and definitions “One definition rule”: many declarations, one definition. Examples: many function declarations (no body, just “signature”, one definition – with body. Legal but bad style int f(),f(int); int f(int); int f(int a); int f(int a) {    return a*2; } Later on we will see the same with structs, and see cases where for a variable a declaration isn’t the same as definition (=storage allocation).

Function signature Syntactically you declare a function exactly the same as you do a variable: <type> <name> With the added “modifier” of “()” to the name. int g(), f, x,i, k(); The above line declares 3 int’s and two int functions.

Function signature In C (not c++) function signature is composed only from it’s name and return type: int f(int a,int b); Has the same type as int f(float c); Why? Once upon a time (80’) you did not write parameter names in function declarations or definitions, so all functions where like this: int f(), g(); And you would need to remember what arguments to pass…

Function signature Today it is still legal but it’s a very bad style and error prone. This leads to a side effect that: int f(int a,char b); int f(float a) { return a*2; } int main() { f(5,'a'); } Will compile and run, and call the second f! But, If you will try to define also the first signature – you will get an error.

To get these warnings with functions that has no arguments declare: Function signature Today it is still legal but it’s a very bad style and error prone. This leads to a side effect that: int f(int a,char b); int f(float a) { return a*2; } int main() { f(5,'a'); } Will compile and run, and call the second f! But, If you will try to define also the first signature – you will get an error. No overloads. If you use high warning level, above code will give you some warning. Pay attention. To get these warnings with functions that has no arguments declare: int f(void);