C Language Elements: Preprocessor Directives Commands that give instructions to the C preprocessor Modifies text of a C program before compilation Two.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Introduction to C Programming
Introduction to C Programming
C Language.
Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
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.
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.
Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Chapter 7: User-Defined Functions II
Introduction to C Programming
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 8 Arrays and Strings
Guide To UNIX Using Linux Third Edition
JavaScript, Third Edition
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Review of C++ Programming Part II Sheng-Fang Huang.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 2.
C Tokens Identifiers Keywords Constants Operators Special symbols.
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
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’};
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
By Mr. Muhammad Pervez Akhtar
C LANGUAGE Characteristics of C · Small size
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
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
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Chapter 7: User-Defined Functions II
Programming Languages and Paradigms
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
Programming Paradigms
Introduction to C Programming
Starting JavaProgramming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to C Programming
Programming Languages and Paradigms
Session 1 – Introduction to Computer and Algorithm (Part 2)‏
Variables in C Topics Naming Variables Declaring Variables
C Language B. DHIVYA 17PCA140 II MCA.
INTRODUCTION TO C.
Presentation transcript:

C Language Elements: Preprocessor Directives Commands that give instructions to the C preprocessor Modifies text of a C program before compilation Two most common: #include #define

C Language Elements Preprocessor Directives: –#define –#include Reserved Words: int void double Standard Identifiers: printf scanf User-defined Identifiers: –Must consist of letters, digits, underscores –Cannot begin with a digit –Cannot use a C reserved word

C Language Elements Variable Declarations Executable Statements Input/Output Operations & Functions –printf, scanf, fprintf, fscanf Three Input/Output streams: – stdin (standard input) – stdout (standard output) – stderr (standard error)

Expressions Arithmetic Operators + - * / % Precedence –Parenthesis Rule –Operator Precedence: unary +, - first *, /, % next binary +, - last –Associativity Rules: unary: right to left binary: left to right

Functions Functions divide code into meaningful parts: Separates factual (what) from procedural (how) Clarifies Algorithm steps Every Function: –Has a return type –Has a Name (following Identifier conventions) –May have Arguments (aka Parameter List) enclosed in parentheses ( )

Functions A function appears in a program three times: 1. Prototype Declaration: –binds the type and the name –provides arguments and types 2. Function Definition: –Defines the procedure the function will execute –Sets the return value 3. Function Call: –A reference to the function in main or another function that calls upon it to execute and return a value

Control Structures Statements are grouped into Control Structures (Blocks) Each block has exactly one entry and one exit point Simple Statements Sequential Statements Compound Statements Block Statements

ANSI C Statements Selection: if, switch (Decision) Iteration: while, for, do-while (Loops) Jump: break, continue, goto, return Label: case, default (used with switch) & label used with goto Expression: any valid expression Block: blocks of code

Relational and Equality Operators OperatorMeaningType <less thanrelational > greater thanrelational <=less than or equal torelational >=greater than or equal torelational ==equal toequality !=not equal toequality

Logical Operators && (logical and) || (logical or) ! (logical not - logical complement or negation)

Precedence (See Table 11.1 p. 536) function callshighest ! + - & (unary operators) * / % + - = > == != && || =lowest

Iteration for loop for (initialization; condition; increment) statement; while loop while (condition) statement; do while loop do { statement; } while(condition) post-fix, pre-fix increment

Modularity Advantages of Modularity A modular program is easier to write and debug. Functional components can be written and debugged separately A modular program is easier to maintain and change  reusability –A modular program is easier to control and divide among different programmers

Modularity Pass by Value: –copies the value of an argument into the formal parameter of the function x = sqrt(value); Pass by Reference: –the address of an argument is copied into the parameter inside the function, the address is used to access the actual argument used in the call

Meaning of the * Symbol (3 uses) * binary operator for multiplication x * y; variable declaration: char *signp –signp is a pointer to a variable of type char "follow the pointer": unary indirection operator *signp = '+'; *fracp = magnitude - *wholep;

Scope The scope rules of a language are the rules that govern whether a piece of code knows about or has access to another piece of code or data. The scope of a name refers to the region of a program where a particular meaning of a name is "visible" or can be referenced

Scope Each function is a discrete block of code A function's code is private to that function and cannot be accessed by any statement in any other function… …only through a call to that function Code and data defined within one function cannot interact with the code or data defined in another function because the two functions have a different scope.

Scope Variables defined within a function are called local variables A local variable comes into existence when the function is entered and destroyed upon exiting the function Constant Macros (#define's) have a scope that begins at their definition, and continues to the end of the source file

Data Types: int vs. double types positive integers are represented by standard binary numbers: –e.g. 13 is in binary, 31 is , 32 is , etc. double (floating point format) are represented by: real number = mantissa X 2 exponent … and stored as: mantissa exponent

Automatic & Explicit Conversion We’ve already seen examples of Automatic conversion: int x=3, y=4; double a=3.4, b=2.7; x = a*b; /* a*b is 9.18, x gets the value 9 */ a = b*x; /* x is converted to double 3.0, a gets the value 8.1 */ Explicit conversion: a = (double)x / (double)y; /* a gets 0.75 */ a = (double) (x/y) /* what does a get?? */ x = (int)a + (int)b /* x gets 5 */

char type: printable ASCII codes 32 = (space) 33 = ! 34 = " 35 = # 36 = $ 37 = % 38 = & 39 = ' 40 = ( 41 = ) 42 = * 43 = + 44 =, 45 = - 46 =. 47 = / 48 = 0 49 = 1 50 = 2 51 = 3 52 = 4 53 = 5 54 = 6 55 = 7 56 = 8 57 = 9 58 = : 59 = ; 60 = < 61 = = 62 = > 63 = ? = A 66 = B 67 = C 68 = D 69 = E 70 = F 71 = G 72 = H 73 = I 74 = J 75 = K 76 = L 77 = M 78 = N 79 = O 80 = P 81 = Q 82 = R 83 = S 84 = T 85 = U 86 = V 87 = W 88 = X 89 = Y 90 = Z 91 = [ 92 = \ 93 = ] 94 = ^ 95 = _ 96 = ` 97 = a 98 = b 99 = c 100 = d 101 = e 102 = f 103 = g 104 = h 105 = i 106 = j 107 = k 108 = l 109 = m 110 = n 111 = o 112 = p 113 = q 114 = r 115 = s 116 = t 117 = u 118 = v 119 = w 120 = x 121 = y 122 = z 123 = { 124 = | 125 = } 126 = ~

Comparisons of type char Comparisons between characters are based on comparisons of binary representations …So '0' < '1' < '2' < '3' < '4' < '5' etc. and 'a' < 'b' < 'c' < 'd' < 'e' < 'f' etc. and 'A' < 'B' < 'C' < 'D' < 'E' < 'F' etc. Is 'a' < 'A' ?

Conversions between int and char Explicit conversion between int and char: int x=65; char a='K'; printf("%c", (char) x); /* prints 'A' */ printf("%d", (int) a); /* prints 75 */ x += 6; a = (char) x; printf("%c", a); /* prints 'G' */

Single Dimension Arrays Size (memory allocation) of an array: total bytes = sizeof(basetype) * size of array Arrays are allocated as sequential locations in memory Arrays are indexed beginning with 0, up to the size_of_the_array - 1 Example: ‘primes’ array ( int primes[10] ): index value

Arrays & Pointers The name of the array is really just a pointer to an array: int primes[9]; /* the identifier "primes" is a pointer to the first element */ int *p; p = primes; /* same as p = &primes[0]; */

Two-Dimensional Arrays Two dimensional arrays are stored in a row-column matrix, where the first index is the row and the second is the column Using this paradigm, the rightmost index changes faster than the leftmost when accessing the elements in an array in the same order that they are stored in memory [0][0][0][1][0][2][0][3] [1][0][1][1][1][2][1][3] [2][0][2][1][2][2][2][3]

Sorting

Strings String Variables Strings may be declared as static or dynamic variables Static Declaration and Initialization Sample Declaration char String1[10]; Sample Combination Declaration & initialization char String1[10] = ”MIS 121"; /* must be <= 10 */

Strings Arrays of Strings An array of strings is a 2D array of char, where each row is a string Example: char months[12][10] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November","December"}; for (i=0;i<12;i++) printf("%s\n",months[i]);

Strings String Functions: strcpy strncpy strlen strcat strncat strcmp strncmp

Structures Structure: a collection of variables referenced under one name. Provides a convenient means of keeping related information together. A structure declaration forms a template that can be used to create structures. The variables that make up the structure are called members of the structure (also commonly referred to as elements or fields)

Definition: typedef struct { // This defines a structure template char Name[20] // that can then be used in other places to int Number; // declare actual variables. char Dept; // This in effect represents int Phone; // a new user defined data type. } EmployeeRecord_t; Declaration : EmployeeRecord_t Employee; // a variable instance // Here attributes such as name, class, type, // and size are bound to this variable. Use of structure: Employee.Phone = ; if (Employee.Number > 17) Definition, Declaration, and Use of a structure Without pointers - Simplified with typedef

Using structs Accessing members with the '. ' or ' -> ' Operators Arrays of structs Passing structs or arrays of structs to functions Arrays of Pointers to structs

Encapsulation The mechanism that binds together code and data, and that keeps both safe from outside interference or misuse. An “Object” is a logical entity that encapsulates both data and the code that manipulates that data. Some of the code or data within an object may be “private” or inaccessible to anything outside the object. An object is a variable of a user-defined type.

Polymorphism “One Interface, Multiple Methods” Allows one interface to be used with a general class of actions. Real-world Example: A thermostat –The thermostat (interface) is the same, no matter what type of furnace you have (coal, gas, electric) It’s the compiler’s job to select the specific action (method) that applies to each situation –(We’ll look at a template class “stack” that works for any data type).

Inheritance Inheritance is the process by which one object can acquire the properties of another object. Supports the concept of classification: An object need only define those qualities that make it unique within its class. AppleFruitFood Golden Delicious is anis a