Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Introduction to C Programming
Introduction to C Programming
C Programming Getting Started Getting Started Hello World Hello World Data types and variables Data types and variables main() main() I/O routines I/O.
Programming Languages and Paradigms The C Programming Language.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Kernighan/Ritchie: Kelley/Pohl:
Chapter 3: Beginning Problem Solving Concepts for the Computer Programming Computer Programming Skills /1436 Department of Computer Science.
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Structure of a C program
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Guide To UNIX Using Linux Third Edition
C Programming for Embedded Systems. fig_06_00 Computer Layers Low-level hardware to high-level software (4GL: “domain-specific”, report-driven, e.g.)
ATmega 2560 Datasheet Main features are listed
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Computer Science 210 Computer Organization Introduction to C.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
8-1 Embedded Systems Fixed-Point Math and Other Optimizations.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
CS212: Object Oriented Analysis and Design Lecture 2: Introduction to C++
Slides created by: Professor Ian G. Harris Typical Embedded C Program #include main() { // initialization code while (1) { // main code }  #include is.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 1.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
By Mr. Muhammad Pervez Akhtar
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
CSCE 206 Structured Programming in C
The Machine Model Memory
Computer Science 210 Computer Organization
C Programming Tutorial – Part I
Programming Languages and Paradigms
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
CS 61C: Great Ideas in Computer Architecture Introduction to C
C Short Overview Lembit Jürimägi.
Programming Paradigms
C Basics.
Introduction to C Programming Language
Variable Names Names are a sequence of visible characters
Introduction to C Programming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
פרטים נוספים בסילבוס של הקורס
פרטים נוספים בסילבוס של הקורס
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
C Programming Getting started Variables Basic C operators Conditionals
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Languages and Paradigms
C Language B. DHIVYA 17PCA140 II MCA.
INTRODUCTION TO C.
Presentation transcript:

Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate) another file  main is the function where execution starts  printf is a library function, in the stdio library

Slides created by: Professor Ian G. Harris C Syntax Basics  Comments: single line //, multiline /* … */  Semicolon used to terminate all statements  # indicates a compiler directive (include, define, etc.)  Brackets { … } group lines of code for execution  Blankspace between lines is not important in C Still need to separate tokens int a, b, c; a = b + c; int a,b,c;a=b+c; correct incorrect (inta)correct

Slides created by: Professor Ian G. Harris Variables  All variables’ types must be declared Memory space requirements known statically Type checking can be performed at compile-time  Numerical Types: int, float, double  Other types: char, void  Type qualifiers: short, long, unsigned, signed, const Specify alternate sizes, constrain use modes

Slides created by: Professor Ian G. Harris Types for PICC Compiler Type Size Arith type  Type modifiers unsigned and signed bit1integer char8integer short16integer int16integer short long24integer long32integer float24real double24 or 32real

Slides created by: Professor Ian G. Harris Local vs. Global  Scope of a variable is limited to the function where it is declared (malloc later)  Local variables “disappear” when its function returns  Global variables are declared outside of a function Globals do not disappear when a function returns int x; main () { int y; foo(); } foo() { int y;} Global scope Local to main Local to foo

Slides created by: Professor Ian G. Harris Volatile Variables  The value of a volatile variable may change at any time, not just at an explicit assignment  Compiler optimizations are not applied to volatile variables  When can variables change without an explicit assignment? 1. Memory-mapped peripheral registers 2. Global variables modified by an interrupt service routine 3. Global variables accessed by multiple tasks within a multi- threaded application

Slides created by: Professor Ian G. Harris Volatile Example. while (*periph != 1); // wait until data transfer. // is complete.  periph is the mapped address of the peripheral status info  *periph is assigned by peripheral directly  Compiled code will move memory contents to a register  Memory will only be moved once because *periph does not change

Slides created by: Professor Ian G. Harris Types of Statements  Assignment – =  Relational -, ==, !=  Control Flow – if, for, while, do, switch  Arithmetic Operations - +, -, *, /, %  Logical Operations - &&, ||, !  Bitwise Logical Operations - &, |, ^

Slides created by: Professor Ian G. Harris Decimal, Binary Hexadecimal  All numbers are internally represented in binary  Decimal is the default representation in C  Binary is often useful in embedded programming – Individual bits are important to see TRISA = 0b110011; – Notated with “0b” prefix  Hexadecimal is succinct and matches binary 4x1 – Notated with “0x” prefix Ex. 39 = 0b = 0x27

Slides created by: Professor Ian G. Harris Logical Operators  These operators accept binary input and produce binary output &&, ||, !  Non-binary inputs are cast to binary values int a=1, b=0; c=56; d=-1, res; res = a && b;// res = 0 res = a || b;// res = 1 res = c && d;// res = 1

Slides created by: Professor Ian G. Harris Bitwise Logical Operators  These operators accept multi-bit inputs and return multi-bit results &, |, ^, >  Operation is performed on each corresponding pair of bits char a = 0b , b = 0b ; res = a & b;// res = 0b res = a | b;// res = 0b res = a ^ b;// res = 0b res = a << 4; // res = 0b res = b >> 4; // res = 0b

Slides created by: Professor Ian G. Harris Bitlevel Manipulation  Often need to control individual bits in registers  Constants may be defined to refer to individual bits TRISA vs. TRISA0 PORTA vs. RA0  Sometimes more convenient to assign many bits at once PORTA = 0b000111; PORTA = 7; TRISA = 0b111000;

Slides created by: Professor Ian G. Harris Function Calls  Functions enable simple code reuse  Control moves to function, returns on completion  Functions return only 1 value main() { int x; x = foo( 3, 4); printf(“%i\n”, x); } int foo(int x, int y) { return (x+y*3); }

Slides created by: Professor Ian G. Harris Function Prototypes main() { int x; x = foo(); } void foo() { printf (“Hi!\n”); }  Function prototypes declare the return value type and the types of the arguments  Allows the compiler to do type checking  Default return type is int if no prototype is provided void foo();

Slides created by: Professor Ian G. Harris Function Call Overhead main() { int x; x = foo(2); printf(“%i\n”, x); } int foo(int x) { int y=3; return (x+y*3); }  Program counter value needs to be restored after call  Local variables are stored on the stack  Function calls place arguments and return address on the stack 20: 21: 22: 30: 31: 103: 3local var 102: 2argument 101: 21return addr 100: 2local var

Slides created by: Professor Ian G. Harris Header Files  Files included at the top of a code file  Traditionally named with.h suffix  Include information to be shared between files Function prototypes extern s of global variables Global #define s  Needed to refer to libraries

Slides created by: Professor Ian G. Harris Type Casts  Tell the compiler to treat a variable as a different type  Allows typing rules to be violated  May change the space and functional requirements int x=5, y=3; float z; z = x / y; z = (float) x / (float) y;  If x and y are integers then x/y = 1  If x and y are floats then x/y = 1.66  Need a floating point divider

Slides created by: Professor Ian G. Harris Strings  Strings are arrays of chars  A char is the ASCII value of the corresponding character  Double quotes used for strings, single quotes for chars  Strings need a '\0' terminator char achar, astr[13]; achar = 'a'; strncpy(astr, “Hello world!”, 13);

Slides created by: Professor Ian G. Harris Pointers  c is a char. Size of c is the size of a char  & is the reference operator  &c is pointer to c, its address in memory  Size of &c is the size of an address  *d is a char  * is the dereference operator  d is the pointer to *d char c; c = 'a'; char *d; *d = 'a';

Slides created by: Professor Ian G. Harris Pointer Arithmetic  Arithmetic can be performed on pointers  Useful to explore known blocks of memory like arrays  Arrays are just pointers to contiguous memory blocks int varr[10]; printf (“%i”, *varr++); printf (“%i”, *varr); // First elt of array // Second elt of array

Slides created by: Professor Ian G. Harris Call by Reference  A pointer can be passed as an argument to a function  The called function can directly manipulate the variable main () { int x=0; foo(&x); printf (“%i\n”, x); } foo (int *var) { *var = 10; }  Normally a called function cannot access the caller's scope

Slides created by: Professor Ian G. Harris Structures  A collection of variables under a single name  Each grouped variable has its own name  A record with several fields  Pre-object oriented method of organization typedef struct { char name[20]; int age; } person; person harris;  Size of structure is the sum of its component fields

Slides created by: Professor Ian G. Harris Accessing Structure Data person p1, *p2; printf (“%i”, p1.age); printf (“%i”, *p2.age); printf (“%i”, p2->age); . operator returns the value of the member  -> operator dereferences structure pointer first  Structures are often passed by reference, so - > is useful