The char Data Type A char is a one byte integer type typically used for storing characters. Example: char oneLetter = ’D’; We enclose the character in.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Lecture 9: Character and String
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
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.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Lab 2 Variables in C.
Introduction to Computers and Programming Lecture 7:
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Homework Reading –Finish K&R Chapter 1 (if not done yet) –Start K&R Chapter 2 for next time. Programming Assignments –DON’T USE and string library functions,
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
Chapter 2: C Fundamentals Dr. Ameer Ali. Overview C Character set Identifiers and Keywords Data Types Constants Variables and Arrays Declarations Expressions.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
CHAPTER 8 CHARACTER AND STRINGS
Input & Output: Console
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
CS 108 Computing Fundamentals March 26, Class Notes Last day to withdraw from a class is Monday, April 6 Next week I sent s to a small number.
BBS514 Structured Programming (Yapısal Programlama)1 Character Processing, Strings and Pointers,
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Characters. Character Data char data type – Represents one character – char literals indicated with ' '
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Introduction to Programming
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Data types  CHAR (size): This data type is used to store character strings values of fixed length. The size in brackets determines the number of characters.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
Agenda Character representation Numerical Conversions ASCII EBCDIC
Road map char data type Reading –Liang 5: Chapter 2: 2.7.4; 2.9; –Liang 6: Chapter 2: 2.7.4; 2.9 –Liang 7: Chapter 2: 2.7.4; 2.9.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example: float myData[] = {3.5, 4.0, 9.34}; myData[0] += 2; printf("myData[0]
Strings, Pointers and Tools
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Characters and Strings
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
Characters and Strings
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Constants and Literals Gabriel Hugh Elkaim Spring 2012.
Dr. Sajib Datta  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for sorting  These.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
1.4 Representation of data in computer systems Character.
Characters and Strings
Character Processing How characters can be treated as small integers?
INTRODUCTION c is a general purpose language which is very closely associated with UNIX for which it was developed in Bell Laboratories. Most of the programs.
Lecture 8b: Strings BJ Furman 15OCT2012.
Characters and Strings
Character Processing How characters can be treated as small integers?
Lexical Elements & Operators
An Introduction to Programming
EECE.2160 ECE Application Programming
C Programming Language
Boolean in C++ CSCE 121.
Presentation transcript:

The char Data Type A char is a one byte integer type typically used for storing characters. Example: char oneLetter = ’D’; We enclose the character in single quotes, not double quotes.

ASCII Plain text is often stored in a format called ASCII (American Standard Code for Information Interchange). There are 128 characters in ASCII, which includes the standard alphanumeric characters as well as some non- printable characters (e.g., tab, newline, and so forth).

char Example ASCII characters can be stored using the character or the decimal value of the character. #include int main(void) { char letter_a = ’a’; /* note the use of single quotes */ char decimal_a = 97; /* 97 is the decimal value for ’a’ in ASCII */ /* note the %c to print characters */ printf("letter_a is %d and %c\n", letter_a, letter_a); printf("decimal_a is %d and %c\n", decimal_a, decimal_a); } Output letter_a is 97 and a decimal_a is 97 and a

char cont. If we check an ASCII chart, we see that the letters A–Z have ASCII values of 65– 90 while a–z have values of 97–122. Because the letters are stored as numbers, we can perform numeric operations on them. #include int main(void) { char uppercase = ’A’; /* 65 in ASCII */ char lowercase; lowercase = uppercase + 32; printf("Adding 32 to %c gives us %c\n", uppercase, lowercase); } prints Adding 32 to A gives us a

Array of char C does not have a string type. Instead, we use an array of chars for storing a string. We can declare this just like we did with other types. Example char letters[10]; See example-char-array.c on the course website.

Strings Storing each character individually is a tedious process. We can do this simultaneously by entering a string. Example char myName[] = “Preetam Ghosh"; This time we DO use double quotes.

Strings cont. There is a difference between the two approaches. When we create a string, a null character (i.e., \0) is added to the end. Note that when writing the null character, you should use a zero, not the letter O. A null allows the compiler to know where the string ends so that we can print it by name. Example char myName[] = “Preetam Ghosh"; printf("My name is %s", myName); /* print using %s */ will print Preetam Ghosh

2D Array of Strings We can create 2D arrays of characters, i.e., an array of strings. #include int main(void) { char text[][6] = {"Tom", "Dick", "Harry"}; int i, j; for(i = 0; i < 3; i++) for(j = 0; j < 6; j++) if(text[i][j] == ’\0’) { printf("\n"); break; } else printf("%c", text[i][j]); }

2D Array of Strings cont. Output of our program: Tom Dick Harry

2D Array of Strings cont. Our 2D array of characters allocated space for 18 bytes, which was more than necessary. It’s unusual to create a 2D array of characters as we did. It’s more typical to create an array of pointers to strings, which we will learn to do later.