Computer Programming 3 th Week Variables, constant, and expressions

Similar documents
Assoc. Prof. Dr. Tansu FİLİK

Computer System and programming in C

C: How to Program. Week /Mar/05

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

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

Programming and Data Structures

Reserved Words and Identifiers

Chapter 2 - Introduction to C Programming

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

Lecture 3. More About C

CS102: Variables and Expressions

Fundamentals of Programming

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

Assoc. Prof. Dr. Tansu FİLİK

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

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

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Chapter 1 & 2 Introduction to C Language

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

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

BSM540 Basics of C Language

Programming. Elementary Concepts

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:

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

A Fast Review of C Essentials Part I

Fundamental of Programming (C)

CSC 1107: Structured Programming

A First Program - Greeting.cpp

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

Lecture 02 C FUNDAMENTALS

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Number Systems, Scalar Types, and Input and Output

Types, Operators and Expressions

ANSI C Programming Simple Programs

BSM540 Basics of C Language

Data types, variables, constants

C Programming

Work relative to other classes

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

UNIT- 3 Introduction to C++

Introduction to C. Systems Programming Concepts

Basic Types, Variables, Literals, Constants

BASIC ELEMENTS OF A COMPUTER PROGRAM

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Types, Operators and Expressions

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

Stream Model of I/O. Basic I/O in C

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

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

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

Chapter 2: Basic Elements of C++

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

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

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

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Computers Programming Course 5. Iulian Năstac

Fundamental of C programming. - Ompal Singh

Input/Output Week 5:Lesson 16.1

Chapter 2: Using Data

Assoc. Prof. Dr. Tansu FİLİK

Memory, Data, & Addressing II CSE 351 Spring

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

Creating a C++ Program

CS313D: ADVANCED PROGRAMMING LANGUAGE

2. Numbers In, Numbers Out

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

3. Types of Algorithmic and Program Instructions

Java Notes. 10th ICSE. Saravanan Ganesh

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

Lesson 3 Introduction to Programming in C

BLM2031 Structured Programming. Zeyneb KURT

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

Conditional Statement

Getting started with Java

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from

Unit 4. Input/Output Functions

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

Fundamentals of C Programming

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

Introduction to Programming Using Java (98-388)

3. Java - Language Constructs I

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Programming for Engineers Introduction to C

C Language, Token, Keywords, Constant, variable

printf( Please enter another number: ); scanf( %d, &num2);

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Assoc. Prof. Dr. Tansu FİLİK

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

Basic Elements of C. Staff Incharge: S.Sasirekha

Chapter 2 Elementary Programming

Basic Types and Formatted I/O

Chapter 2: Using Data

Basic Assignment and Arithmetic Operators

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

2. Numbers In, Numbers Out

Lexical Considerations

Programming with Java

Programming for Engineers Arrays

Transcription:

3 th Week Variables, constant, and expressions Hazırlayan Asst. Prof. Dr. Tansu Filik

Previously on Bil 200 Unitary operations (applied to single variable, +.-,++,--) Type casting Operation/Operator precedence Comments/Documentation Formatted writing/output

Formats ( printf & scanf ) int b = 756; printf( %d, b); printf ( %9d\n, b);

Formats (int)

Formatted writing, Example - printf int a, b; float c, d; a = 17; b = a / 2; printf( %d\n, b); printf( %3d\n, b); printf( %03d\n, b); c = 21.3; d = c / 3; printf("%4.2f\n", d);

Formatted writing, Example - printf %w.pf The w sets the maximum width of the entire number, including the decimal place. The p sets precision. For example: printf("%9.2f",12.456); This statement outputs four spaces and then 12.45. Those four spaces plus 12.45 (five characters total) equal the 9 in the width. Only two values are shown to the right of the decimal because.2 is used in the %f conversion character. ----12.45

Formatted input scanf ( ) ; The function is used for reading values of variables whose types are specified: scanf ( format", &var1, &var2, ) ; format" indicates the types of variables whose values are entered. We use & symbol before the variable names. This & sign indicates the memory address of the variable.

Formatted input Examples float a; int b; scanf("%d %f", &b, &a); float a; long int b; scanf("%4ld %4.2f", &b, &a);

Inputting from console int num; scanf( %d,&num); The entered number from the keyboard is stored in a physical address (of two bytes) allocated for num int a, b; scanf( %d %d, &a, &b);

Inputting from console In scanf(), the parameters are always addresses. For numeric variables, you put & before the variable name to make it an address &num &num 2 byte Reading from keyboard (Assume 10 is entered) 0 A

Components of a C program Types, Variables, Keywords, Assignments, Constants

Components of a C program: Types C language has 4 basic types: char, int, float, double These types may be extended by type variations: long, short, const Users may define custom types as arrays and structures. What is the type of string? Actually strings are arrays of characters.

Components of a C program: Types Char Characters are stored as small integer numbers. Each character has a corresponding numerical value. These are known as ASCII codes. For example, the numerical value of character A is 65. ASCII: American Standard Code for Information Interchange

Components of a C program: Types Char ASCII values are between 0 and 127. Value 0 is special, and indicated as'\0' or NULL. Value 127 corresponds to DEL There are several other non printable or printable special characters: '\n' '\t' '\'' '\\' etc. Characters with values over 127 are known as extended character set. They are not standard, and may vary according to the platform.

ASCII codes

Components of a C program: Types Char - Example Numerical value can be assigned to characters: char ch; ch = 65; Letters can also be assigned to characters. Use single quotes for letters. The following lines do exactly the same as the above line codes. char ch; ch = 'A';

Components of a C program: Types char 1 bytes -128 to 127 unsigned char 1 bytes 0 to 255 short int 2 bytes -32768 to 32767 unsigned short int 2 bytes 0 to 65535 int 4 bytes -2147483648 to 2147483647 unsigned int 4 bytes 0 to 4294967295 long int 4 bytes -2147483648 to 2147483647 unsigned long int 4 bytes 0 to 4294967295 float 4 bytes 1.175494e-38 to 3.402823e+38 double 8 bytes 2.225074e-308 to 1.797693e+308 precision of float 6 digits precision of double 15 digits

Numerical Values 0, 1, 2, -100 Integer 0.0, 1e25, -0.01e-12 Floating point 0x10, 0xca, 0xdeadbeef 16-hex 010, 036, -0100 8-octal 123456789ul unsigned long int 'A', ' ', '&', '\'' Character "A", "Buffy", "@#*!" String '\101', '\x41' oct/hex 'A' "\101\x42" AB '\013', '\xb' BELL - beep '\007', '\x7' VTAB vert. tab

Components of a C program: Types Examples int i; float sonuc; int i = 0; char ilk = 'K'; float a = 0.0; double db32 = 1e1; long int li; long float son; short int si = 0; unsigned char ilk = 'K'; long double ldb = 1e-3;

Components of a C program: Keywords int i; float sonuc; int i = 0; char ilk = 'K'; float a = 0.0; double db32 = 1e1; ------------------------------------------------------------------------------------------------------ long int li; long float son; short int si = 0; unsigned char ilk = 'K'; long double ldb = 1e-3;

Components of a C program: Expressions Character lists that consist of letters numbers, and _ (underscore). Never start with numbers. Must be different than keywords. Case sensitive. No Turkish specific letters! Examples merhaba, x1, y2, _benim_sayi_

Components of a C program: Assignments Gives a value to a variable. Assignment operator is =

Examples float x = 2.5 ; Components of a C program: Assignments char ch ; int sayi ; ch = '\n' ; sayi = 4 + 5 ; /* now the value is 9 */ sayi = sayi * 2; /* now the value is 18. */

Components of a C program: Assignments Examples: #include <stdio.h> /* assignments.*/ int main() { int tamsayi; char karakter; float kayarnokta; tamsayi = 33; karakter = 33; kayarnokta = 33; tamsayi = 'A'; karakter = 'A'; kayarnokta = 'A'; tamsayi = 33.33; karakter = 33.33; kayarnokta = 33.33; tamsayi = kayarnokta; kayarnokta = tamsayi; return 0; }

Components of a C program: Assignments Examples: int tamsayi; char karakter; float kayarnokta; float ayarnokta; printf("tamsayi degeri %d \n", tamsayi); printf("karakter degeri %c \n", karakter); printf("kayarnokta degeri %f \n",kayarnokta); printf("ayarnokta degeri %f \n", ayarnokta); tamsayi = 33; karakter = 33; kayarnokta = 33;

Components of a C program: Assignments Examples: tamsayi degeri 33 karakter degeri! kayarnokta degeri 33.000000 ayarnokta degeri -107374176.000000

C Programming Language: Example Print "Dereceyi Gir:" Read derece #include <stdio.h> /*Convert degrees to radians */ const float PI = 3.1415926; int main() { float derece; float radyan; Write explanatory comments in your program Global variable Local variables Variable declaration: must defined before using radyan = PI / 180 * derece Print radyan printf("dereceyi gir:"); scanf("%f", &derece); radyan = PI/180*derece; printf("%f\n", radyan); return 0; } Formatted printing is done by printf(). It is defined inside stdio.h

Operators Logical and arithmetic operators

Operators boolean Boolean is a type that may have True or False values. They are used for determining a logical situation. Specifically useful for conditional executions. Logical expressions are formulas that may evaluate to True or False.

Operators Boolean in C The boolean type in C is int. The value 0 corresponds to False. All other integer values correspond to True.

Operators Example #include <stdio.h> int main() { int neyse = 0; } if (neyse) { printf( Doğru mu?\n ); } else { printf( Hayır değil.\n ); } return 0;

Operators Boolean expressions These expressions result in 0 or 1. In this way, we control their truth. If the expression is logically true, its value is 1, else its value is 0.

Operators Boolean operators These are used to construct boolean expressions. They are also called logical operators. And && Or Not! Equals == Not equals!= Relational <, >, <=, >=

Operators Boolean operators - And All values must be different than 0. 1 && 2 1 && 0 && -1

Operators Boolean operators - Or At least one value must be 1. 1 2 1 0-1

Operators Boolean operators - Not Turns true into false, and false into true.!1!0

Operators Boolean operators - Equals Returns true, if both sides are the same. 1 == 2 1 == 0 42 == 42 0 == 0

Operators Boolean operators - Not equals Returns true if both sides are different. 1!= 2 1!= 0 42!= 42

Operators Boolean operators - Relational If the inequality is logically true, returns 1. 1 < 2 0 > 1 42 <= 42 yas >= 18

Highest Lowest Operator precedence Parentheses Not (!) Relational (<, >, <=, >=) Equals (==) Not equals (!=) And (&&) Or ( ) Operators

Common Mistakes The most common mistake is the careless construction of expressions without considering operation precedences. Another common mistake is writing a single (=) instead of a (==). In this case, the compiler does not give errors, but the assignment will almost always be TRUE (if the assigned value is different than 0).

Common Mistakes, Example (mistake) #include <stdio.h> int main() { int not; (correct) #include <stdio.h> int main() { int not; scanf("%d", &not); scanf("%d", &not); } if (not = 48 not = 49) { printf("çok yakın!\n"); } return 0; } if (not == 48 not == 49) { printf("çok yakın!\n"); } return 0;

Common Mistakes, Example (mistake) #include <stdio.h> int main() { int not; (correct) #include <stdio.h> int main() { int not; scanf("%d", &not); scanf("%d", &not); } if ( 0 < not < 48 ) { } printf("yanlis\n"); return 0; } if ( 0 < not && not < 48 ) { } printf("yanlis\n"); return 0;

Control structures if-else mechanism

Control structures: if-else mechanism Single selection Structure is: if ( expression ) { commands; } or if ( expression ) command;

Example #include <stdio.h> Control structures: if-else mechanism void main ( ) { int a = 1, b = 2, c ; } if (a > b) { } else { } c = a; c = b;

if else if else Control structures: if-else mechanism The condition structure selects a group of commands to be executed according to the logical value of a boolean statement. The logical statements return true or false values. If the statement is TRUE, the if part of the commands are executed. Otherwise, the else part gets executed.

if else if else Control structures: if-else mechanism A simple if statement is called a singular selection. It either works a command or not, according to whether the logical expression is true or not. A statement with both if and else is a double selection. It executes a separate list of commands when the logical statement is false. Nested lists of if/else statements may control more than two situations.

if else if else Control structures: if-else mechanism if has a general form: if (condition1){ } statement ; else if (condition2) { statement ; } else if (condition3) { } else { } statement ; statemens ;