Strings in C++ and Java. Questions:

Size: px
Start display at page:

Download "Strings in C++ and Java. Questions:"

Transcription

1 Strings in C++ and Java Questions: 1

2 1. What kind of access control is achieved by the access control modifier protected? 2

3 2. There is a slight difference between how protected works in C++ and how it works in Java? How would you explain the difference? 3

4 3. What s wrong with the following code fragment and how will you fix it without changing the access control level of each member? class X { int m; int n; public: X( int mm, int nn ) { m = mm; n = nn; } }; class Y { X* x; int t; public: Y( X* xobj ) { x = xobj; t = x->m + x->n; } int get_t() { return t; } }; 4

5 4. If you saw the following class in a C++ source code file, what would be your first thought about the nature of the class? class Shape { public: virtual double area( ) = 0; virtual double circumference() = 0; }; 5

6 5. What is a Java interface? Shown below is a small part of the Java interface Collection. A Collection in Java is meant for holding objects, much like an array or a linked-list. interface Collection { public boolean add( Object o ); public boolean remove( Object o ); // other methods } So what do you think is the purpose of the functions add and remove declared in the interface? 6

7 6. What are the two different types of comparisons one can make for comparing objects on the basis of their content? 7

8 7. Which C++ operator is typically used for a boolean comparison between two objects? 8

9 8. Let s say that you as a programmer define a new class as follows: class Apple { Taste taste; Size size; Weight wt; //... }; How would the compiler know how to carry out a boolean comparison of two apples with the == operator? 9

10 9. The operator == in Java also carries out a boolean comparison, but it does not work in the same way as in C++. What s the difference? 10

11 10. You obviously cannot use == for a content-based boolean comparison between two objects in Java. So what are you supposed to do? 11

12 11. JAVA question: class X { private int p; public X( int m ) { p = m; } } X x1 = new X(10); X x2 = new X(10); X x3 = new X( ); X x4 = x1; X x5 = x2; x1 == x1;???? x1 == x2;???? x1 == x4;???? x1 == x5;???? x1.equals( x2 );???? x1.equals( x4 );???? x1.equals( x3 );???? 12

13 12. JAVA Question: class X { private int p; public X( int m ) { p = m; } boolean equals( X other ) { return p == other.p; } } X x1 = new X(10); X x2 = new X(10); X x3 = new X( ); X x4 = x1; X x5 = x2; x1 == x1;???? x1 == x2;???? x1 == x4;???? x1 == x5;???? x1.equals( x2 );???? x1.equals( x4 );???? x1.equals( x3 );???? 13

14 Strings in C, A Brief Review A C-style string is defined as an array of characters that terminates in the null character. To declare a string variable str with a storage allocation of 6 characters, the last character being reserved for the terminating null character: char str[6]; 14

15 If we wish to also initialize a string variable at the time it is declared, we can do so by char str[5 + 1] = "hello"; or by char str[] = "hello"; 15

16 char str[5 + 1] = "hello"; char str[] = "hello"; The double quoted string of characters on the right hand side is called what? What exactly is achieved by the initialization syntax? 16

17 We can also use a character pointer directly to represent a string, as in char* str = "hello"; // (B) 17

18 char str[5 + 1] = "hello"; char str[] = "hello"; char* str = "hello"; While we may declare a string variable to be an array of characters or a character pointer, the two versions are not always interchangeable. Why? 18

19 Some C and C++ compilers do allow a string literal to be modified through a pointer to which the string literal is assigned. For example, the following will work with many compilers char* str = "hello"; *str = j ; But try not do it because it can result in non-portable code. If you must modify a string literal, it is best to first copy it into an array, that is stored at a location different from where the string literal itself is stored, as in char str[] = "hello"; str[0] = j ; 19

20 Frequently Used Functions for C-style Strings Provided by the string.h header file. The string comparison function strcmp int strcmp( const char* arg1, const char* arg2 ); returns a value less than, equal to, or greater than 0 depending on whether arg1 is less than, equal to, or greater than arg2. 20

21 Typically, ASCII character sets are used and strings are compared using the ASCII integer codes associated with the characters. For example, the following inequality is true for for one-character strings strcmp( "A", "a" ) < 0 because the ASCII code for the character A is 65, whereas the ASCII code for a is 96, making the string literal A less than the string literal a. 21

22 Given a character by character comparison on the basis of ASCII codes, longer strings are compared using lexicographic ordering an ordering that is akin to how words are arranged in a dictionary. For example, in lexicographic ordering, the string abs will occur before the string absent, so the former is less than the latter. However, the string Zebra will occur before the string debra, as the former is less than the latter because the ASCII codes for all uppercase letters, A through Z, occupy the range 65 through 90, whereas the codes for lowercase letters, a through z, occupy the range 97 through

23 Function for determining the length of a string: size_t strlen( const char* arg ); strlen( "hello" ) returns 5. 23

24 A function for copying a C-style string: char* strcpy( char* arg1, const char* arg2 ); which copies the characters from the string arg2 into the memory locations pointed to by arg1. We could say char str1[6]; char* str2 = "hello"; strcpy( str1, str2 ); or, using the C memory allocation function malloc(), char* str1 = (char*) malloc( 6 ); char* str2 = "hello"; strcpy( str1, str2 ); 24

25 A function for concatenating two strings: char* strcat( char* arg1, const char* arg2 ); This function appends the string pointed to by arg2 to the string pointed to by arg1. For example, char str1[8]; strcpy( str1, hi ); strcat( str1, there ); will cause the string hithere to be stored at the memory locations pointed to by str1. 25

26 Some Common Shortcomings of C-Style Strings C-style strings can be painful to use, especially after you have seen the more modern representations of strings in other languages. Frequent source of bugs: No memory allocation where needed or incorrect memory allocation. 26

27 Convenience of modern string types: string str1 = hi ; string str2 = there ; string str3; str3 = str1 + str2; 27

28 Errors easily made when programming with C-style strings. 28

29 Consider, for example, the following routine for calculating the length of a C-style string: int str_len( const char* ptr ) { int count = 0; if ( ptr ) /* A */ while ( *ptr++ ) ++count; /* B */ return count; } In a hurry, you might write the following version of the program: int str_len( const char* ptr ) { int count = 0; if ( ptr ) while ( ptr++ ) ++count; /* WRONG */ return count; } 29

30 For another readily made error, suppose we wish to include in the program fragment a print statement that would print out the string and its length, as in #include <string.h> const char* ptr = "hi there"; int main() { int count = 0; if ( ptr ) while ( *ptr++ ) ++count; /* D */ printf( "The length of the string %s is %d\n", ptr, count ); return 0; } Where do you think is the bug in this program? 30

31 #include <string.h> const char* ptr = "hi there"; int main() { int count = 0; if ( ptr ) while ( *ptr++ ) ++count; /* D */ ptr = ptr - count - 1; printf( "The length of the string %s is %d\n", ptr, count ); return 0; } #include <string.h> const char* ptr = "hi there"; int main() { int count = 0; const char* s = ptr; /* F * if ( ptr ) while ( *s++ ) ++count; printf( "The length of the string %s is %d\n", ptr, count ); return 0; } 31

1) String initialization can be carried out in the following ways, similar to that of an array :

1) String initialization can be carried out in the following ways, similar to that of an array : Strings in C : Overview : String data type is not supported in C Programming. String is essentially a collection of characters to form particular word. String is useful whenever we accept name of the person,

More information

strsep exercises Introduction C strings Arrays of char

strsep exercises Introduction C strings Arrays of char strsep exercises Introduction The standard library function strsep enables a C programmer to parse or decompose a string into substrings, each terminated by a specified character. The goals of this document

More information

Characters and Strings. Constants

Characters and Strings. Constants Characters and Strings Constants Characters are the fundamental building blocks of source programs Character constants One character surrounded by single quotes A or? Actually an int value represented

More information

C Programming Strings. Mrs. Hajah T. Sueno, MSIT instructor

C Programming Strings. Mrs. Hajah T. Sueno, MSIT instructor C Programming Strings Mrs. Hajah T. Sueno, MSIT instructor String Basics 3 Characters Characters are small integers (0-255) Character constants are integers that represent corresponding characters. 0 48

More information

Engineering Problem Solving with C++, Etter

Engineering Problem Solving with C++, Etter Engineering Problem Solving with C++, Etter Chapter 7 Strings 12-11-13 1 Strings Character Strings The string Class. 2 C style strings functions defined in cstring CHARACTER STRINGS 3 C Style Character

More information

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal. Problem 1 Recall the definition of root in project 1. (The declaration of struct entrynode appears below.) struct entrynode * root; Give the type of each of the following expressions. The answer may be

More information

Arrays and Pointers (part 1)

Arrays and Pointers (part 1) Arrays and Pointers (part 1) EECS 2031 25 September 2016 1 Arrays l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. 2 1 Arrays: Example

More information

STRINGS. If you follow the rule of array initialization then you can write the above statement as follows:

STRINGS. If you follow the rule of array initialization then you can write the above statement as follows: STRINGS The string in C programming language is actually a one-dimensional array of characters which is terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise

More information

Introduction to C. Memory Model. Instructor: Yin Lou 02/04/2011. Introduction to C CS 2022, Spring 2011, Lecture 6

Introduction to C. Memory Model. Instructor: Yin Lou 02/04/2011. Introduction to C CS 2022, Spring 2011, Lecture 6 Introduction to C Memory Model Instructor: Yin Lou 02/04/2011 Recap: Pointers int *ptr; Pointers are variables that store memory address of other variables Type of variable pointed to depends on type of

More information

Common Errors in C. David Chisnall. February 15, 2011

Common Errors in C. David Chisnall. February 15, 2011 Common Errors in C David Chisnall February 15, 2011 The C Preprocessor Runs before parsing Allows some metaprogramming Preprocessor Macros Are Not Functions The preprocessor performs token substitution

More information

Programming in C. Characters and Strings

Programming in C. Characters and Strings Programming in C Characters and Strings ASCII The American Standard Code for Information Interchange (ASCII) character set, has 128 characters designed to encode the Roman alphabet used in English and

More information

UNIT III: 1. What is an array? How to declare and initialize arrays? Explain with examples

UNIT III: 1. What is an array? How to declare and initialize arrays? Explain with examples UNIT III: Arrays: Introduction, One-dimensional arrays, Declaring and Initializing arrays, Multidimensional arrays. Strings: Introduction to Strings, String operations with and without using String handling

More information

Operator Overloading; String and Array Objects

Operator Overloading; String and Array Objects 11 Operator Overloading; String and Array Objects The whole difference between construction and creation is exactly this: that a thing constructed can only be loved after it is constructed; but a thing

More information

1 Problem Description

1 Problem Description CSCE2014 Programming Foundations II Lab Two (week 9-9 to 9-13) By Wing Ning Li 1 Problem Description We have introduced and studied the concept of pointer variable and learned that the name of an array

More information

CIS 190: C/C++ Programming. Lecture 1 Introduction and Getting Started

CIS 190: C/C++ Programming. Lecture 1 Introduction and Getting Started CIS 190: C/C++ Programming Lecture 1 Introduction and Getting Started This course will teach you the basics of C and C++ give you more programming experience be appropriate for majors and non-majors not

More information

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters The char Type ASCII Encoding The C char type stores small integers. It is usually 8 bits. char variables guaranteed to be able to hold integers 0.. +127. char variables mostly used to store characters

More information

Reading and Writing String

Reading and Writing String Reading and writing strings Reading and Writing String Reading Format conversion %s can be used in scanf for reading strings not containing white spaces: scanf("%s", str) & not required before str as it

More information

Arrays. Arrays, Argument Passing, Promotion, Demotion

Arrays. Arrays, Argument Passing, Promotion, Demotion Arrays Arrays, Argument Passing, Promotion, Demotion Review Introduction to C C History Compiling C Identifiers Variables Declaration, Definition, Initialization Variable Types Logical Operators Control

More information

STRINGS. PREPAED BY T.DEVI IT DEPARTMENT Vidya Jyothi Institute of Technology. Hyderabad

STRINGS. PREPAED BY T.DEVI IT DEPARTMENT Vidya Jyothi Institute of Technology. Hyderabad STRINGS PREPAED BY T.DEVI IT DEPARTMENT Vidya Jyothi Institute of Technology. Hyderabad Definition Array of character are called strings. A string is terminated by null character /0. For example: "Vjit

More information

MAFS 5240 C++ Basics Arrays & Pointers

MAFS 5240 C++ Basics Arrays & Pointers MAFS 5240 C++ Basics Arrays & Pointers Arrays Pointers 1 Arrays holds a collection of objects of some type arrays are fixed size; new elements cannot be added individual objects are not named; each one

More information

MPI and C-Language Seminars 2010

MPI and C-Language Seminars 2010 MPI and C-Language Seminars 2010 Seminar Plan (1/3) Aim: Introduce the C Programming Language. Plan to cover: Basic C, and programming techniques needed for HPC coursework. C-bindings for the Message Passing

More information

Lecture: #6. More About Characters, Strings, and the string Class

Lecture: #6. More About Characters, Strings, and the string Class Lecture: #6 More About Characters, Strings, and the string Class 1 C-Strings Topics 2 Library Functions for Working with C-Strings 3 Conversions Between Numbers and Strings 4 Character Testing 5 Character

More information

C Strings. Handout #06 January 23, CS106L Winter Introduction

C Strings. Handout #06 January 23, CS106L Winter Introduction CS106L Winter 2007-2008 Handout #06 January 23, 2008 C Strings Introduction C strings are very difficult to work with. Very difficult. In fact, they are so difficult to work with that C++ programmers invented

More information

an array of 10 characters would require 10 bytes of storage for data. On the other hand, would require 10*sizeof(int) amount of storage.

an array of 10 characters would require 10 bytes of storage for data. On the other hand, would require 10*sizeof(int) amount of storage. Lecture 05 C Arrays & pointers In this lecture Introduction to 1D arrays Array representation, access and updates Passing arrays to functions Array as a const pointer Dynamic arrays and resizing Introduction

More information

String Representation in C

String Representation in C String Representation in C 1 There is no special type for (character) strings in C; rather, char arrays are used. char Word[7] = "foobar"; Word[6] Word[5] Word[4] Word[3] Word[2] Word[1] Word[0] 'f' 'o'

More information

Introduction to Programming (in C++) Data types and visibility. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. Computer Science, UPC

Introduction to Programming (in C++) Data types and visibility. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. Computer Science, UPC Introduction to Programming (in C++) Data types and visibility Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. Computer Science, UPC Outline Data types Type conversion Visibility Introduction to

More information

Lecture 4 Notes: Arrays and Strings

Lecture 4 Notes: Arrays and Strings 6.096 Introduction to C++ January 10, 2011 Massachusetts Institute of Technology John Marrero Lecture 4 Notes: Arrays and Strings 1 Arrays So far we have used variables to store values in memory for later

More information

3/13/2012. Esc101: Strings. String input and output. Array of characters: String

3/13/2012. Esc101: Strings. String input and output. Array of characters: String Esc101: Strings Instructor: Krithika Venkataramani Semester 2, 2011-2012 The contents of most of these slides are from the lecture slides of Prof. Arnab Bhattacharya 1 2 Array of characters: String String

More information

First Java Programs. V. Paúl Pauca. CSC 111D Fall, 2015. Department of Computer Science Wake Forest University. Introduction to Computer Science

First Java Programs. V. Paúl Pauca. CSC 111D Fall, 2015. Department of Computer Science Wake Forest University. Introduction to Computer Science First Java Programs V. Paúl Pauca Department of Computer Science Wake Forest University CSC 111D Fall, 2015 Hello World revisited / 8/23/15 The f i r s t o b l i g a t o r y Java program @author Paul Pauca

More information

The char Data Type. Character and String Processing. Another Example /* Capitalize all lowercase letters */ while ((c = getchar())!

The char Data Type. Character and String Processing. Another Example /* Capitalize all lowercase letters */ while ((c = getchar())! Character and String Processing CSE 130: Introduction to C Programming Spring 2005 The char Data Type A char value can be thought of as either a character or a small integer printf( %d, a ); /* prints

More information

Strings. A special kind of array is an array of characters ending in the null character \0 called string arrays

Strings. A special kind of array is an array of characters ending in the null character \0 called string arrays Strings A special kind of array is an array of characters ending in the null character \0 called string arrays A string is declared as an array of characters char s[10] char p[30] When declaring a string

More information

Review: arrays, pointers, structures (Chapter 1) Data Types. Review: Arrays. Data Types (C/C++) Data Type: CS 3358 Summer I 2012.

Review: arrays, pointers, structures (Chapter 1) Data Types. Review: Arrays. Data Types (C/C++) Data Type: CS 3358 Summer I 2012. Review: arrays, pointers, structures (Chapter 1) Data Types Data Type: CS 3358 Summer I 2012 Jill Seaman set of values set of operations over those values example: Integer whole numbers, -32768 to 32767

More information

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013 Oct 4, 2013, p 1 Name: CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013 1. (max 18) 4. (max 16) 2. (max 12) 5. (max 12) 3. (max 24) 6. (max 18) Total: (max 100)

More information

Supplement V.F: C-Strings. For Introduction to C++ Programming By Y. Daniel Liang

Supplement V.F: C-Strings. For Introduction to C++ Programming By Y. Daniel Liang Supplement V.F: C-Strings For Introduction to C++ Programming By Y. Daniel Liang NOTE 7.8 gave a brief introduction on C-string from the array s perspectives. This supplement provides a detailed coverage

More information

Going from Python to C

Going from Python to C Going from Python to C Darin Brezeale December 8, 2011 Python is a high-level, interpreted language. C has many of the same types of programming constructs as in Python: arrays, loops, conditionals, functions,

More information

(a) Arrays. Definition

(a) Arrays. Definition Part 3: Aggregate Data Types (a) Arrays Definition An array is a sequence of objects of a given type Therefore it is not a type of its own It is rather an organizational concept Array elements can be accessed

More information

Advanced Systems Programming

Advanced Systems Programming Advanced Systems Programming Introduction to C++ Martin Küttler September 23, 2016 1 / 21 About this presentation 2 / 21 About this presentation This presentation is not about learning to program 2 / 21

More information

String Processing in C

String Processing in C String Processing in C C Programming and Software Tools N.C. State Department of Computer Science Standard Library: Many functions for checking whether a character is a digit, is upper case,

More information

C for Java Programmers

C for Java Programmers C for Java Programmers CS 414 / CS 415 Niranjan Nagarajan Department of Computer Science Cornell University niranjan@cs.cornell.edu Original Slides: Alin Dobra Why use C instead of Java Intermediate-level

More information

C Primer. Fall Introduction C vs. Java... 1

C Primer. Fall Introduction C vs. Java... 1 CS 33 Intro Computer Systems Doeppner C Primer Fall 2016 Contents 1 Introduction 1 1.1 C vs. Java.......................................... 1 2 Functions 1 2.1 The main() Function....................................

More information

Your first C program. Which one is best?

Your first C program. Which one is best? Your first C program #include void main(void) { printf( Hello, world!\n ); } #include void main(void) { printf( Hello, ); printf( world! ); printf( \n ); } Which one is best? #include

More information

Number Representation

Number Representation Number Representation CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Topics to be Discussed How are numeric data

More information

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists Lecture 11 Doubly Linked Lists & Array of Linked Lists In this lecture Doubly linked lists Array of Linked Lists Creating an Array of Linked Lists Representing a Sparse Matrix Defining a Node for a Sparse

More information

3) Some coders debug their programs by placing comment symbols on some codes instead of deleting it. How does this aid in debugging?

3) Some coders debug their programs by placing comment symbols on some codes instead of deleting it. How does this aid in debugging? Freshers Club Important 100 C Interview Questions & Answers 1) How do you construct an increment statement or decrement statement in C? There are actually two ways you can do this. One is to use the increment

More information

8.5 Arrays of pointers to string

8.5 Arrays of pointers to string 8.5 Arrays of pointers to string 211 8.5 Arrays of pointers to string In Chapter 7 we learned that an initialized string can be declared, for example, in the following way: char some_string[] = "Some initialization

More information

Semantic Analysis: Types and Type Checking

Semantic Analysis: Types and Type Checking Semantic Analysis Semantic Analysis: Types and Type Checking CS 471 October 10, 2007 Source code Lexical Analysis tokens Syntactic Analysis AST Semantic Analysis AST Intermediate Code Gen lexical errors

More information

UNIT-1 PART-A UNIT-2 PART-A

UNIT-1 PART-A UNIT-2 PART-A UNIT-1 1. Define computer? 2. Discuss briefly about input and output devices? 3. What is a RAM? 4. What is arom? 5. What is a compiler? 6. Write about a linker? 7. List any 5 key wordsof c? 8. Illustrate

More information

Exam in EDAA25 C Programming

Exam in EDAA25 C Programming Exam in EDAA25 C Programming October 30, 2014, 14-19 Inga hjälpmedel! Examinator: Jonas Skeppstedt Grading instructions In general: assess if a function or program works as intended while ignoring syntax

More information

Data Structure with C

Data Structure with C Subject: Data Structure with C Topic: Strings In this chapter we are emphasizing the reading and storing string and also manipulation concepts through the following millstones. The discussion starts with

More information

Tutorial No. 8 - Solution (Strings and User Defined Functions)

Tutorial No. 8 - Solution (Strings and User Defined Functions) Tutorial No. 8 - Solution (Strings and User Defined Functions) 1. Explain strcat( ), strcpy( ),strncmp( ) and strstr( ) string manipulation function. [6] strcat() Function: The strcat function joins two

More information

CMPE-013/L. Introduction to C Programming

CMPE-013/L. Introduction to C Programming CMPE-013/L Introduction to C Programming Gabriel Hugh Elkaim Winter 2015 Text I/O 1 Text I/O Within : Formatted text: scanf()/printf() Characters: getchar()/ putchar() Strings/Lines: fgets()/puts()

More information

Chapter 2. Values, Variables, and Types. 2.1 Java Values in DrJava s Interaction Pane

Chapter 2. Values, Variables, and Types. 2.1 Java Values in DrJava s Interaction Pane 14 Chapter 2 Values, Variables, and Types In this chapter we explore the primitive building blocks that are used to develop Java programs. We experiment with the following concepts within DrJava s interactive

More information

Format String Vulnerability. printf ( user input );

Format String Vulnerability. printf ( user input ); Lecture Notes (Syracuse University) Format String Vulnerability: 1 Format String Vulnerability printf ( user input ); The above statement is quite common in C programs. In the lecture, we will find out

More information

AFF 826. Sub. Code 4BSO1C1. Sp2. B.Sc. DEGREE EXAMINATION, NOVEMBER First Semester. Software FUNDAMENTALS OF COMPUTERS AND C PROGRAMMING

AFF 826. Sub. Code 4BSO1C1. Sp2. B.Sc. DEGREE EXAMINATION, NOVEMBER First Semester. Software FUNDAMENTALS OF COMPUTERS AND C PROGRAMMING Sp2 AFF 826 Sub. Code 4BSO1C1 B.Sc. DEGREE EXAMINATION, NOVEMBER 2015 First Semester Software FUNDAMENTALS OF COMPUTERS AND C PROGRAMMING (CBCS 2014 onwards) Time : 3 Hours Maximum : 75 Marks Part A (10

More information

COMP 321: Introduction to Computer Systems

COMP 321: Introduction to Computer Systems Assigned: 1/21/16, Due: 2/4/16, 11:55 PM Important: This project must be done individually. Be sure to carefully read the course policies for assignments (including the honor code policy) on the assignments

More information

TOPICS IN C PROGRAMMING

TOPICS IN C PROGRAMMING TOPICS IN C PROGRAMMING By Bob Hain (ME Net) Introduction This document is not intended to be a text on C programming. Because many of you may not have had the opportunity to use or practice C programming,

More information

Lecture 7: Arrays, strings, and functions

Lecture 7: Arrays, strings, and functions Handout 8 17/03/03 1 Arrays Lecture 7: Arrays, strings, and functions Arrays are declared to store a certain number of variables of a specified type either on the stack, which is the local static memory

More information

Basic C Syntax. Comp-206 : Introduction to Software Systems Lecture 10. Alexandre Denault Computer Science McGill University Fall 2006

Basic C Syntax. Comp-206 : Introduction to Software Systems Lecture 10. Alexandre Denault Computer Science McGill University Fall 2006 Basic C Syntax Comp-206 : Introduction to Software Systems Lecture 10 Alexandre Denault Computer Science McGill University Fall 2006 Next Week I'm away for the week. I'll still check my mails though. No

More information

Chapter 2: Problem Solving Using C++

Chapter 2: Problem Solving Using C++ Chapter 2: Problem Solving Using C++ 1 Objectives In this chapter, you will learn about: Modular programs Programming style Data types Arithmetic operations Variables and declaration statements Common

More information

The if-statement. Simple and compound statements. The if-statement comes in two forms: Simple statements:

The if-statement. Simple and compound statements. The if-statement comes in two forms: Simple statements: 1 2 Simple and compound s The if- Simple s: E.g.: expression; Various jumps : break, goto, continue, return. k = a * p + 3; printf("k = %d\n", k); 1 + 2; ; The if- comes in two forms: or E.g.: if (expression)

More information

C A short introduction

C A short introduction About these lectures C A short introduction Stefan Johansson Department of Computing Science Umeå University Objectives Give a short introduction to C and the C programming environment in Linux/Unix Go

More information

EC312 Chapter 4: Arrays and Strings

EC312 Chapter 4: Arrays and Strings Objectives: (a) Describe how an array is stored in memory. (b) Define a string, and describe how strings are stored. EC312 Chapter 4: Arrays and Strings (c) Describe the implications of reading or writing

More information

Chapter 18 I/O in C. Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Chapter 18 I/O in C. Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 I/O in C Standard C Library I/O commands are not included as part of the C language. Instead, they are part of the Standard C Library. A collection of functions and macros that must be implemented

More information

Primitive Data Types Summer 2010 Margaret Reid-Miller

Primitive Data Types Summer 2010 Margaret Reid-Miller Primitive Data Types 15-110 Summer 2010 Margaret Reid-Miller Data Types Data stored in memory is a string of bits (0 or 1). What does 1000010 mean? 66? 'B'? 9.2E-44? How the computer interprets the string

More information

A Rudimentary Intro to C programming

A Rudimentary Intro to C programming A Rudimentary Intro to C programming Wayne Goddard School of Computing, Clemson University, 2008 Part 4: Strings and Pointers 18 Strings.................................... D1 19 String Functions..............................

More information

Standard printing function in C is printf Prints everything numbers, strings, etc. May be complex to use. Standard C library is called libc

Standard printing function in C is printf Prints everything numbers, strings, etc. May be complex to use. Standard C library is called libc Arrays and Structs and Pointers, Oh My! Programming in C Input and output Using printf Standard input and output Pointers Arrays Structures Combining these things together Arrays and Structs and Pointers,

More information

LESSON 7. ptr = &i; ptr = 0; ptr = NULL; /* equivalent to ptr == 0; */ ptr = (int *) 1999; /* an absolute address in memory */

LESSON 7. ptr = &i; ptr = 0; ptr = NULL; /* equivalent to ptr == 0; */ ptr = (int *) 1999; /* an absolute address in memory */ LESSON 7 POINTERS AND CALL BY REFERENCE When an expression is passed as an argument to a function, a copy of the value of the expression is made, and it is this copy that is passed to the function. Suppose

More information

Variables, Constants, and Data Types

Variables, Constants, and Data Types Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class: L&L, 2.1-2.3, App C 1 Primitive Data There are eight

More information

Outline. Compiling, interpreting, and running. The C Programming Language. Java vs. C. Administrative trivia Goals of the class Introduction to C

Outline. Compiling, interpreting, and running. The C Programming Language. Java vs. C. Administrative trivia Goals of the class Introduction to C Outline ompiling, interpreting, and running Administrative trivia Goals of the class Introduction to 1 2 The Programming Language Java vs. Systems programming language Originally used to write Unix and

More information

C Programming Language CIS 218

C Programming Language CIS 218 C Programming Language CIS 218 Description C is a procedural languages designed to provide lowlevel access to computer system resources, provide language constructs that map efficiently to machine instructions,

More information

Outline TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA. What is DrJava? Dr Java

Outline TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA. What is DrJava? Dr Java 1 Outline TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared

More information

Dept. of CSE, IIT KGP

Dept. of CSE, IIT KGP Programming in C: Basics CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Types of variable We must declare the

More information

C++ Introduction to class and data abstraction

C++ Introduction to class and data abstraction C++ Introduction to class and data abstraction 1 Data abstraction A data abstraction is a simplified view of an object by specifying what can be done with the object while hiding unnecessary details In

More information

Convert Temperature CMPE-013/L. Constants and Literals. The Code #include Gabriel Hugh Elkaim Winter 2014

Convert Temperature CMPE-013/L. Constants and Literals. The Code #include <stdio.h> Gabriel Hugh Elkaim Winter 2014 CMPE-013/L Constants and Literals Gabriel Hugh Elkaim Winter 2014 Convert Temperature Want to write a program to convert the temperature from Fahrenheit to Celsius for a range of temperatures. Demonstrates

More information

C programming. Intro to syntax & basic operations

C programming. Intro to syntax & basic operations C programming Intro to syntax & basic operations Example 1: simple calculation with I/O Program, line by line Line 1: preprocessor directive; used to incorporate code from existing library not actually

More information

05 Case Study: C Programming Language

05 Case Study: C Programming Language CS 2SC3 and SE 2S03 Fall 2009 05 Case Study: C Programming Language William M. Farmer Department of Computing and Software McMaster University 18 November 2009 The C Programming Language Developed by Dennis

More information

TN203. Porting a Program to Dynamic C. Introduction

TN203. Porting a Program to Dynamic C. Introduction TN203 Porting a Program to Dynamic C Introduction Dynamic C has a number of improvements and differences compared to many other C compiler systems. This application note gives instructions and suggestions

More information

Previously we saw that a string constant was just a sequence of characters enclosed within quotation marks. Now we take a look at string variables.

Previously we saw that a string constant was just a sequence of characters enclosed within quotation marks. Now we take a look at string variables. 1 Programming with C Terry Marris November 2010 2 Strings Previously we saw that a string constant was just a sequence of characters enclosed within quotation marks. Now we take a look at string variables.

More information

Strings. Java Primer Strings-1 Scott MacKenzie. String greeting;

Strings. Java Primer Strings-1 Scott MacKenzie. String greeting; Strings Is a string an object or a primitive data type? This question has a clear answer a string is an object! however, the way strings typically appear in Java programs can lead to confusion. There are

More information

Introduction to C Programming

Introduction to C Programming Introduction to C Programming C HOW TO PROGRAM, 6/E 1992-2010 by Pearson Education, Inc. All Rights Reserved. 2.1 Introduction The C language facilitates a structured and disciplined approach to computer

More information

CSCE 111 Exam 1 TRUE/FALSE

CSCE 111 Exam 1 TRUE/FALSE CSCE 111 Exam 1 FORM B TRUE/FALSE 1. Java runs differently on different CPU architectures. F 2. A declared variable is always visible to the entire method in which it is declared. F 3. Because the operator

More information

Introduction to the C++ Programming Language

Introduction to the C++ Programming Language L E S S O N S E T 2 Introduction to the C++ Programming Language PURPOSE PROCEDURE 1. To briefly introduce the C++ programming language 2. To show the use of memory in programming 3. To introduce variables

More information

Assignment #1 (Due 11:59 P.M.; must be sent by the due date at 11:59 P.M.)

Assignment #1 (Due 11:59 P.M.;  must be sent by the due date at 11:59 P.M.) Assignment #1 (Due 9/26/2016 @ 11:59 P.M.; e-mail must be sent by the due date at 11:59 P.M.) This project is designed to help you master pointers. To that end, you'll get the most out of it by working

More information

A Comparison of the Basic Syntax of Python and Java

A Comparison of the Basic Syntax of Python and Java Python Python supports many (but not all) aspects of object-oriented programming; but it is possible to write a Python program without making any use of OO concepts. Python is designed to be used interpretively.

More information

6.087 Lecture 5 January 15, 2010

6.087 Lecture 5 January 15, 2010 6.087 Lecture 5 January 15, 2010 Review Pointers and Memory Addresses Physical and Virtual Memory Addressing and Indirection Functions with Multiple Outputs Arrays and Pointer Arithmetic Strings String

More information

Computer Systems Architecture

Computer Systems Architecture Computer Systems Architecture http://cs.nott.ac.uk/ txa/g51csa/ Thorsten Altenkirch and Liyang Hu School of Computer Science University of Nottingham Lecture 11: Pointers and References What does the following

More information

CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16

CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16 CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16 Basics of C Remark: Skim Chapters 1 through 6 of Deitel & Deitel. You will notice the following: C is (more or less) a subset of Java. (So, you are

More information

Advanced C/C++ Programming. String, Array

Advanced C/C++ Programming. String, Array Advanced C/C++ Programming String, Array Goal of this lecture Get to know the string library and built-in arrays Namespaces Large problems tend to use independently developed libraries, which tend to define

More information

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. Exam Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) The JDK command to compile a class in the file Test.java is A) java Test.java B) java

More information

Dynamic Memory Allocation

Dynamic Memory Allocation Dynamic Memory Allocation Dynamic memory allocation How to allocate memory for variables (esp. arrays/strings) during run time malloc(), calloc(), realloc(), and free() 1 Why dynamic memory allocation?

More information

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables CMSC 131 - Lecture Outlines - set

More information

GTU Questions. Computer Programming & Utilization. Chief Course Coordinator Prof. Mitul K.Patel. (Head of Department & Assistant Professor)

GTU Questions. Computer Programming & Utilization. Chief Course Coordinator Prof. Mitul K.Patel. (Head of Department & Assistant Professor) GTU Questions Computer Programming & Utilization Chief Course Coordinator Prof. Mitul K.Patel (Head of Department & Assistant Professor) Course Coordinator Prof. Vrutti D. Shah (Assistant Professor) Lab

More information

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 11, SPRING 2013

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 11, SPRING 2013 CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 11, SPRING 2013 TOPICS TODAY C Input/Output Characters & Strings in C Structures in C Project 4 C INPUT/OUTPUT stdin, stdout, stderr

More information

The C Programming Language course syllabus associate level

The C Programming Language course syllabus associate level TECHNOLOGIES The C Programming Language course syllabus associate level Course description The course fully covers the basics of programming in the C programming language and demonstrates fundamental programming

More information

Comp Arrays and Pointers. Class Notes. T h e G r o u p o f T h r e e

Comp Arrays and Pointers. Class Notes. T h e G r o u p o f T h r e e Comp 2401 Arrays and Pointers Class Notes 2013 T h e G r o u p o f T h r e e Introduction To Arrays: In C programming, one of the frequently problem is to handle similar types of data. For example: if

More information

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013 Masters programmes in Computer Science and Information Systems Object-Oriented Design and Programming Sample module entry test xxth December 2013 This sample paper has more questions than the real paper

More information

Week 12 Character Array (String)

Week 12 Character Array (String) CME111 Programming Languages I Week 12 Character Array (String) Assist. Prof. Dr. Caner ÖZCAN String Definition We learned multidimensional arrays and arrays. String is actually an array of what we call

More information

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program. Name: Class: Date: Exam #1 - Prep True/False Indicate whether the statement is true or false. 1. Programming is the process of writing a computer program in a language that the computer can respond to

More information

C Programming 1. C Programming

C Programming 1. C Programming C Programming 1 1 C Programming 1. Who developed the C language? Dennis M.Ritchie in 1972 2. What type of language is C? Semi-high level language 3. What is main()? The main() is a special function used

More information

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct Dr. Martin O. Steinhauser University of Basel Graduate Lecture Spring Semester 2014 Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct Friday, 7 th March

More information