C Summary Wilmer Arellano. References Excerpts from the book: Predko, Myke. (2005). 123 PIC MICROCONTROLLER EXPERIMENTS FOR THE EVIL GENIOUS. USA: McGraw-Hill.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Some basic I/O.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Data types and variables
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Railway Foundation Electronic, Electrical and Processor Engineering.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Introduction to C Programming
Railway Foundation Electronic, Electrical and Processor Engineering.
The switch Statement, DecimalFormat, and Introduction to Looping
Introduction to C Language
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Objectives You should be able to describe: Data Types
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
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:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
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.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
C++ Programming: Basic Elements of C++.
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.
Chapter 05 (Part III) Control Statements: Part II.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 12 Variables and Operators. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Basic C Elements.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Primitive Variables.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Computer Programming Control Structure
C Summary Wilmer Arellano. References Excerpts from the book: Predko, Myke. (2005). 123 PIC MICROCONTROLLER EXPERIMENTS FOR THE EVIL GENIOUS. USA: McGraw-Hill.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Presentation By :- Nikhil R. Anande ( ) Electronic & Communication Engineering. 3 nd Year / 5 th Semester FACULTY GUIDE : RAHIUL PATEL SIR MICROCONTROLLER.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
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.
A First Book of C++ Chapter 4 Selection.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Operators and Expressions
Chap. 2. Types, Operators, and Expressions
Week 3 - Friday CS222.
Java Programming: From Problem Analysis to Program Design, 4e
Introduction to Programming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Homework Homework Continue Reading K&R Chapter 2 Questions?
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Variables and Constants
Chapter 12 Variables and Operators
Presentation transcript:

C Summary Wilmer Arellano

References Excerpts from the book: Predko, Myke. (2005). 123 PIC MICROCONTROLLER EXPERIMENTS FOR THE EVIL GENIOUS. USA: McGraw-Hill. ISBN:

Variable Declaration Statements int VariableName; VariableName is a label and can start with any upper- or lowercase letter or the underscore character. After the starting character, the rest of the label can be any letter, number, or underscore character. Blank characters cannot be used in a label. int x = 47; For standard variables, two options exist that you should he aware of. The first is the ability to initialize the variable when it is declared. By adding an equals sign and a constant value, you can set your variable to Another option that is available to the declaration statement is the const keyword, which converts the declared value from a variable to a constant: const int xConstant = 47;

Variable Declaration Statements Please try to make an effort to make them representative of what they are being used for. PWMV1 is much better than XEG4 Variable types are important because of limited RAM Be careful when passing data among variables C-style casts can be used to convert any type into any other type, potentially with unsafe results (such as casting an integer into a pointer type). i = (iType)j; double result = (double)4/5;

C Data Types

Constant Formatting

main() { PORTA = 0; CMCON0 = 7; // Turn off Comparators ANSEL = 0; // Turn off ADC TRISA = 0x49; // Enable PORTA LED Outputs while(1 == 1) // Loop Forever { if (0 == (PORTA & (1 << 3))) PORTA = '3'; // Turn on Four LEDs else PORTA = 4; // Turn on Remaining Four LEDs } // elihw } // End cConfuse main() { PORTA = 0; CMCON0 = 7; // Turn off Comparators ANSEL = 0; // Turn off ADC TRISA = 0b001001; // Enable PORTA LED Outputs while(1 == 1) // Loop Forever { if (0 == RA3) PORTA = 0b010010; // Button Pressed, D0, D2, D4, D7 On else PORTA = 0b000100; // Button Released, D1, D3, D5, D6 On } // elihw } // End cClear

Assignment Statements VariableName = Expression; Although C is somewhat tolerant of assigning different data types, you should always try to make sure that the expression’s type is the same as the variable’s, and if it isn’t, make sure you understand any potential issues.

Assignment Statements int i; // Unitialized Variable Declaration int j = 23; // Variable Declared with Initial // value assignment (Initialization) main() { i = 47; // The variable "i" assigned (or loaded // with) the constant value 47 i = j; // The variable "i" assigned contents of // of "j". i = j = 1; // "i" and "j" assigned the same value. i = i + 1; while(1 == 1); } // End cAssign

Expressions int i, j; main() { i = 3; // "3" is the Expression j = i; // "i" is the Expression i = j * 11; // Simple Arithmetic Expression i = j * 0x00B; // Multiply by a Hex Value i = j * 0b ; // Multiply by a Binary Value i = '0' + j; // Load i with ASCII "3" // Change "Watch" Window // display Format to ASCII // to verify. // ASCII “O” IS 48; =51; ASCII “3” IS 51

Expressions j = 12; i = (j - 5) % 7; // Complex Arithmetic Expression // Involving Two Operations // and Forced Order of Operations // i = (j - 5) % 7 // = (12 - 5) % 7 // = 7 % 7 // = 0 i = j - 5 % 7; // Same As previous but no // Forced Order of Operations // i = j - 5 % 7 // = % 7 // = (5 % 7 = 5) // = 7 i = (j = i / 6) * 2; // Embedded assignment: // j = i / 6 // = 7 / 6 // = 1 // i = (i / 6) * 2 // = j * 2 // = 2 while (1 == 1); // Loop Forever } // End cExpression

Bitwise Operators The four basic bitwise operators are as follows: & — bitwise AND. When two values are ANDed together, each bit in the result is loaded with the AND value of the corresponding bits in the two values (set if both the bits are set).  — bitwise (inclusive) OR. When two values are ORed together, each bit in the result is loaded with the OR value of the corresponding bits in the two values (set if either hit is set). ^ — bitwise XOR. When two values are exclusively ORed together. each bit in the result is loaded with the XOR value of the corresponding hits in the two values (set if only one of the two parameter bits is set). ~ — bitwise negation. This operator will return the negated or complementary value for each bit of the single input parameter (invert each bit). This operator must never be confused with the logical operator, which will he shown to invert the logical value, not the hitwise value.

Bitwise Operators char i, j, k; // Use 8 Bit Variables main() { i = 47; // Initialize Values j = 137; k = i & j; // AND Values together k = i | j; // OR Values together k = i ^ j; // XOR Values together k = ~j; // Invert the Bits in "j" k = (i * 2) - j + (1 << 7); // Mix Binary Operators with // Arithmetic while(1 == 1); } // End cBitwise

Logical Expression

i = 0xl234 * (j > 4) // if (j > 4) is True, then 1 returned which is equivalent to: if (j > 4) i = 0x1234; else i = 0;

Conditional Execution Using the If Statement if (Expression) // Test to see if “Expression” is Not Zero Statement // Statement executed if “Expression” !5 0 else // Optional “else” Statement which Statement // Executes if Expression is Zero if (0 != (j/ 3)) // Execute following statement // if “j / 3” is not zero if (j / 3) // Execute following statement // if “j / 3” is not zero.

Conditional Execution Using the If Statement if (Expression) // Test if “Expression” is // Not Zero {// Opening Brace to Collect Statements // Multiple Statements that // Execute if Expression // is Not Zero Statement; } // Closing Brace to End true “statement”

Conditional Execution Using the If Statement main() { if (44 == i) // "i" equals a constant { n = n + 1; // Increment "n" if "i" == 44 } else { n = n - 1; // Decrement if Not Equals } // fi if ((j = (i / 3)) == 7) { j = j + 1; } // fi if (k = i) // "i" equals contents of Variable n = n + 1; // Increment "n" if "i" == "k" else if (j = i) // "i" equals contents of another Variable n = n - 1; // Note that there is a single statement, // so no braces required. while(1 == 1); } // End cIf

Nested Conditional Statements if (i > j) { if (k < n) { // Statement(s) Executed if “i > j” and “k < n” } else { // Statement(s) Executed if “i > j” and “k >= n” } // fi if ((i > j) && (k < n)) { // Statement(s) Executed if “1 > j” and “k < n” } else if (i > j) { // Statement(s) Executed if “i > j” and “k >= n” ) } // fi

Nested Conditional Statements if (i > j) if (k < n) { // Statement(s) Executed if “i > j” and “k < n” } else { // Statement(s) Executed if “i > j” and “k >= n” } // fi