 Integers and Characters  Character Strings  Input and Output.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Dale Roberts Basic I/O – scanf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
String Escape Sequences
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
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.
1 IPC144 Session 11 The C Programming Language. 2 Objectives To format a #define statement correctly To use a #define statement in a C program To construct.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
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.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
CS 1704 Introduction to Data Structures and Software Engineering.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Formatted Input and Output
Strings (Continued) Chapter 13
Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
© 2016 Pearson Education, Ltd. All rights reserved.
Input/output.
TMF1414 Introduction to Programming
Introduction to C CSE 2031 Fall /3/ :33 AM.
Programming in C Input / Output.
Input and Output Lecture 4.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Formatted Input/Output
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Formatted Input/Output
Chapter 9 - Formatted Input/Output
Formatted Input/Output
EECE.2160 ECE Application Programming
Formatted Input/Output
Introduction to C EECS May 2019.
EECE.2160 ECE Application Programming
DATA TYPES There are four basic data types associated with variables:
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
EECE.2160 ECE Application Programming
Presentation transcript:

 Integers and Characters  Character Strings  Input and Output

 To date all we have seen are numeric data types  int, short, long, long long,...  float, double,...  char  The char type stores a numeric code which is translated to a character when appropriate  And C treats them as numbers

 It’s easy to print the ASCII code for a character  char ch = ‘x’;  printf(“code for %c = %d”, ch, ch);  The first placeholder print the letter that the code represents  The second placeholder prints the code  C will also allow arithmetic to be performed on char variables  The underlying numeric codes are operated

 Let’s say that we want to print all of the letters from A to Z  We could write 26 printf statements ▪ printf('A'); ▪ printf('B'); ▪...  Or we could do this char ch = 'A'; while(ch < 'A' + 26){ printf("%c\n", ch); ch++; }

 We have used character strings in printf calls  e.g. printf(“Hello World”);  It would also be useful to store character strings in variables  And to get user input in the form of strings  In C a character string is a sequence of characters  Stored in an array

 Let’s write a program to find out the name and age of the user  And then print them

int main() { char name[20]; int age; printf("What is your name? "); scanf("%s", name); printf("What is your age? "); scanf("%d", &age); printf("Your name is %s, and your age is %d\n", name, age); return 0; } things to note square brackets no & What is your name? Jenny What is your age? 11 Your name is Jenny, and your age is 11 What is your name? Jenny What is your age? 11 Your name is Jenny, and your age is 11

 The line char name[20]; declares an array of 20 characters  A sequence in main memory with enough space for twenty characters  An array is an ordered sequence of data elements of one type  The brackets identify name as an array rather than a single character  And 20 indicates the size of the array

...Jenny\0 A sequence of 2o adjacent bytes in main memory Why 20 bytes? Because each character is stored in one byte What is the \0 in the 6 th. byte? A null character to indicate the end of the string Why is this necessary? Because memory locations can’t be empty, so it is important to distinguish between characters we want to store and garbage values

 We used scanf like this: scanf(“%s”, name);  The format specification %s is for a string  The variable name is not preceded by an &  Remember that when using scanf it is necessary to provide the address of a variable  Using the address of operator (i.e. &)  Array variables, like name, in fact contain addresses already, an array variable is a pointer ▪ Which is why the & is not necessary

 A string consists of an array containing the words in the string and the null character  Consequently, a string containing a single character is not the same as a char variable  The type of a character array is not the same as the type of a character  That is char name[20] declares an array not a char a a the character 'a' a a the string "a" \0

 Usually we (as programmers) are not responsible for inserting the null character  This task is performed by a function that is responsible for creating a string  Such as scanf, for string input  Note also that functions such as printf are able to detect the null character  To print the appropriate part of a character array

 There are a number of functions that are useful when working with strings  It can be important to know how long a character array is and how long a string is  The maximum length  And the length of the string currently stored in the array  The sizeof and strlen functions can be used to determine this information

 The sizeof function can be used to find the size in bytes of any variable or type int x = 212; double d = 2.713; char ch = 'a'; char str[20]; printf("sizeof(x) = %d\n", sizeof(x)); printf("sizeof(d) = %d\n", sizeof(d)); printf("sizeof(ch) = %d\n", sizeof(ch)); printf("sizeof(str) = %d\n", sizeof(str)); printf("sizeof(short) = %d\n", sizeof(short));

 The strlen function can be used to find the size of a string stored in character array  The number of characters before the null character char name[20]; printf("What is your name? "); scanf("%s", name); printf("char[] size = %d\n", sizeof(name)); printf("size of %s = %d\n", name, strlen(name)); char name[20]; printf("What is your name? "); scanf("%s", name); printf("char[] size = %d\n", sizeof(name)); printf("size of %s = %d\n", name, strlen(name));

 What gets printed? #define BATMAN "Bruce Wayne" int main() { printf("size of %s = %d\n", BATMAN, strlen(BATMAN)); printf("BATMAN size = %d\n\n", sizeof(BATMAN)); return 0; } #define BATMAN "Bruce Wayne" int main() { printf("size of %s = %d\n", BATMAN, strlen(BATMAN)); printf("BATMAN size = %d\n\n", sizeof(BATMAN)); return 0; } The difference is the null character

 The #define pre-processor directive can be used to create string constants  As well as any other kind of constant ▪ #define SFU "Simon Fraser University"  When using #define it is not necessary to specify the type of data being defined  This is because the directive is essentially replacing the constants with the data  Before the compiler runs

 Define constants using #define like this  #define  #define PI  #define TITLE “Lord of the Rings”  #define DAYS 365  Constants are in upper-case by convention  To distinguish them from variables

 If you want to find the size of various types they are recorded in the limits.h file  e.g. INT_MAX, LONG_MAX, LLONG_MAX  This can be useful if you want to know the byte size of a type on a particular OS  There are similar constants for floats in float.h

 A more modern approach to creating constants is to use the const modifier  const double PI = ;  Unlike #define this is not a pre-processor directive so must  Declare the type,  Use assignment, and  End in a semi-colon  Defining constants with const is more flexible when dealing with projects with multiple files

 As we’ve seen the printf function can take the following arguments (i.e. input)  A string – required  Placeholders (conversion specifiers) in the string ▪ Introduced by the % symbol  Variable or literal arguments that should match the placeholders ▪ If the variables don’t match the placeholders then the output will be incorrect

 Placeholders indicate the type to be printed  e.g. %d for decimal integer  These can be further modified for  Type size ▪ e.g. %lld specifies a long long int  Field width, right justified by default ▪ e.g. %4d specifies a minimum field width of four  Precision – used for %e, %E, and %f to indicate the number of digits to the right of the decimal place ▪ e.g. %4.2f prints a float five characters wide with two digits after the decimal place

 Output can be further modified by the use of flags  - left justifies the output ▪ e.g. %-20s prints a left justified string of width 20  + indicates signed values with +, or – ▪ e.g. "**%+8.2f " prints a float like this: **  indicates signed values should start either with a space, or the – sign, to allow columns of signed values to line up ▪ e.g. " **% 10.3f " prints a float like this: **  0 indicates that numbers should be padded with zeros ▪ e.g. " **%8d " prints an int like this: **  I’ve printed **s to show the spacing

 We used different placeholders to print a character and a number  Consider this common error double dbl = 123.1; printf("dbl = %d", dbl);  The placeholder indicates how the data stored in the argument should be printed  Strictly speaking it also indicates how many bytes of the argument should be accessed

 If a variable doesn’t match the placeholder the output may be incorrect  The placeholder indicates two things  The number of bytes of an argument to look at  What those bytes represent  Consider the %d placeholder  It is an instruction to look at 4 bytes of the appropriate argument  And interpret them as a 2’s complement int

Here is an example of a mismatched conversion Incidentally printing the fourth line was fun... printf("printf(\"n1 = %d, n2 = %d, n3 = %d\",n1, n2, n3);\n\n"); Note that the first placeholder prints the right most 4 bytes of n1 and the second prints the left most 4 bytes printf, and all other function calls use an area of memory called the stack... n3 (4bytes) n2 (4 bytes) n1 (8 bytes)... 1 st %d 2 nd %d 3 rd %d part of the stack

 A long C statement may take up more than one line  Since the compiler ignores white space  A string may not take up more than one line  The compiler gives an error if a string takes up more than one line  But printf will concatenate two strings written on two lines printf("blah blah blah"); printf("blah blah blah"); error! printf("blah blah " "blah"); printf("blah blah " "blah"); OK

 Similar to printf, scanf uses a control string with a list of arguments  The control string contains shows the destination data types for the input stream  %s, %f, %s and so on  Characters that do not match the destination types will cause problems  The arguments contains the addresses of the variables

 The scanf function uses whitespace to determine that the end of input is reached  New lines, tabs or spaces  Other input functions allow whitespace to be included in input (for strings or characters)  The scanf function treats input as a stream of characters  Processing or ignoring each character  Ignored characters are left in the stream

Here is a code fragment to request a name and age int age; char name[20]; printf("Enter your age, and last name: "); scanf("%d%s", &age, name); printf("Your name is %s, and you are aged %d\n", name, age); int age; char name[20]; printf("Enter your age, and last name: "); scanf("%d%s", &age, name); printf("Your name is %s, and you are aged %d\n", name, age); I could have written this in two lines: scanf("%d", &age); scanf("%s", name); I could have written this in two lines: scanf("%d", &age); scanf("%s", name); What should happen: What might happen: When a non-numeric character is reached (e) input stops for age Then e3 was read as the name, and input stops once whitespace is encountered When a non-numeric character is reached (e) input stops for age Then e3 was read as the name, and input stops once whitespace is encountered

 It is permissible to put ordinary characters in the format string  These characters must be matched exactly by the input string ▪ e.g. scanf(“%d,%d”, &x, &y); ▪ scanf expects the user to type a number, a comma, and another number ▪ I would recommend not doing this!

 The scanf function returns a value, allowing for functions to detect unexpected results  The value is the number of items that have been successfully read int x; int chars_read; printf("Please enter a number: "); chars_read = scanf("%d", &x); printf("Items successfully read = %d\n\n", chars_read); int x; int chars_read; printf("Please enter a number: "); chars_read = scanf("%d", &x); printf("Items successfully read = %d\n\n", chars_read);

 We will cover more details on many of these subjects later  Arrays  Strings  Pointers  More string functions  Other input and output functions