Fundamentals of Programming

Save this PDF as:
 WORD  PNG  TXT  JPG

Size: px
Start display at page:

Download "Fundamentals of Programming"

Transcription

1 Fundamentals of Programming Lecture 3 - Constants, Variables, Data Types, And Operations Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad

2 Outline C Program Data types Variables Constants Operations Arithmetic Operators unary operators operators that require only one operand binary operators operators that require two operands Assignment Operators Equality and Relational Operators Logical Operators Bitwise Operators Conditional Operator Comma Operator sizeof 2

3 C Program Constants and variables stdout (Screen) A B C D stderr (Screen) stdin (Keyboard) main X Y Z Instructions and operating procedures Operations and functions 3

4 // Simplest c program int main() { return 0; } Simple Program /* Objective: print on screen */ #include <stdio.h> // preprocessor statements have not ; int main() { printf("welcome to c!!!"); return 0; // indicate that program ended successfully } 4

5 #include <stdio.h> // (preprocessor ) Examples: Header file Constant Main function Variables Input and output Process Control flow #define PI 3.14 // PI constant (preprocessor ) // calculating area of circle int main() { /* variable definition */ float Radius; float Area = 0; // get radius of circle form user printf("enter Radius :\n"); scanf("%f", &Radius); // calculating area of circle Area = PI * Radius * Radius; printf( Area = %f", Area ); } system("pause"); return 0; 5

6 #include <stdio.h> // (preprocessor ) Examples: Header file Constant Main function Variables Input and output Process Control flow #define PI 3.14 // PI constant (preprocessor ) // calculating area of circle int main() { /* variable definition */ float Radius; float Area = 0; // get radius of circle form user printf("enter Radius :\n"); scanf("%f", &Radius); // calculating area of circle Area = PI * Radius * Radius; printf( Area = %f", Area ); } system("pause"); return 0; 6

7 #include <stdio.h> // (preprocessor ) Examples: Header file Constant Main function Variables Input and output Process Control flow #define PI 3.14 // PI constant (preprocessor ) // calculating area of circle int main() { /* variable definition */ float Radius; float Area = 0; // get radius of circle form user printf("enter Radius :\n"); scanf("%f", &Radius); // calculating area of circle Area = PI * Radius * Radius; printf( Area = %f", Area ); } system("pause"); return 0; 7

8 #include <stdio.h> // (preprocessor ) Examples: Header file Constant Main function Variables Input and output Process Control flow #define PI 3.14 // PI constant (preprocessor ) // calculating area of circle int main() { /* variable definition */ float Radius; float Area = 0; // get radius of circle form user printf("enter Radius :\n"); scanf("%f", &Radius); // calculating area of circle Area = PI * Radius * Radius; printf( Area = %f", Area ); } system("pause"); return 0; 8

9 #include <stdio.h> // (preprocessor ) Examples: Header file Constant Main function Variables Input and output Process Control flow #define PI 3.14 // PI constant (preprocessor ) // calculating area of circle int main() { /* variable definition */ float Radius; float Area = 0; // get radius of circle form user printf("enter Radius :\n"); scanf("%f", &Radius); // calculating area of circle Area = PI * Radius * Radius; printf( Area = %f", Area ); } system("pause"); return 0; 9

10 #include <stdio.h> // (preprocessor ) Examples: Header file Constant Main function Variables Input and output Process Control flow #define PI 3.14 // PI constant (preprocessor ) // calculating area of circle int main() { /* variable definition */ float Radius; float Area = 0; // get radius of circle form user printf("enter Radius :\n"); scanf("%f", &Radius); // calculating area of circle Area = PI * Radius * Radius; printf( Area = %f", Area ); } system("pause"); return 0; 10

11 #include <stdio.h> // (preprocessor ) Examples: Header file Constant Main function Variables Input and output Process Control flow #define PI 3.14 // PI constant (preprocessor ) // calculating area of circle int main() { /* variable definition */ float Radius; float Area = 0; // get radius of circle form user printf("enter Radius :\n"); scanf("%f", &Radius); // calculating area of circle Area = PI * Radius * Radius; printf( Area = %f", Area ); } system("pause"); return 0; 11

12 Variables Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a = 5 x = a + b valid identifier in C may be given representations containing multiple characters A-Z, a-z, 0-9, and _ (underscore character) First character must be a letter or underscore (no, _no 9no) Usually only the first 32 characters are significant There can be no embedded blanks (student no) Identifiers are case sensitive (area, Area, AREA, ArEa) Keywords cannot be used as identifiers 12

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

14 Naming Conventions C programmers generally agree on the following conventions for identifier: Use meaningful identifiers Separate words within identifiers with: underscores capitalize each word Examples: surfacearea SurfaceArea Surface_Area 14

15 Variable declaration Before using a variable, you must declare it Data_Type Identifier; int width; // width of rectangle float area; // result of calculating area stored in it char separator; // word separator Data_Type Identifier = Initial_Value; int width = 10; // width of rectangle float area = 255; // result of calculating area stored in it char seperator =, ; // word separator Data_Type Identifier, Identifier, Identifier,.; int width, length, temporary; float radius, area = 0; 15

16 Variable declaration When we declare a variable Space is set aside in memory to hold a value of the specified data type That space is associated with the variable name That space is associated with a unique address Visualization of the declaration int width = 95; // get width form user // &width is 22ff40 // *&width is 95 // sizeof width is 4 & * address data 22ff ff44 16

17 Data types Minimal set of basic data types primitive data types int float double char Void The size and range of these data types may vary among processor types and compilers 17

18 Data type qualifiers Modify the behavior of data type to which they are applied: Size qualifiers: alter the size of the basic data types: short: multiply by 0.5 long: multiply by 2 short can be applied to: int long can be applied to: int and double Sign qualifiers: can hold both positive and negative numbers, or only positive numbers.: signed: + and - unsigned: + they can be applied to : int and char 18

19 Data type char int float double Qualifier signed unsigned signed unsigned short long long Data type size and range Example signed char c; unsigned char c; char c; signed short i; signed short int i; unsigned int i; int i; signed int i; short i; short int i; long i; long int i; signed long i; signed long int i; long long i; long long int i; signed long long i; signed long long int i; float f; double d; long double d; Size or or or Range signed (16): unsigned (16): signed (32): unsigned (32): /- 3.4e +/- 38 (~7 digits) +/- 1.7e +/- 308 (~15 digits) 19

20 Overflow and Underflow #include <stdio.h> int main() { char letter = 'A'; // char variable to show ASCII code short shortvariable = 32769; // short variable for test overflow } printf("current value of shortvariable is = %d\n", shortvariable); printf("current value of letter is = %d", letter); printf("current value of letter is = %c", letter); system("pause"); // pause the execution to show press any key return 0; // indicate that program ended successfully current value of shortvariable is = current value of letter is = 65 current value of letter is = A 20

21 Compile-time or syntax Program Error is caused when the compiler cannot recognize a statement Run-time E.g. division by zero Logical E.g. Overflow and Underflow 21

22 Integer constant value Base 10: Base 16: 0x1 0X5 0x7fab unsigned: 5000u 4U long: l 56L unsigned long: ul long long : LL 25lL Example : 0xABu 0123uL 017LL 22

23 floating-point constant value A floating-point value contains a decimal point For example, the value is represented in scientific notation as X 10 2 and is represented in exponential notation (by the computer) as E+02 This notation indicates that is multiplied by 10 raised to the second power (E+02) The E stands for exponent 23

24 Char Char and string constant value char c; c = 'A'; // d = 65; String printf("string is array of char!!!"); printf("example of escape sequence is \n"); 24

25 Constant Constants provide a way to define a variable which cannot be modified by any other part in the code #define: without memory consume const: memory consume #define Identifier constant_value #define PI 3.14 #define ERROR "Disk error " #define ERROR "multiline \ message" #define ONE 1 #define TWO ONE + ONE 25

26 Constant const [Data_Type] Identifier = constant_value; const p = 3; // const int p = 3; const p; p = 3.14; // compile error const p = 3.14; // p = 3 because default is int const float p = 3.14; 26

27 Operators Arithmetic Operators unary operators operators that require only one operand binary operators operators that require two operands Assignment Operators Equality and Relational Operators Logical Operators Bitwise Operators Conditional Operator Comma Operator sizeof Operator Width * High Operand 27

28 Unary Operator Arithmetic Operators C operation Operator Expression Explanation Positive + a = +3; Negative - b = -4; Increment ++ i++; Equivalent to i = i + 1 Decrement - - i - -; Equivalent to i = i

29 PRE / POST Increment Consider this example: int width = 9; printf("%d\n", width++); printf("%d\n", width); But if we have: 9 10 int width = 9; printf("%d\n", width); width++; printf("%d\n", width); int width = 9; printf("%d\n", ++width); printf("%d\n", width); int width = 9; width++; printf("%d\n", width); printf("%d\n", width); 29

30 int R = 10; int count = 10; PRE / POST Increment ++ Or -- Statement Equivalent Statements R count R = count++; R = ++count; R = count--; R = --count; R = count; count = count + 1; count = count + 1; R = count; R = count; count = count 1; count = count 1; R = count;

31 Binary Operators Arithmetic Operators C operation Operator Expression Addition + b = a + 3; Subtraction - b = a 4; Multiplication * b = a * 3; Division / b = a / c; Modulus (integer) % b = a % c; 31

32 Division The division of variables of type integer will always produce a variable of type integer as the result Example int a = 7, b; float z; b = a / 2; z = a / 2.0; printf("b = %d, z = %f\n", b, z); Since b is declared as an integer, the result of a/2 is 3, not 3.5 b = 3, z =

33 Modulus You could only use modulus (%) operation on integer variables (int, long, char) z = a % 2.0; // error z = a % 0; // error Modulus will result in the remainder of a/2. Example int a = 7, b, c; b = a % 2; c = a / 2; printf("b = %d\n", b); printf("c = %d\n", c); a/ integral a%2 remainder 33

34 lvalue = rvalue; Assignment Operators int i; float f; i = 2; // *&i = 2; 2 = i; // error: invalid lvalue in assignment f = 5.6; i = f; // i = 5; i = -5.9; // i = -5; 34

35 Assignment Operators Assignment operators are used to combine the '=' operator with one of the binary arithmetic or bitwise operators Operator Expression Equivalent Statement Results += c += 7; c = c + 7; c = 16 -= c -= 8; c = c 8; c = 1 *= c *= 10; c = c * 10; c = 90 /= c /= 5; c = c / 5; c = 1 %= c %= 5; c = c % 5; c = 4 &= c &= 2 ; c = c & 2; c = 0 ^= c ^= 2; c = c ^ 2; c = 11 Example : c = 9; = c = 2; c = c 2; c = 11 <<= c <<= 2; c = c << 2; c = 36 >>= c >>= 2; c = c >> 2; c = 2 35

36 Precedence Rules The rules specify which of the operators will be evaluated first For example: x = 3 * a - ++b%3; Precedence Operator Associativity Level 1 (highest) () left to right 2 unary right to left 3 * / % left to right left to right c = b = d = 5; 5 (lowest) = += -= *= /= %= right to left 36

37 Precedence Rules how would this statement be evaluated? : x = 3 * a - ++b % 3; What is the value for X, for: a = 2, b = 4? x = 3 * a - ++b % 3; x = 3 * a - 5 % 3; x = 3 * a - 5 % 3; x = 6-5 % 3; x = 6 2; x = 4; 37

38 Precedence Rules If we intend to have a statement evaluated differently from the way specified by the precedence rules, we need to specify it using parentheses ( ) x = 3 * a - ++b % 3; Consider having the following statement: x = 3 * ((a - ++b)%3); the expression inside a parentheses will be evaluated first The inner parentheses will be evaluated earlier compared to the outer parentheses 38

39 Precedence Rules how would this statement be evaluated? x = 3 * ((a - ++b)%3); What is the value for X, for: a = 2, b = 4? x = 3 * ((a - ++b) % 3); x = 3 * ((a - 5) % 3); x = 3 * ((-3) % 3); x = 3 * 0; x = 0; 39

40 Precedence Rules how would this statement be evaluated? x = 3 * ++a b--%3; What is the value for X, for: a = 2, b = 4? x = 3 * ++a b-- % 3; x = 3 * ++a b-- % 3; x = 3 * ++a 4 % 3; x = 3 * 3 4 % 3; x = 9 1; x = 8; b = b -1, b = 3; 40

41 Equality and Relational Operators Equality Operators: Operator Example Meaning == x == y x is equal to y!= x!= y x is not equal to y Relational Operators: Operator Example Meaning > x > y x is greater than y < x < y x is less than y >= x >= y x is greater than or equal to y <= x <= y x is less than or equal to y 41

42 Logical Operators Logical operators are useful when we want to test multiple conditions AND OR NOT C has not bool data type, but: 0: evaluate to false If(0) printf(" "); other: evaluate to true If(1) printf(" "); If(-13) printf(" "); 42

43 && - Logical AND All the conditions must be true for the whole expression to be true Example: if (a == 1 && b == 2 && c == 3) means that the if statement is only true when a == 1 and b == 2 and c == 3 If (a = 5) e1 e2 Result = e1 && e e1 e2 Result = e1 && e2 false false false false true false true false false true true true 43

44 - Logical OR The truth of one condition is enough to make the whole expression true Example: if (a == 1 b == 2 c == 3) means the if statement is true when either one of a, b or c has the right value e1 e2 Result = e1 e e1 e2 Result = e1 e2 false false false false true true true false true true true true 44

45 ! - Logical NOT Reverse the meaning of a condition Example: if (!(radius > 90)) Means if radius not bigger than 90. e1 Result =!e1 e1 Result =!e false false true true true true false false 45

46 Bitwise Operators Apply to all kinds of int and char types: signed and unsigned char, short, int, long, long long Operator Name Description & AND Result is 1 if both operand bits are 1 OR Result is 1 if either operand bit is 1 ^ XOR Result is 1 if operand bits are different ~ Not (Ones Complement) Each bit is reversed << Left Shift Multiply by 2 >> Right Shift Divide by 2 46

47 Bitwise Operators Applicable for low level programming, e.g.: Port manipulation I/O programming Usually: &: set OFF one bit : set ON one bit ^: reverse one bit 47

48 XOR False when bits have same value e1 e2 Result = e1 ^ e

49 Examples a = 199; b = 90; c = a & b = 66; c = a b = 233; c = a ^ b = 157; c = ~a = 56 c = a << 2 = 28; c = a >> 3 = 24;

50 Conditional Operator The conditional operator (?:) is used to simplify an if/else statement Condition? Expression1 : Expression2; The statement above is equivalent to: if (Condition) Expression1; else Expression2; Which are more readable? 50

51 Example: Conditional Operator if/else statement: if (total > 12) grade = P ; else grade = F ; conditional statement: (total > 12)? grade = P : grade = F ; grade =( total > 12)? P : F ; 51

52 Example: if/else statement: Conditional Operator if (total > 12) printf( Passed!!\n ); else printf( Failed!!\n ); Conditional Statement: printf( %s!!\n, total > 12? Passed : Failed ); 52

53 sizeof The sizeof keyword returns the number of bytes of the given expression or type returns an unsigned integer result sizeof variable_identifier; sizeof (variable_identifier); sizeof (Data_Taype); Example: int x; printf("size of x = %d", sizeof x); printf("size of long long = %d", sizeof(long long)); printf("size of x = %d", sizeof (x)); 53

54 Type Casting Explicit Type cast: carried out by programmer using casting int k, i = 7; float f = 10.14; char c = 'B'; k = (i + f) % 3; // error k = (int)(i + f) % 3; Implicit Type cast: carried out by compiler automatically f = 65.6; i = f; //f = (int)f; c = i; // c = (int)i; 54

55 char c = 'A'; short s = 1; int i; float f; double d; Type Casting d = (c / i) + (f * d) + (f + i); (char) (int) (float) (double) (float) (int) int double float double i = (c + s); (int) (char) (short) double (short) (int) 55

56 Precedence Rules Primary Expression Operators () []. -> left-to-right Unary Operators * & + -! ~ ++expr --expr (typecast) sizeof right-to-left * / % + - >> << < > <= >= Binary Operators ==!= & ^ && left-to-right Ternary Operator?: right-to-left Assignment Operators = += -= *= /= %= >>= <<= &= ^= = right-to-left Post increment expr++ expr-- - Comma, left-to-right 56

57 Good Programming Practices Place each variable declaration on its own line with a descriptive comment Place a comment before each logical chunk of code describing what it does Do not place a comment on the same line as code with the exception of variable declarations Use spaces around all arithmetic and assignment operators Use blank lines to enhance readability 57

58 Good Programming Practices AVOID: variable names starting with an underscore often used by the operating system and easy to miss Place a blank line between the last variable declaration and the first executable statement of the program Indent the body of the program Use all uppercase for symbolic constants (used in #define preprocessor directives) Examples: #define PI #define AGE 52 58

C: How to Program. Week /Mar/05

C: How to Program. Week /Mar/05 1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

More information

Work relative to other classes

Work relative to other classes Work relative to other classes 1 Hours/week on projects 2 C BOOTCAMP DAY 1 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh Overview C: A language

More information

Chapter 2 - Introduction to C Programming

Chapter 2 - Introduction to C Programming Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic

More information

6.096 Introduction to C++ January (IAP) 2009

6.096 Introduction to C++ January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture

More information

ANSI C Programming Simple Programs

ANSI C Programming Simple Programs ANSI C Programming Simple Programs /* This program computes the distance between two points */ #include #include #include main() { /* Declare and initialize variables */ double

More information

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York CSc 10200! Introduction to Computing Lecture 2-3 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 2 Problem Solving Using C++ 2 Objectives In this

More information

Data Types and Variables in C language

Data Types and Variables in C language Data Types and Variables in C language Basic structure of C programming To write a C program, we first create functions and then put them together. A C program may contain one or more sections. They are

More information

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands Introduction Operators are the symbols which operates on value or a variable. It tells the compiler to perform certain mathematical or logical manipulations. Can be of following categories: Unary requires

More information

LECTURE 02 INTRODUCTION TO C++

LECTURE 02 INTRODUCTION TO C++ PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 02 INTRODUCTION

More information

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Parts of a C++ Program Parts of a C++ Program // sample C++ program

More information

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Copyright 2009 Publishing Pearson as Pearson Education, Addison-Wesley Inc. Publishing as Pearson Addison-Wesley

More information

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

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock) C/Java Syntax 1 Lecture 02 Summary Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 2 By the end of this lecture, you will be able to identify the

More information

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

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Introduction to C Programming Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Printing texts Adding 2 integers Comparing 2 integers C.E.,

More information

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

Unit-II Programming and Problem Solving (BE1/4 CSE-2) Unit-II Programming and Problem Solving (BE1/4 CSE-2) Problem Solving: Algorithm: It is a part of the plan for the computer program. An algorithm is an effective procedure for solving a problem in a finite

More information

Introduction to C# Applications

Introduction to C# Applications 1 2 3 Introduction to C# Applications OBJECTIVES To write simple C# applications To write statements that input and output data to the screen. To declare and use data of various types. To write decision-making

More information

Programming in C++ 6. Floating point data types

Programming in C++ 6. Floating point data types Programming in C++ 6. Floating point data types! Introduction! Type double! Type float! Changing types! Type promotion & conversion! Casts! Initialization! Assignment operators! Summary 1 Introduction

More information

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

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions Lecture 02 Summary C/Java Syntax Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 1 2 By the end of this lecture, you will be able to identify the

More information

Chapter 2: Using Data

Chapter 2: Using Data Chapter 2: Using Data Declaring Variables Constant Cannot be changed after a program is compiled Variable A named location in computer memory that can hold different values at different points in time

More information

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Op. Use Description + x + y adds x and y x y

More information

Programming in C++ 5. Integral data types

Programming in C++ 5. Integral data types Programming in C++ 5. Integral data types! Introduction! Type int! Integer multiplication & division! Increment & decrement operators! Associativity & precedence of operators! Some common operators! Long

More information

Data types, variables, constants

Data types, variables, constants Data types, variables, constants Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic in C 2.6 Decision

More information

Basic Elements of C. Staff Incharge: S.Sasirekha

Basic Elements of C. Staff Incharge: S.Sasirekha Basic Elements of C Staff Incharge: S.Sasirekha Basic Elements of C Character Set Identifiers & Keywords Constants Variables Data Types Declaration Expressions & Statements C Character Set Letters Uppercase

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

THE FUNDAMENTAL DATA TYPES

THE FUNDAMENTAL DATA TYPES THE FUNDAMENTAL DATA TYPES Declarations, Expressions, and Assignments Variables and constants are the objects that a prog. manipulates. All variables must be declared before they can be used. #include

More information

Lecture 2 Tao Wang 1

Lecture 2 Tao Wang 1 Lecture 2 Tao Wang 1 Objectives In this chapter, you will learn about: Modular programs Programming style Data types Arithmetic operations Variables and declaration statements Common programming errors

More information

Chapter-8 DATA TYPES. Introduction. Variable:

Chapter-8 DATA TYPES. Introduction. Variable: Chapter-8 DATA TYPES Introduction To understand any programming languages we need to first understand the elementary concepts which form the building block of that program. The basic building blocks include

More information

ISA 563 : Fundamentals of Systems Programming

ISA 563 : Fundamentals of Systems Programming ISA 563 : Fundamentals of Systems Programming Variables, Primitive Types, Operators, and Expressions September 4 th 2008 Outline Define Expressions Discuss how to represent data in a program variable name

More information

Course Outline Introduction to C-Programming

Course Outline Introduction to C-Programming ECE3411 Fall 2015 Lecture 1a. Course Outline Introduction to C-Programming Marten van Dijk, Syed Kamran Haider Department of Electrical & Computer Engineering University of Connecticut Email: {vandijk,

More information

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur Operators and Expressions 8/24/2012 Dept of CS&E 2 Arithmetic operators Relational operators Logical operators

More information

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

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso 1 Topics Naming Variables Declaring Variables Using Variables The Assignment Statement 2 a + b Variables are notthe same thing as variables in algebra.

More information

UNIT-2 Introduction to C++

UNIT-2 Introduction to C++ UNIT-2 Introduction to C++ C++ CHARACTER SET Character set is asset of valid characters that a language can recognize. A character can represents any letter, digit, or any other sign. Following are some

More information

Creating a C++ Program

Creating a C++ Program 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. 1 Creating a C++ Program created using an

More information

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme] Programming in C and Data Structures [15PCD13/23] 1 PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme] Course objectives: The objectives of this course is to make students

More information

ARG! Language Reference Manual

ARG! Language Reference Manual ARG! Language Reference Manual Ryan Eagan, Mike Goldin, River Keefer, Shivangi Saxena 1. Introduction ARG is a language to be used to make programming a less frustrating experience. It is similar to C

More information

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Basic Operators Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

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

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach C Fundamentals & Formatted Input/Output adopted from KNK C Programming : A Modern Approach C Fundamentals 2 Program: Printing a Pun The file name doesn t matter, but the.c extension is often required.

More information

Operators in C. Staff Incharge: S.Sasirekha

Operators in C. Staff Incharge: S.Sasirekha Operators in C Staff Incharge: S.Sasirekha Operators An operator is a symbol which helps the user to command the computer to do a certain mathematical or logical manipulations. Operators are used in C

More information

LECTURE 3 C++ Basics Part 2

LECTURE 3 C++ Basics Part 2 LECTURE 3 C++ Basics Part 2 OVERVIEW Operators Type Conversions OPERATORS Operators are special built-in symbols that have functionality, and work on operands. Operators are actually functions that use

More information

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

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure C Overview Basic C Program Structure C OVERVIEW BASIC C PROGRAM STRUCTURE Goals The function main( )is found in every C program and is where every C program begins speed execution portability C uses braces

More information

Chapter 3. Fundamental Data Types

Chapter 3. Fundamental Data Types Chapter 3. Fundamental Data Types Byoung-Tak Zhang TA: Hanock Kwak Biointelligence Laboratory School of Computer Science and Engineering Seoul National Univertisy http://bi.snu.ac.kr Variable Declaration

More information

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed CS 150 Introduction to Computer Science I Data Types Today Last we covered o main function o cout object o How data that is used by a program can be declared and stored Today we will o Investigate the

More information

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

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been

More information

Structured programming

Structured programming Exercises 2 Version 1.0, 22 September, 2016 Table of Contents 1. Simple C program structure................................................... 1 2. C Functions..................................................................

More information

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

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012 XSEDE Scholars Program Introduction to C Programming John Lockman III June 7 th, 2012 Homework 1 Problem 1 Find the error in the following code #include int main(){ } printf(find the error!\n");

More information

SECTION II: LANGUAGE BASICS

SECTION II: LANGUAGE BASICS Chapter 5 SECTION II: LANGUAGE BASICS Operators Chapter 04: Basic Fundamentals demonstrated declaring and initializing variables. This chapter depicts how to do something with them, using operators. Operators

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting

More information

Variables. Data Types.

Variables. Data Types. Variables. Data Types. The usefulness of the "Hello World" programs shown in the previous section is quite questionable. We had to write several lines of code, compile them, and then execute the resulting

More information

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003 Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 Java Programs A Java program contains at least one class definition. public class Hello { public static void

More information

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols. EEE-117 COMPUTER PROGRAMMING Basic Elements of C++ Objectives General Questions Become familiar with the basic components of a C++ program functions, special symbols, and identifiers Data types Arithmetic

More information

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

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab. University of Technology Laser & Optoelectronics Engineering Department C++ Lab. Second week Variables Data Types. The usefulness of the "Hello World" programs shown in the previous section is quite questionable.

More information

2 nd Week Lecture Notes

2 nd Week Lecture Notes 2 nd Week Lecture Notes Scope of variables All the variables that we intend to use in a program must have been declared with its type specifier in an earlier point in the code, like we did in the previous

More information

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

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC. XC Specification IN THIS DOCUMENT Lexical Conventions Syntax Notation Meaning of Identifiers Objects and Lvalues Conversions Expressions Declarations Statements External Declarations Scope and Linkage

More information

Operators & Expressions

Operators & Expressions Operators & Expressions Operator An operator is a symbol used to indicate a specific operation on variables in a program. Example : symbol + is an add operator that adds two data items called operands.

More information

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words. , ean, arithmetic s s on acters Comp Sci 1570 Introduction to C++ Outline s s on acters 1 2 3 4 s s on acters Outline s s on acters 1 2 3 4 s s on acters ASCII s s on acters ASCII s s on acters Type: acter

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 4 Input & Output Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline printf scanf putchar getchar getch getche Input and Output in

More information

Information Science 1

Information Science 1 Information Science 1 Simple Calcula,ons Week 09 College of Information Science and Engineering Ritsumeikan University Topics covered l Terms and concepts from Week 8 l Simple calculations Documenting

More information

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types Programming Fundamentals for Engineers 0702113 5. Basic Data Types Muntaser Abulafi Yacoub Sabatin Omar Qaraeen 1 2 C Data Types Variable definition C has a concept of 'data types' which are used to define

More information

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value Lecture Notes 1. Comments a. /* */ b. // 2. Program Structures a. public class ComputeArea { public static void main(string[ ] args) { // input radius // compute area algorithm // output area Actions to

More information

Programming Fundamentals and Methodology. COMP104: C++ Basics

Programming Fundamentals and Methodology. COMP104: C++ Basics Programming Fundamentals and Methodology COMP104: C++ Basics Dr. Brian Mak Dr. Pedro Sander Dr. Zhen Zhou Department of Computer Science & Engineering The Hong Kong University of Science and Technology

More information

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Spring 2005 Lecture 1 Jan 6, 2005 Course Information 2 Lecture: James B D Joshi Tuesdays/Thursdays: 1:00-2:15 PM Office Hours:

More information

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

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary GATE- 2016-17 Postal Correspondence 1 C-Programming Computer Science & Information Technology (CS) 20 Rank under AIR 100 Postal Correspondence Examination Oriented Theory, Practice Set Key concepts, Analysis

More information

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA COP 3275: Chapter 02 Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA Program: Printing a Pun #include int main(void) { printf("to C, or not to C: that is the question.\n");

More information

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Operators. Lecture 3 COP 3014 Spring January 16, 2018 Operators Lecture 3 COP 3014 Spring 2018 January 16, 2018 Operators Special built-in symbols that have functionality, and work on operands operand an input to an operator Arity - how many operands an operator

More information

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

Procedures, Parameters, Values and Variables. Steven R. Bagley Procedures, Parameters, Values and Variables Steven R. Bagley Recap A Program is a sequence of statements (instructions) Statements executed one-by-one in order Unless it is changed by the programmer e.g.

More information

Engineering Problem Solving with C++, Etter/Ingber

Engineering Problem Solving with C++, Etter/Ingber Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs C++, Second Edition, J. Ingber 1 Simple C++ Programs Program Structure Constants and Variables C++ Operators Standard Input

More information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:

More information

Tokens, Expressions and Control Structures

Tokens, Expressions and Control Structures 3 Tokens, Expressions and Control Structures Tokens Keywords Identifiers Data types User-defined types Derived types Symbolic constants Declaration of variables Initialization Reference variables Type

More information

Arithmetic Operators. Portability: Printing Numbers

Arithmetic Operators. Portability: Printing Numbers Arithmetic Operators Normal binary arithmetic operators: + - * / Modulus or remainder operator: % x%y is the remainder when x is divided by y well defined only when x > 0 and y > 0 Unary operators: - +

More information

CS242 COMPUTER PROGRAMMING

CS242 COMPUTER PROGRAMMING CS242 COMPUTER PROGRAMMING I.Safa a Alawneh Variables Outline 2 Data Type C++ Built-in Data Types o o o o bool Data Type char Data Type int Data Type Floating-Point Data Types Variable Declaration Initializing

More information

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used. Declaration Fundamental Data Types All variables must be declared before being used. Tells compiler to set aside an appropriate amount of space in memory to hold a value. Enables the compiler to perform

More information

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( ) Sir Muhammad Naveed Arslan Ahmed Shaad (1163135 ) Muhammad Bilal ( 1163122 ) www.techo786.wordpress.com CHAPTER: 2 NOTES:- VARIABLES AND OPERATORS The given Questions can also be attempted as Long Questions.

More information

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value. 3. EXPRESSIONS It is a sequence of operands and operators that reduce to a single value. Operator : It is a symbolic token that represents an action to be taken. Ex: * is an multiplication operator. Operand:

More information

Basic Types and Formatted I/O

Basic Types and Formatted I/O Basic Types and Formatted I/O C Variables Names (1) Variable Names Names may contain letters, digits and underscores The first character must be a letter or an underscore. the underscore can be used but

More information

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

More information

JAVA Programming Fundamentals

JAVA Programming Fundamentals Chapter 4 JAVA Programming Fundamentals By: Deepak Bhinde PGT Comp.Sc. JAVA character set Character set is a set of valid characters that a language can recognize. It may be any letter, digit or any symbol

More information

Chapter 3. Numeric Types, Expressions, and Output

Chapter 3. Numeric Types, Expressions, and Output Chapter 3 Numeric Types, Expressions, and Output 1 Chapter 3 Topics Constants of Type int and float Evaluating Arithmetic Expressions Implicit Type Coercion and Explicit Type Conversion Calling a Value-Returning

More information

Chapter 2 - Control Structures

Chapter 2 - Control Structures Chapter 2 - Control Structures 1 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures 2.5 if Selection Structure 2.6 if/else Selection Structure 2.7 while Repetition Structure 2.8 Formulating

More information

Structured Programming. Jon Macey

Structured Programming. Jon Macey Structured Programming Jon Macey Structured Programming Structured programming is an attempt to formalise the process of program development. There are several basis for a theorem of structured programming,

More information

WARM UP LESSONS BARE BASICS

WARM UP LESSONS BARE BASICS WARM UP LESSONS BARE BASICS CONTENTS Common primitive data types for variables... 2 About standard input / output... 2 More on standard output in C standard... 3 Practice Exercise... 6 About Math Expressions

More information

2. C99 standard guarantees uniqueness of characters for internal names. A. 12 B. 26 C. 31 D. 48

2. C99 standard guarantees uniqueness of characters for internal names. A. 12 B. 26 C. 31 D. 48 1. How can you make an infinite loop in C? A. while(1) { } B. loop:... goto loop; C. for(;;) { } D. All answers are right 2. C99 standard guarantees uniqueness of characters for internal names. A. 12 B.

More information

Variables and literals

Variables and literals Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of

More information

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords. Chapter 1 File Extensions: Source code (cpp), Object code (obj), and Executable code (exe). Preprocessor processes directives and produces modified source Compiler takes modified source and produces object

More information

Programming with C++ Language

Programming with C++ Language Programming with C++ Language Fourth stage Prepared by: Eng. Samir Jasim Ahmed Email: engsamirjasim@yahoo.com Prepared By: Eng. Samir Jasim Page 1 Introduction: Programming languages: A programming language

More information

More Programming Constructs -- Introduction

More Programming Constructs -- Introduction More Programming Constructs -- Introduction We can now examine some additional programming concepts and constructs Chapter 5 focuses on: internal data representation conversions between one data type and

More information

REVIEW. The C++ Programming Language. CS 151 Review #2

REVIEW. The C++ Programming Language. CS 151 Review #2 REVIEW The C++ Programming Language Computer programming courses generally concentrate on program design that can be applied to any number of programming languages on the market. It is imperative, however,

More information

Princeton University COS 333: Advanced Programming Techniques A Subset of C90

Princeton University COS 333: Advanced Programming Techniques A Subset of C90 Princeton University COS 333: Advanced Programming Techniques A Subset of C90 Program Structure /* Print "hello, world" to stdout. Return 0. */ { printf("hello, world\n"); -----------------------------------------------------------------------------------

More information

C++ Programming Language

C++ Programming Language C++ Programming Language 1 st week All computer systems consist of similar hardware devices and software components. Hardware: Hardware refers to the physical components that a computer is made of. A computer

More information

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char Week 1 Operators, Data Types & I/O Gaddis: Chapters 1, 2, 3 CS 5301 Fall 2016 Jill Seaman Programming A program is a set of instructions that the computer follows to perform a task It must be translated

More information

Fundamentals of C. Structure of a C Program

Fundamentals of C. Structure of a C Program Fundamentals of C Structure of a C Program 1 Our First Simple Program Comments - Different Modes 2 Comments - Rules Preprocessor Directives Preprocessor directives start with # e.g. #include copies a file

More information

Week 2 C Fundamentals; Formatted Input/Output; int and float Types

Week 2 C Fundamentals; Formatted Input/Output; int and float Types Week 2 C Fundamentals; Formatted Input/Output; int and float Types How Computer Store Numbers int and float Types C Fundamentals General Forms of a C Program Variables, Identifiers and Assignment Formatted

More information

Language Fundamentals Summary

Language Fundamentals Summary Language Fundamentals Summary Claudia Niederée, Joachim W. Schmidt Software Systems Institute Object-oriented Analysis and Design 1998/99 c.niederee@tu-harburg.de http://www.sts.tu-harburg.de Keywords

More information

Chapter 2 Elementary Programming

Chapter 2 Elementary Programming Chapter 2 Elementary Programming Part I 1 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from this chapter, you will learn how to solve practical

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information

Variables and Constants

Variables and Constants HOUR 3 Variables and Constants Programs need a way to store the data they use. Variables and constants offer various ways to work with numbers and other values. In this hour you learn: How to declare and

More information

Lecture 3 Tao Wang 1

Lecture 3 Tao Wang 1 Lecture 3 Tao Wang 1 Objectives In this chapter, you will learn about: Arithmetic operations Variables and declaration statements Program input using the cin object Common programming errors C++ for Engineers

More information

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

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program 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

More information

C Programming

C Programming 204216 -- C Programming Chapter 3 Processing and Interactive Input Adapted/Assembled for 204216 by Areerat Trongratsameethong A First Book of ANSI C, Fourth Edition Objectives Assignment Mathematical Library

More information

Language Fundamentals Summary

Language Fundamentals Summary Language Fundamentals Summary Claudia Niederée, Joachim W. Schmidt, Michael Skusa Software Systems Institute Object-oriented Analysis and Design 1999/2000 c.niederee@tu-harburg.de http://www.sts.tu-harburg.de

More information

Operators And Expressions

Operators And Expressions Operators And Expressions Operators Arithmetic Operators Relational and Logical Operators Special Operators Arithmetic Operators Operator Action Subtraction, also unary minus + Addition * Multiplication

More information

Week 2 Introduction to Computer and Algorithm (Part 2)

Week 2 Introduction to Computer and Algorithm (Part 2) Week 2 Introduction to Computer and Algorithm (Part 2) UniMAP Sem II-11/12 DKT121 1 Outline Pseudo code & flowchart Sample programming question Sample C program Identifiers and reserved words Program comments

More information