Lecture 02 C FUNDAMENTALS

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

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

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

Types, Operators and Expressions

DEPARTMENT OF MATHS, MJ COLLEGE

Types, Operators and Expressions

Chapter 2 - Introduction to C Programming

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

C: How to Program. Week /Mar/05

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

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

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

Fundamentals of Programming

THE FUNDAMENTAL DATA TYPES

ANSI C Programming Simple Programs

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

Engineering Computing I

C-LANGUAGE CURRICULAM

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

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

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

Fundamental of Programming (C)

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

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

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

CSC 1107: Structured Programming

Data types, variables, constants

Introduction to the C Programming Language

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

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

6.096 Introduction to C++ January (IAP) 2009

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:

CS1100 Introduction to Programming

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

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Chapter 1 & 2 Introduction to C Language

C Programming Multiple. Choice

JAVA Programming Fundamentals

Princeton University COS 333: Advanced Programming Techniques A Subset of C90

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

CSC 1214: Object-Oriented Programming

Fundamental of C programming. - Ompal Singh


C Programming Language (Chapter 2 of K&R) Variables and Constants

Continued from previous lecture

Lecture 3. More About C

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

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

Arithmetic Operators. Portability: Printing Numbers

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

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

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

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

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

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Java Notes. 10th ICSE. Saravanan Ganesh

The Design of C: A Rational Reconstruction: Part 2

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS)

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

Operators and Expressions:

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Basic Elements of C. Staff Incharge: S.Sasirekha

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

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

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

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

JAVA OPERATORS GENERAL

Data Types and Variables in C language

C Programming a Q & A Approach

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

UNIT 3 OPERATORS. [Marks- 12]

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

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

BSM540 Basics of C Language

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

Computer System and programming in C

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

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

Syntax and Variables

C Programming Class I

Programming. Elementary Concepts

Basic Types, Variables, Literals, Constants

Department of Computer Applications

Introduction to C++ with content from

Work relative to other classes

PROGRAMMAZIONE I A.A. 2018/2019

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

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

Fall, 2015 Prof. Jungkeun Park

Data Types. Data Types. Integer Types. Signed Integers

Computers Programming Course 6. Iulian Năstac

Computers Programming Course 5. Iulian Năstac

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

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Chapter 3 Basic Data Types. Lecture 3 1

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

ET156 Introduction to C Programming

Java Programming Fundamentals. Visit for more.

Programming and Data Structures

Transcription:

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 default goto sizeof volatile do if static while 2

Variable Type C has the following simple data types: 3

Data Types char, int, float, double long int (long), short int (short), long double signed char, signed int unsigned char, unsigned int 1234L is long integer 1234 is integer 12.34 is float 12.34L is long float 4

Reading and Writing Integers unsigned int u; scanf( %u, &u); /* reads u in base 10 */ printf( %u, u); /* writes u in base 10 */ scanf( %o, &u); /* reads u in base 8 */ printf( %o, u); /* writes u in base 8 */ scanf( %x, &u); /* reads u in base 16 */ printf( %x, u); /* writes u in base 16*/ short int x; scanf( %hd, &x); printf( %hd, x); long int x; scanf( %ld, &x); printf( %ld, x); 5

float double long double Type Floating Types single-precision floating-point double-precision floating-point extended-precision floating-point Smallest Positive Value Largest Value Precision float 1.17*10-38 3.40*10 38 6 digits double 2.22*10-308 1.79*10 308 15 digits double x; long double x; scanf( %lf, &x); scanf( %Lf, &x); printf( %lf, x); printf( %Lf, x); 6

Character Types char ch; int i; i = a ; /* i is now 97 */ ch = 65; /* ch is now A */ ch = ch + 1; /* ch is now B */ ch++; /* ch is now C */ if( a <= ch && ch <= z ) for(ch = A ; ch <= Z ; ch++) 7

Constants Declaration: #define MAXLINE 1000 char line[maxline+1] a, \t, \n, \0, etc. are character constants strings: character arrays (see <string.h> for string functions) "I am a string always null ( \0 ) terminated. 8

narrower types are converted into wider types f + i int i converted to characters <---> integers <ctype.h> library contains conversion functions, e.g: tolower(c) isdigit(c) etc. Boolean values: true : >= 1 false: 0 Type Conversion 9

Type Conversion long double double float Unsigned long int long int unsigned int int 10

Type Conversion char c; short int s; int i; unsigned int u; long int l; unsigned long int ul; float f; double d; long double ld; i = i + c; /*c change to int*/ i = i + s; u = u +i; l = l + u ul =ul + l; f = f + ul; d = d + f; ld = ld + d; 11

Expressions Arithmetic operator: +, -, *, /, %, ++, -- Arithmetic operator: +,-,*./,% Relation operators: <,>,<=,>=,==,!= Logical operator: && (and), (or) Increment and decrement operators: ++,--,++,-- Assignment operators: +=,-=,/= 12

Relation Operators Relation operators has lower precedentce than arithmetic operators: >, >=, <, <= ==,!= Don t confuse = and ==! The compiler will warn suggest parens. int x=5; if (x==6) /* false */ { /*... */ } /* x is still 5 */ int x=5; if (x=6) /* always true */ { /* x is now 6 */ } /*... */ 13

Increment and Decrement Operators x++ post-increment x ++x pre-increment x x-- post-decrement x --x pre-decrement x Note the difference between ++x and x++: int x=5; int y; y = ++x; /* x == 6, y == 6 */ int x=5; int y; y = x++; /* x == 6, y == 5 */ recommendation 14

Bitwise Operations Applied to char, int, short, long And & Or Exclusive Or ^ Left-shift << Right-shift >> one's complement ~ 15

Assignment Operators Most binary operators have a corresponding assignment operator op +, -, *, /, %, <<, >>, &, ^, expr1 op= expr2 <=> expr1 = (expr1) op (expr2) x *= y+1 <=> x = x* (y+1) Assignment statement has a value While ((c = getchar())!= EOF) 16

Operator Precedence and Associativity highest: + - (unary) * / % lowest: + - (binary) -i * -j = (-i) * (-j) +i + j / k = (+i) + (j / k) left/right associative: it groups from left/right to right/left The binary arithmetic operators (*, /, %, + and -) are all left associative i j k = (i j) k i * j / k = (i * j) / k The unary arithmetic operators( + and -) are both right associative - + i = - ( +i ) 17

Expression Evaluation Precedenc e Name Symbol(s) Associativity 1 X++/X-- left 2 ++X/--X unary +/- right 3 multiplicative *, /, % left 4 additive +, - left 5 assignment =, *=, /=, +=, -= right 18

Expression Evaluation a = b += c++ - d + --e / -f a = b += (c++) - d + --e / -f a = b += (c++) - d + (--e) / -f a = b += (c++) - d + (--e) / (-f) a = b += (c++) - d + ((--e) / (-f)) a = b += ((c++) d) + ((--e) / (-f)) a = b += (((c++) d) + ((--e) / (-f))) a = (b += (((c++) d) + ((--e) / (-f)))) (a = (b += (((c++) d) + ((--e) / (-f))))) 19

/* */ Example: Bit Count count the 1 bits in a number e.g. bitcount(0x45) (01000101 binary) returns 3 int bitcount (unsigned int x) { int b; for (b=0; x!= 0; x = x >> 1) if (x & 01) /* octal 1 = 000000001 */ b++; } return b; 20

Conditional Expressions Conditional expressions expr1? expr2:expr3; if expr1 is true then expr2 else expr3 21

printf function Formatted Input/Output printf(string, expr1, expr2,..) string: ordinary characters and conversion specifications (%) %d --- int %s --- string %f --- float printf( i=%d, j=%d. x=%f\n, i, j, x); 22

Formatted Input/Output Escape Sequence Enable strings to contain characters that would otherwise cause problems for the compiler alert \a new line \n \ \ backspace \b horizontal tab \t \\ 23

Exercices Type Conversion(1/3) void tc1(){ char a; int b; float c; double d; b = 1025; a = b; 1, 1.570000 d = 1.569999789; c=d; printf("%d, %f\n",a,c); } 24

Exercices Type Conversion(2/3) void tc2(){ unsigned char a,b,c,d; a=0; b=255; c=b+1; d=a-b; printf("%d, %d\n",c,d); 0, 1 } 25

Exercices Type Conversion(3/3) void tc3() { unsigned char a,b,c,d; a=0; b=251; c=(a-b)/4; d=a-b; printf("%d, %d\n",c,d); 194, 5 } 26

Exercices Expression Evaluation examples(1/2) int a,b,c,d,e,f,g; a=1; b=2; c=3; d =!(a+b)+c-1&&b+c/2; e = a--&&b++&&++c; f = a+=b++-c*d ; a: -1 b: 4 c: 4 d: 1 e: 1 f: -1 27

Exercices Expression Evaluation examples(2/2) int a,b,c,d,e,f,g; a=1; b=2; c=3; d = c-1&b; e = a--&b; f = a =b++ ; a: 2 b: 3 c: 3 d: 2 e: 0 f: 2 28