M.CS201 Programming language

Similar documents
M.EC201 Programming language

M.CS201 Programming language

Computers Programming Course 5. Iulian Năstac

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

CS102: Variables and Expressions

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

M.CS201 Programming language

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Fundamentals of Programming. Lecture 3: Introduction to C Programming

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Objectives. In this chapter, you will:

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

Programming for Engineers Arrays

A Fast Review of C Essentials Part I

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Work relative to other classes

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

Character Strings. String-copy Example

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Understanding Pointers

Fundamentals of Programming Session 4

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

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

printf( Please enter another number: ); scanf( %d, &num2);

Programming for Engineers Introduction to C

Programming and Data Structures

DECLARAING AND INITIALIZING POINTERS

Fundamental of Programming (C)

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Variation of Pointers

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Dynamic memory allocation

Arrays and Pointers in C. Alan L. Cox

C Language, Token, Keywords, Constant, variable

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

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

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

today cs3157-fall2002-sklar-lect05 1

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Chapter 7. Basic Types

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

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

Full file at

Chapter 2, Part I Introduction to C Programming

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Memory Management. CSC215 Lecture

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

CS 61c: Great Ideas in Computer Architecture

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

Characters Lesson Outline

CS 11 C track: lecture 5

BSM540 Basics of C Language

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

Dynamic memory. EECS 211 Winter 2019

EM108 Software Development for Engineers

CS201 Some Important Definitions

Computer Organization & Systems Exam I Example Questions

C Introduction. Comparison w/ Java, Memory Model, and Pointers

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

M1-R4: Programing and Problem Solving using C (JULY 2018)

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Dynamic Memory Allocation

Differentiate Between Keywords and Identifiers

C Programming Multiple. Choice

BLM2031 Structured Programming. Zeyneb KURT

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

Computational Expression

Lecture 04 Introduction to pointers

Computers Programming Course 10. Iulian Năstac

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

Introduction to C Language

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Short Notes of CS201

Darshan Institute of Engineering & Technology for Diploma studies Unit 4

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

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

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

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

CS-201 Introduction to Programming with Java

Programming Language B

CS201 - Introduction to Programming Glossary By

Number Systems, Scalar Types, and Input and Output

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

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

Content. In this chapter, you will learn:

CSE 333 Midterm Exam 5/10/13

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

Fundamentals of Programming. Lecture 11: C Characters and Strings

M.CS201 Programming language

Transcription:

Power Engineering School M.CS201 Programming language Lecture 9 Lecturer: Prof. Dr. T.Uranchimeg

Agenda The char Data Type Using Character Variables Printing extended ASCII characters Arrays of Characters 2

The char Data Type ASCII code - American Standard Code for Information Interchange. The code assigns values between 0 and 255 for upper- and lowercase letters, numeric digits, punctuation marks, and other symbols. 3

Using Character Variables Like other variables, you must declare chars before using them, and you can initialize them at the time of declaration. char a, b, c; char code = x 4

literal character constants To create literal character constants, you enclose a single character in single quotation marks. The compiler automatically translates literal character constants into the corresponding ASCII codes, and the numeric code value is assigned to the variable. 5

Example You can create symbolic character constants by using either the #define directive or the const keyword: #define EX x char code = EX const char A = Z ; 6

#include <stdio.h> char c1 = a ; char c2 = 90; main { Example - 1 7

Example cont. - 2 printf("\nas a character, variable c1 is %c", c1); printf("\nas a number, variable c1 is %d", c1); printf("\nas a character, variable c2 is %c", c2); 8

Example cont. - 3 printf("\nas a number, variable c2 is %d\n", c2); return 0; } 9

Printing extended ASCII characters #include <stdio.h> main { for (x = 180; x < 204; x++) { 10

Continue printf("ascii code %d is character %c\n", x, x); } return 0; } 11

Example 12

Using Strings Variables of type char can hold only a single character, so they have limited usefulness. You also need a way to store strings, which are sequences of characters. A person's name and address are examples of strings. 13

Arrays of Characters To hold a string of six characters, for example, you need to declare an array of type char with seven elements. Arrays of type char are declared like arrays of other data types. 14

Example For example, the statement: char string[10]; declares a 10-element array of type char. This array could be used to hold a string of nine or fewer characters. 15

the null character In C, a string is defined as a sequence of characters ending with the null character, a special character represented by \0. Although it's represented by two characters (backslash and zero), the null character is interpreted as a single character and has the ASCII value of 0. 16

Explain When a C program stores the string Alabama, for example, it stores the seven characters A, l, a, b, a, m, and a, followed by the null character \0, for a total of eight characters. Thus, a character array can hold a string of characters numbering one less than the total number of elements in the array. 17

Initializing Character Arrays Like other C data types, character arrays can be initialized when they are declared. Character arrays can be assigned values element by element, as shown here: char string[10] = { `A', `l', `a', `b', `a', `m', `a', `\0' }; 18

literal string It's more convenient, however, to use a literal string (тодорхой), which is a sequence of characters enclosed in double quotes: char string[10] = "Alabama"; 19

literal string in program When you use a literal string in your program, the compiler automatically adds the terminating null character at the end of the string. If you don't specify the number of subscripts when you declare an array, the compiler calculates the size of the array for you. 20

Declaration Thus, the following line creates and initializes an eightelement array: char string[] = "Alabama"; 21

Strings Without Arrays The array's name is a type char pointer to the beginning of the string. The null marks the string's end. The actual space occupied by the string in an array is incidental. In fact, the only purpose the array serves is to provide allocated space for the string. 22

Memory for string What if you could find some memory storage space without allocating an array? You could then store a string with its terminating null character there instead. A pointer to the first character could serve to specify the string's beginning just as if the string were in an allocated array. 23

malloc() How do you go about finding memory storage space? There are two methods: One allocates space for a literal string when the program is compiled, and the other uses the malloc() function to allocate space while the program is executing, a process known as dynamic allocation. 24

Allocating String Space at Compilation The start of a string, as mentioned earlier, is indicated by a pointer to a variable of type char. You might recall how to declare such a pointer: char *message; This statement declares a pointer to a variable of type char named message. 25

the pointer declaration It doesn't point to anything now, but what if you changed the pointer declaration to read char *message = "Great Name\'s AAA!"; When this statement executes, the string Great Name's AAA! is stored somewhere in memory, and the pointer message is initialized to point to the first character of the string. 26

memory Don't worry where in memory the string is stored; it's handled automatically by the compiler. Once defined, message is a pointer to the string and can be used as such. 27

two notations The preceding declaration/initialization is equivalent to the following, and the two notations *message and message[] also are equivalent; they both mean "a pointer to." char message[] = "Great Caesar\'s Ghost!"; 28

on-the-fly This method of allocating space for string storage is fine when you know what you need while writing the program. What if the program has varying string storage needs, depending on user input or other factors that are unknown when you're writing the program? You use the malloc() function, which lets you allocate storage space "on-the-fly." 29

The malloc() Function The malloc() function is one of C's memory allocation functions. When you call malloc(), you pass it the number of bytes of memory needed. malloc() finds and reserves a block of memory of the required size and returns the address of the first byte in the block. You don't need to worry about where the memory is found; it's handled automatically. 30

The malloc() function The malloc() function returns an address, and its return type is a pointer to type void. Why void? A pointer to type void is compatible with all data types. Because the memory allocated by malloc() can be used to store any of C's data types, the void return type is appropriate. 31

malloc() #include <stdlib.h> void *malloc(size_t size); 32

How to work? malloc() allocates a block of memory that is the number of bytes stated in size. By allocating memory as needed with malloc() instead of all at once when a program starts, you can use a computer's memory more efficiently. 33

header file When using malloc(), you need to include the STDLIB.H header file. Some compilers have other header files that can be included; for portability, however, it's best to include STDLIB.H. 34

Some explian malloc() returns a pointer to the allocated block of memory. If malloc() was unable to allocate the required amount of memory, it returns null. Whenever you try to allocate memory, you should always check the return value, even if the amount of memory to be allocated is small. 35

#include <stdlib.h> #include <stdio.h> main() { char *str; Example 1 36

Example cont. if (( str = (char *) malloc(100)) == NULL) } { printf( "Not enough memory to allocate buffer\n"); exit(1); } printf( "String was allocated!"\n ); return 0; 37

Examples 38

Using the malloc() Function You can use malloc() to allocate memory to store a single type char. First, declare a pointer to type char: char *ptr; 39

#include <stdio.h> #include <stdlib.h> char count, *ptr, *p; main() { Example 40

Example ptr = malloc(35 * sizeof(char)); if (ptr == NULL) { puts("memory allocation error."); exit(1); } 41

Example p = ptr; for (count = 65; count < 91 ; count++) *p++ = count; *p = `\0'; puts(ptr); return 0; } 42

Displaying Strings and Characters #include <stdio.h> char *message1 = "C"; char *message2 = "is the"; char *message3 = "best"; char *message4 = "programming"; 43

Example char *message5 = "language!!"; main() { puts(message1); puts(message2); 44

puts(message3); puts(message4); puts(message5); return 0; } Example 45

The printf() Function You can also display strings using the printf() library function. To display a string, use the conversion specifier %s. char *str = "A message to display"; printf("%s", str); 46

Example You can also display multiple strings and mix them with literal text and/or numeric variables: 47

Summary The char Data Type Using Character Variables Printing extended ASCII characters Arrays of Characters 48

Any questions? 49

Thank you for attention 50