Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Numerical Recipes The Art of Scientific Computing (with some applications in computational physics)
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Lectures 10 & 11.
Programming Languages and Paradigms The C Programming Language.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
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.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Chapter Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Pointers in C Rohit Khokher
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
Kernighan/Ritchie: Kelley/Pohl:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
ISP - 2 nd Recitation Functions Pointers Structs Files Code Examples Homework!
CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
C Slides and captured lecture (video and sound) are available at:
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
1 C Basics. 2 The C Language Spirit Made by professional programmers for professional programmers Very flexible, very efficient, very liberal Does not.
Lecture 10 Introduction to Programming in C Prof. Dr. Arne Kutzner Hanyang University / Seoul Korea.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
5-1 Embedded Systems C Programming Language Review and Dissection III Lecture 5.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Copyright ©: University of Illinois CS 241 Staff1 C Survival Guide.
C LANGUAGE Characteristics of C · Small size
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
5.6 String Processing Part 2. Sprintf(destnvar,…..regularprintf) Write formatted data to string Same as printf except the output is put in variable. A.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
Strings, Pointers and Tools
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
C Primer Session – 1/25/01 Outline Hello World Command Line Arguments Bit-wise Operators Dynamic Memory / Pointers Function Parameters Structures.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
1 C Basics. 2 The C Language Spirit Made by professional programmers for professional programmers Very flexible, very efficient, very liberal Does not.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSC 482/582: Computer Security
Computer Organization and Design Pointers, Arrays and Strings in C
C Basics.
Arrays in C.
Programming Languages and Paradigms
Programming in C Pointer Basics.
Outline Defining and using Pointers Operations on pointers
Programming in C Pointer Basics.
Presentation transcript:

Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher 2 The C Language Spirit Made by professional programmers for professional programmers Very flexible, very efficient, very liberal Does not protect the programmers from themselves. Rationale: programmers know what they are doing UNIX and most “serious” system software (servers, compilers, etc) are written in C. Can do everything Java and C++ can. It’ll just look uglier in C

Copyright ©: Nahrstedt, Angrave, Abdelzaher 3 Compiler gcc See manual “man” for options man gcc Preprocessor Compiler Linker C89 versus C99 C99: inline functions, variable length arrays, … make – a compilation utility Google for make files (or GNU Make)

Copyright ©: Nahrstedt, Angrave, Abdelzaher 4 Programming in C C = Variables + Instructions

Copyright ©: Nahrstedt, Angrave, Abdelzaher 5 Programming in C C = Variables + Instructions int char float … pointer array struct { …}

Copyright ©: Nahrstedt, Angrave, Abdelzaher 6 Programming in C C = Variables + Instructions int char float struct { …} … pointer array function call assignment if switch … for while

Copyright ©: Nahrstedt, Angrave, Abdelzaher 7 10,000 10,004 10,012 10,016 10,020 … Variables Value1 Value2 Value3 Value4 Value5 x y z p d Name (in program text) Value Memory int x; double y; float z; double* p; int d; Type of each variable (also determines size)

Copyright ©: Nahrstedt, Angrave, Abdelzaher 8 10,000 10,004 10,012 10,016 10,020 … Variables Value1 Value2 Value3 Value4 Value5 x y z p d Memory Address (“name” at run-time) Name (in program text) Value Memory int x; double y; float z; double* p; int d; Type of each variable (also determines size)

Copyright ©: Nahrstedt, Angrave, Abdelzaher 9 10,000 10,004 10,012 10,016 10,020 … The “&” Operator: Reads “Address of” Value1 Value2 Value3 Value4 Value5 x y z p d Name Value &y Memory

Copyright ©: Nahrstedt, Angrave, Abdelzaher 10 10,000 10,004 10,012 10,016 10,020 … Pointers Value1 Value2 Value3 10,004 Value5 x y z p d Name Value A pointer is a variable whose value is a memory address: p = &y; Memory

Copyright ©: Nahrstedt, Angrave, Abdelzaher 11 10,000 10,004 10,012 10,016 10,020 … The “*” Operator Reads “Location pointed to by” Value1 Value2 Value3 10,004 Value5 x y z p d Name Value A pointer is a variable whose value is the address of another: p = &y; *p Memory

Copyright ©: Nahrstedt, Angrave, Abdelzaher 12 What is the Output? main() { int *p, q, x; x=10; p=&x; *p=x+1; q=x; printf (“q = %d\n“, q); }

Copyright ©: Nahrstedt, Angrave, Abdelzaher 13 What is the Output? main() { int *p, q, x; x=10; p=&x; *p=x+1; q=x; printf (“q = %d\n“, q); } @*%^ p q x

Copyright ©: Nahrstedt, Angrave, Abdelzaher 14 What is the Output? main() { int *p, q, x; x=10; p=&x; *p=x+1; q=x; printf (“q = %d\n“, q); } 10 p q x

Copyright ©: Nahrstedt, Angrave, Abdelzaher 15 What is the Output? main() { int *p, q, x; x=10; p=&x; *p=x+1; q=x; printf (“q = %d\n“, q); } 10 p q x

Copyright ©: Nahrstedt, Angrave, Abdelzaher 16 What is the Output? main() { int *p, q, x; x=10; p=&x; *p=x+1; q=x; printf (“q = %d\n“, q); } 11 p q x

Copyright ©: Nahrstedt, Angrave, Abdelzaher 17 What is the Output? main() { int *p, q, x; x=10; p=&x; *p=x+1; q=x; printf (“q = %d\n“, q); } 11 p q x

Copyright ©: Nahrstedt, Angrave, Abdelzaher 18 Cardinal Rule: Must Initialize Pointers before Using them int *p; *p = 10; GOOD or BAD???

Copyright ©: Nahrstedt, Angrave, Abdelzaher 19 Cardinal Rule: Must Initialize Pointers before Using them int *p; *p = 10; p ?? Pointing somewhere random BAD!

Copyright ©: Nahrstedt, Angrave, Abdelzaher 20 Cardinal Rule: Must Initialize Pointers before Using them int *p; *p = 10; p 10 BAD!

Copyright ©: Nahrstedt, Angrave, Abdelzaher 21 How to Initialize Pointers

Copyright ©: Nahrstedt, Angrave, Abdelzaher 22 How to Initialize Pointers Set pointer equal to address of known variable int *p; int x; … p=&x;

Copyright ©: Nahrstedt, Angrave, Abdelzaher 23 How to Initialize Pointers Use void *malloc(size_t size); int *p; … p=(int*) malloc (sizeof (int)); malloc() allocates memory dynamically: “heap” sizeof(T) gives the size of a program type T.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 24 How to Initialize Pointers Copy another initialized pointer value int *p, *q; … q = (int*) malloc(sizeof(int)); p = q;

Copyright ©: Nahrstedt, Angrave, Abdelzaher 25 How to Initialize Pointers Create an Array int p[10]; Same as: int *p; p=(int*) malloc (10*sizeof (int));

Copyright ©: Nahrstedt, Angrave, Abdelzaher 26 Arrays int p[5]; p[0] p[1] p[2] p[3] p[4] Name of array (is a pointer) Shorthand: *(p+1) is called ……? *(p+2) is called ……? etc..

Copyright ©: Nahrstedt, Angrave, Abdelzaher 27 Arrays int p[5]; p[0] p[1] p[2] p[3] p[4] Name of array (is a pointer) p Shorthand: *(p+1) is called p[1] *(p+2) is called p[2] etc..

Copyright ©: Nahrstedt, Angrave, Abdelzaher 28 Example int y[4]; y[1]=6; y[2]=2; 6 2 y[0] y[1] y[2] y[3] y

Copyright ©: Nahrstedt, Angrave, Abdelzaher 29 Array Name as Pointer What’s the difference between the examples below? Example 1: int z[8]; int *q; q=z; Example 2: int z[8]; int *q; q=&z[0];

Copyright ©: Nahrstedt, Angrave, Abdelzaher 30 Array Name as Pointer What’s the difference between the examples below? Example 1: int z[8]; int *q; q=z; Example 2: int z[8]; int *q; q=&z[0]; NOTHING!! z (the array name) is a pointer to the beginning of the array, which is &z[0]

Copyright ©: Nahrstedt, Angrave, Abdelzaher 31 Example: How much is y at the end? int y, x, *p; x = 20; *p = 10; y = x + *p;

Copyright ©: Nahrstedt, Angrave, Abdelzaher 32 Example: How much is y at the end? int y, x, *p; x = 20; *p = 10; y = x + *p; BAD!! Dereferencing an unitialized pointer will likely segfault or overwrite something! Segfault = unauthorized memory access

Copyright ©: Nahrstedt, Angrave, Abdelzaher 33 Question: What’s the difference between int* q; int q[5]; What’s wrong with: int ptr[2]; ptr[1] = 1; ptr[2] = 2;

Copyright ©: Nahrstedt, Angrave, Abdelzaher 34 Question: What is the value of b[2] at the end? int b[3]; int* q; b[0]=48; b[1]=113; b[2]=1; q=b; *(q+1)=2; b[2]=*b b[2]=b[2]+b[1];

Copyright ©: Nahrstedt, Angrave, Abdelzaher 35 Question: What is the value of b[2] at the end? int b[3]; int* q; b[0]=48; b[1]=113; b[2]=1; q=b; *(q+1)=2;  b[1] = 2 b[2]=*b  b[2] = b[0] = 48 b[2]=b[2]+b[1];  b[2] =

Copyright ©: Nahrstedt, Angrave, Abdelzaher 36 Strings (Null-terminated Arrays of char) Example char s[5]; Strings are arrays that contain byte values ending in a “Null” character to indicate end of string. Do not forget to leave room for the null character s[0] s[1] s[2] s[3] s[4] s

Copyright ©: Nahrstedt, Angrave, Abdelzaher 37 String and char constants String constants “wassup”// how many bytes? “c” // how many bytes? Character constants ‘c’ ‘\0’// the Null character

Copyright ©: Nahrstedt, Angrave, Abdelzaher 38 String Operations strcpy strlen strcat strcmp

Copyright ©: Nahrstedt, Angrave, Abdelzaher 39 strcpy, strlen Syntax: strcpy(ptr1, ptr2); where ptr1 and ptr2 are pointers to char value = strlen(ptr); where value is an integer and ptr is a pointer to char Example: int len; char str[15]; strcpy (str, "Hello, world!"); len = strlen(str);

Copyright ©: Nahrstedt, Angrave, Abdelzaher 40 strcpy, strlen What’s wrong with char str[5]; strcpy (str, "Hello");

Copyright ©: Nahrstedt, Angrave, Abdelzaher 41 strncpy Syntax: strncpy(ptr1, ptr2, num); where ptr1 and ptr2 are pointers to char num is the number of characters to be copied Example: int len; char str1[15], str2[15]; strcpy (str1, "Hello, world!"); strncpy (str2, str1, 5); Is the example correct?

Copyright ©: Nahrstedt, Angrave, Abdelzaher 42 strncpy Syntax: strncpy(ptr1, ptr2, num); where ptr1 and ptr2 are pointers to char num is the number of characters to be copied Example: int len; char str1[15], str2[15]; strcpy (str1, "Hello, world!"); strncpy (str2, str1, 5); Caution: strncpy blindly copies the characters. It does not voluntarily append the string-terminating null character.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 43 strcat Syntax: strcat(ptr1, ptr2); where ptr1 and ptr2 are pointers to char Concatenates the 2 nd argument to the 1 st (both must be null terminated strings or havoc results!). char S[25] = "world!"; char D[25] = "Hello, "; strcat(D, S);

Copyright ©: Nahrstedt, Angrave, Abdelzaher 44 strcat Example What’s wrong with: char S[25] = "world!"; strcat(“Hello, ”, S);

Copyright ©: Nahrstedt, Angrave, Abdelzaher 45 strcmp Syntax: diff = strcmp(ptr1, ptr2); where diff is an integer and ptr1 and ptr2 are pointers to char Returns zero if strings are identical (or >0 or <0) according to the lexicographical order int diff; char s1[25] = "pat"; char s2[25] = "pet"; diff = strcmp(s1, s2);

Formatted I/O printf, fprintf: Write data to file printf( “Lose %d pounds in %d %s\n”, N, D, “weeks”); scanf, fscanf: Read data from file scanf( “%d %f %80s\n”, &N, &F, buf); // buf must be char* Copyright ©: Nahrstedt, Angrave, Abdelzaher 46

Copyright ©: Nahrstedt, Angrave, Abdelzaher 47 Math: Increment and Decrement Operators Example 1: int x, y, z, w; y=10; w=2; x=++y; z=--w; Example 2: int x, y; y=10; w=2; x=y++; z=w--;

Copyright ©: Nahrstedt, Angrave, Abdelzaher 48 Math: Increment and Decrement Operators Example 1: int x, y, z, w; y=10; w=2; x=++y; z=--w; Example 2: int x, y; y=10; w=2; x=y++; z=w--; First increment/decrement then assign result x is 11, z is 1 First assign then increment/decrement x is 10, z is 2

Copyright ©: Nahrstedt, Angrave, Abdelzaher 49 Math: Increment and Decrement Operators on Pointers Example 1: int a[3]; int number1, number2, *p; a[0]=1; a[1]=10; a[2]=100; p=a; number1 = *p++; number2 = *p; What will number1 and number2 be at the end?

Copyright ©: Nahrstedt, Angrave, Abdelzaher 50 Math: Increment and Decrement Operators on Pointers Example 1: int a[3]; int number1, number2, *p; a[0]=1; a[1]=10; a[2]=100; p=a; number1 = *p++; number2 = *p; What will number1 and number2 be at the end? number1 =1, number2 =10. Hint: ++ increments pointer p not variable *p  check C operator precedence table!

Copyright ©: Nahrstedt, Angrave, Abdelzaher 51 Logic: Relational (Condition) Operators == equal to != not equal to > greater than < less than >= greater than or equal to <= less than or equal to

Copyright ©: Nahrstedt, Angrave, Abdelzaher 52 Logic Example if (a == b) printf (“Equal.”); else printf (“Not Equal.”); Question: what will happen if I replaced the above with: if (a = b) printf (“Equal.”); else printf (“Not Equal.”);

Copyright ©: Nahrstedt, Angrave, Abdelzaher 53 Logic Example Question: what will happen if I replaced the above with: if (a = b) printf (“Equal.”); else printf (“Not Equal.”);  This is a perfectly LEGAL C statement (syntactically speaking) which copies the value in “b" into “a", and delivers this value, which will then be interpreted as TRUE if b is non-zero.