Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Similar documents
Syntax and Variables

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Programming in C++ 4. The lexical basis of C++

Presented By : Gaurav Juneja

C: How to Program. Week /Mar/05

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

Chapter 2 - Introduction to C Programming

Data Types and Variables in C language

C Language, Token, Keywords, Constant, variable

Binghamton University. CS-120 Summer Introduction to C. Text: Introduction to Computer Systems : Chapters 11, 12, 14, 13

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

A Fast Review of C Essentials Part I

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

INTRODUCTION 1 AND REVIEW

CMPE-013/L. Introduction to C Programming

Basic Elements of C. Staff Incharge: S.Sasirekha

Fundamental of Programming (C)

Tokens, Expressions and Control Structures

DEPARTMENT OF MATHS, MJ COLLEGE

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

>B<82. 2Soft ware. C Language manual. Copyright COSMIC Software 1999, 2001 All rights reserved.

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

VARIABLES AND CONSTANTS

Introduction to Computing Lecture 01: Introduction to C

Java Notes. 10th ICSE. Saravanan Ganesh

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Procedures, Parameters, Values and Variables. Steven R. Bagley

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

UNIT- 3 Introduction to C++

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

UEE1302 (1102) F10: Introduction to Computers and Programming

6.096 Introduction to C++ January (IAP) 2009

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

CS Programming In C

Chapter 1 & 2 Introduction to C Language

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

Basic Types, Variables, Literals, Constants

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Programming. C++ Basics

Variables. Data Types.

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

BLM2031 Structured Programming. Zeyneb KURT

Programming and Data Structures

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

1 Lexical Considerations

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

PROGRAMMAZIONE I A.A. 2018/2019

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

Short Notes of CS201

EL6483: Brief Overview of C Programming Language

Chapter-8 DATA TYPES. Introduction. Variable:

Week 3 Lecture 2. Types Constants and Variables

Lexical Considerations

CS201 - Introduction to Programming Glossary By

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Lexical Considerations

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

CS201 Some Important Definitions

3. Simple Types, Variables, and Constants

C Programming Review CSC 4320/6320

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Review of the C Programming Language for Principles of Operating Systems

Lecture 02 C FUNDAMENTALS

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Fundamentals of Programming

The C++ Language. Arizona State University 1

ET156 Introduction to C Programming

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

Computer Components. Software{ User Programs. Operating System. Hardware

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

CHW 469 : Embedded Systems

Data types, variables, constants

CS-211 Fall 2017 Test 1 Version A Oct. 2, Name:

Lecture 2 Tao Wang 1

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

EL2310 Scientific Programming

Program Fundamentals

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

Introduction to C# Applications

UNIT-2 Introduction to C++

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

COMP 2355 Introduction to Systems Programming

Work relative to other classes

Basics of Java Programming

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Introduction To C Programming

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

LEXICAL 2 CONVENTIONS

Transcription:

Syntax What the Compiler needs to understand your program 1

Pre-Processing Any line that starts with # is a pre-processor directive Pre-processor consumes that entire line Possibly replacing it with other C code For example #include <stdio.h> replaces the #include line with the contents of the stdio.h file. 2

White Space Needed to separate tokens e.g. then break are two contiguous keywords, but thenbreak is a single identifier Otherwise, ignored then break is the same as then break is the same as then break Enables programmer to choose formatting preferences 4

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

Keyword Classification Variable Declaration / Types char, const, double, enum, float, int, long, short, signed, struct, typedef, union, unsigned, void Control Flow break, case, continue, default, do, else, for, if, return, switch, while Advanced auto, extern, goto, sizeof, static Arcane asm, fortran, register, volatile 6

Identifiers Function, parameter, and variable names Must start with a letter or an underscore Underscores usually avoided May not contain white space After the first letter, can be any number, letter, or underscore Identifiers are case sensitive polyarea is different from PolyArea Choose names that are descriptive, and easy to type Be4aTgh9_fr37200aBy is probably not a good choice 7

Comments Anything starting with /* up to the next */ is a comment /* comments may span lines and continue on to the next line */ Comments do NOT nest /* x=3; /* reset x to 3 */ this was a mistake */ ^ syntax error this not declared // causes a comment to the end of the line (white space matters) Use comments to help reader understand what the code does! No need to comment the obvious: a=a+3; // add three to a Comments ignored by compiler 8

Block Comments /* ----------------------------------------------- I like this style of comment because I can add or remove lines from the comment without special comment reformat intervention. Besides, it looks clean. ------------------------------------------------*/ 9

C Statements A statement is a list of tokens that ends with a semi-colon a=a+3; int c=7; int cent_to_far(int c); A C statement may span more than one line in the file int MyLongFunctionNamedFunctionThatTakesLotsOfArguments( int arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7, int arg8); There may be more than one statement on one line in the file int a=5; a=a+6; int b; b=cent_to_far(a); 10

C Blocks A block of statements is a list of statements, surrounded by { and } { - Left curly brace } Right curly brace A block can be used anywhere a statement can be used Blocks of statements can be nested { statement1; } { statement2; statement3; } statement4; Inside Block Outside Block 11

Function Definition Function Signature, Followed by Embodiment Block of statements 12

C File Some stuff up front #include pre-processor directives Function Declarations Global Variable Declarations (more in the future) Function Definitions 13

Statements in Functions Variable Declarations Assignment/ Expression Statements (Lecture 05) Control Flow Statements (Lecture 06?) 14

C Variables A variable is a named piece of data Variables in C have A name (specified by the programmer) A value (may be unassigned/unknown) A location in memory (determined by the compiler) A type (size and interpretation) (more to come scope/ storage class/ etc.) Variables must be declared before they are used! 15

Variable Declaration Statement type name; type : One of the built-in types or a derived type name : Any valid identifier Examples: int age; char First_Initial; float gpa; 16

Variable Concept Age Memory???????????????????????????????????????????????????????????????????????? First_Initial gpa 17

Problem All computer data consists of strings of 0 s and 1 s How does the computer know what the string means? 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 12 12 11 10 9 8 7 6 5 4 3 2 1 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Float: -2.75 Unsigned Int: 3,244,371,200 Char: abcdefg Int: -1,070,596,096 18

Solution Tag each piece of data / location in memory with a type Type tells compiler how many bits to use Type tells compiler how to interpret those bits Enables automatic conversion from one type to another Enables compiler to check to make sure data is used correctly 19

Built In Type Number Symbol void Integer Real Char Bit 8 bit 16 bit 32 bit 64 bit float double char char char unsigned char short unsigned short int unsigned int long unsigned long 20

Q: Why four flavors of Integers? A: Allows programmer to choose size Type # Bits 1 Min Max U Max char 8-128 127 255 short 16-32,768 32,767 65,535 int 32 ~-2.15 x 10 9 ~2.15 x 10 9 ~4.3 x 10 9 long 64 ~-9 x 10 18 ~9 x 10 18 ~18.5x10 18 1 Number of bits may be different on different machines! 21

Q: Why signed vs. unsigned? A: Unsigned goes from 0 to 2 x max A: Some data can never go negative e.g. width Wouldn t it be nice if compiler checked for you! Signed is the default (what you get if you ask for int ) Avoid unsigned data can get you into trouble! 22

Q: What is char? char is ambiguous it can be used for small integers, characters, or bit flags It s up to the programmer to determine how to use char data! 23

ASCII Character Coding ASCII American Standard Code for Information Interchange Mapping from numbers 0-255, to characters

Variable Declaration Statement (w/ init.) type name = const; type : One of the built-in types or a derived type name : Any valid identifier const : Constant specification (should be of type type) Examples: int age = 17; char First_Initial = a ; float gpa = 3.985; 25

Constant Number Symbol Integer Real Character String Decimal Octal Hexadecimal Binary 26

Specifying Constant Numbers Decimal: digits 0-9 or underscore; no leading zeroes! e.g. 4 23 0 93 16 934531 23_391 1_973_300_023 Octal : digits 0-7 or underscore; with leading zero! e.g. 0 04 027 0135 020 03441203 055_537 016_547_425_467 Hexadecimal : prefix 0x, digits 0-9 + ABCDEF and underscore e.g. 0x0 0x4 0x17 0x87 0x10 0xE4283 0x5b5f 0x759E_2B37 Binary : prefix 0b, digits 0 and 1 and underscore e.g. 0b0 0b0100 0b00010111 0b10000111 0b00010000 Real : digits 0-9 with decimal point and underscore and e e.g. 4.0 3.1414 6.22e23

Specifying Letters A character constant is a single letter enclosed in SINGLE quotes e.g. a A 3 % Θ A string is a list of letters enclosed in DOUBLE quotes e.g. Professor Bartenstein student id: There is no built-in string type in C strings are arrays of characters Strings don t always act the way we expect them to More later

Local Variables Variable Declaration within function (usually at top) Automatic storage class by default New variable each time function starts Initialized when function starts (if initializer specified) No longer available when function ends 29

Global Variables Very rarely used in this class! Declared outside of all functions usually at top of file Static storage class by default New variable ONLY when program starts Initialized ONLY when program starts (if initializer specified) Available until program ends i.e. available to all functions

Parameters: Special Variables Like Variables, have Name Type Value Location in Memory Created when a function is invoked Initialized with argument value (as specified in the invocation) Destroyed after function returns Implication: changing a parameter value DOES NOT change the argument! 31

Resources Programming in C, Chapter 3 WikiPedia: C Syntax (https://en.wikipedia.org/wiki/c_syntax) WikiPedia: C Data Types (https://en.wikipedia.org/wiki/c_data_types) WikiPedia: Type Theory (https://en.wikipedia.org/wiki/type_theory) 32