Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)

Slides:



Advertisements
Similar presentations
Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
Advertisements

IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Structure of a C program
Introduction to C Systems Programming. Systems Programming: Introduction to C 2 Systems Programming: 2 Introduction to C  A ‘C’ Program –Variable Declarations.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Chapter 2 Data Types, Declarations, and Displays
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
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.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.
Data Types Declarations Expressions Data storage C++ Basics.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
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.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
C is a high level language (HLL)
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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.
Today… Continue Building Expressions: –Literals –Constants –Casting –Array Declaration –Operators and Operator Precedence –Keywords Winter 2016CMPE212.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
User Interaction and Variables
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chap. 2. Types, Operators, and Expressions
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Variables A variable is a placeholder for a value. It is a named memory location where that value is stored. Use the name of a variable to access or update.
Multiple variables can be created in one declaration
C Short Overview Lembit Jürimägi.
Java Programming: From Problem Analysis to Program Design, 4e
Conversions of the type of the value of an expression
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Lecture 8.
Chapter 2: Basic Elements of Java
Chapter 2: Java Fundamentals
Recap Week 2 and 3.
Expressions and Assignment
elementary programming
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Module 2 Variables, Data Types and Arithmetic
Chapter 2 Primitive Data Types and Operations
DATA TYPES There are four basic data types associated with variables:
C Language B. DHIVYA 17PCA140 II MCA.
Presentation transcript:

Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int) –float (fraction floating point) –double (fraction high precision floating point) Modified types –unsigned char or unsigned int (no sign bit) –long double (more precision than just double) –short int –register int (for data used often In a program) –static double (private if global, persist if in function) Rules –short int (not longer than int –int (at least 16 bits wide, not longer than long int) –long int (at least 32 bits wide)

Strings and Boolean Strings: char *stringData; –* is a pointer (like java reference variables) –A string is an array of characters –Length(variable or data-type): sizeof( ) or sizeof( ) Boolean –declare as int –true is non-zero, false is zero –No true/false reserved word (use #define if you wish) –Example: int flag=1; if (flag) { … true part … } else { … false part … }

Defining Constants Using preprocessor directive: #define PI Note: Preprocessing directives do not end with ; Note: Preprocessing replaces by PI with const int MONTHS=12; Analogous to 'final' in Java

Strongly Typed Like Java, C is a Typed Language –Java is Strongly typed: compile errors occur when rules are violated –C is Somewhat Strongly types: errors do not always occur if rules are violated (ex: int i=5.6;) C and Java –All variables must be declared before use –Variables start with alphanumeric or underscore, then can have alphanumeric, underscore, or numbers –Variables are case sensitive Some C compilers –variables must be declared at the start of a block –Recent C's sometimes relax this requirement

Scope Variables declared outside of functions are global –They are accessible by any function –They are accessible by separately compiled functions (extern ) unless declared as static (analigous to Java private) –Only use global variables when necessary Local variable is declared within a function –Only accessible within the declared function –This is because of the activation record concept –Those declared static persist in memory after the function returns Example int m; /* global variable */ int main(int argc, char *argv[]) { int i; /* local variable */ }

printf function (one of many built-in functions) Syntax: printf("template", args … ) "template" –Mixes text with conversion specifiers –Example: int i=5; printf("i is %d\n", i); –Example: int i=5; c='a'; float v = 6.2; printf("%d %c %f\n", i, c, v); /* call the function */ output: 5a Notes: –Java has System.out.printf (JVM 1.5) –awk allows printf –C gives No errors when variables don't match formats Note: break characters like \n, \t, \r, \a are JLJ

printf specifiers cCharacterOutput d or iSigned integer392 e or EScientific3.92e+2 fFloating point g or GShorter of %d or %e oSigned octal610 sStringSample uUnsigned integer7235 x or XUnsigned hexadecimal7Fa l[idoux]Long L[eEfgG]double h[idoux]Short7Fa

Additional Features Specify width of field: printf(“%4.2f", ); outputs " " Force a sign: printf(“%+3i", 37); outputs "+37" Left justify (default is right justify): printf(“%-6i", 37); outputs "37 " Right justify: printf(“%6i", 37); outputs " 37" Dynamic width formatting: printf(“%*d", 5, 37); outputs " 37" Fill with zeroes: printf(“%05d", 37); outputs "00037"

Control Statements: Almost JLJ JLJ –if –if-else –switch –while –do-while –for –continue and break –( )? : Not JLJ –Boolean is an int (0 = false, not 0 = true) –for (int i=0; i<10; i++) is illegal; declare i explicitly first.

(condition)?exp1:exp2 x = (y<0)? –y:y; if (y<0) x = -y; else x = y; x = (y<0)? x + y – 1: y – x + 1; if (y<0) x = x + y -1; else x = y – x + 1; max = (a>b)? a:b; if (a>b) max = a; else max = b; Note: this construct is handy for function calls

Expressions and operators: Almost JLJ JLJ –variable = –Multiple assignments: i = j = k = 0; –Increment and decrement: n++, n--, ++n, and --n –Adjust previous value: +=, -=, *=, /=, %=, &=, |= –Logical: &&, ||, and ! Not JLJ –str + 3 does not concatenate Use sprintf: sprintf(buf, “%s%d”, str, 3)); –printf("%d %d\n", n, n++) Could execute n++ before n or the other way around.

Precedence and Operations Almost JLJ JLJ –5 / 3 gives a truncated result of 1 –5.0 / 3 gives a fractional result of –5 % 3 gives a result of 2 –Precedence is like java except for the star operator (see below) –No exponentiation operator –Unary + and – –Casting: float v = 5 / (float) 3; –Short circuit logical expression evaluation Not JLJ –Java: all non-primitives are reference variables –C: non primitives may not be reference variables, so a dereference operator is needed (*). More later