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

Similar documents
Fundamentals of Programming

Fundamental of Programming (C)

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

Basic Types and Formatted I/O

Chapter 4: Basic C Operators

Work relative to other classes

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

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

THE FUNDAMENTAL DATA TYPES

ANSI C Programming Simple Programs

Computer System and programming in C

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Data Types and Variables in C language

UNIT- 3 Introduction to C++

CSC 1107: Structured Programming

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

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Java Notes. 10th ICSE. Saravanan Ganesh

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

Basic Elements of C. Staff Incharge: S.Sasirekha

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

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

Programming and Data Structures

Fundamental of C programming. - Ompal Singh

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

Introduction to C Programming

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

CHAPTER 3 Expressions, Functions, Output

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

Basics of Programming

Programming for Engineers Introduction to C

Programming in C++ 4. The lexical basis of C++

Programming. Elementary Concepts

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

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

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

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

Programming in C++ 6. Floating point data types

INTRODUCTION 1 AND REVIEW

C: How to Program. Week /Mar/05

Chapter 3. Fundamental Data Types

CS102: Variables and Expressions

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

First of all, it is a variable, just like other variables you studied

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

C Language, Token, Keywords, Constant, variable

CSc Introduction to Computing

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Number Systems, Scalar Types, and Input and Output

Chapter 2 - Introduction to C Programming

ARG! Language Reference Manual

ISA 563 : Fundamentals of Systems Programming

Introduction to C. Systems Programming Concepts

Lecture 2 Tao Wang 1

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

Chapter 7. Basic Types

Chapter 2: Introduction to C++

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

2. Numbers In, Numbers Out

6.096 Introduction to C++ January (IAP) 2009

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

Fundamentals of C. Structure of a C Program

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

C - Basic Introduction

Fundamentals of C Programming

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

Full file at

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

C Programming

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

The C++ Language. Arizona State University 1

Unit 3. Operators. School of Science and Technology INTRODUCTION

Introduction to C# Applications

Variables and Constants

CS242 COMPUTER PROGRAMMING

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

4. Inputting data or messages to a function is called passing data to the function.

Visual C# Instructor s Manual Table of Contents

Chapter 2: Basic Elements of C++

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

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

Computer Programming 3 th Week Variables, constant, and expressions

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

2. Numbers In, Numbers Out

CMPT 125: Lecture 3 Data and Expressions

Lesson 3 Introduction to Programming in C

Variables and literals

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

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:

Getting started with Java

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

BSM540 Basics of C Language

Transcription:

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 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 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 9no)

Before using a variable, you must declare it All variables must be defined with a name and a data type. 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;

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

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

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

A floating-point value contains a decimal point 33.5 0.0-657.983.2 6. For example, the value 150.4582 is represented in scientific notation as 1.504582 X 102 and is represented in exponential notation (by the computer) as 1.504582E+02 This notation indicates that 1.504582 is multiplied by 10 raised to the second power (E+02) The E stands for exponent

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

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

Type Casting In C Language. Type casting is a way to convert a variable from one data type to another data type. For example, if you want to store a long value into a simple integer then you can type cast long to int.

If an operand is long double, the other operand is converted to long double! Else if an operand is double, the other operand is converted to double! Else if an operand is float, the other operand is converted to float! Else if an operand is unsigned long, the other operand is converted to unsigned long! Else if an operand is long, the other operand is converted to long! Else if an operand is unsigned int, the other operand is converted to unsigned int!

char ch; int i; float f; double d; result= (ch/i) + (f*d) - (f+i); int double float int double float double double double

May lose the data! char ch; int i; float f; double d; ch=i; i=f; f=ch; f=i;

source destination The lost data char signed char If char>127, destination will be negative short int char 8 most significant bits int char 8 most significant bits (16 bits system) 24 most significant bits(32bits system) long int char 24 most significant bits int short int No lost data! (16 bits system) 16 most significant bits(32 bits system) long int int 16 most significant bits No lost data! (16 bits system) float int The mantissa part double float Precision lost & the data is rounded long double double Precision lost & the data is rounded

sizeof(int) sizeof(char)

#include <stdio.h> int main() { int num; int a=10,b=3; float c; c=a/b; printf("the result without casting %f\n",c); c=(float)a/b; printf("the result with casting %f\n",c); return 0; } #include <stdio.h> int main() { float a=10; int b=3; float c=a/b; printf("%f",c); printf("%d", a/b); }

This function provides for formatted output to the screen. The syntax is: printf("format", var1, var2, ) ; The format includes, some text and conversion specifications A conversion specifier begins with the % character. After the % character come the following in this order: [flags]:control the conversion(optional) [width]:the number of characters to print(optional) [.precision]:the amount of precision to print for a number type(optional) [modifier]:overrides the size (type) of the argument(optional) [type]:the type of conversion to be applied(required) Example:%[flags][width][.precision][modifier]type

These functions take input in a manner that is specified by the format argument and store each input field into the following arguments in a left to right fashion Each input field is specified in the format string with a conversion specified which specifies how the input is to be stored in the appropriate variable Other characters in the format string specify characters that must be matched from the input, but are not stored in any of the following arguments If the input does not match then the function stops scanning and returns A white space character may match with any white space character (space, tab, carriage return, newline, vertical tab, or form feed) or the next incompatible character

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?

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

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

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 4 + - left to right 5 (lowest) = += -= *= /= %= right to left

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 = 6-5 % 3; x = 6 2; x = 4;

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

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;