1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
TDBA66, VT-03 Lecture - Ch. 21 A complete C-program Display Fig. 2.1 and comment on different things such as Preprocessor directives Header files Identifiers.
Dale/Weems/Headington
1 CS 201 Introduction to C (1) Debzani Deb. 2 Outline Overview of C General form of a C program C Language Elements.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Structure of a C program
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
Topic 2 – Introduction to the C Programming Language.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
Introduction to C Programming
CISC 105 – Topic 2 Last Topic Review Name the principle components of a computer. What advantage does assembly language have over machine language? What.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
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 Getting Started in C Programming
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
1 C++ Syntax and Semantics, and the Program Development Process.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
ELE118 Introduction to Programming
CC112 Structured Programming Lecture 2 1 Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Overview of C. C—a high-level programming language developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories. We will discuss: –the elements of a.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Lecture2.
Numbers in ‘C’ Two general categories: Integers Floats
CSCE 206 Structured Programming in C
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2 - Introduction to C Programming
Basic Elements of C++.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 2 - Introduction to C Programming
Lecture2.
Basic Elements of C++ Chapter 2.
Chapter 2 - Introduction to C Programming
CC213 Programming Applications
Chapter 2 - Introduction to C Programming
Introduction to CS Your First C Programs
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Lecture3.
WEEK-2.
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Presentation transcript:

1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two

2 Lecture Two Outline C Program Structure C Language Elements Preprocessor directives Function Header, and Function body Executable statements Reserved word, Standard identifiers, user defined identifiers Data Types and Variable Declarations Expressions and Assignment in C

3 Lecture Two Outline Input/Output operations Function Output function Input function Place holders Escape sequences Comments in C Example

4 C Program Structure

5 C Program structure A Sample C Program Function Header Function Body

6 Write a program to Convert given distance in miles to kilometers. C Program structure Example for Another C Program

7 C Program structure A Sample C Program

8 C Language Elements Preprocessor Directives Are commands that give instructions to the C preprocessor Begins with a (#) as its nonblank character. C preprocessor is a system program that modifies a C program prior to its compilation Example #include –The C language cannot do I/O by itself, so we need help from the library “stdio.h” to use the screen/Keyboard ! –We can use other libraries, too, as needed. –Another popular “library” is math.h, for advanced math functions.

9 C Language Elements Preprocessor Directives Examples #define KMS_PER_MILE # define AT # define VOTING_AGE 18 –Define is a a preprocessor directives. –Valid constant declarations –A named constant is a location in memory that we can refer to by a name, and in which a data value that cannot be changed is stored. –This directives instructs the processor to replace each occurrence of KMS_PER_MILE in the text of the C program by before compilation begins.

10 C Language Elements The Function Header int main ( ) type of returned value name of function says no parameters

11 A C program is a collection of one or more “functions” (parts) There must be a function called main( ) A function body has two parts declaration and executable statements. Execution always begins with the first statement in function main( ) Any other functions in the program are subprograms and are not executed until they are called C Language Elements The Function Body

12 printf(“ Hello, World “); This statement means: Display the words Hello, World on the screen return ( 0 ); This statement STOPs the program and returns Zero to the O.S. (more about this later) C Language Elements The Executable Statements

13 Reserved Word A word that has special meaning in C Standard Identifiers A word having special meaning but one that a programmer may redefine (but redefinition is not recommended) User Defined Identifiers An identifier must consist only of letters, digits, and underscores. An identifier cannot begin with a digit A C reserved word cannot be used as an identifier. An identifier defined in a C standard library should not be redefined. C is a case-sensitive language. The names Pressure, pressure, and PRESSURE are viewed by the compiler as different identifier. C Language Elements Words

14 Invalid identifiers 1Letter begins with a number double reserved word int reserved word TWO*FOUR character * not allowed joe’s character ‘ not allowed age# character # not allowed Age-of-cat character – is not underscore character (_) Valid Identifiers Age_of_person taxRateY2k PrintHeading C Language Elements Invalid/Valid identifiers

15 Reserved Word int void Double return Standard Identifiers Printf scanf User Defined Identifiers KMS_PER_MILE miles kms C Language Elements Words in the second example

16 Integral Types represent whole numbers and their negatives declared as int, short, or long Int sample values 4578, -4578, 0 Floating Types represent real numbers with a decimal point declared as float, or double Float sample values Character Types represent single characters declared as char Char sample values B d 4 ? * Standard Data Type in C

17 What about words and sentences ? a String is a sequence of characters enclosed in double quotes sample values “Hello” “Year 2004” “1234” Not in Standard C, We’ll cover it later (in Chapter 7!)

18 Variable A name associated with a memory cell whose value can change. Variable Declaration Statements that communicate to the compiler the names of variables in the program and the type of information stored in each variable. Variable Declaration in C

19 Giving a Value to a Variable You can assign (give) a value to a variable by using the assignment operator = VARIABLE DECLARATIONS char code ; int i; long national_debt; float payRate; double pi; VALID ASSIGNMENT STATEMENTS code = ‘B’ ; i = 14; national_debt = ; pay_rate = 14.25; pi = ;

20 Giving a Value to a Variable

21 What is an Expression in C? An expression is a valid arrangement of variables, constants, and operators. in C each expression is evaluated to compute a value of a given type the value of the expression is 14

Variable = Expression First, Expression on right is evaluated. Then the resulting value is stored in the memory location of Variable on left. Any information that was stored in the variable will be lost. NOTE: An automatic type conversion occurs after evaluation but before the value is stored if the types differ for Expression and Variable Example kms = KMS_PER_MILE * miles; Assignment Operator Syntax

23 What is the content of variables a and b after each step? int a, b ; a = 5 ; b = 7 ; b = a + b ; a = b - 1 ; b = 3 ;

24 printf (Output Function) The printf function displays the value of its format string after substituting in left-to-right order the values of the expressions in the print list for their list for their placeholders in the format string and after replacing escape sequences such as \n by their meanings. scanf (Input Function) The scanf function copies into memory data entered during the program execution. The order of the placeholders must correspond to the order of the variables in the input list. The data must be entered in the same order in the input list. You should insert one or more blank characters or carriage returns between numeric items. printf/scanf function

25 SYNTAX Examples : printf( format string, print list ) ; printf(format string); printf(“That equals %f kilometers. \n”, kms); printf(“enter the distance in miles> ”); printf( “Hello, World?\n“); Output Function Place holder Escape sequence

26 Output Function

27 SYNTAX Examples : scanf( format string, input list ) ; scanf(“%lf”, &miles); Input Function Place holder Ampersand

28 Input Function

29 Place Holders Function useVariable typePlaceholder printf/scanfchar%c printf/scanfint%d printfdouble%f scanfdouble%lf A placeholder is a symbol beginning with % in a format string that indicates where to display the output value

30 Escape sequence \n Start a new line of output \t tab printf(“This is one line \n“); printf(“ and \n this \t is \t another \n “); This is one line and this isanother The backslash (\) is called an escape character Indicates that printf is supposed to do something unusual When encountering a backslash, printf looks to the next character and combines it with the backslash to form an escape sequence

31 C comments A comment is used to provide information, but it is ignored by the processor. Begin with /* and ends with */ /* Name : Foulan El Foulany */ /* This is a multi line comment */

32 Example Write a program to create two integer variables, and store in them the values 3 and 5. Then calculate their sum and their product and display the result.

33 /* * Name: Ali Reda * This program gets the sum and product of 2 integers */ #include /* library containing printf */ int main ( ) { int first ;/* declaring first variable */ int second ;/* declaring second variable */ int sum ;/* variable to hold the sum */ int product ;/* variable to hold the product */ C Program

34 C Code Continued first = 3 ;/* assignment statement */ second = 5 ; sum = first + second ; printf(“ The sum is %d \n “, sum );/* output */ product = first * second ; printf(“ The product is %d \n “, product ); return (0); }

35 Output of Program The sum is 8 The product is 15

36 /* * Name: Ismael Youssef * This program gets the sum and product of 2 integers */ #include /* library containing printf */ int main ( ) { int first = 3 ;/* declaring and assigning first variable */ int second = 5 ;/* declaring and assigning second variable */ int sum, product ; sum = fiirst + second ; product = first * second ; printf(“ The sum is %d \n The product is %d \n “, sum, product ); return ( 0 ) ; } Same program, different style