Programming. Elementary Concepts

Size: px
Start display at page:

Download "Programming. Elementary Concepts"

Transcription

1 Programming Elementary Concepts

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

3 Review: C Programming Language } C is a fast, small, general-purpose, platform independent programming language } C is used in several applications: } Compilers and interpreters } Operating systems } Database systems } Microcontrollers } etc. } C is a structured, static compiled and typed } "C is quirky, flawed, and an enormous success." Ritchie 3

4 Use of Comments /* ** This program reads input lines from the standard input and prints ** each input line, followed by just some portions of the lines, to ** the standard output. ** ** The first input is a list of column numbers, which ends with a ** negative number. The column numbers are paired and specify ** ranges of columns from the input line that are to be printed. ** For example, indicates that only columns 0 through 3 ** and columns 10 through 12 will be printed. */ } Only /* */ for comments in C89 } Do not nest /* */ comments within comments } /* is matched with the very next */ that comes along } Support for // comments in C99 4

5 Use of Comments } Possible uses: } Identify the authors } Describe the program } Describe the code and the variables } Explain decisions } Hide code } Can appear in any place 5

6 Preprocessor Directives #include <stdio.h> #include <stdlib.h> #include <string.h> } The #include directives paste the contents of the files stdio.h, stdlib.h and string.h into your source code, at the place where the directives appear } These files contain information about some library functions used in the program: } stdio stands for standard I/O, stdlib stands for standard library, and string.h includes useful string manipulation functions. } Can be used to include own header files 6

7 Preprocessor Directives #define MAX_COLS 20 #define MAX_INPUT 1000 } The #define directives performs global replacements : } every instance of MAX_COLS is replaced with 20, and every instance of MAX_INPUT is replaced with

8 The main() Function } main() is always the first function called in a program execution. int main( void ) { } void indicates that the function takes no arguments } int indicates that the function returns an integer value } Through returning some values, the program can indicate the state of termination (success vs failure) } Operating system can react accordingly 8

9 The printf() Function printf( "Original input : %s\n", input ); } printf() is a library function declared in <stdio.h> } Syntax: printf( FormatString, Expr, Expr...) 9 } FormatString: String of text to print } Exprs: Values to print } FormatString has placeholders to show where to put the values (note: #placeholders should match #Exprs) } Placeholders: %s (print as string), %c (print as char), Make sure you pick the right one! %d (print as integer), %f (print as floating-point) } \n indicates a newline character Text line printed only when \n encountered Don t forget \n when printing final results

10 return vs. exit } The return statement in main(): return EXIT_SUCCESS; } EXIT_SUCCESS is a constant defined in stdlib } Returning this value means successful termination. puts( Last column number is not paired. ); exit( EXIT_FAILURE ); } EXIT_FAILURE is another constant, signifying that something wrong happened requiring termination } exit differs from return in that execution terminates immediately control is not passed back to the calling function main() 10

11 Definitions } The datatype of an object in memory determines the set of values it may have and what operations supports } C is a weakly typed language. It allows implicit conversions as well as forced (potentially dangerous) casting } Operators specify how an object can be manipulated and can be: } Unary(e.g., -,++), Binary (e.g., +,-,*,/), Ternary (?:) 11

12 Definitions } An expression in a programming language is a combination of values, variables, operators and functions } Changes state, print value, etc. } A variable is as named link/reference to a value stored in the system s memory } Consider: int x=0, y=0; y=x+2; } x, y are variables } y=x+2 is an expression } + is an operator 12

13 Four Basic Data Types } Integer: char, short int, int, long int, enum } Floating-point: float, double, long double } Pointer } Aggregate: arrays, struct, union } Integer and floating-point types are atomic, but pointers and aggregate types combine with other types 13

14 Characters } From a C perspective, a character is indistinguishable from its numeric ASCII value } The only difference is the way it s displayed } Ex: converting a character digit to its numeric value } The value of '2' is not 2 it s 50 } To convert, subtract the ASCII value of '0' (which is 48) char digit, digit_num_value;... digit_num_value = digit - '0'; Behaviorally, this is identical to digit - 48 Why is digit - '0' preferable? 14

15 Integers used as Boolean } There is no Boolean type } Relational operators (==, <, etc.) return either 0 or 1 } Boolean operators (&&,, etc.) return either 0 or 1, and take any int values as operands } How to interpret an arbitrary int as a Boolean value: } 0 false } Any other value true 15

16 Numeric Data Types } Depending on the precision and range required, several datatypes can be used: } short int: 16 bits } int: 32 bits } float: 32 bits (base + exponent) } double: 64 bits } long double: 128 bits } The sizeof() function returns the number of bytes in a data type } printf("size of char... = %d byte(s)\n", sizeof(char)); one of the following datatypes. signed unsigned short short int x;short y; unsigned short x;unsigned short int y; default int x; unsigned int x; long long x; unsigned long x; float float x; N/A double double x; N/A char char x; signed char x; unsigned char x; 16

17 Ranges of Int and Char Types Type Min value Max value char 0 UCHAR_MAX ( 127) signed char SCHAR_MIN ( -127) SCHAR_MAX ( 127) unsigned char 0 UCHAR_MAX ( 255) short int SHRT_MIN ( ) SHRT_MAX ( 32767) unsigned short int 0 USHRT_MAX ( 65535) int INT_MIN ( ) INT_MAX ( 32767) unsigned int 0 INT_MAX ( 65535) long int LONG_MIN ( ) LONG_MAX ( ) unsigned long int 0 ULONG_MAX ( ) 17

18 Declaring Variables } Must declare variables before use } Should have a datatype } Can assume different values } Variable declaration (type followed by name): } int n; } float phi; } Uninitialized, a variable assumes a default value } Variables initialized via assignment operator: } n = 10; } Can also be initialized at declaration: } float phi = ; } Can declare/initialize multiple variables at once: } int a, b, c = 0, d = 4; 18

19 Variables } A value can be assigned to a variable } Example: } a = 123 b = a + 1 } The datatype of the variable (left) should be the same as the datatype of the constant (right) } Wrong: } 123 = a } a + 2 = 123 } They are not equations!!!!! 19

20 Which Names to Use? } Variable names can contain letters, digits and _ } Variable names should start with letters } Keywords (e.g., for, while etc.) cannot be used as variable names } Variable names are case sensitive } int x; int X declares two different variables 20

21 Characters } Constant values: } a \n \t } Variable declaration: } char c; } char c1, c2; } Represents only one char: } c= c ; } a = c; } American Standard Code for Information Interchange (ASCII) } 7 bits used for internal representation } Each character has a value. Some examples: } ' ' space, '\n' newline, '\t' tab, '\b' bell, '\0' null terminator 21

22 Constants } Constants are literal/fixed values assigned to variables or used directly in expressions directly in expressions. Datatype example meaning int i =3; integer long l=3; long integer integer unsigned long ul= 3UL; unsigned long int i =0xA; hexadecimal int i =012; octal number float pi= float floating point float pi=3.141f float double pi= l double 22 Datatype example meaning character string enumeration A \x41 \0101 "hello world" "hello""world" enum BOOL {NO,YES} enum COLOR {R=1,G,B,Y=10} character specified in hex specified in octal string literal same as "hello world" NO=0,YES=1 G=2,B=3

23 Operators operator meaning examples / division modulus % (remainder) float x=3/2; / produces x=1 (int /) / float x=3.0/2 / produces x=1.5 (float /) / int x=3.0/2; / produces x=1 (int conversion) / int x=3%2; / produces x=1 / int y=7;int x=y%4; / produces 3 / int y=7;int x=y%10; / produces 7 / operator meaning examples + addition x=3+2; / constants / y+z; / variables / x+y+2; / both / - subtraction * multiplication 3 2; / constants / int x=y z; / variables / y 2 z; / both / int x=3 2; / constants / int x=y z; / variables / x y 2; / both / 23

24 Relational Operators } Relational operators compare two operands to produce a boolean result } Any non-zero value (1 by convention) is considered to be true and 0 is considered to be false } Only variables of the same datatype should be compared considered to be true and 0 is considered to be false. operator meaning examples > greater than >= greater than or equal to < lesser than <= lesser than or equal to 3>2; / evaluates to 1 / 2.99>3 / evaluates to 0 / 3>=3; / evaluates to 1 / 2.99>=3 / evaluates to 0 / 3<3; / evaluates to 0 / A < B / evaluates to 1 / 3<=3; / evaluates to 1 / 3.99<3 / evaluates to 0 / 24

25 Relational Operators } Testing equality is one of the most commonly used relational operator } The "==" equality operator is different from the "= assignment operator } The "==" equality operator on float variables is tricky because of finite precision quality is one of the most commonly used relational operator meaning examples == equal to 3==3; / evaluates to 1 / A == a / evaluates to 0 /!= not equal to 3!=3; / evaluates to 0 / 2.99!=3 / evaluates to 1 / 25

26 Example: Relational Operators } int a, b, c; } a = 10; b = 40; } c = (a > b); /* c <- 0 (false) */ } c = (a!= b); /* c <- 1 (true)*/ } c = (a <= b); /* c <- 1 (true)*/ 26

27 Logical Operators } Allows to concatenate several expressions } Unary Not:!(a < b) } Binary AND: (a < 10) && (a > 0) } Binary OR: (a >= 10) (a <=0) } The evaluation of an expression is discontinued if the value of a conditional expression can be determined early operator meaning examples && AND OR ((9/3)==3) && (2 3==6); / evaluates to 1 / ( A == a ) && (3==3) / evaluates to 0 / 2==3 A == A ; / evaluates to 1 / 2.99>=3 0 / evaluates to 0 /!(3==3); / evaluates to 0 /! NOT!(2.99>=3) / evaluates to 1 / Short circuit: The evaluation of an expression is discontinued 27

28 Increment and Decrement Operators } Increment and decrement are common arithmetic operations: } x++ is the same as x=x+1 } x-- is the same as x=x-1 } ++x is the same as x=x+1 } --x is the same as x=x-1 } y=x++ is the same as y=x; x=x+1; } x is evaluated before it is incremented. } y=x-- is the same as y=x; x=x-1; } x is evaluated before it is decremented } y=++x is the same as x=x+1;y=x; } x is evaluated after it is incremented. } y=--x is the same as x=x 1;y=x; } x is evaluated after it is decremented 28

29 Bitwise Operators } Operations can be performed on a bit level using bitwise operators } Make the operation on strings of eight bits (bytes) each time. 29 operator meaning examples & AND OR ˆ XOR «left shift» right shift Notes: 0x77 & 0x3; / evaluates to 0x3 / 0x77 & 0x0; / evaluates to 0 / 0x700 0x33; / evaluates to 0x733 / 0x070 0 / evaluates to 0x070 / 0x770 ^ 0x773; / evaluates to 0x3 / 0x33 ^ 0x33; / evaluates to 0 / 0x01<<4; / evaluates to 0x10 / 1<<2; / evaluates to 4 / 0x010>>4; / evaluates to 0x01 / 4>>1 / evaluates to 2 /

30 Bitwise Operators } Left shift: value << n } Discard the n leftmost bits, and add n zeroes to the right } Right shift: value >> n } Discard the n rightmost bits, and add n zeroes to the left } Binary operators &,, ^ } Perform bitwise and, or, xor on each bit of the operands & } Unary operator ~ ^ } Perform one s complement of the operand: change each 0 to a 1 and vice versa 30

31 Example: Binary Operators a: (0x2E) b: (0x5B) ~a: (0xD1) ~b: (0xA4) a&b a b a^b (0x0A) (0x7F) (0x75) INT_MAX INT_MAX >>

32 Assignments Operators } Another common expression found while programming in C is: var = var (op) expr } x=x+2 } x=x 20 } x=x/4 } C provides compact assignment operators that may also be used: } x+=1 / is the same as x=x+1 / } x-=1 / is the same as x=x-1 / } x =10 / is the same as x=x 10 / } x/=2 / is the same as x=x/2 */ } x%=2 / is the same as x=x%2 */ 32

33 Assignment Operators Operator Description Example = += -= *= /= %= Simple assignment operator, Assigns values from right side operands to left side operand Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand C = A + B; will assign value of A + B into C; C += A; is equivalent to C = C + A; C -= A; is equivalent to C = C A; C *= A; is equivalent to C = C * A; C /= A; is equivalent to C = C / A; C %= A; is equivalent to C = C % A;

34 Conditional Expressions } A common pattern in many programming languages is the following: } if (cond) x=<express_a>; } else } x=<express_b >; } C allows to express the same using the ternary operator?: } sign = x>=0?1:-1; } if (x>=0) } sign=1 } else } sign=-1 34

35 Casting: Converting One Type to Another } The compiler does a certain amount of type conversion: int a = A ; /* char literal converted to int */ } In some circumstances, you need to explicitly cast an expression as a different type by putting the desired type name in parentheses before the expression } e.g. (int) will return the int value 3 35

36 Type Conversions } When variables are converted for types with higher precision, data is preserved } int i; } float f ; } f=i ; / i is promoted to float, f=(float)i / } Another conversion done automatically is char int. } isupper=(c>= A && c<= Z )?1:0; / c and literal constants are converted to int / } if (! isupper) } c=c- a + A ; / subtraction is possible because of integer conversion / } As a rule (with exceptions), the compiler promotes each term in an binary expression to the highest precision operand 36

37 Type Conversions } Operators with different type } Conversion rules according to the datatypes Operator 1 Operator 2 Conversion Result int int none int int float op1 int float float float int op2 int float float float float none float 37

38 Precedence, associativity, evaluation order } Precedence: given two operators of different types, which is evaluated first? } Associativity: given a sequence of instances of the same operator, are they evaluated left-to-right or right-to-left? } Evaluation order: given an operator with a number of operands, in what order are the operands evaluated? 38

39 Precedence and Order of Evaluation } Some precedences: } ++,--,(cast),sizeof have the highest priority } *,/,% have higher priority than +, } ==,!= have higher priority than &&, } assignment operators have very low priority } Use () generously to avoid ambiguities or side effects associated with precendence of operators. } y=x 3+2 / same as y=(x 3)+2 / } x!=0 && y==0 / same as (x!=0) && (y==0) / } d= c>= 0 && c<= 9 / same as d=(c>= 0 ) && (c<= 9 ) / 39

40 Precedences 1! + - (unary operators) Highest 2 * / % (sum and subtraction) 4 < > <= >= 5 ==!= 6 && 7 8 = (assignment) Lowest 40

41 Example: Expression evaluation a * b + c * d + e * f } * has precedence over + } So, multiplications are performed before additions } This leaves us with a sequence of two additions } + has left-to-right associativity, so do the leftmost addition first } Note: the multiplications can be done in any order } * has left-to-right associativity, but it doesn t matter much 41

42 Example: Precedences } Evaluation from left to right } * 4 % 6-1 } 3 + ((10 * 4) % 6) - 1 } 3 + ( 40 % 6) - 1 } } 6 42

43 Another Example: Precedence } int a, b, c, d, e, f, g; } a =1; b = 4; c=3; d= 1; e=10; f=8; } g =f = a*b+c>=c &&d<b == a!e; } g = (f=(((((a*b)+c)>=c) && } ((d<b) == a)) } (!e))) 43

44 Review: Writing on the Screen } int printf ( const char * format,... ); } Receives a string as a parameter } Returns the number of written characters } Takes as input a variable number of arguments } String contains placeholders } Placeholders are changed with the values of the variables } Examples: } printf( the result is: %d\n, x+12); } printf( the values are: %f and %f\n, x1, x2); 44

45 Review: Printf Placeholders } The format specification has the following components } %[flags ][ width ][. precision ][ length]<type> Type Meaning Example d,i integer printf( %d,10); /* prints 10 */ x,x integer(hex) printf( %c,10); /* prints 0xa */ u unsigned integer printf( %u,10); /* prints 10 */ c character printf( %c, A ); /* prints A */ s string printf( %s, hello ); /* prints hello */ f float (decimal) printf( %f, 2.3); /* prints 2.3 */ e,e float (scientific) printf( %e, 392.0); /* prints 3.92e+2 */ o unsigned octal printf( %o, 100); /* prints 144 */ % Literal % printf( %% ); /* prints % */ 45

46 Review: Printf Placeholders } %[flags ][ width ][. precision ][ length]<type> format output printf ("%d",10) "10" printf ("%4d",10) printf ("%s","hello") printf ("%7s","hello") bb10 (b:space) hello bbhello modifier h l L meaning interpreted as short. Use with i,d,o,u,x interpreted as long. Use with i,d,o,u,x interpreted as double. Use with e,f,g format output printf ("%.2f,%.0f,1.141,1.141) 1.14,1 printf ("%.2e,%.0e,1.141,100.00) printf ("%.4s","hello") printf ("%.1s","hello") 1.14e+00,1e+02 hell h 46

47 To Review } Marques de Sá } Capítulo 2 } Damas } Capítulo 1 e 2 } Kernighan and Ritchie } Chapter 2 47

Fundamental of Programming (C)

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

More information

Fundamentals of Programming

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

More information

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C C expressions (Reek, Ch. 5) 1 Shift operations Left shift: value > n Two definitions: logical version: discard the n

More information

Reserved Words and Identifiers

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

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

UNIT- 3 Introduction to C++

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

More information

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

More information

Computer System and programming in C

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

More information

Types, Operators and Expressions

Types, Operators and Expressions Types, Operators and Expressions EECS 2031 18 September 2017 1 Variable Names (2.1) l Combinations of letters, numbers, and underscore character ( _ ) that do not start with a number; are not a keyword.

More information

Types, Operators and Expressions

Types, Operators and Expressions Types, Operators and Expressions CSE 2031 Fall 2011 9/11/2011 5:24 PM 1 Variable Names (2.1) Combinations of letters, numbers, and underscore character ( _ ) that do not start with a number; are not a

More information

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

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.

More information

File Handling in C. EECS 2031 Fall October 27, 2014

File Handling in C. EECS 2031 Fall October 27, 2014 File Handling in C EECS 2031 Fall 2014 October 27, 2014 1 Reading from and writing to files in C l stdio.h contains several functions that allow us to read from and write to files l Their names typically

More information

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

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,

More information

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

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

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

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

CS107, Lecture 3 Bits and Bytes; Bitwise Operators CS107, Lecture 3 Bits and Bytes; Bitwise Operators reading: Bryant & O Hallaron, Ch. 2.1 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution

More information

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

More information

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

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

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

More information

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1 Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1 Topics 1. Expressions 2. Operator precedence 3. Shorthand operators 4. Data/Type Conversion 1/15/19 CSE 1321 MODULE 2 2 Expressions

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

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

More information

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

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

More information

Number Systems, Scalar Types, and Input and Output

Number Systems, Scalar Types, and Input and Output Number Systems, Scalar Types, and Input and Output Outline: Binary, Octal, Hexadecimal, and Decimal Numbers Character Set Comments Declaration Data Types and Constants Integral Data Types Floating-Point

More information

Expression and Operator

Expression and Operator Expression and Operator Examples: Two types: Expressions and Operators 3 + 5; x; x=0; x=x+1; printf("%d",x); Function calls The expressions formed by data and operators An expression in C usually has a

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

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

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

The Arithmetic Operators

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

More information

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

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

More information

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

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.

More information

Chapter 3: Operators, Expressions and Type Conversion

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

More information

Computers Programming Course 5. Iulian Năstac

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

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

A Java program contains at least one class definition.

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

More information

Informatics Ingeniería en Electrónica y Automática Industrial

Informatics Ingeniería en Electrónica y Automática Industrial Informatics Ingeniería en Electrónica y Automática Industrial Operators and expressions in C Operators and expressions in C Numerical expressions and operators Arithmetical operators Relational and logical

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

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

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

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

More information

Chapter 2: Basic Elements of C++

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

More information

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

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

More information

Lecture 3. More About C

Lecture 3. More About C Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-1 Lecture 3. More About C Programming languages have their lingo Programming language Types are categories of values int, float, char Constants

More information

A First Program - Greeting.cpp

A First Program - Greeting.cpp C++ Basics A First Program - Greeting.cpp Preprocessor directives Function named main() indicates start of program // Program: Display greetings #include using namespace std; int main() { cout

More information

Arithmetic and Bitwise Operations on Binary Data

Arithmetic and Bitwise Operations on Binary Data Arithmetic and Bitwise Operations on Binary Data CSCI 2400: Computer Architecture ECE 3217: Computer Architecture and Organization Instructor: David Ferry Slides adapted from Bryant & O Hallaron s slides

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

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

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

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

More information

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

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.

More information

COMP2121: Microprocessors and Interfacing. Number Systems

COMP2121: Microprocessors and Interfacing. Number Systems COMP2121: Microprocessors and Interfacing Number Systems http://www.cse.unsw.edu.au/~cs2121 Lecturer: Hui Wu Session 2, 2017 1 1 Overview Positional notation Decimal, hexadecimal, octal and binary Converting

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

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

Java Notes. 10th ICSE. Saravanan Ganesh

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

More information

Lecture 02 C FUNDAMENTALS

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

More information

High Performance Computing in C and C++

High Performance Computing in C and C++ High Performance Computing in C and C++ Rita Borgo Computer Science Department, Swansea University Summary Introduction to C Writing a simple C program Compiling a simple C program Running a simple C program

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

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

DEPARTMENT OF MATHS, MJ COLLEGE

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,

More information

Programming and Data Structures

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()

More information

A Fast Review of C Essentials Part I

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

More information

JAVA OPERATORS GENERAL

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

More information

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html UQC146S1 Introductory Image Processing in C Ian Johnson Room 3P16 Telephone: extension 3167 Email: Ian.Johnson@uwe.ac.uk http://www.csm.uwe.ac.uk/ ~irjohnson/uqc146s1.html Ian Johnson 1 UQC146S1 What is

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

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

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

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

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

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

More information

Visual C# Instructor s Manual Table of Contents

Visual C# Instructor s Manual Table of Contents 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

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 2. Overview of C++ Prof. Amr Goneid, AUC 1 Overview of C++ Prof. Amr Goneid, AUC 2 Overview of C++ Historical C++ Basics Some Library

More information

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

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

More information

CSC 1107: Structured Programming

CSC 1107: Structured Programming CSC 1107: Structured Programming J. Kizito Makerere University e-mail: www: materials: e-learning environment: office: alt. office: jkizito@cis.mak.ac.ug http://serval.ug/~jona http://serval.ug/~jona/materials/csc1107

More information

Unit 3. Operators. School of Science and Technology INTRODUCTION

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.

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a

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

Types, Variables, and Constants

Types, Variables, and Constants , Variables, and Constants What is a Type The space in which a value is defined Space All possible allowed values All defined operations Integer Space whole numbers +, -, x No divide 2 tj Why Types No

More information

Language Reference Manual simplicity

Language Reference Manual simplicity Language Reference Manual simplicity Course: COMS S4115 Professor: Dr. Stephen Edwards TA: Graham Gobieski Date: July 20, 2016 Group members Rui Gu rg2970 Adam Hadar anh2130 Zachary Moffitt znm2104 Suzanna

More information

ME 461 C review Session Fall 2009 S. Keres

ME 461 C review Session Fall 2009 S. Keres ME 461 C review Session Fall 2009 S. Keres DISCLAIMER: These notes are in no way intended to be a complete reference for the C programming material you will need for the class. They are intended to help

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

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

More information

Engineering Computing I

Engineering Computing I Engineering Computing I Types, Operators, and Expressions Types Operators Expressions 2 1 2.1 Variable Names Names are made up of letters and digits The first character must be a letter The underscore

More information

Chapter 7. Basic Types

Chapter 7. Basic Types Chapter 7 Basic Types Dr. D. J. Jackson Lecture 7-1 Basic Types C s basic (built-in) types: Integer types, including long integers, short integers, and unsigned integers Floating types (float, double,

More information

1.3b Type Conversion

1.3b Type Conversion 1.3b Type Conversion Type Conversion When we write expressions involved data that involves two different data types, such as multiplying an integer and floating point number, we need to perform a type

More information

Fundamental of C programming. - Ompal Singh

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

More information

CS107, Lecture 2 Bits and Bytes; Integer Representations

CS107, Lecture 2 Bits and Bytes; Integer Representations CS107, Lecture 2 Bits and Bytes; Integer Representations reading: Bryant & O Hallaron, Ch. 2.2-2.3 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons

More information

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

More information

Computers Programming Course 6. Iulian Năstac

Computers Programming Course 6. Iulian Năstac Computers Programming Course 6 Iulian Năstac Recap from previous course Data types four basic arithmetic type specifiers: char int float double void optional specifiers: signed, unsigned short long 2 Recap

More information

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language 1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC

More information

COMP327 Mobile Computing Session: Tutorial 2 - Operators and Control Flow

COMP327 Mobile Computing Session: Tutorial 2 - Operators and Control Flow COMP327 Mobile Computing Session: 2010-2011 Tutorial 2 - Operators and Control Flow 1 In these Tutorial Slides... To cover in more depth most of the aspects of C, including: Types, Operands, and Expressions

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

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

Introduction to C Language

Introduction to C Language Introduction to C Language Instructor: Professor I. Charles Ume ME 6405 Introduction to Mechatronics Fall 2006 Instructor: Professor Charles Ume Introduction to C Language History of C Language In 1972,

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

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

Hardware: Logical View

Hardware: Logical View Hardware: Logical View CPU Memory Bus Disks Net USB Etc. 1 Hardware: Physical View USB I/O controller Storage connections CPU Memory 2 Hardware: 351 View (version 0) instructions? Memory CPU data CPU executes

More information

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C Mechatronics and Microcontrollers Szilárd Aradi PhD Refresh of C About the C programming language The C programming language is developed by Dennis M Ritchie in the beginning of the 70s One of the most

More information

The C language. Introductory course #1

The C language. Introductory course #1 The C language Introductory course #1 History of C Born at AT&T Bell Laboratory of USA in 1972. Written by Dennis Ritchie C language was created for designing the UNIX operating system Quickly adopted

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

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

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

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

More information