C Basics. Chapter 2: C Basics. C Programming Language. v3.3 February 6, 2017

Similar documents
UNIT- 3 Introduction to C++

6.096 Introduction to C++ January (IAP) 2009

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

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

Basic Types, Variables, Literals, Constants

Fundamentals of Programming

Fundamental of Programming (C)

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

INTRODUCTION 1 AND REVIEW

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

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Data Types and Variables in C language

Chapter 2 - Introduction to C Programming

Computers Programming Course 5. Iulian Năstac

C: How to Program. Week /Mar/05

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

Introduction to C# Applications

Fundamental of C programming. - Ompal Singh

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

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

Basic Elements of C. Staff Incharge: S.Sasirekha

C++ Programming Basics

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

Tokens, Expressions and Control Structures

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

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

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

Java Notes. 10th ICSE. Saravanan Ganesh

JAVA Programming Fundamentals

Basic Types and Formatted I/O

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

A Fast Review of C Essentials Part I

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

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

Number Systems, Scalar Types, and Input and Output

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

Programming. C++ Basics

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Chapter 1 & 2 Introduction to C Language

VARIABLES AND CONSTANTS

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

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

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

Review of the C Programming Language for Principles of Operating Systems

Presented By : Gaurav Juneja

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

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

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

.Net Technologies. Components of.net Framework

EL6483: Brief Overview of C Programming Language

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

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

LEXICAL 2 CONVENTIONS

Work relative to other classes

Introduction to C Language

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Basics of Java Programming

Programming and Data Structures

Creating a C++ Program

C Language, Token, Keywords, Constant, variable

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

Review of the C Programming Language

Lexical Considerations

Java Programming Fundamentals. Visit for more.

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

DEPARTMENT OF MATHS, MJ COLLEGE

THE FUNDAMENTAL DATA TYPES

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

Types, Operators and Expressions

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

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Visual C# Instructor s Manual Table of Contents

CSC 1107: Structured Programming

3. Java - Language Constructs I

Types, Operators and Expressions

Lecture 2 Tao Wang 1

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012


BITG 1233: Introduction to C++

Variables and literals

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

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

Computer System and programming in C

ME 461 C review Session Fall 2009 S. Keres

The MaSH Programming Language At the Statements Level

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

BLM2031 Structured Programming. Zeyneb KURT

Lecture 02 C FUNDAMENTALS

VLC : Language Reference Manual

Syntax and Variables

Reserved Words and Identifiers

Programming. Elementary Concepts

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Types, Variables, and Constants

Transcription:

Chapter 2: C Programming Language Šimon Řeřucha v3.3 February 6, 2017

There are only two kinds of programming languages: those people always bitch about and those nobody uses. Bjarne Stroustrup.

General small number of keywords auto, break, case, char, const, continue, default, do, double, enum, extern, float, for, goto, if, int, long, register, return, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, while lot of arithmetical and logical operators free-format, case sensitive simple structure aimed at algorithmization relies on libraries for a meaningful operation

Variables static typed, weakly-enforced (implicit type conversion possible) single namespace, no sigil (e.g. $var) first class: accessible as a unit integer, structure but not array compound types array, enum, struct lexical scope of visibility allows user-defined types

Functions C is procedure (function)-based declaration syntax similar to usage syntax fn definition outside functions basic polymorphism

Other Features low-level access to computer memory basic modularity separated compilation preprocessor for macros, source text inclusion, conditional compilation linker for pre-compiled objects inclusion complex function delegated to (standard) library routines

What is not included object-oriented approach but basic concepts are usable exceptions, range checking, polymorphism garbage collection proper disposal of unused memory is left to programmer GUI library, multithreading, data persistence, lambda calculus

Basic syntax free-form, case sensitive

Lexical Conventions case sensitive, keywords are lower case comments: /* multiline */ // singleline

Source character ser Letters: a - z, A - Z, Digits: 0 -- 9 Punctuation: ~! @ # \% ^ & * ( ) - + = : ; " < >,.? / \ { } [ ] Whitespace: space, horizontal tab, vertical tab, form feed, newline Others possible, hardly portable (ASCII 127+, UNICODE)

Integer Constants decimal 0-9 (default type) minus sign - for negative numbers Ex.: 15, 0, 1, -2 octal 0-7 begin with zero Ex.: 065, 015, 0, 01 hexadecimal 0-9, A - F begin with 0x (case insensitive) Ex.: 0x12, 0xDeadBeef, 0Xcd binary 0,1 non-standard! its support depends on compiler typically begin with 0b (case insensitive) Ex.: 0b10110010 ~L, ~U suffixes to explicitly declare long / unsigned constant, e.g. 129u, 12345LU

Real Constants decimal 15., 56.8,.84, 3.14 exponential 5e6, 7e23 double by default ~F, ~L suffixes to explicitly declare float / long double constant, e.g. 3.14f, 12e3L

Character Constants straight a, x, # octal backslash + 3 octal digits, e.g. \012, \x007 hexa \x + 2 hex digits, e.g. \x0a, \x00 Well known constants: \n newline \r carriage return \t tab \b backspace \\ backslash \ apostrophe \0 zero character (e.g. string termination)

String Constants aka Literals delimited by quotes: "" ex. "A string","much longer string" automatic concatenation (chaining), e.g. "All this" " pieces" "will turn into one string!" Escape sequences used for input of special characters: "Be aware of \"nothing\" from girls mouth!" "Do not mismatch slash / and backslash \ "

Variables Variable is a memory-stored element of data, that features: name (unique identifier) data type (known at compile-time) scope (visibility)

Identifiers identifiers start with alphabetic or underscore character, may contain numbers identifier begginging with underscore are often system reserved, not recommended (e.g. _demo ) identifier length used to be limited (e.g. 8 or 32 charactes) Smart naming conventions Use meaningfull names: long, self-descriptive Use short names just for loop indices.

Basic datatypes int integer, signed or unsigned, min 16-bit, typ. 16 32-bit char smallest unit, integer type, typ. 8-bit long int min 32-bit, abbreviated as long float single precision floating-point, typ 32-bit (sign + 8exp + 23man) double double precision floating-point, typ 64-bit (1 + 11 + 53) long double extended precision, typ 64--80-bit C99: adds long long, bool

Basic datatypes: quiz Pop quiz (correct/incorrect): Which declaration are correct? int money$owed; (incorrect: cannot contain $) int total_count (correct) int score2 (correct) int 2ndscore (incorrect: must start with a letter) int long (incorrect: cannot use keyword)

Basic datatypes II. all integer types either signed (implicit) or unsigned signed range ( 2 (n 1), 2 (n 1) 1) unsigned range (0, 2 n 1) floating point types stored in form m 2 n

Examples bool flag; // true = 1; c99 only char c; // signed! int i; // same as "signed int i" unsigned int ui; // "unsigned ui" also valid long int a; // "long a" also valid long long int u; // c99 only float f; // long double xxx; // extended precision

Fixed-width datatypes (C99+) intn t integer of exactly N-bits, e.g. int16 uintn t unsigned variant int leastn t, uint leastn t (unsigned) integer at least N-bits wide int fastn t, uint fastn t fastest type of (unsigned) integer at least N-bits wide intmax t maximum width integer type Ex: int16 t, uint fast32 t

Operators I. basic assignment: = arithmetics: +, -, *, /, %, relations: ==,!=, <, >, <=, >= logical:!, &&, bitwise logic: ~, &,, ^ shift operators: <<, >> advanced assignment: +=, -=, *=, /=, %=, &=, =, ~=, <<=, >>= increment/decrement: ++, -- subexpresion: ()

Operators II. conditional:? : calling function: () (de)referencing: *, &, [] sequencing:, type conversion: (type) addressing:., -> misc: sizeof()

Operator Precedence

Operator Precedence II.

Assignment Basic operator: = Advanced ops: +=, *=, -=,... a = 1; b = -2; f = 1.34; // real number f *= -2; // equal to f = f * -2; a = max (1,3); // saving return value

Comparison operators ==... equality!=... inequality <, >... less/bigger than <=, >=... less/bigger or equal to Zero is treated as FALSE, non-zero as TRUE newer compilers provide bool datatype, but any integer is feasible for logic evaluation.

Assignment vs. Comparison Caution! Mismatching = and == is eternal source of error and grey hair. E.g.: i == x is TRUE if i is equal to x. i = x is TRUE if x is nonzero. Now days are brighter, compiler often issues a warning.

Logic operators int a = 0; // assignment

Ex.: Comparison + Logic int i = 1, j = 1; j = j && (i = 2); // true j = j && (i == 3); // false j = j (i / 2); // true j =!j && (i = i + 1); // false

Lazy Expression Evaluation if ( y!= 0 && x / y < z)... Generally, for y = 0, evaluation would end up with error. Due to lazy evaluation, the result is known before second part is evaluated.

Bit operators Important stuff for embedded! int a = 1,b,c; c = a << 2; // bitshift by 2 bits ~ multiply by 4 a = 0x3; // b = 0b011 b = 0x5; // b = 0b101 b = a & b // = 1; bit AND b = a b // = 0b111; bit OR

Bit operators Important stuff for embedded! char a = 1,b,c; c = a << 2; // bitshift by 2 bits ~ multiply by 4 a = 0x3; // b = 0b011 b = 0x5; // b = 0b101 c = a & b // = 1; bit AND c = a b // = 0b111; bit OR c = a ^ b // = 0b110; bit XOR c = ~b // - 0b1111 1010; bit negation

Ex. Bit operators // set x-th bit a = 1 << x; // clear x-th bit a &= ~(1 << x); // toggle x-th bit a ^= (1 << x); // check value of x-th bit bit = (a >> x) & 1;

Ex. Bit operators II. char a = 0xA; // a = 10 = 0b1010 // check value of bits 3and4 val = (a >> 2) & 3; // set value of bits 3and4 mask = 0xc; // = 0x1100 ~ bits 3+4 a &= ~mask; // clear bits with mask val &= 0x3; // ensure val has max 2 bits a = (val << 2); // shift the value and set the bits // or in a single command a = (a & ~mask) ((val & 0x3) << 2);

Functions Function is a block of statements with defined arguments and return value. Any function features: identifier arguments (typed) return value (typed) scope declaration, definition

Functions Declaration: tells the compiler that the function or variable is defined (somewhere) function or variabl must be (at least) declared before it is used declaration of function specifies the name and type of arguments and return value (also referred to as function prototype) declaration of variable specifies the name and type Definition: defines the function as a sequence of statements defines the variable as the memory-stored element

Ex.: Functions // declaration - fn prototype double pi_times (int num); // definition double circ (int r){ return pi_times( 2 * r ) ; } double pi_times (int num){ return num * 3.14; }

Ex.: Program w/ function // declaration double pi_times (int num); // main void main(void){ int a = 2; double d; // function call d = pi_times(a); } printf("result: %d * pi = %lf",a,) // definition double pi_times (int num){ return num * 3.14; }

Fn + Var Visibility: Lexical Scope both functions and variables have limited visibility both need to be known before first use they are known via either declaration or definition visibility related to modules, functions, blocks

Function Scope A function might be called at a certain point in source text when: the function is previously defined within same module the function is previously declared within same module i.e. the function is defined within same module, or the function is defined in another module The prototypes are typically kept in header files.

Variable Scope A variable might be referenced at a certain point in source text when: it is previously defined within same function or globally it is previously declared within same function or globally that indicates the variable is defined within same module, or that indicates the variable exists in another module and is made accessible to other modules. The local variables are defined within functions (even in blocks by newer standards), global variables are typically defined within modules source text and declared in header files.

Void Type useful e.g. for explicit declaration of empty parameters or empty return value and more, as seen later void print(int a); int rand(void); // fn that returns nothing // fn w/ no parameters

Two-way Branching: IF Syntax: if(condition) then expr1; or Syntax: if(condition) then expr1; else expr2; if (a == 1) b = 2; if (a == 1){ b = 2; c = 1; }else{ a = 1; }

Multi-way Branching: SWITCH case takes integer or character constant x switch takes integer expression c each case analogous to if(c == x)... int c; switch(c){ case 1:... break; case 2:... break; default:... break; }

Multi-way Branching: SWITCH II. char c; switch(c){ case f :... // case F :... break; default:... break; } when break is ommited, evaluation continues in next branch

While Loop Syntax: while(condition) expr1; loop while condition holds condition evaluated before evaluating expr a = 123; while (a > 0){ b = cool_fn(a); }

Do.. While Loop Syntax: a = 32; do expr1 while(condition); loop while condition holds condition evaluated after evaluating expr do{ b = cool_fn(a); }while (a > 0);

FOR Loop Syntax: for(e start; e end; e iter) expr; predefined number of cycles e start evaluated once at the beginning e end evaluated every beginning of iteration e iter evaluated every end of iteration // iterate through 0 to 9 for (i = 0; i < 10; i++){ b = cool_fn(i); }

FOR Loop II. any of e start, e end, e iter might be ommited // same function as previous loop i = 0; for (; i < 10; ){ b = cool_fn(i); i++; } // endless loop for(;;){... }

Ex. More Loops // empty loop while (i < 1) ; // nested loops for(i = 0; i < 10; i++){ for(j = 0; j < 10; j++){ s = compute(i, j); } }

Loop Control break immediately finishes innermost looping continue skips rest of current iteration // skip trendy_fn() if cool_fn() returns value below zero for (i = 0; i < 10; i++){ b = cool_fn(i); if(b < 0) continue; c = trendy_fn(i,b); } // stop looping when cool_fn() returns zero for(;;){ b = cool_fn(++i); if(b = 0) break; }

GOTO command Syntax: label:... goto label; be AWARE! // trivial example -- to be AVOIDED puentezero:... goto puentezero;

Ex.: Meaningfull use of GOTO // escaping nested loops for(i = 0; i < 10; i++){ for(j = 0; j < 10; j++){ for(k = 0; k < 10; k++){ if(a[j] == 0) goto error; n = b[k] + a[i] / a[j]; } } } goto allright; // handle zero divisor error:... allright:...

Ex.: better without GOTO // escaping nested loops int error = 0; for(i = 0; i < 10; i++){ for(j = 0; j < 10; j++){ for(k = 0; k < 10; k++){ if(a[j] == 0){ error = 1; break; } n = b[k] + a[i] / a[j]; } if(error) break; } if(error) break; } // handle zero divisor if(error){...}...

RETURN statement Syntax: return expr; or return(expr); return; return 1; return (a); return cool_fn(a); // return from void fn // return constant, same as "return(1)" // return value of a variable // return a return value of other fn finishes current function define the return value of function in main() finishes entire program;

Conditional operator Syntax: condition? expr1 : expr2 corresponds to: if(condition) expr1; else expr2; int i, k, j = 2; i = (j == 2)? 1 : 3; k = (i > j)? i : j; k = ((i > j)? do_it() : do_sth_else()); (i == 1)? i++ ; j++;

Comma operator Syntax: expr1, expr2 first expression is evaluated, then second expression is evaluated, its value is returned. // equal to i = b; i = (a, b); //... use in for loop for (i = 0; i < 10; i++, j--)...

Type Casting implicit vs. explicit declare that object of defined type should be treated as object of another type Implicit: int -> unsigned -> long -> float -> double int a; unsigned b; long l; float f; l = a + b; // cast to unsigned, then to long f = l / a; // cast to long, then to float

Type Casting II. void print_int(int a); float f = 3.14195; // f is implicitly casted to int print_int(f); // the same, explicitly print_int((int) f); Use explicit typecasting for better readability!

Ex.: Type Casting Where explicit typecasting matters: int a = 20; int b = 30; long l; double d; d = a / b; // d = 0 d = a / (double)b; // d = 2/3 // for 2 byte in l = 360000 mod 65535 l = a * b * a * b l = (long)a * b * a * b // l = 360000