Punctuation in C. Identifiers and Expressions. Identifiers. Variables. Keywords. Identifier Examples

Similar documents
Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Sources: On the Web: Slides will be available on:

Informatica e Sistemi in Tempo Reale

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Chapter One Introduction to Programming

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Pemrograman Dasar. Basic Elements Of Java

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

The C Programming Language course syllabus associate level

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

C++ Programming Language

VB.NET Programming Fundamentals

Computer Science 281 Binary and Hexadecimal Review

C++ Language Tutorial

Computer Programming Tutorial

Fundamentals of Programming

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

C PROGRAMMING FOR MATHEMATICAL COMPUTING

Chapter 2: Elements of Java

Introduction to Java

Java Basics: Data Types, Variables, and Loops

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Programming Microcontrollers in C

Object Oriented Software Design

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Chapter 2 Introduction to Java programming

C++ INTERVIEW QUESTIONS

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

DNA Data and Program Representation. Alexandre David

Object Oriented Software Design

Chapter 2 Elementary Programming

JavaScript: Control Statements I

arrays C Programming Language - Arrays

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

Introduction to Java. CS 3: Computer Programming in Java

Moving from CS 61A Scheme to CS 61B Java

Instruction Set Architecture (ISA)

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Java Interview Questions and Answers

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

MISRA-C:2012 Standards Model Summary for C / C++

Lecture 1 Introduction to Java

Tutorial on C Language Programming

6.087 Lecture 2 January 12, 2010

5 Arrays and Pointers

Introduction to Python

The programming language C. sws1 1

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Example of a Java program

Chapter 13 Storage classes

Introduction to Visual C++.NET Programming. Using.NET Environment

Objective-C Tutorial

How To Write Portable Programs In C

Stacks. Linear data structures

C Coding Style Guide. Technotes, HowTo Series. 1 About the C# Coding Style Guide. 2 File Organization. Version 0.3. Contents

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

CS 241 Data Organization Coding Standards

Programming languages C

1 Abstract Data Types Information Hiding

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Keil C51 Cross Compiler

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

So far we have considered only numeric processing, i.e. processing of numeric data represented

Programming Languages CIS 443

In this Chapter you ll learn:

FEEG Applied Programming 5 - Tutorial Session

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Chapter 3. Input and output. 3.1 The System class

Java CPD (I) Frans Coenen Department of Computer Science

Lecture 5: Java Fundamentals III

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

Comp151. Definitions & Declarations

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Lecture 2 Notes: Flow of Control

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Chapter 3 Operators and Control Flow

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Chapter 5 Names, Bindings, Type Checking, and Scopes

Bonus ChapteR. Objective-C

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Some Scanner Class Methods

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

3.GETTING STARTED WITH ORACLE8i

Java (12 Weeks) Introduction to Java Programming Language

Number Representation

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example

C++ Essentials. Sharam Hekmat PragSoft Corporation

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Chapter 7D The Java Virtual Machine

Transcription:

Identifiers and Expressions CSE 130: Introduction to C Programming Spring 2005 Punctuation in C Statements are terminated with a ; Groups of statements are enclosed by curly braces: { and } Commas separate function arguments Whitespace is ignored 1 2 Variables Remember that variables are named blocks of memory Variables have two properties: 1. name a unique identifier 2. type what sort of value is stored Identifiers Identifiers give unique names to various objects in a program An identifier may contain letters, digits, and the underscore character ( _ ) An identifier must begin with a letter or _ Identifiers should be meaningful (and nouns) Style convention: the second and subsequent words in an identifier are capitalized 3 4 Identifier Examples Good Identifiers tax_rate taxrate level4score Bad Identifiers 1stName /* starts with a digit */ %discount /* contains invalid character */ Keywords Some words may not be used as identifiers These words have special meaning in C C has 32 reserved words Ex. for, if, while, switch 5 6

Reserved Words in C Data Types auto break case char const continue default do double else enum extern float for goto if int struct long switch register typedef return union short unsigned signed void sizeof volatile static while int stores integer values (ex. 5) float stores decimal values (ex. 3.14) double stores larger decimal values than float (double the precision of a float) char stores an integer representing a character (ex. A ) Also short, unsigned, and long 7 8 Type Size Hierarchy char->short->int->unsigned->long->float->double Size (storage capacity) increases from left to right Ex. char: 1 byte, int: 4 bytes, double: 8 bytes Actual size varies based on machine architecture Use sizeof() to get actual size Type Conversion A value can always be stored in a variable of a larger (further right) type Ex. an int can be stored in a float If you want to go the other way (to store a value in a smaller type), you must cast: int x = (int)3.14159; Casting can lose (truncate) data 9 10 Conversion Examples double average = 100.0/8.0 average = 100.0/8 average = 100/8 int sumgrades = 100.0/8.0 sumgrades = (int)100.0/8.0 sumgrades = (int)(100.0/8.0) double fiftypercent = 50/100; fiftypercent = 50.0/100.0; Operators in C Some characters have special meanings +, -, *, /, and % are arithmetic operators &&,,!, ==, and!= are logical operators A character s meaning may depend on context Ex. printf( %d, a); vs. b = c % 4; 11 12

Integer Arithmetic in C Addition, subtraction, and multiplication work as you would expect Division (/) returns the whole part of the division (the quotient) 12 / 3 = 4 15 / 2 = 7 Modulus (%) returns the remainder 12 % 3 = 0 15 % 2 = 1 Shorthand Operators Some operators are shortcuts for others Ex. +=, -=, *=, /=, %=, ++, -- x += 5; is the same as x = x + 5; y++; is the same as y = y + 1; 13 14 Increment (++) and Decrement (--) Operators When used by themselves, y++; and ++y; have identical results In an expression, they have different results The relative order of the operator matters: y++: use y s value, then increment it. ++y: increment y, then use new value. The same is true for decrement (--) Operator Precedence Precedence rules specify the order in which operators are evaluated Remember PMDAS: Parentheses, Multiplication, Division, Addition, Subtraction Associativity determines left-right order 15 16 Operator Precedence Operator Associativity () ++ -- (postfix) left to right! right to left * / % left to right + - left to right < <= > >= left to right ==!= left to right && left to right left to right?: right to left = += -= *= /= %= right to left Precedence Examples 3-8 / 4 / has the highest precedence, so we compute 8 / 4 first, then subtract the result from 3 Equivalent expression: 3 - (8 / 4) What is the value of 3 * 4 + 18 / 2? 17 18

Precedence Examples Parentheses 5-3 + 4 + 2-1 + 7 + and - have equal precedence, so this expression is evaluated left to right: (((((5-3) + 4) + 2) - 1) + 7) Innermost parentheses are evaluated first Parentheses can be used to force a different order of evaluation: 12-5 * 2 = 2 (12-5) * 2 = 14 19 20 Expression Examples What do the following expressions evaluate to? 1 + 2 * 3 (1 + 2) * 3 13 % 5 23 % 4 * 6 More Expression Examples 27.0 / 6.0 27.0 / 6 27 / 6 Given: int x = 5; int y = x++ * 6 int y = ++x * 6 21 22 Constants A constant is a value that cannot change Ex. numbers (42, 23, 3.14) Variables can be declared as constants using the const keyword: const double pi = 3.1415926; Strings (sequences of characters enclosed in double quotes) are also constants. User Input The printf() function is used to display output on the screen The scanf() function is used to read input from the keyboard scanf() is also defined in stdio.h 23 24

Using scanf() scanf() reads in data and stores it in one or more variables Ex. int userage; scanf( %d, &userage); scanf() Usage The first argument (the control string) contains a series of placeholders These are like the ones that printf() uses %d = int, %f = float, %c = char, etc. Spaces are used to separate placeholders and absorb whitespace %d absorbs leading spaces and reads an integer value 25 26 scanf() Usage, Pt. 2 The remaining arguments to scanf() are a comma-separated list of variable names Input is stored in these variables Each variable name is preceded by & &i means the memory address associated with variable i We ll talk more about this later on scanf() Examples int a, b, c; char d; scanf( %d %d, &a, &b); scanf( %c %d, &d, &c); 27 28 Next Time Relational Operators Conditional (selection) statements 29