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

Similar documents
THE FUNDAMENTAL DATA TYPES

Fundamentals of Programming

Fundamental of Programming (C)

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

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

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

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

DEPARTMENT OF MATHS, MJ COLLEGE

Work relative to other classes

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

Data Types and Variables in C language

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

A Fast Review of C Essentials Part I

VARIABLES AND CONSTANTS

ANSI C Programming Simple Programs

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

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

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

C: How to Program. Week /Mar/05

Lecture 02 C FUNDAMENTALS

6.096 Introduction to C++ January (IAP) 2009

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

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

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

Chapter 2 - Introduction to C Programming

Chapter 3. Fundamental Data Types

Fundamental of C programming. - Ompal Singh

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

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:

Data types, variables, constants

Programming and Data Structures

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

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

Basic Elements of C. Staff Incharge: S.Sasirekha

Chapter 2. Lexical Elements & Operators

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

EEE145 Computer Programming

Presented By : Gaurav Juneja

Programming. C++ Basics

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

INTRODUCTION 1 AND REVIEW

Java Notes. 10th ICSE. Saravanan Ganesh

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

BLM2031 Structured Programming. Zeyneb KURT

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

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

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

Computers Programming Course 5. Iulian Năstac

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

Basic Types and Formatted I/O

Lecture 2 Tao Wang 1

The C++ Language. Arizona State University 1

Differentiate Between Keywords and Identifiers

Chapter 1 & 2 Introduction to C Language

LECTURE 02 INTRODUCTION TO C++

ISA 563 : Fundamentals of Systems Programming

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

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Basic Types, Variables, Literals, Constants

Technical Questions. Q 1) What are the key features in C programming language?

C - Basic Introduction

Data Type Fall 2014 Jinkyu Jeong

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

Approximately a Test II CPSC 206

Fundamentals of Programming Session 4

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


Reserved Words and Identifiers

Review of the C Programming Language for Principles of Operating Systems

ET156 Introduction to C Programming

OBJECT ORIENTED PROGRAMMING

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

C Language, Token, Keywords, Constant, variable

Procedures, Parameters, Values and Variables. Steven R. Bagley

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

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

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

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

>B<82. 2Soft ware. C Language manual. Copyright COSMIC Software 1999, 2001 All rights reserved.

UEE1302 (1102) F10: Introduction to Computers and Programming

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

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

C Functions. 5.2 Program Modules in C

BITG 1233: Introduction to C++

Introduction to C Language

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

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

LESSON 4. The DATA TYPE char

Introduction to C. Systems Programming Concepts

2. Numbers In, Numbers Out

INTRODUCTION TO COMPUTER SCIENCE - LAB

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

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

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

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

EL2310 Scientific Programming

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

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

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

Transcription:

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 The library Other tools (editors, debuggers, etc.)

The Preprocessor The preprocessor is a program that scans a source file before it is compiled The preprocessor makes substitutions in the source file Preprocessor directives (instructions) begin with # For example, #include stdio.h tells the preprocessor to replace that line with a copy of the referenced file Quotes search in the current directory and other systemdependent places; < > only search in the other places

The Standard Library The standard library contains many useful functions that you can include in your C programs For example, math functions, random numbers, etc. The C compiler knows where to find the (pre-compiled) definitions of these functions However, your program must still include function prototypes for any library functions that you use This is generally done by #include-ing the appropriate header (.h) files

Example: Random Numbers Use the rand() function (found in stdlib.h) to generate random integer values printf( %7d, rand()); If you put this into a program, you ll find that your program generates the same random values each time it runs To fix this, you must seed the random number generator with an everchanging value from time.h: srand(time(null)); /* goes at start of code block */

Fundamental Data Types

Variables Remember that variables are named blocks of memory Variables have two properties: name a unique identifier type what sort of value is stored

Identifiers Identifiers give unique names to various objects in a program An identifier may contain letters, digits, and the underscore character ( _ ) An identifier must begin with a letter or _ Identifiers should be meaningful (and nouns) Style convention: the second and subsequent words in an identifier are capitalized

Identifier Examples Good Identifiers tax_rate taxrate level4score Bad Identifiers 1stName /* starts with a digit */ %discount /* contains invalid character */

Keywords Some words may not be used as identifiers These words have special meaning in C C has 32 reserved words Ex. for, if, while, switch

Reserved Words in C auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while

Data Types int stores integer values (ex. 5) float stores decimal values (ex. 3.14) double stores larger decimal values than float (double the precision of a float) char stores an integer representing a character (ex. A ) Also short, unsigned, and long

The char Data Type C variables of any integer type (typically char and int) may be used to represent characters In some cases, an int is required for technical reasons Character constants (literals) like a and + are of type int, not char The char type can also hold small integers char is stored in 1 byte (8 bits) of memory

Manipulating Characters Because characters are inherently integers, we can compare them using the standard relational operators e.g., to test for a lowercase letter: if (input >= a && input <= z ) We can also perform arithmetic on them: /* convert lowercase letter to equivalent uppercase letter */ c = c - a + A ;

Escape Sequences We can use escape sequences to print some hard-toprint characters A backslash (\) changes the meaning of the character that follows it e.g., \n means newline, and \t means tab

Interchangeable ints and chars Consider the following code fragment: char c = a ; printf( %c, c); /* produces a */ printf( %d, c); /* produces 97 */ printf( %c%c%c, c, c+1, c+2); /* produces abc */

Memory Representation Computer data is stored as sequences of bits (1s and 0s) Just like in decimal (base 10), each bit position represents a power of the base (in this case, 2): 2 n 2 n-1...2 2 2 1 2 0 Consider the character a, whose memory representation is 01100001: 0x2 7 + 1x2 6 + 1x2 5 + 0x2 4 + 0x2 3 + 0x2 2 + 0x2 1 + 1x2 0

The int Data Type Integers are stored in different sizes of memory blocks on different platforms e.g., 2 bytes (16 bit systems) or 4 bytes (32-bit systems) This affects the number of values that can be stored Storing too large a value can cause overflow Beware of integer values that begin with a leading 0! 0x precedes a hexadecimal value; 0 precedes an octal value

Floating-Point Types Use float, double, and long double to store real numbers like 0.001 and 3.14159 Use a suffix (f for float, l for long double) to specify the type of a floating constant; otherwise, it s a double by default e.g., 3.19f or 4.62l Exponential notation is also available, e.g. 1.234e5

Character and Integer Types Type Size Value Range char 1 byte -128 to 127 or 0 to 255 unsigned char 1 byte 0 to 255 signed char 1 byte -128 to 127 int 4 bytes -2,147,483,648 to 2,147,483,647 unsigned 4 bytes 0 to 4,294,967,295 short 2 bytes -32,768 to 32,767 unsigned short 2 bytes 0 to 65,535 long 8 bytes --9223372036854775808 to 9223372036854775807 unsigned long 8 bytes 0 to 18446744073709551615

Floating-point Types Type Storage Size Value Range Precision float 4 bytes 1.2E-38 to 3.4E+38 6 decimal double 8 bytes 1.2E-38 to 3.4E+38 15 decimal long double 16 bytes 3.4E-49321 to 1.2E+1049321 20 decimal

typedef Use typedef to associate a type with a mnemonic identifier typedef int INCHES; typedef char uppercase; You can then use the identifier to declare a variable or function typedef lets you abbreviate long declarations or easily redefine types when porting code to different machines

The sizeof Operator sizeof() returns the number of bytes needed to store an object (a type or an expression) parentheses are only required when applied to a type sizeof(char) is always 1 sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) sizeof(signed) == sizeof(unsigned) == sizeof(int) sizeof(float) <= sizeof(double) <= sizeof(long double)

getchar() and putchar() These are macros from stdio.h that are used to read and print characters one at a time They work with int values, not char values! stdio.h defines a symbolic constant named EOF that represents an end-of-file mark For example, to read one character at a time from the keyboard: while ( (c = getchar() )!= EOF) {... }

Mathematical Functions These are generally defined in math.h sqrt(), pow(), exp(), log(), sin(), cos(), tan(), etc. Most of these functions take one argument of type double, and return a double result pow() takes two double arguments (base and exponent) instead You can use abs() (integer absolute value) and fabs() (floatingpoint absolute value) as well

Operators Types Operators Arithmetic + - * / % Increment/ Decrement ++ -- Assignment = += -= *= /= %= Relational == < > <= >=!= Logical &&(AND) (OR)!(NOT) Bitwise &(AND) (OR) ^(XOR) ~(complement) << (left shift) >> (right shift) Ternary :? (conditionalexpression? expr1 : expr2)

Operator Precedence and Associativity Operators Associativity () ++(postfix) --(postfix) left to right +(unary) (unary) ++(prefix) --(prefix) right to left * / % left to right + - left to right = += -= *= /= %= right to left