C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Programming Languages and Paradigms The C Programming Language.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
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.
SPLINT STATIC CHECKING TOOL Sripriya Subramanian 10/29/2002.
Hello World Program The source code #include int main() { printf("Hello World\n"); return(0); }
Chapter 7: User-Defined Functions II
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
C Programming - Lecture 5
Programming in C Pointers and Arrays, malloc( ). 7/28/092 Dynamic memory In Java, objects are created on the heap using reference variables and the new.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction.
Kernighan/Ritchie: Kelley/Pohl:
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
C Language Summary HTML version. I/O Data Types Expressions Functions Loops and Decisions Preprocessor Statements.
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Lecture 13 Static vs Dynamic Memory Allocation
Stack and Heap Memory Stack resident variables include:
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
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.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
APS105 Malloc and 2D Arrays (text Ch6.4, Ch10.2).
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
C++ Lesson 1.
The Machine Model Memory
C Primer.
A bit of C programming Lecture 3 Uli Raich.
C Programming Tutorial – Part I
Programming Languages and Paradigms
C Short Overview Lembit Jürimägi.
Programming Paradigms
CSCI206 - Computer Organization & Programming
Programming Languages and Paradigms
Pointers.
Chapter 14 - Advanced C Topics
Pointers.
Programming Language C Language.
Fundamental Programming
C Programming - Lecture 5
Programming Languages and Paradigms
15213 C Primer 17 September 2002.
Pointers.
C Tutorial Adapted from Wei Qian’s C tutorial slides.
Presentation transcript:

C Programming Revision Malcolm Wilson

Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;

Keywords C has a small number of “keywords”

Standard I/O printf() scanf() Format specifiers %d, %f, %c, %s

Operations +, -, *, /, %, ^ Operation dependent on variable type Try some

Boolean Any value other than zero is true. Watch out for == “is equal to”.

Control for(i=0, i<9, i++) { code block} while (x<8) {code block} if (y==2) {code block} elseif(y==7) {code block} else {code block}

Control switch( myvar) case 1 : {code block break;} case 2: {code block break;} default {code block} us/library/66k51h7a%28VS.80%29.aspx

Control Code block is surrounded by {} if more than one line. Don’t need {} if code one line long. eg for(i=0; i<5; i++) printf(“ number is: %d /n”, i);

Functions and Prototypes C is composed of functions, and must have at least one function called main(). Functions accept parameters and return values A “prototype” should be written which indicates what data types a function should accept and return. – Eg int mynumberfunction( int num1, int num2) ;

Scope and storage class Used for AVR Static,will remain even after function has exited. Global Volatile, can be changed by unpredicable actions.

Preprocessor directives #include – “localfile” – /usr/include #define – #define WIDTH 80 – #define LENGTH ( WIDTH + 10 ) – #define u8 unsigned char

Arrays and strings int myarray[5]; int myarray[5]={1, 2, 3}; int myarray[]={1,2,3,4,5}; char mychararray=“malcolm”; A string is a “null terminated” char array.

Structures struct struct_name { structure_member;... } instance_1,instance_2 instance_n; OR struct struct_name instance_1,instance_2,instance3 After defining the structure.

Structures Using typedef to avoid struct structurename all the time. typedef struct{ unsigned int house_number; char street_name[50]; int zip_code; char country[50]; } address; address billing_addr; address shipping_addr;

Pointers Declared as using * int *p says p in a pointer to an integer. p points to the memory location where a integer is stored. Confusing, in the code. *p means the contents of memory location p. And &p is the memory address of p.

Pointers and arrays myarray is the same as &myarray[0] So if an array is initialised as char name[]=“malcolm”; *(name+3) will be ‘c’;

Dynamic memory allocation Allocates memory on the “heap” malloc(n) calloc(s, nbytes) intialises memory free();

sizeof() Used for malloc to allocate memory

Pointers and structures #include main() { printf("hello world \n"); struct mystruct{ int age; char buffer[20]; }mydata; mydata.age=45; printf("age is %d \n", mydata.age); struct mystruct * d; d=malloc(sizeof(struct mystruct)); d->age=53; printf("pointed age is %d \n", d->age); }

argv and argc int main (int argc, char *argv[]) { } argc, argument count argv, argument vector

Boo Boo’s in C Forgetting the semicolon Using = in a boolean expression instead of ==. Completing a loop with no code being executed. – while(test); { code block}

Deep C Lvalues, Rvalues Inline functions Pointers to functions Pointers to pointers and multidimensional arrays.