Programing in C. Pierre-Alain FOUQUE

Similar documents
EL2310 Scientific Programming

Fundamentals of Programming

Work relative to other classes

Informatica e Sistemi in Tempo Reale

Computers Programming Course 5. Iulian Năstac

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

UNIT- 3 Introduction to C++

A Fast Review of C Essentials Part I

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

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Topic 6: A Quick Intro To C

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

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

EL2310 Scientific Programming

l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

C Introduction. Comparison w/ Java, Memory Model, and Pointers

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

Programming refresher and intro to C programming

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

EC 413 Computer Organization

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

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

C - Basics, Bitwise Operator. Zhaoguo Wang

BLM2031 Structured Programming. Zeyneb KURT

Beginning C Programming for Engineers

Lecture 3. More About C

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

PRINCIPLES OF OPERATING SYSTEMS

OBJECT ORIENTED PROGRAMMING

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 Language

CS240: Programming in C. Lecture 2: Overview

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

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

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

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

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

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

Compiling and Running a C Program in Unix

Data Types and Variables in C language

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Computer System and programming in C

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

Data Type Fall 2014 Jinkyu Jeong

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

CS240: Programming in C

Course Outline Introduction to C-Programming

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

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

Presented By : Gaurav Juneja

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal

F28HS2 Hardware-Software Interface. Lecture 1: Programming in C 1

Chapter 3 Basic Data Types. Lecture 3 1

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:

C and Programming Basics

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

6.096 Introduction to C++ January (IAP) 2009

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

Algorithms, Data Structures, and Problem Solving

ISA 563 : Fundamentals of Systems Programming

Variables and literals

THE FUNDAMENTAL DATA TYPES

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

INTRODUCTION 1 AND REVIEW

CS133 C Programming. Instructor: Jialiang Lu Office: Information Center 703

Fundamental of C programming. - Ompal Singh

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

Introduction to C An overview of the programming language C, syntax, data types and input/output

ME 461 C review Session Fall 2009 S. Keres

Contents. Preface. Introduction. Introduction to C Programming

C Programming Class I

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

Introduction Presentation A

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

Motivation was to facilitate development of systems software, especially OS development.

Lecture 03 Bits, Bytes and Data Types

Course Information and Introduction

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

Fundamentals of Programming

Programming. Elementary Concepts

BSM540 Basics of C Language

Motivation was to facilitate development of systems software, especially OS development.

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

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

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

DEPARTMENT OF MATHS, MJ COLLEGE

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

ME 172. Sourav Saha. Md. Mahamudul Hossain Kazi Fazle Rabbi Saddam Hossain Joy Kamruzzaman Lecturer,Dept. of ME,BUET

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

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

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

C-programming. Goal To give basic knowledge of the C language. Some previous experiences in programming are assumed. Litterature

Lecture 2: C Programming Basic

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5

Transcription:

Programing in C Pierre-Alain FOUQUE

Summary 1 - The C Language 2 - Layout of a program 3 - Data Typing 4 - Conditions 5 - Conditional loops while 2

Programming Language The processor controls everything, but understands only the machine language (i. e. sequence of numbers) that designs operation to perform specific to each processor Not easy to use / not portable (from one machine to another) Programming Language : interface between we and the machine 3

C Language Control Structures (if, for, while,...) Usage of pointers (to access the memory) Iterative Programming (the program controls the changes in the memory) Recursive Programming (function defined by calling itself) Data Typing (restricted to type that can be efficiently translated into machine language) 4

a C Program Program include (objects, data predefined) types (new types) variables (memory allocation) function list Functions header (output viewpoint) operating mode : instruction list simples : termined by «;» composed : simple instructions between 5

First program : «hello» #include <stdio.h> int main() printf( Hello World!!\n ); return 0; 6

First program : «hello» inclusions #include <stdio.h> int main() printf( Hello World!!\n ); return 0; 6

First program : «hello» inclusions #include <stdio.h> int main() printf( Hello World!!\n ); return 0; 6

First program : «hello» inclusions function #include <stdio.h> int main() printf( Hello World!!\n ); return 0; 6

First program : «hello» inclusions function #include <stdio.h> int main() printf( Hello World!!\n ); return 0; 6

First program : «hello» inclusions function #include <stdio.h> int main() printf( Hello World!!\n ); return 0; instruction 6

First program : «hello» inclusions function #include <stdio.h> int main() printf( Hello World!!\n ); return 0; instruction 6

First program : «hello» inclusions function #include <stdio.h> int main() printf( Hello World!!\n ); return 0; instruction «main» : main function only this function is called when the program is launched distribute the tasks 6

Usage #include <stdio.h> int main() printf( Hello World!!\n ); return 0; 7

Edite this program xemacs hello.c & Usage #include <stdio.h> int main() printf( Hello World!!\n ); return 0; 7

Edite this program xemacs hello.c & Usage Compile this program gcc -Wall hello.c -o hello #include <stdio.h> int main() printf( Hello World!!\n ); return 0; 7

Edite this program xemacs hello.c & Usage Compile this program gcc -Wall hello.c -o hello Execution./hello #include <stdio.h> int main() printf( Hello World!!\n ); return 0; 7

Edite this program xemacs hello.c & Usage Compile this program gcc -Wall hello.c -o hello Execution./hello #include <stdio.h> int main() printf( Hello World!!\n ); return 0; 7

Edite this program xemacs hello.c & Usage Compile this program gcc -Wall hello.c -o hello Execution./hello #include <stdio.h> int main() printf( Hello World!!\n ); return 0; 7

Classical Errors The main classical errors are : error in the name of a function the compilater does not know it forget «;» at the end of an instruction use a variable not declared the compilator does not know if it is an int, a float or a string! no «main» function 8

The «main» function «main» : main function function called when the program is launched no other function is automatically executed cannot be missed 9

Generic Program /* Commentaires */ /* Directives pour le preprocessor */ #include <stdio.h> #define TAILLE 3 #define SQ(x) x*x /* New types */ typedef int[taille] tableau; /* Global Variables */ int globale; 10

Generic Program Explanation : /* */ /* Commentaires */ /* Directives pour le preprocessor */ #include <stdio.h> #define TAILLE 3 #define SQ(x) x*x /* New types */ typedef int[taille] tableau; /* Global Variables */ int globale; 10

Generic Program Explanation : /* */ /* Commentaires */ Preprocessor : inclusions constantes macros /* Directives pour le preprocessor */ #include <stdio.h> #define TAILLE 3 #define SQ(x) x*x /* New types */ typedef int[taille] tableau; /* Global Variables */ int globale; 10

Generic Program Explanation : /* */ /* Commentaires */ Preprocessor : inclusions constantes macros Types /* Directives pour le preprocessor */ #include <stdio.h> #define TAILLE 3 #define SQ(x) x*x /* New types */ typedef int[taille] tableau; /* Global Variables */ int globale; 10

Generic Program Explanation : /* */ Preprocessor : inclusions constantes macros Types Global Variables /* Commentaires */ /* Directives pour le preprocessor */ #include <stdio.h> #define TAILLE 3 #define SQ(x) x*x /* New types */ typedef int[taille] tableau; /* Global Variables */ int globale; 10

Functions int addition(int x, int y) int z; z = x + y; return z; int main() int a,b,c; a = 3; b = 5; c = addition(a,b); printf( %d+%d=%d\n,a,b,c); return 0; 11

Functions int addition(int x, int y) int z; z = x + y; return z; Header int main() int a,b,c; a = 3; b = 5; c = addition(a,b); printf( %d+%d=%d\n,a,b,c); return 0; 11

Functions int addition(int x, int y) int z; z = x + y; return z; Header int main() int a,b,c; a = 3; b = 5; c = addition(a,b); printf( %d+%d=%d\n,a,b,c); return 0; 11

Functions int addition(int x, int y) int z; z = x + y; return z; Header Declaration variables int main() int a,b,c; a = 3; b = 5; c = addition(a,b); printf( %d+%d=%d\n,a,b,c); return 0; 11

Functions int addition(int x, int y) int z; z = x + y; return z; Header Declaration variables int main() int a,b,c; a = 3; b = 5; c = addition(a,b); printf( %d+%d=%d\n,a,b,c); return 0; 11

Functions int addition(int x, int y) int z; z = x + y; return z; Header Declaration variables Instruction simple int main() int a,b,c; a = 3; b = 5; c = addition(a,b); printf( %d+%d=%d\n,a,b,c); return 0; 11

Functions int addition(int x, int y) int z; z = x + y; return z; Header Declaration variables Instruction simple int main() int a,b,c; a = 3; b = 5; c = addition(a,b); printf( %d+%d=%d\n,a,b,c); return 0; 11

Functions int addition(int x, int y) int z; z = x + y; return z; int main() int a,b,c; a = 3; b = 5; c = addition(a,b); printf( %d+%d=%d\n,a,b,c); return 0; Header Declaration variables Instruction simple Header 11

Functions int addition(int x, int y) int z; z = x + y; return z; int main() int a,b,c; a = 3; b = 5; c = addition(a,b); printf( %d+%d=%d\n,a,b,c); return 0; Header Declaration variables Instruction simple Header 11

Functions int addition(int x, int y) int z; z = x + y; return z; int main() int a,b,c; a = 3; b = 5; c = addition(a,b); printf( %d+%d=%d\n,a,b,c); return 0; Header Declaration variables Instruction simple Header Declaration variables 11

Functions int addition(int x, int y) int z; z = x + y; return z; int main() int a,b,c; a = 3; b = 5; c = addition(a,b); printf( %d+%d=%d\n,a,b,c); return 0; Header Declaration variables Instruction simple Header Declaration variables 11

Functions int addition(int x, int y) int z; z = x + y; return z; int main() int a,b,c; a = 3; b = 5; c = addition(a,b); printf( %d+%d=%d\n,a,b,c); return 0; Header Declaration variables Instruction simple Header Declaration variables Instructions simples 11

Memory The memory stores either the program to execute (sequence of numbers in machine language for the processor) the variables changed by the program storage by bytes or words (blocks of 8 bits or 32 bits) 12

Typing What is the coding of 01010111? the integer 87 (= 64+16+4+2+1) the float 0,00390625 (= 2.2-9 ) the character X an instruction in machine language we need to associate a type for each value The typing defines the coding 13

Integers : short, int, long and long long In practice, according the types and machines, the integers are coded using 8, 16, 32 or 64 bits : (GCC under Linux) char (8 bits) +/- 127 short (16 bits) +/- 32767 int/long (32 bits) ~ +/- 2.109 long long (64 bits) ~ +/- 9.1018 unsigned int precises positive integers we do not need a sign bit 14

Representating reals called floating (real with floating point) (m,e) m: mantissae and e: exponent rep.: Mb e where M is the number coded in m base b=2, or 10 in general Form normalised: 1>M>=1/b If b=10, M>=0.1 and 3.14 is coded: (0.314,1) 0 cannot be represented in normalized form 15

Floating : float, double and long double float (24 + 8 bits) Precision 2-23 Min 10-38 Max 3.10 38 double (53 + 11 bits) Precision 2-53 Min 2.10-308 Max 10 308 long double (64 + 16 bits) Precision 2-64 Min 10-4931 Max 10 4932 16

Declaration and initialization In a program or a function, we can declare variables, then (or simultaneously) initialize them 17

Declaration and initialization In a program or a function, we can declare variables, then (or simultaneously) initialize them #include <stdio.h> long a = 1034827498; float x = 1023.234; int main() int b; double y; float z; b = 1234; y = 1.365; z=1.0/y; 17

Declaration and initialization In a program or a function, we can declare variables, then (or simultaneously) initialize them During the declaration, the contain of the variable is random! #include <stdio.h> long a = 1034827498; float x = 1023.234; int main() int b; double y; float z; b = 1234; y = 1.365; z=1.0/y; 17

Operators on numbers The integers/floats can be manipulated thanks to the classical following operators : a + b : addition a - b : soustraction a * b : multiplication a / b : division (euclidean division on integers) (floating division on reals) a % b : modulo on integers (rest of the euclidean division) 18

Printing variables printf displays on the screen, the variable contain : %d for an int or long (%3d) for 3 spaces (ideal for alignment) %f for a float or double (%g) (%6.2f) for 6 spaces, 2 digits after point 19

Printing variables printf displays on the screen, the variable contain : %d for an int or long (%3d) for 3 spaces (ideal for alignment) %f for a float or double (%g) (%6.2f) for 6 spaces, 2 digits after point printf( a = %d et b = %d,a,b); printf( x = %f et y = %f,x,y); return 0; 19

Bitstring x and y are of type unsigned int x & y = x AND y x y = x OR y x ^ y = x XOR y work bit by bit. x << 8 = shift of 8 bits in the left (corresponds to the multiplication by 2 8 modulo 2 32 ) x >> 5 = shift of 5 bits in the right ~x = complement to 1 of x 20

Representation in other bases Decimal: Example: 1234 Octal: First digit is a zero. Example: 0177 Hexadecimal: begins by 0x or 0X. Ex: 0x1BF and 0XF2A To print an integer in hexadecimal form int a; printf( %x,a); 21

scanf work conversely as the printf function enter a value (keyboard) into a variable call scanf with a format and a variable which will be modified int a; printf( Enter a value:\n ); scanf( %d,&a);

Conditionnal Execution According the result of a test, we can hope the execution of an instruction, or not :

Conditionnal Execution According the result of a test, we can hope the execution of an instruction, or not : if (a > 0) printf( Positive \n );

Conditionnal Execution According the result of a test, we can hope the execution of an instruction, or not : if (a > 0) printf( Positive \n ); A choice:

Conditionnal Execution According the result of a test, we can hope the execution of an instruction, or not : if (a > 0) printf( Positive \n ); A choice: if (a > 0) printf( Positive \n ); else printf( Négative or nul\n );

A test? The result of a test is an integer : nul = false non nul = truth Operators of test a==b : equality test a!=b : difference test a<b or a>b : strict comparaison a<=b or a>=b : comparaison large

Test Combinaison It is possible to combine (negation, conjonction, disjonction, etc) of tests (!(<test>)) : negation of <test> ((<test1>) && (<test2>)) : conjonction (<test1> AND <test2>) ((<test1>) (<test2>)) : disjonction (<test1> OR <test2>)

Remarks on tests Do not hesitate to put parenthesis No order is respected at execution time we have to see that all tests and subtests can be made so that the program can not loop! A single instruction is allowed after the if or the else if many instructions depend on the result of a test composed instructions

Conditional Loops In C, it is possible to execute an instruction many times : Loops fix number of iterations : for (cf. following lesson) number of iterations depending on a test : while and do while

Loop while An instruction is executed while a test is satisfied : <test> non oui <instruction>

Loop while An instruction is executed while a test is satisfied : while <test> <test> oui non <instruction>

Loop while An instruction is executed while a test is satisfied : while <test> <instruction> <test> oui non <instruction>

Loop while An instruction is executed while a test is satisfied : while <test> <instruction> instruction can be never executed <test> oui non <instruction>

Loop do while An instruction is executed, then repeated while a test is satisfied :

Loop do while An instruction is executed, then repeated while a test is satisfied : do

Loop do while An instruction is executed, then repeated while a test is satisfied : do <instruction>

Loop do while An instruction is executed, then repeated while a test is satisfied : do <instruction> while <test>

Loop do while An instruction is executed, then repeated while a test is satisfied : do <instruction> while <test> <instruction> <test> non oui

Loop do while An instruction is executed, then repeated while a test is satisfied : do <instruction> while <test> <instruction> the instruction is always executed at least one time <test> oui non

Remarks on the loops A single instruction is allowed in the loop while <test> <instruction> do <instruction> while <test>; if many instructions must be repeated composed instructions The indentation helps to see what is repeated (with the help of emacs)

Books Kernigham & Ritchie : Le langage C Bracquelaire: Méthodologie de la programmation en C Delannoy: Le livre du C Premier Langage Sedgewick : Algorithmes en C Cormen, Leicerson, Rivest, Stein : Introduction à l algorithmique Knuth : The Art of Computer Programming 31