Introduction to C CS 3410.

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

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.
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
MPI and C-Language Seminars Seminar Plan (1/3)  Aim: Introduce the ‘C’ Programming Language.  Plan to cover: Basic C, and programming techniques.
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
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?
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Arrays and Pointers in C Alan L. Cox
Computer Science 210 Computer Organization Introduction to C.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
1 計算機程式設計 Introduction to Computer Programming Lecture01: Introduction and Hello World 9/10/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Lecture #5 Introduction to C++
C Lab 1 Introduction to C. Basics of C Developed by Dennis Ritchie in the 1970s. Maps very easily to machine instructions. Even allows inline assembly!
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Object Oriented Software Development 4. C# data types, objects and references.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Department of Electronic & Electrical Engineering Types and Memory Addresses Pointers & and * operators.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 1 Scott Marino.
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
C is a high level language (HLL)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
1 C Basics. 2 The C Language Spirit Made by professional programmers for professional programmers Very flexible, very efficient, very liberal Does not.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lecture 3 Translation.
Computer Organization and Design Pointers, Arrays and Strings in C
The Machine Model Memory
Chapter 7 Pointers and C-Strings
Computer Science 210 Computer Organization
CSE 451 C refresher.
Memory, Data, & Addressing II CSE 351 Autumn 2017
Review Questions If the word size of a machine is 64-bits, which of the following is usually true? (pick all that apply) 64 bits is the size of a pointer.
C Programming Tutorial – Part I
C Language By Sra Sontisirikit
Quiz 11/15/16 – C functions, arrays and strings
Debugging with gdb gdb is the GNU debugger on our CS machines.
Lecture-5 Arrays.
Learning Objectives Pointers Pointer in function call
C programming language
C Basics.
Lecture 6 C++ Programming
INTRODUCTION c is a general purpose language which is very closely associated with UNIX for which it was developed in Bell Laboratories. Most of the programs.
Andy Wang Object Oriented Programming in C++ COP 3330
جامعة البحر الاحمر كلية العلوم التطبيقية قسم الفيزياء التطبيقية الفصل الداسي الثاني IIالمقرر: حاسوب د. خالد عثمان العالم.
Computer Science 210 Computer Organization
Memory, Data, & Addressing II CSE 410 Winter 2017
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Pointers, Dynamic Data, and Reference Types
Review Questions If the word size of a machine is 64-bits, which of the following is usually true? (pick all that apply) 64 bits is the size of a pointer.
جامعة البحر الاحمر كلية العلوم التطبيقية قسمي الحاسوب وتقنية المعلومات الفصل الداسي الثاني المقرر: اساليب برمجة 1 محاضرة رقم 1 د. خالد عثمان العالم.
Memory, Data, & Addressing II CSE 351 Winter 2018
Introduction to C Topics Compilation Using the gcc Compiler
Memory, Data, & Addressing II CSE 351 Summer 2018
Memory, Data, & Addressing II CSE 351 Autumn 2018
Memory, Data, & Addressing II CSE 351 Winter 2018
Introduction to C Topics Compilation Using the gcc Compiler
Memory, Data, & Addressing II CSE 351 Winter 2019
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Pointers, Dynamic Data, and Reference Types
cout << str1;  pear
SPL – PS1 Introduction to C++.
Computer Architecture and System Programming Laboratory
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Introduction to C CS 3410

Basics of C Developed by Dennis Ritchie in the 1970s. Maps very easily to machine instructions. Even allows inline assembly! Unlike Java or Python, the programmer is in charge of memory management. There is no garbage collector. Not Object Oriented! No inheritance or interfaces.

Terminology Compiler: The program that converts source code into object code (assembly/machine code) You will be using GCC to compile your source before you can run it. Debugger: The program that allows you to inspect a program while it is running. You will be using GDB. A good alternative is to use print statements everywhere! Header: File of function and variable declarations If you want to use functions defined in other files, you must include a corresponding header (.h) file.

Structure of a C Program Which lines are preprocessor directives? Function declarations?

Arrays Array To refer to an element, specify Format: Name of array (Note that all elements of this array have the same name, c) Position number of the element within array c c[6] -45 6 72 1543 -89 62 -3 1 6453 78 c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4] Arrays Array Group of consecutive memory locations Same name and type To refer to an element, specify Array name Position number Format: arrayname[ position number ] First element at position 0 n element array named c: c[ 0 ], c[ 1 ]...c[ n – 1 ]

Arrays declaration initialization

SEGFAULT Common Array Mistakes C has no array-bound checks. You won’t even get a warning! At best you’ll get a segfault when you try to run it. Do not use sizeof(array), it will return the size of one single element, not the underlying memory size. SEGFAULT

Strings in C… Just null terminated char arrays! For example: “CS3410” is equivalent to: char str[] = {‘C’,‘S’,‘3’,‘4’,‘1’,‘0’,‘\0’}; Things to note: <string.h> has common string functions: strlen(s) does not include the terminal character. Be careful when using memory operations which does include the character!

Pointers A pointer is a bit-pattern (generally a number) that represents a memory location. To access the data in the memory that the pointer points to, you dereference the pointer. The type of a pointer tells the compiler what kind of object to expect when it is dereferenced. A “double*” is a pointer representing the memory location of a double. A pointer does not actually create the data it points to. For the pointer to contain data, some other function must create the data it will point to. This is typically a call to malloc.

Pointers

Pointers: A visual representation char* d1; d1 = “CS3410”; Memory char* d2; d2 = d1+1; Address Value 0x07 ‘\0’ 0x06 ‘0’ 0x05 ‘1’ 0x04 ‘4’ 0x03 ‘3’ 0x02 ‘S’ 0x01 ‘C’ Stack //d2[0]==‘S’; char* d1 char* d2 d2[6]=?

Pointers: A visual representation int* d1; d1 = {3,4,1,0,9,8,7}; Memory int* d2; d2 = d1+1; Address Value 0x18 7 0x14 8 0x10 9 0x0c 0x08 1 0x04 4 0x00 3 Stack //d2[0]==4; int* d1 int* d2 d2[6]=?

Pointers to stack variables float* d1; float local = 42.42; d1 = &local; *d1 = 25; Stack float* d1 local = 25 local = 42.42