C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Chapter 3: Beginning Problem Solving Concepts for the Computer Programming Computer Programming Skills /1436 Department of Computer Science.
Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Structure of a C program
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
© 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.
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.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Week 1 Algorithmization and Programming Languages.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 1.
© 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.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
1 Lexical Elements, Operators, and the C System. 2 Outline Characters and Lexical Elements Syntax Rules Comments Keywords Identifiers Constants String.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Numbers in ‘C’ Two general categories: Integers Floats
C Formatted Input/Output
Input/output.
TMF1414 Introduction to Programming
ICS103 Programming in C Lecture 3: Introduction to C (2)
Introduction to C Programming
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
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.
Lecture 13 Input/Output Files.
C++ Basics.
Introduction to C++ Programming
Variables in C Topics Naming Variables Declaring Variables
Basics of ‘C’.
CS111 Computer Programming
Introduction to C Programming
Variables in C Topics Naming Variables Declaring Variables
UMBC CMSC 104 – Section 01, Fall 2016
Variables in C Declaring , Naming, and Using Variables.
WEEK-2.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Lexical Elements & Operators
Variables in C Topics Naming Variables Declaring Variables
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
C Language B. DHIVYA 17PCA140 II MCA.
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

C Programming Lecture 4

Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer language.Tokens make up the basic vocabulary of a computer language. b The compiler then checks the tokens to see if they can be formed into legal strings according to the syntax (the grammar rules) of the language.

Characters Used in C Programs b Lowercase letters a b c... za b c... z b Uppercase letters A B C... ZA B C... Z b Digits b Other characters + - * / = ( ) { } [ ] ‘ “+ - * / = ( ) { } [ ] ‘ “ # $ % & _ ^ ~ \., ; : # $ % & _ ^ ~ \., ; : ? b White space characters blank, newline, tab, etc.blank, newline, tab, etc.

The Six Kinds of Tokens in ANSI C b Keywords b Identifiers b Constants b String Constants b Operators b Punctuators

Keywords b Keywords are C tokens that have a strict meaning. They are explicitly reserved and cannot be redefined.They are explicitly reserved and cannot be redefined. b ANSII C has 32 key words. Some implementations such as Borland’s C or Microsoft’s C have additional key words.Some implementations such as Borland’s C or Microsoft’s C have additional key words.

ANSII C Keywords auto do goto signed unsigned break double if sizeof void case else int static volatile char enum long struct while const extern register switch continue float return typedef default for short union

Identifiers b An identifier is a token: Composed of a sequence of letters, digits, and the underscore character _Composed of a sequence of letters, digits, and the underscore character _ –Note: Variable names are identifiers b Lower- and uppercase letters are treated as distinct. b Identifiers should be chosen so that they contribute to the readability and documentation of the program.

Special Identifiers b main C programs always begin execution at the function main.C programs always begin execution at the function main. b Identifiers that begin with an underscore should be used only by systems programmers Because they can conflict with system names.Because they can conflict with system names.

The Length of Discriminated Identifiers b On older systems only the first eight characters of an identifier are discriminated. er_one and identifier_two would be the same identifier.identifier_one and identifier_two would be the same identifier. b In ANSI C, at least the first 31 characters of an identifier are discriminated.

Constants b Integer Constants 25 and 025 and 0 b Floating Constants and and 0.1 b Character Constants ‘a’ and ‘B’ and ‘+’ and ‘;’ but not “a” or “B”‘a’ and ‘B’ and ‘+’ and ‘;’ but not “a” or “B”

Special Character Constants b The backslash is called the escape character. The newline character ‘\n’ represents a single character called newline.The newline character ‘\n’ represents a single character called newline. Think of \n as “escaping” the usual meaning of n.Think of \n as “escaping” the usual meaning of n. b Enumeration constants will be discussed later in the course.

String Constants b A sequence of characters enclosed in a pair of double quote marks, such as “abc” is a, or a b A sequence of characters enclosed in a pair of double quote marks, such as “abc” is a string constant, or a string literal. b Character sequences that would have meaning if outside a string constant are just a sequence of characters when surrounded by double quotes. b String constants are treated by the compiler as tokens and the compiler provides the space in memory to store them.

Is it a String or Not a String? b “this is a string constant” b “” /* the null string */ b “ “ /* a string of blanks */ b “ a = b + c; “ /* is not executed */ b “ /* this is not a comment */ “ b /* “ this is not a string “ */ b “ and neither is this “ b ‘a’ /* a character, not a string */

The Mathematical Operators b We looked at the mathematical operators briefly in the 3rd class: + - * / % b In a C program we typically put white space around binary operators to improve readability. a + b rather than a+b

The sizeof Operator b The C sizeof unary operator if used to find the number of bytes needed to store an object. sizeof(object) returns an integer that represents the number of bytes needed to store the object in memory.sizeof(object) returns an integer that represents the number of bytes needed to store the object in memory.

printf() printf(control string, other arguments); b The expressions in other_arguments are evaluated and converted according to the formats in the control string and are then placed in the output stream. printf(“%-14sPayRate: $%-4.2f\n”, “James Smith”, 8.95); James Smith Pay Rate: $8.95 b Characters in the control string that are not part of a format are placed directly in the output stream.

The Formats in the Control String printf(“Get set: %d %s %f %c%c\n”, 1, “two”, 3.33, ‘G’, ‘O’); 1, “two”, 3.33, ‘G’, ‘O’); b %d Print 1 as a decimal number b %s Print “two” as a string –“string” means a sequence of characters. b %f Print 3.33 as a float –decimal or floating-point number b %c Print ‘G’ & ‘0’ as characters.

printf() Conversion Characters Conversion characterHow the corresponding argument is printed cas a character d,ias a decimal integer uas an unsigned decimal integer oas an unsigned octal integer x,Xas an unsigned hexadecimal integer eas a floating-point number: e+00 Eas a floating-point number: E+00 gin the shorter of the e-format or f-format Gin the shorter of the E-format or f-format sas a string pthe corresponding argument is a pointer to void; it prints as a hexadecimal number. nargument is a pointer to an integer into which the number of characters written so far is printed; the argument is not converted. %with the format % a single % is written; there is no corresponding argument to be converted.

printf( ) Conversion Specifications b field width (optional) An optional positive integerAn optional positive integer If the converted argument has fewer characters than the specified width, it will be padded with spaces on the left or right depending on the left or right justification.If the converted argument has fewer characters than the specified width, it will be padded with spaces on the left or right depending on the left or right justification. If the converted argument has more characters, the field width will be extended to whatever is required.If the converted argument has more characters, the field width will be extended to whatever is required. b precision (optional) Specified by a period followed by a nonnegative integer.Specified by a period followed by a nonnegative integer. Minimum number of digits to be printed for d, i, o, u, x, and X conversions.Minimum number of digits to be printed for d, i, o, u, x, and X conversions. Minimum number of digits to the right of the decimal point for e, E, and f conversions.Minimum number of digits to the right of the decimal point for e, E, and f conversions. Maximum number of significant digits for G and g conversions.Maximum number of significant digits for G and g conversions. Maximum number of characters to be printed for an s conversion.Maximum number of characters to be printed for an s conversion.

printf ( ) Example printf(“Get set: %d %s %f %c%c\n”, 1, “two”, 3.33, ‘G’, ‘O’); 1, “two”, 3.33, ‘G’, ‘O’); The first argument is the control string “Get set: %d %s %f %c%c\n” The formats in the control string are matched (in order of occurrence) with the other arguments.

Use of printf ( ) b printf is used for printing output. When printf( ) is called it is passed a list of arguments of the form: b printf( ) is used for printing output. When printf( ) is called it is passed a list of arguments of the form: control string & other arguments control string & other arguments b The arguments to printf( ) are separated by commas.

Errors in printf ( ) Formats b A floating point format in a printf ( ) statement is of the form %m.nf The value of m specifies the field width, not the number of digits to the left of the decimal point.The value of m specifies the field width, not the number of digits to the left of the decimal point. The value of n specifies the number of digits to the right of the decimal point.The value of n specifies the number of digits to the right of the decimal point. b To specify two decimal digits to the left of the decimal point and three to the right, use %6.3f.

Use of scanf() b scanf() is analogous to printf(), but is used for input rather than output. scanf()in a program stops the execution of the program while you type something in from the keyboard.scanf()in a program stops the execution of the program while you type something in from the keyboard.

scanf ( ) Arguments b The first argument is a control string with formats similar to those used with printf(). The formats determine how characters in the input stream (what you are typing) will be interpreted so they can be properly stored in memory.The formats determine how characters in the input stream (what you are typing) will be interpreted so they can be properly stored in memory.

Scanf ( )’s Other Arguments b After the control string, the other arguments are addresses. b Example: assume x is declared as an integer variable. scanf(“%d”, &x); The & is the address operator. It says “store the value entered at the address of the memory location named x”.

scanf ( ) Conversion Conversion How characters in the Character input stream are converted. c d f lf Lf s Character decimal integer floating-pint number (float) floating-point number (double) floating-point number (long double) string

A Peculiarity of scanf ( ) b With printf() the %f format is used to print either a float or a double. b With scanf() the format %f is used to read in a float, and %lf is used to read in a double.

Another scanf() Peculiarity b When reading in numbers, scanf() will skip white space characters (blanks, newlines, and tabs). b When reading characters, white space is not skipped.

The Return Value of scanf() b When the scanf() function reads in data typed by a user, it returns the number of successful conversions. scanf(“%d%d%d”, &first, &second, &third);scanf(“%d%d%d”, &first, &second, &third); –Should return a value 3 if the user correctly types three integers. –Suppose the user enters 2 integers followed by a string -- what happens? –What does our system do?

Common Programming Errors b Failure to correctly terminate a comment. b Leaving off a closing double quote character at the end of a string. b Misspelling or not declaring a variable. b Misspelling a function name. b Omitting the ampersand (&) with scanf( ).

Interrupting Program Execution b An executing program on a UNIX system can often be interrupted by entering a ^c from the keyboard. b The kill command is another way of ending program execution. b If your program is in an infinite loop you will have to use one of these methods to interrupt its execution.

How the Compiler Handles Comments /* This is a comment */ /* This is a comment */ The compiler first replaces each comment with a single blank. Thereafter, the compiler either disregards white space or uses it to separate tokens.

System Considerations b Syntax (Compile -Time) Errors Syntax errors are caught by the compiler.Syntax errors are caught by the compiler. The compiler attempts to identify the error and display a helpful error message.The compiler attempts to identify the error and display a helpful error message. b Run-Time Errors Errors that occur during program execution.Errors that occur during program execution. Memory errors caused by not using the address operator & with a scanf ( ) argument.Memory errors caused by not using the address operator & with a scanf ( ) argument.

Style b Use white space and comments to make your code easier to read and understand. Indent logical subgroups of code by 3 spaces.Indent logical subgroups of code by 3 spaces. b Choose variable names that convey their use in the program. b Place all #includes, #defines, main()s, and braces { } -- that begin and end the body of a function -- in column 1.