# Fundamentals of Programming

Size: px
Start display at page:

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

### Fundamental of Programming (C)

Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

### Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

Morteza Noferesti 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

### Programming and Data Structures

Programming and Data Structures Teacher: Sudeshna Sarkar sudeshna@cse.iitkgp.ernet.in Department of Computer Science and Engineering Indian Institute of Technology Kharagpur #include int main()

### Chapter 4: Basic C Operators

Chapter 4: Basic C Operators In this chapter, you will learn about: Arithmetic operators Unary operators Binary operators Assignment operators Equalities and relational operators Logical operators Conditional

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

Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Uppercase Alphabets Lowercase Alphabets Character Set A, B, C, Y, Z a, b, c, y, z Digits

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

Fundamental Data Types CSE 130: Introduction to Programming in C Stony Brook University Program Organization in C The C System C consists of several parts: The C language The preprocessor The compiler

### DEPARTMENT OF MATHS, MJ COLLEGE

T. Y. B.Sc. Mathematics MTH- 356 (A) : Programming in C Unit 1 : Basic Concepts Syllabus : Introduction, Character set, C token, Keywords, Constants, Variables, Data types, Symbolic constants, Over flow,

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

Variables in C CMSC 104, Spring 2014 Christopher S. Marron (thanks to John Park for slides) 1 Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement 2 What

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

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah Lecturer Department of Computer Science & IT University of Balochistan 1 Outline p Introduction p Program development p C language and beginning with

### 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

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

Variables in C CMSC 104, Fall 2012 John Y. Park 1 Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement 2 What Are Variables in C? Variables in C have the

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

C introduction Variables Variables 1 / 14 Contents Variables Data types Variable I/O Variables 2 / 14 Usage Declaration: t y p e i d e n t i f i e r ; Assignment: i d e n t i f i e r = v a l u e ; Definition

### 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

### These are reserved words of the C language. For example int, float, if, else, for, while etc.

Tokens in C Keywords These are reserved words of the C language. For example int, float, if, else, for, while etc. Identifiers An Identifier is a sequence of letters and digits, but must start with a letter.

### Computer System and programming in C

1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign

### 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

### UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . \$, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

### 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

### GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

http://www.tutorialspoint.com/go/go_operators.htm GO - OPERATORS Copyright tutorialspoint.com An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.

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

LESSON 1 FUNDAMENTALS OF C The purpose of this lesson is to explain the fundamental elements of the C programming language. C like other languages has all alphabet and rules for putting together words

### 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

### Reserved Words and Identifiers

1 Programming in C Reserved Words and Identifiers Reserved word Word that has a specific meaning in C Ex: int, return Identifier Word used to name and refer to a data element or object manipulated by the

### BASIC ELEMENTS OF A COMPUTER PROGRAM

BASIC ELEMENTS OF A COMPUTER PROGRAM CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING LOGO Contents 1 Identifier 2 3 Rules for naming and declaring data variables Basic data types 4 Arithmetic operators

### Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Data Types Basic Types Enumerated types The type void Derived types

### 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

### Programming. Elementary Concepts

Programming Elementary Concepts Summary } C Language Basic Concepts } Comments, Preprocessor, Main } Key Definitions } Datatypes } Variables } Constants } Operators } Conditional expressions } Type conversions

### A Fast Review of C Essentials Part I

A Fast Review of C Essentials Part I Structural Programming by Z. Cihan TAYSI Outline Program development C Essentials Functions Variables & constants Names Formatting Comments Preprocessor Data types

### 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

### 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

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

Data types, variables, constants Outline.1 Introduction. Text.3 Memory Concepts.4 Naming Convention of Variables.5 Arithmetic in C.6 Type Conversion Definition: Computer Program A Computer program is a

### Chapter 1 & 2 Introduction to C Language

1 Chapter 1 & 2 Introduction to C Language Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 1 & 2 - Introduction to C Language 2 Outline 1.1 The History

### Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators JAVA Standard Edition Java - Basic Operators Java provides a rich set of operators to manipulate variables.

### Chapter 2: Introduction to C++

Chapter 2: Introduction to C++ Copyright 2010 Pearson Education, Inc. Copyright Publishing as 2010 Pearson Pearson Addison-Wesley Education, Inc. Publishing as Pearson Addison-Wesley 2.1 Parts of a C++

### 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

### Programming. C++ Basics

Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++

### 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

### CS102: Variables and Expressions

CS102: Variables and Expressions The topic of variables is one of the most important in C or any other high-level programming language. We will start with a simple example: int x; printf("the value of

### 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

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

Laboratory 2: Programming Basics and Variables Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information 3. Comment: a. name your program with extension.c b. use o option to specify

### Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

### 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.,

### UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

( Word to PDF Converter - Unregistered ) http://www.word-to-pdf-converter.net FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING INTRODUCTION TO C UNIT IV Overview of C Constants, Variables and Data Types

### CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

CS113: Lecture 3 Topics: Variables Data types Arithmetic and Bitwise Operators Order of Evaluation 1 Variables Names of variables: Composed of letters, digits, and the underscore ( ) character. (NO spaces;

### Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja

### 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

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

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

### Operators and Expressions:

Operators and Expressions: Operators and expression using numeric and relational operators, mixed operands, type conversion, logical operators, bit operations, assignment operator, operator precedence

### Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Q1. Explain the structure of a C program Structure of the C program is shown below: Preprocessor Directives Global Declarations Int main (void) Local Declarations Statements Other functions as required

### 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

### CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

CSE 1001 Fundamentals of Software Development 1 Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018 Identifiers, Variables and Data Types Reserved Words Identifiers in C Variables and Values

### Chapter 3: Operators, Expressions and Type Conversion

101 Chapter 3 Operators, Expressions and Type Conversion Chapter 3: Operators, Expressions and Type Conversion Objectives To use basic arithmetic operators. To use increment and decrement operators. To

### Lecture 02 C FUNDAMENTALS

Lecture 02 C FUNDAMENTALS 1 Keywords C Fundamentals auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void

### JAVA OPERATORS GENERAL

JAVA OPERATORS GENERAL 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

### VARIABLES AND CONSTANTS

UNIT 3 Structure VARIABLES AND CONSTANTS Variables and Constants 3.0 Introduction 3.1 Objectives 3.2 Character Set 3.3 Identifiers and Keywords 3.3.1 Rules for Forming Identifiers 3.3.2 Keywords 3.4 Data

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

The component base of C language Nguyễn Dũng Faculty of IT Hue College of Science Content A brief history of C Standard of C Characteristics of C The C compilation model Character set and keyword Data

### 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

### 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

### Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

300580 Programming Fundamentals 3 With C++ Variable Declaration, Evaluation and Assignment 1 Today s Topics Variable declaration Assignment to variables Typecasting Counting Mathematical functions Keyboard

### A flow chart is a graphical or symbolic representation of a process.

Q1. Define Algorithm with example? Answer:- A sequential solution of any program that written in human language, called algorithm. Algorithm is first step of the solution process, after the analysis of

### 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

### P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

P.G.TRB - COMPUTER SCIENCE Total Marks : 50 Time : 30 Minutes 1. C was primarily developed as a a)systems programming language b) general purpose language c) data processing language d) none of the above

### Fundamental of C programming. - Ompal Singh

Fundamental of C programming - Ompal Singh HISTORY OF C LANGUAGE IN 1960 ALGOL BY INTERNATIONAL COMMITTEE. IT WAS TOO GENERAL AND ABSTRUCT. IN 1963 CPL(COMBINED PROGRAMMING LANGUAGE) WAS DEVELOPED AT CAMBRIDGE

### Java Notes. 10th ICSE. Saravanan Ganesh

Java Notes 10th ICSE Saravanan Ganesh 13 Java Character Set Character set is a set of valid characters that a language can recognise A character represents any letter, digit or any other sign Java uses

### 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

### 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

### Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

### The C++ Language. Arizona State University 1

The C++ Language CSE100 Principles of Programming with C++ (based off Chapter 2 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

### Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Course Name: Advanced Java Lecture 2 Topics to be covered Variables Operators Variables -Introduction A variables can be considered as a name given to the location in memory where values are stored. One

### Unit 3. Operators. School of Science and Technology INTRODUCTION

INTRODUCTION Operators Unit 3 In the previous units (unit 1 and 2) you have learned about the basics of computer programming, different data types, constants, keywords and basic structure of a C program.

### Presented By : Gaurav Juneja

Presented By : Gaurav Juneja Introduction C is a general purpose language which is very closely associated with UNIX for which it was developed in Bell Laboratories. Most of the programs of UNIX are written

### Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Operators Overview Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators Operands and Operators Mathematical or logical relationships

### CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

CprE 288 Introduction to Embedded Systems Exam 1 Review http://class.ece.iastate.edu/cpre288 1 Overview of Today s Lecture Announcements Exam 1 Review http://class.ece.iastate.edu/cpre288 2 Announcements

### Introduction to Programming

Introduction to Programming session 6 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Spring 2011 These slides are created using Deitel s slides Sharif University of Technology Outlines

### 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

### The Arithmetic Operators

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

### 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

Visual C# 2005 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion Topics Additional Projects Additional Resources Key Terms

### Full file at C How to Program, 6/e Multiple Choice Test Bank

2.1 Introduction 2.2 A Simple Program: Printing a Line of Text 2.1 Lines beginning with let the computer know that the rest of the line is a comment. (a) /* (b) ** (c) REM (d)

### C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,

### 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

### CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Overview of Source Code Components Comments Library declaration Classes Functions Variables Comments Can

### A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

1.3a Expressions Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a syntactical token that requires an action be taken An operand is an object

### 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.

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

C Programming Notes Dr. Karne Towson University Reference for C http://www.cplusplus.com/reference/ Main Program #include main() printf( Hello ); Comments: /* comment */ //comment 1 Data Types

### 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

### 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

### 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

### Chapter 2: Basic Elements of C++

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

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

Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

### 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

### Operators. Java operators are classified into three categories:

Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

### Computers Programming Course 5. Iulian Năstac

Computers Programming Course 5 Iulian Năstac Recap from previous course Classification of the programming languages High level (Ada, Pascal, Fortran, etc.) programming languages with strong abstraction

### .Net Technologies. Components of.net Framework

.Net Technologies Components of.net Framework There are many articles are available in the web on this topic; I just want to add one more article over the web by explaining Components of.net Framework.

### C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine

### 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

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

C Overview C OVERVIEW Goals speed portability allow access to features of the architecture speed C fast executables allows high-level structure without losing access to machine features many popular languages

### 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.

### 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");