C programming Lecture 2. Marina Krstic Marinkovic School of Mathematics Trinity College Dublin

Similar documents
Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Character Strings. String-copy Example

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Chapter Overview. Pointers and Dynamic Arrays. Pointers. Pointers. Declaring Pointers. Pointers Tell Where To Find A Variable. 9.

First of all, it is a variable, just like other variables you studied

Intermediate Programming, Spring 2017*

Lecture 19: Functions, Types and Data Structures in Haskell

C programming Lecture 15. School of Mathematics Trinity College Dublin. Marina Krstic Marinkovic


Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

arrays review arrays and memory arrays: character array example cis15 advanced programming techniques, using c++ summer 2008 lecture # V.

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Syntax and Variables

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

DEPARTMENT OF MATHS, MJ COLLEGE

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Chapter 9. Pointers and Dynamic Arrays

Programming Fundamentals (CS-302 )

Arrays and Strings. Arash Rafiey. September 12, 2017

Numerical Methods 5633

Principles of C and Memory Management

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

The New C Standard (Excerpted material)

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

CAAM 420 FALL 2012 Lecture 9. Mishael Owoyemi

VARIABLES AND CONSTANTS

DECLARAING AND INITIALIZING POINTERS

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Single Dimension Arrays

Lecture 02 C FUNDAMENTALS

Principles of Programming. Chapter 6: Arrays

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Week 7: Pointers and Arrays. BJ Furman 02OCT2009

Lesson 7. Reading and Writing a.k.a. Input and Output

Procedures, Parameters, Values and Variables. Steven R. Bagley

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

BSM540 Basics of C Language

BLM2031 Structured Programming. Zeyneb KURT

LOÏC CAPPANERA. 1. Memory management The variables involved in a C program can be stored either statically or dynamically.

Arrays in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Introduction to Programming Using Java (98-388)

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

Fundamentals of Programming

Chapter 3. Fundamental Data Types

BSM540 Basics of C Language

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

IV Unit Second Part STRUCTURES

C for Engineers and Scientists: An Interpretive Approach. Chapter 10: Arrays

INTRODUCTION 1 AND REVIEW

ARRAYS(II Unit Part II)

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Informatica e Sistemi in Tempo Reale

UNIT-V. Structures. The general syntax of structure is given below: Struct <tagname> { datatype membername1; datatype membername2; };

A Fast Review of C Essentials Part I

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

M.CS201 Programming language

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

Q 1. Attempt any TEN of the following:

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Computers Programming Course 5. Iulian Năstac

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

6.096 Introduction to C++ January (IAP) 2009

Programming and Data Structures

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

C Syntax Out: 15 September, 1995

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

C library = Header files + Reserved words + main method

Data Type Fall 2014 Jinkyu Jeong

Week 3 Lecture 2. Types Constants and Variables

Functions. Introduction :

VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANGULATHUR

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

Binghamton University. CS-211 Fall Pointers vs. Arrays

Chapter 4. Section 4.5 Initialization of Arrays. CS 50 Hathairat Rattanasook

Pointers and Dynamic Arrays

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

The Warhol Language Reference Manual

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal


M1-R4: Programing and Problem Solving using C (JAN 2019)

today cs3157-fall2002-sklar-lect05 1

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -VARIABLE AND DATA TYPES- SPRING 2015, SEON-JU AHN, CNU EE

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Multidimension array, array of strings

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Transcription:

C programming 5613 Lecture 2 Marina Krstic Marinkovic marina.marinkovic@cern.ch School of Mathematics Trinity College Dublin Marina Krstic Marinkovic 1 / 10 5613 - C programming

Arrays Array: indexed sequence of data stored contiguously in memory square brackets used for indexing: x[i] int x[5]; int int int int int 5 23 17 1746 912 x[0] x[1] x[2] x[3] x[4] Arrays are indexed with zero offset (first element is x[0]) Size must be known at time of declaration of an array Marina Krstic Marinkovic 2 / 10 5613 - C programming

Arrays 2- dim arrray - declaring sizes: int f(int k) return 2*k*k; int main() int n=10; int x[f(3)][n]; int x[]=-3,7,-5; x[18][10] The size of an array should be computable at the time the array is declared Arrays size can be automatically computed from an initialiser Arrays can take local or global scope and be declared static, just like other variables. Marina Krstic Marinkovic 3 / 10 5613 - C programming

Arrays Arrays as function arguments: void f(float arr[]) arr[1]+=arr[2]; int main() float x[] = 1.2, 5.6, 7.8; f(x); printf( %f\n",x[1]); Note the [] syntax in the function The function does not need the array length although it can be given Marina Krstic Marinkovic 4 / 10 5613 - C programming

Arrays Multi-dimensional arrays: void f(float arr[]) arr[1]+=arr[2]; int main() float x[] = 1.2, 5.6, 7.8; f(x); printf( %f\n",x[1]); Arrays are indexed using sets of square brackets All indices are zero offset (so first element is x[0][0]...[0]) Ordering can be confusing: read int x[2][3] as a two-element array of int[3]s All dimension must be known. Left-most can be auto-sized int f[][2]=3,4,2,1,6,7 Marina Krstic Marinkovic 5 / 10 5613 - C programming

Exercises Arrays: 1. Determine the number of components in the arrays with the following definition: float x[120]; double y[33]; 2. Assuming the definition: int x[26],y[23]; char nr1[7]; determine which of the following variable assignments are correct? a) x[1]=y[2]; b) x[25]=y[1]+1; c) x[1]=x[26]; d) x=y; e) nr1[0]= 1 ; Marina Krstic Marinkovic 6 / 10 5613 - C programming

Strings ASCII text characters are stored in the char data type. Each char occupied 1 byte of memory. AC string is an array of characters, terminated with the zero character, \0 char w[]= HPC w[0] w[1] w[2] w[3] H' P C \0 strlen(w)=3 A C string is written inside double quote marks ("). It can be automatically sized for array initialisation. (strlen(w) The length of a C string w is returned by the function strlen(w). Because of the \0 character marking the end, string s takes strlen(s)+1 bytes to store. Marina Krstic Marinkovic 7 / 10 5613 - C programming

Typedef Can define aliases for complicated data types using typedef keyword. typedef double Real defines a new type called Real that can be used interchangeably with a double. Useful if you want to switch precision between 32-bit and 64-bit floating point typedef int mytype[3]; mytype q; q[0]=123;q[1]=234;q[2]=345; printf( %d\n,q[0]); printf("size of mytype: %d bytes\n",sizeof(mytype)); defines a new type called mytype that is a length-three array of integers. Can use this short-hand to declare: The sizeof built-in function gives the amount of memory needed to store a data-type. Marina Krstic Marinkovic 8 / 10 5613 - C programming

Integer types: Variables and types Floating-point types: Marina Krstic Marinkovic 9 / 10 5613 - C programming

Typedef Can define aliases for complicated data types using typedef keyword. typedef double Real defines a new type called Real that can be used interchangeably with a double. Useful if you want to switch precision between 32-bit and 64-bit floating point typedef int mytype[3]; mytype q; q[0]=123;q[1]=234;q[2]=345; printf( %d\n,q[0]); printf("size of Integer in bytes: %d \n,sizeof(int)); printf("size of mytype: %d bytes\n",sizeof(mytype)); defines a new type called mytype that is a length-three array of integers. Can use this short-hand to declare: The sizeof built-in function gives the amount of memory needed to store a data-type. Marina Krstic Marinkovic 10 / 10 5613 - C programming