int main(void) { int a, b, c; /* declaration */

Similar documents
THE FUNDAMENTAL DATA TYPES

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

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

Data Type Fall 2014 Jinkyu Jeong

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

Chapter 3. Fundamental Data Types

Basic Types and Formatted I/O

Internal representation. Bitwise operators

Internal representation. Bitwise operators

Internal representation. Bitwise operators

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

DEPARTMENT OF COMPUTER ENGINEERING CMPE101: Foundation of Computer Engineering EXPERIMENT 3. Introduction to C Programming: Sequential code structure

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

Princeton University COS 217: Introduction to Programming Systems C Primitive Data Types

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Number Systems, Scalar Types, and Input and Output

Fundamentals of Programming

Fundamental of Programming (C)

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 5. Playing with Data Modifiers and Math Functions Getting Controls

Introduction to C. Systems Programming Concepts

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

CSC 1107: Structured Programming

Computer System and programming in C

Lecture 3. More About C

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Chapter 7. Basic Types

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

ECET 264 C Programming Language with Applications

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

ECET 264 C Programming Language with Applications

Princeton University Computer Science 217: Introduction to Programming Systems The C Programming Language Part 1

Pointers (2) Applications

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

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

Introduction to Programming

A Short Course for REU Students Summer Instructor: Ben Ransford

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

2/12/17. Goals of this Lecture. Historical context Princeton University Computer Science 217: Introduction to Programming Systems

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

LESSON 4. The DATA TYPE char

printf("%c\n", character); printf("%s\n", "This is a string"); printf("%s\n", string); printf("%s\n",stringptr); return 0;

Introduction to C Language

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

Lecture 02 C FUNDAMENTALS

CS1100 Introduction to Programming

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

ISA 563 : Fundamentals of Systems Programming

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

Unit 4. Input/Output Functions

Week 1 / Lecture 2 8 March 2017 NWEN 241 C Fundamentals. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

Fundamentals of Programming Session 8

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

Programming in C++ 6. Floating point data types

Types, Operators and Expressions

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Fundamental of Programming (C)

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

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Types, Operators and Expressions

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Fundamentals of Programming

CS 33. Data Representation (Part 3) CS33 Intro to Computer Systems VIII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

INTRODUCTION TO COMPUTER SCIENCE - LAB

INFO-I308 Laboratory Session #2

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

Operators and Expressions:

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

int main() { return 0; }

CS61B Lecture #14: Integers

Work relative to other classes

Input/output functions

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Approximately a Test II CPSC 206

Representing and Manipulating Floating Points. Jo, Heeseung

Principles of C and Memory Management

ANSI C Programming Simple Programs

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

Functions. Systems Programming Concepts

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

Basic Assignment and Arithmetic Operators

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

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

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

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

C Programming

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

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

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

Systems Programming and Computer Architecture ( )

Standard I/O in C and C++

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Input/output functions

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Practice Sheet #07 with Solutions

Transcription:

&KDSWHULQ$%& #include <stdio.h> int main(void) { int a, b, c; /* declaration */ float x, y=3.3, z=-7.7; /* declaration with initialization */ printf("input two integers: "); /* function call */ scanf("%d%d", &b, &c); /* function call */ a = b + c; /* assignment stmt */ x = y + z; /* assignment stmt */ return 0; Software 1, TAU - 3.1 1

Fundamental data types Integral (signed or unsigned) char int Floating point types float double fundamental data types: long form char signed char unsigned char signed short int signed int signed long int usigned short int unsigned int unsigned long int float double long double fundamental data types char signed char unsigned char short int long usigned short unsigned unsigned long float double long double fundamental data types grouped by funcionality integral types: char signed char unsigned char short int long usigned short unsigned unsigned long floating types: float double long double arithmetic types: integral types + floating types Software 1, TAU - 3.2 2

Constants char int long unsigned float double long double a, x, 0, &, \n 1, 0, -54, 4234567 0L 123l 7766L 0u 23U 3000000000u 1.2f,.2f, 1.f, 3.14159F 1.0, -3.1412 1.0l 2.4433L some character constants and their integer values character: a b c... z integer value: 97 98 99... 112 character: A B C... Z integer value: 65 66 67... 90 character: 0 1 2... 9 integer value: 48 49 50... 57 character: & * + integer value: 38 42 43 name of character written in C integer value alert \a 7 backslash \\ 92 horizontal tab \t 9 newline \n 10 null character \0 0 Quote \ 39 Software 1, TAU - 3.3 3

char c = a ; printf("%c", c); /* a is printed */ printf("%d", c); /* 97 is printed */ printf("%c%c%c", c, c+1, c+2); /* abc is printed */ Software 1, TAU - 3.4 some character constants and their integer values character: A B C... Z integer value: 65 66 67... 90 char c; int i; for (i= a ; i<= z ; ++i) printf("%c", i); /* abc...z is printed */ for (c=65; c<=90; ++c) printf("%c", c); /* ABC...Z is printed */ for (c= 0 ; c<= 9 ; ++c) printf("%d", c); /* 48 49 50... 57 is printed */ Software 1, TAU - 3.5 4

in stdio.h: #define EOF (-1) #include <stdio.h> int main(void) { int c; while ((c = getchar())!= EOF) { putchar(c); putchar(c); return 0; Software 1, TAU - 3.11 Constants Type char int double Description Single character Special character Hexadecimal code Decimal Octal Hexadecimal int.frac int.frac e[+,-]exp Example g \n \x1a -1, 2, 0, 123456, 010, -020, 080 0xfffe, 0x1000, 0x1, 0x2, 0x4, 0x8 1.0 2.1712, 1.23 123e-2, 12.3e-1 5

/* Decimal, hexadecimal, octal conversions. */ #include <stdio.h> int main(void) { printf("%d %x %o\n", 19, 19, 19); /* 19 13 23 */ printf("%d %x %o\n", 0x1c, 0x1c, 0x1c); /* 28 1c 34 */ printf("%d %x %o\n", 017, 017, 017); /* 15 f 17 */ printf("%d\n", 11 + 0x11 + 011); /* 37 */ printf("%x\n", 2097151); /* 1fffff */ printf("%d\n", 0x1FfFFf); /* 2097151 */ return 0; Software 1, TAU - 3.16 /* Compute the size of some fundamental types. */ #include <stdio.h> int main(void) { printf("the size of some fundamental types is computed.\n\n"); printf(" char:%3d byte \n", sizeof(char)); printf(" short:%3d bytes\n", sizeof(short)); printf(" int:%3d bytes\n", sizeof(int)); printf(" long:%3d bytes\n", sizeof(long)); printf(" unsigned:%3d bytes\n", sizeof(unsigned)); printf(" float:%3d bytes\n", sizeof(float)); printf(" double:%3d bytes\n", sizeof(double)); printf("long double:%3d bytes\n", sizeof(long double)); return 0; Software 1, TAU - 3.6 6

run on PC Pentium 3": The size of some fundamental types is computed. char: 1 byte short: 2 bytes int: 4 bytes long: 4 bytes unsigned: 4 bytes float: 4 bytes double: 8 bytes long double: 8 bytes Software 1, TAU - 3.7 sizeof(char) = 1 sizeof(short) <= sizeof(int) <= sizeof(long) sizeof(signed) = sizeof(unsigned) = sizeof(int) sizeof(float) <= sizeof(double) <= sizeof(long double) Software 1, TAU - 3.8 7

#include <stdio.h> int main(void) { int i; unsigned u = UINT_MAX; /* typically 4294967295 */ for (i = 0; i < 5; ++i) printf("%u + %d = %u\n", u, i, u + i); for (i = 0; i < 5; ++i) printf("%u * %d = %u\n", u, i, u * i); return 0; Software 1, TAU - 3.9 run on "scorpio": 4294967295 + 0 = 4294967295 4294967295 + 1 = 0 4294967295 + 2 = 1 4294967295 + 3 = 2 4294967295 + 4 = 3 4294967295 * 0 = 0 4294967295 * 1 = 4294967295 4294967295 * 2 = 4294967294 4294967295 * 3 = 4294967293 4294967295 * 4 = 4294967292 Software 1, TAU - 3.10 8

type representation value Binary value unsigned char unsigned char unsigned char unsigned char unsigned char signed char signed char signed char signed char Signed char signed short signed int 0 255 127 128 0-1 127-128 vvvv vvvv 0000 0000 1111 1111 0111 1111 1000 0000 svvv vvvv 0000 0000 1111 1111 0111 1111 1000 0000 svvv vvvv vvvv vvvv svvv vvvv vvvv vvvv vvvv vvvv vvvv vvvv signed char sc = -128; /* 1000 0000 */ unsigned char uc = 255; /* 1111 1111 */ sc = sc - 1; /* 0111 1111 = 127 Underflow */ uc = uc + 1; /* 0000 0000 = 0 Overflow */ uc = 255; /* 1111 1111 */ uc = uc*2; /* 1111 1110 = 254 */ 9

float representation Decimal: Binary: int.frac*10 exp 2.5*10-27 (=25*10-28) 1.frac*2 exp 1.1*2-11111010 Fixed size, limited accuracy seee eeee sfff ffff ffff ffff ffff ffff double uses 8 bytes #include <stdio.h> int main() { int i; float f; f = 0; for (i = 0; i < 100; ++i) f += 0.01f; printf("%f\n", f); return 0; Output: 0.999999 10

special float values NaN Not a Number represents an illegal value printf( %f\n, sqrt(-1)); will print -1.#IND00 or NAN INF infinity will print 1.#INF00 Mathematical Functions sqrt() pow() exp() #include <math.h> double!! log() sin() cos() tan() 11

#include <math.h> #include <stdio.h> int main(void) { double x; printf("\n%s\n%s\n%s\n\n", "The square root of x and x raised", "to the x power will be computed.", "---"); while (1) { /* do it forever */ printf("input x: "); scanf("%lf", &x); if (x >= 0.0) printf("\n%15s%22.15e\n%15s%22.15e\n%15s%22.15e\n\n", "x = ", x, "sqrt(x) = ", sqrt(x), "pow(x, x) = ", pow(x, x)); else printf("\nsorry, your number must be nonnegative.\n\n"); return 0; Software 1, TAU - 3.12 The square root of x and x raised to the x power will be computed. Input x: 2 x = 2.000000000000000e+000 sqrt(x) = 1.414213562373095e+000 pow(x, x) = 4.000000000000000e+000 Input x: Software 1, TAU - 3.13 12

The usual arithmetic conversion (promotion) If either operand is of type {long double, the other operand is converted to {long double. Otherwise, if either operand is of type {double, the other operand is converted to {double. Otherwise, if either operand is of type {float, the other operand is converted to {float. Otherwise, the "integral promotions" are performed on both operands, and the following rules are applied: If either operand is of type {unsigned long, the other operand is converted to {unsigned long. Otherwise, if one operand has type {long and the other operand has type {unsigned then one of two possibilities occurs: If a long can represent all the values of an {unsigned, then the operand of type {unsigned is converted to {long. If a long cannot represent all the values of an {unsigned, then both operands are converted to {unsigned long. Otherwise, if either operand is of type {long, the other operand is converted to {long. Otherwise, if either operand is of type {unsigned, the other operand is converted to {unsigned. Otherwise, both operands have type {int. Software 1, TAU - 3.14 char c; short s; int i; unsigned u; unsigned long ul; float f; double d; long double ld; expression type c - s / i int u * 2.0 - i double c + 3 int c + 5.0 double d + s double 2 * i / l long expression type u * 7 - i unsigned f * 7 - i float 7 * s * ul unsigned long ld + c long double u - ul unsigned long u - l *system dependent* Software 1, TAU - 3.15 13

cast double d = 3.3; int i; unsigned ui; i = (int)d; /* i = 3 */ d = (double)i / 2; /* d = 1.5 */ ui = (unsigned)i; 14