return return else return

Similar documents
else return for return

Week 4, continued. This is CS50. Harvard University. Fall Anna Whitney

Week 4, continued. This is CS50. Harvard University. Fall Cheng Gong

cs50.c /**************************************************************************** * CS50 Library 6 *

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

lectures/7/src7/bmp.h /**************************************************************************** * bmp.h * Computer Science 50 * Problem Set 5

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

University of Calgary Department of Electrical and Computer Engineering ENCM 335 Instructor: Steve Norman

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

argv1.c 1 // Printing command-line arguments 5 6 int main(int argc, string argv[]) 7 { 8 for (int i = 0; i < argc; i++) 9 { 10 printf("%s\n", argv[i])

Approximately a Test II CPSC 206

Language comparison. C has pointers. Java has references. C++ has pointers and references

This is CS50. Harvard University Fall Quiz 0 Answer Key

Arrays and Pointers (part 1)

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

Slides adopted from T. Ferguson Spring 2016

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

EM108 Software Development for Engineers

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program, which includes three function definitions, including the main function.

ESC101N: Fundamentals of Computing End-sem st semester

Dynamic Data Structures. CSCI 112: Programming in C

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

COP 3502 Section 2 Exam #2 Version A Spring /23/2017

CS 222: Pointers and Manual Memory Management

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

Arrays and Pointers (part 1)

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

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

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Signature: ECE 551 Midterm Exam

20 Dynamic allocation of memory: malloc and calloc

C Language Part 3. Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

today cs3157-fall2002-sklar-lect05 1

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Character Strings. String-copy Example

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

Arrays and Pointers in C. Alan L. Cox

C programming basics T3-1 -

Dynamic Memory CMSC 104 Spring 2014, Section 02, Lecture 24 Jason Tang

Compiling and Running a C Program in Unix

SOFTWARE Ph.D. Qualifying Exam Fall 2017

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

PDS Class Test 2. Room Sections No of students

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Decisions II. Switch Statement. If else allows a 2 way decision Switch allows for n-way decisions

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.

LAB7 : Characters and Strings

Programming in C Quick Start! Biostatistics 615 Lecture 4

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Programming in C. UVic SEng 265. Daniel M. German Department of Computer Science University of Victoria. October 23, 2002 Version: 1.

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Lectures 5-6: Introduction to C

More Pointers Week 11

C Characters and Strings

Do not start the test until instructed to do so!

Lectures 5-6: Introduction to C

8. Characters, Strings and Files

Tin học cơ sở 4$ Structures!

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

list-0.c * list-0.c * David J. Malan * * Demonstrates a linked list for numbers.

Basic and Practice in Programming Lab7

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Computer Programming Lecture 12 Pointers

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

Princeton University COS 217: Introduction to Programming Systems Spring 2009 Final Exam Answers

Memory, Arrays & Pointers

Final Intro to C Review

CS 241 Data Organization Pointers and Arrays

Algorithms & Programming. Midterm Examination # 2. Student Name: Student ID Number: Duration of examination:

Dynamic memory. EECS 211 Winter 2019

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program which consists of two function definitions including the main function.

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Lecture 9 - C Functions

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

ARRAYS(II Unit Part II)

Self-referential Structures and Linked List. Programming and Data Structure 1

COP 3223 Final Review

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Course organization. Course introduction ( Week 1)

Lab 3. Pointers Programming Lab (Using C) XU Silei

Final Exam 1 /12 2 /12 3 /10 4 /7 5 /4 6 /10 7 /8 8 /9 9 /8 10 /11 11 /8 12 /10 13 /9 14 /13 15 /10 16 /10 17 /12. Faculty of Computer Science

Intermediate Programming, Spring 2017*

Introduction to string

Overview. Concepts this lecture String constants Null-terminated array representation String library <strlib.h> String initializers Arrays of strings

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm Solutions

Introduction to C Language (M3-R )

Fundamental of Programming (C)

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #07. Topic: Pointer in C Date:

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME

1 SAT-DANCE-HEULE INTRO 1

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

Contents. A Review of C language. Visual C Visual C++ 6.0

Transcription:

compare0.c 1 // Compares two strings' addresses 4 #include <stdio.h> 5 6 int main(void) 7 { 8 // get two strings 9 string s = get_string("s: "); 10 string t = get_string("t: "); 11 1 // compare strings' addresses 13 if (s == t) 14 { 15 printf("same\n"); 16 } 17 else 18 { 19 printf("different\n"); 0 } 1 }

compare1.c 1 // Compares two strings for equality 4 #include <stdio.h> 5 #include <string.h> 6 7 int main(void) 8 { 9 // get two strings 10 char *s = get_string("s: "); 11 char *t = get_string("t: "); 1 13 // compare strings for equality 14 if (strcmp(s, t) == 0) 15 { 16 printf("same\n"); 17 } 18 else 19 { 0 printf("different\n"); 1 } }

compare.c 1 // Compares two strings for equality while checking for errors 4 #include <stdio.h> 5 #include <string.h> 6 7 int main(void) 8 { 9 // get a string 10 char *s = get_string("s: "); 11 if (s == NULL) 1 { 13 return 1; 14 } 15 16 // get another string 17 char *t = get_string("t: "); 18 if (t == NULL) 19 { 0 return 1; 1 } 3 // compare strings for equality 4 if (strcmp(s, t) == 0) 5 { 6 printf("same\n"); 7 } 8 else 9 { 30 printf("different\n"); 31 } 3 return 0; 33 }

compare3.c 1 // Compares two strings for equality while checking (succinctly) for errors 4 #include <stdio.h> 5 #include <string.h> 6 7 int main(void) 8 { 9 // get a string 10 char *s = get_string("s: "); 11 if (!s) 1 { 13 return 1; 14 } 15 16 // get another string 17 char *t = get_string("t: "); 18 if (!t) 19 { 0 return 1; 1 } 3 // compare strings for equality 4 if (strcmp(s, t) == 0) 5 { 6 printf("same\n"); 7 } 8 else 9 { 30 printf("different\n"); 31 } 3 return 0; 33 }

copy0.c 1 // Capitalizes a string 4 #include <ctype.h> 5 #include <stdio.h> 6 #include <string.h> 7 8 int main(void) 9 { 10 // get a string 11 string s = get_string("s: "); 1 13 // copy string's address 14 string t = s; 15 16 // capitalize first letter in string 17 if (strlen(t) > 0) 18 { 19 t[0] = toupper(t[0]); 0 } 1 // print string twice 3 printf("s: %s\n", s); 4 printf("t: %s\n", t); 5 }

copy1.c 1 // Capitalizes a copy of a string while checking for errors 4 #include <ctype.h> 5 #include <stdio.h> 6 #include <string.h> 7 8 int main(void) 9 { 10 // get a string 11 char *s = get_string("s: "); 1 if (!s) 13 { 14 return 1; 15 } 16 17 // allocate memory for another string 18 char *t = malloc((strlen(s) + 1) * sizeof(char)); 19 if (!t) 0 { 1 return 1; } 3 4 // copy string into memory 5 for (int i = 0, n = strlen(s); i <= n; i++) 6 { 7 t[i] = s[i]; 8 } 9 30 // capitalize first letter in copy 31 if (strlen(t) > 0) 3 { 33 t[0] = toupper(t[0]); 34 } 35 36 // print strings 37 printf("s: %s\n", s); 38 printf("t: %s\n", t); 39 40 // free memory 41 free(t); 4 return 0; 43 }

memory.c 1 // http://valgrind.org/docs/manual/quick-start.html#quick-start.prepare 3 #include <stdlib.h> 4 5 void f(void) 6 { 7 int *x = malloc(10 * sizeof(int)); 8 x[10] = 0; 9 } 10 11 int main(void) 1 { 13 f(); 14 return 0; 15 }

noswap.c 1 // Fails to swap two integers 3 #include <stdio.h> 4 5 void swap(int a, int b); 6 7 int main(void) 8 { 9 int x = 1; 10 int y = ; 11 1 printf("x is %i, y is %i\n", x, y); 13 swap(x, y); 14 printf("x is %i, y is %i\n", x, y); 15 } 16 17 void swap(int a, int b) 18 { 19 int tmp = a; 0 a = b; 1 b = tmp; }

scanf0.c 1 // Gets an int from user using scanf 3 #include <stdio.h> 4 5 int main(void) 6 { 7 int x; 8 printf("x: "); 9 scanf("%i", &x); 10 printf("x: %i\n", x); 11 }

scanf1.c 1 // Incorrectly gets a string from user using scanf 3 #include <stdio.h> 4 5 int main(void) 6 { 7 char *s; 8 printf("s: "); 9 scanf("%s", s); 10 printf("s: %s\n", s); 11 }

scanf.c 1 // Dangerously gets a string from user using scanf 3 #include <stdio.h> 4 5 int main(void) 6 { 7 char s[5]; 8 printf("s: "); 9 scanf("%s", s); 10 printf("s: %s\n", s); 11 }

string0.c 1 // Prints a string's characters using square brackets 4 #include <stdio.h> 5 #include <string.h> 6 7 int main(void) 8 { 9 // get a string 10 char *s = get_string("string: "); 11 if (!s) 1 { 13 return 1; 14 } 15 16 // print string, one character per line 17 for (int i = 0, n = strlen(s); i < n; i++) 18 { 19 printf("%c\n", s[i]); 0 } 1 return 0; }

string1.c 1 // Prints a string's characters using pointer arithmetic 4 #include <stdio.h> 5 #include <string.h> 6 7 int main(void) 8 { 9 // get a string 10 char *s = get_string("string: "); 11 if (!s) 1 { 13 return 1; 14 } 15 16 // print string, one character per line 17 for (int i = 0, n = strlen(s); i < n; i++) 18 { 19 printf("%c\n", *(s + i)); 0 } 1 return 0; }

struct0.c 1 // Demonstrates structs 4 #include <stdio.h> 5 #include <string.h> 6 7 #include "struct.h" 8 9 int main(void) 10 { 11 // allocate space for students 1 int enrollment = get_int("enrollment: "); 13 student students[enrollment]; 14 15 // prompt for students' names and dorms 16 for (int i = 0; i < enrollment; i++) 17 { 18 students[i].name = get_string("name: "); 19 students[i].dorm = get_string("dorm: "); 0 } 1 // print students' names and dorms 3 for (int i = 0; i < enrollment; i++) 4 { 5 printf("%s is in %s.\n", students[i].name, students[i].dorm); 6 } 7 }

struct1.c 1 // Demonstrates file I/O 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 8 #include "structs.h" 9 10 int main(void) 11 { 1 // allocate memory for students 13 int enrollment = get_int("enrollment: "); 14 student students[enrollment]; 15 16 // prompt for students' names and dorms 17 for (int i = 0; i < enrollment; i++) 18 { 19 students[i].name = get_string("name: "); 0 students[i].dorm = get_string("dorm: "); 1 } 3 // save students to disk 4 FILE *file = fopen("students.csv", "w"); 5 if (file) 6 { 7 for (int i = 0; i < enrollment; i++) 8 { 9 fprintf(file, "%s,%s\n", students[i].name, students[i].dorm); 30 } 31 fclose(file); 3 } 33 }

1 // Represents a student 3 typedef struct 4 { 5 char *name; 6 char *dorm; 7 } 8 student; struct.h

swap.c 1 // Swaps two integers using pointers 3 #include <stdio.h> 4 5 void swap(int *a, int *b); 6 7 int main(void) 8 { 9 int x = 1; 10 int y = ; 11 1 printf("x is %i, y is %i\n", x, y); 13 swap(&x, &y); 14 printf("x is %i, y is %i\n", x, y); 15 } 16 17 void swap(int *a, int *b) 18 { 19 int tmp = *a; 0 *a = *b; 1 *b = tmp; }