The C language. Introductory course #1

Similar documents
Lecture 3. More About C

HISTORY OF C LANGUAGE. Facts about C. Why Use C?

Fundamental of Programming (C)

C: How to Program. Week /Mar/05

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

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

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

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

Chapter 2 - Introduction to C Programming

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

Introduction to the C Programming Language

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Chapter 1 & 2 Introduction to C Language

Introduction to C. Systems Programming Concepts

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

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Informatica e Sistemi in Tempo Reale

Fundamentals of Programming

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

Programming. Elementary Concepts

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

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

Fundamentals of Programming

KARMAYOGI ENGINEERING COLLEGE, Shelve Pandharpur

Introduction to C Programming

PRINCIPLES OF OPERATING SYSTEMS

Work relative to other classes

Basic Assignment and Arithmetic Operators

C Programming. The C Preprocessor and Some Advanced Topics. Learn More about #define. Define a macro name Create function-like macros.

C-LANGUAGE CURRICULAM

Topic 6: A Quick Intro To C

Course Outline Introduction to C-Programming

Chapter 2. Introduction to C language. Together Towards A Green Environment

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2


CSE 303 Lecture 8. Intro to C programming

Lecture 02 C FUNDAMENTALS

COMP s1 Lecture 1

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

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Programming and Data Structure

EL2310 Scientific Programming

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Computers Programming Course 5. Iulian Năstac

EC 413 Computer Organization

In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard.

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

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:

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

High Performance Programming Programming in C part 1

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Operators & Expressions

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

1.3b Type Conversion

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

EL2310 Scientific Programming

Variables and literals

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

C: How to Program. Week /Apr/23

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

Lecture 5: C programming

INTRODUCTION TO C-PROGRAMING LANGUAGE

OBJECT ORIENTED PROGRAMMING

Compiling and Running a C Program in Unix

BSM540 Basics of C Language

CS 241 Data Organization Pointers and Arrays

JAVA OPERATORS GENERAL

Number Systems, Scalar Types, and Input and Output

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

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

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

mith College Computer Science CSC231 Bash Labs Week #10, 11, 12 Spring 2017 Introduction to C Dominique Thiébaut

Computer System and programming in C

Introduction to Computing Lecture 03: Basic input / output operations

Computer Programing. for Physicists [SCPY204] Class 02: 25 Jan 2018

Programming in C and C++

Differentiate Between Keywords and Identifiers

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

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CSC231 C Tutorial Fall 2018 Introduction to C

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

Flow Control. CSC215 Lecture

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

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Outline. Welcome to C Syntax Variables Operations Function calls Control statement More data type Timer examples

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

Programming. Data Structure

6.096 Introduction to C++ January (IAP) 2009

2. Numbers In, Numbers Out

Preview from Notesale.co.uk Page 6 of 52

Reserved Words and Identifiers

PROGRAMMAZIONE I A.A. 2017/2018

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

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

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

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

Unit 3. Operators. School of Science and Technology INTRODUCTION

Dept. of CSE, IIT KGP

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

CS 61C: Great Ideas in Computer Architecture Introduction to C

Transcription:

The C language Introductory course #1

History of C Born at AT&T Bell Laboratory of USA in 1972. Written by Dennis Ritchie C language was created for designing the UNIX operating system Quickly adopted as a mainstream language because of its powerful features

Main Features of C (Reasonably) High level as well as (very) low level programming support User defined data structure, modular programming bit manipulation, efficient pointer manipulation, close to assembly language but much more comfortable Program portability

Applications of C Computer application implementation Embedded software Drivers and firmware programming Complex tools implementation such as verification and validation tools, interpreters, compilers Operating systems and OS extensions

Outline History, main features and applications of C Hello example coding, compiling and running Literals, constant, variables and types Basic input/output Expressions and statements

Let s go #include <stdlib.h> #include <stdio.h> int main(){ printf("hello"); // comment return EXIT_SUCCESS; }

Compilation: from the code to the executable # edition of the code > vi hello.c # compilation with the GNU compiler > gcc hello.c # running of the executable >./a.out # as a result, «Hello» output Hello>

Compilation: from the code to the executable # specifying the output name > gcc hello.c -o hello # running of the executable >./hello

Literals Integers: 123 Characters: 'a', 'B' Special characters: '\n', '\t' Strings: "ab \t123", "Hello\n" Floats: 1.23, 0.123, 1e+2 Arrays: { 1, 2, 3 }

Constants Two ways: with the const keyword or with a #define directive #define AGE int main { const int age = 45; printf("hello, I m %d years old", AGE); printf("hello, I m %d years old", age); return EXIT_SUCCESS; }

Variables A variable is a container that stores a data Must be declared before being used Declaration: a type followed by the variable name the type specifies the kind of data that can be stored int main { int age; // declaration age = 24; // storage of the integer 24 printf("hello, I m %d years old", age); return EXIT_SUCCESS; }

Types A type is an information used by the compiler to know the size of the container and to check that data are valid (type compatibility checked at compile time) Type informations are absent at run time

int main () { int age; age = "24"; printf("hello, I m %d years old", age); return EXIT_SUCCESS; } > gcc assign_error.c assign_error.c:5:6: warning: incompatible pointer to integer conversion assigning to 'int' from 'char [3]' [-Wint-conversion] age = "24"; ^ ~~~~ 1 warning generated >./a.out Hello, I m 166592382 years old

Main builtin types int, long int : for integers char : for characters float, double, long double : for real numbers Remarks: No String type : a string is an array of chars No boolean : 0 for false, whatever value except 0 for true A char is handled as an integer More builtin types exist

int main () { int age; age = 24; char gender = M ; float rate = 4.56754; int succes = EXIT_SUCCESS; > gcc name_age_gender.c >./a.out Name: Paulette Age: 24 Gender: M Rate: 4.567540 > } char * name = "Paulette"; printf("name: %s\n", name); printf("age: %d\n", age); printf("gender: %c\n", gender); printf("rate: %f\n", rate); return success;

Input/Output very basic approach value printing and input through 2 functions: printf and scanf: at least one string argument which is a template for the output or the input

printf One string argument, eventually followed by any number of arguments int main () { char * name = "Paulette"; int age = 67; printf("hello\n"); printf("\ti'm %s and I'm %d years old\n", name, age); %d for int, %c for char, %s for char *, %f for float, %d for double char * is for a pointer on character, we will see that later

scanf use a & followed by a variable name (need to pass variable address, we will explain that point later in deeper detail) int main () { char gender; int age; scanf("%d", &age); scanf("%c", &gender); printf("i'm %d years old [%c]\n", age, gender);

scanf It is also possible to read a string Useful for simple applications #include <stdio.h> int main( ) { char str[100]; printf( "Enter a string followed by a return:"); scanf("%s", str); printf( "You entered: [%s]\n", str); return 0; }

Operators Expressions are specified by using builtin operators 6 kind of operators: Assignment Operators Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Misc Operators

Assignment Operator Description Example = Simple assignment operator += Add AND assignment operator -= Subtract AND assignment operator *= Multiply AND assignment operator /= Divide AND assignment operator %= Modulus AND assignment operator

#include <stdio.h> main() { int a = 21; int c ; c = a; printf("line 1 - = Operator Example, Value of c = %d\n", c ); c += a; printf("line 2 - += Operator Example, Value of c = %d\n", c ); c -= a; printf("line 3 - -= Operator Example, Value of c = %d\n", c ); c *= a; printf("line 4 - *= Operator Example, Value of c = %d\n", c ); c /= a; printf("line 5 - /= Operator Example, Value of c = %d\n", c ); c = 200; c %= a; printf("line 6 - %= Operator Example, Value of c = %d\n", c ); }

Arithmetic, relational and logical operators Arithmetic operators +, -, *, /, % ++, Relational operators ==, =, <, <=, >, >= Logical operators &&,, Misc operators sizeof(), &, *,?:

Associativity and precedence Associativity : left to right except for + - ~ ++ - - (type)* & sizeof Precedence: From the highest to the lowest: () [] ->. ++ -- - & * (unary operators) * / % + - (binary operators) < > <= >= == = && = += -+ *= /= %=

main() { int a = 20; int b = 10; int c = 15; int d = 5; int e; e = (a + b) * c / d; // ( 30 * 15 ) / 5 printf("value of (a + b) * c / d is : %d\n", e); e = ((a + b) * c) / d; e ); // (30) * (15/5) printf("value of ((a + b) * c) / d is : %d\n", e); e = (a + b) * (c / d); // 20 + (150/5) printf("value of (a + b) * (c / d) is : %d\n", e); e = a + (b * c) / d; printf("value of a + (b * c) / d is : %d\n", e); return 0; }

Decision making if-else if-else if-else if-else switch?: operator

#include <stdio.h> int main () { int a = 100; if( a == 10 ) { printf("value of a is 10\n" ); } else if( a == 20 ) { printf("value of a is 20\n" ); } else if( a == 30 ) { printf("value of a is 30\n" ); } else { printf("none of the values is matching\n" ); } printf("exact value of a is: %d\n", a ); return 0; }

switch(ch1) { case 'A': printf("this A is part of outer switch" ); switch(ch2) { case 'A': printf("this A is part of inner switch" ); break; case 'B': /* case code */ } } break; case 'B': /* case code */

int main() { int num; printf("enter the Number : "); scanf("%d",&num); flag = ((num%2==0)?1:0); if(flag==0) printf("\neven"); else printf("\nodd"); } Can be coded as follow: int main() { int num; printf("enter the Number : "); scanf("%d",&num); (num%2 ==0)? printf("even") : printf("odd"); }

Loops Three loop statements while do-while for Loop related statements break continue

The while loop statement while ( <condition>) <statement> while ( <condition>){ <statement_1> <statement_n> }

A while loop example int sum = 0; int pos = 0; int val; while ( (val = array[pos] ) > -1 ) { sum += val; pos ++; }

A second while loop example int done = 0; int sum = 0; while ( done) { printf( enter 0 to exit, else enter any number\n ); scanf( %d, &done); sum += done; } printf ( the sum of entered integers is %d\, sum); Remark: the loop is run at least one time.

The do-while loop statement do <statement> while ( <condition>) ; do { <statement_1> <statement_n> } while ( <condition>) ;

A do-while loop example For the previous example, the do-while is more appropriate int done; int sum = 0; do { printf( enter 0 to exit, else enter any number\n ); scanf( %d, &done); sum += done; } while ( done); printf ( the sum of entered integers is %d\, sum);

The for loop statement for ( <initialization> ; <stop_condition> ; <increment>) <statement> for ( <initialization> ; <stop_condition> ; <increment>){ <statement_1> <statement_n> }

A for loop example int i; int tab[100]; for ( i = 0; i < 100; i++) tab[i] = 0;

We have learned How to code, compile and run a simple example How to write simple algorithms with variables, expressions and statements