Introduction to the C Programming Language

Similar documents
C: How to Program. Week /Mar/05

Chapter 2 - Introduction to C Programming

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

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

Chapter 1 & 2 Introduction to C Language

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

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

Lecture 02 C FUNDAMENTALS

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

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

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

C Language, Token, Keywords, Constant, variable

Data types, variables, constants

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

6.096 Introduction to C++ January (IAP) 2009

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

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

Fundamental of Programming (C)

Chapter 1 Introduction to Computers and C++ Programming

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

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

ANSI C Programming Simple Programs

Introduction to Computing Lecture 01: Introduction to C

Introduction to Programming

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

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


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

Program Control Structures

Full file at C How to Program, 6/e Multiple Choice Test Bank

A Fast Review of C Essentials Part I

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

ET156 Introduction to C Programming

Fundamentals of Programming

IS 0020 Program Design and Software Tools

Programming and Data Structures

Computers Programming Course 5. Iulian Năstac

The C language. Introductory course #1

Lecture 3. More About C

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

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

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

Data Types and Variables in C language

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

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

BLM2031 Structured Programming. Zeyneb KURT

Basic Elements of C. Staff Incharge: S.Sasirekha

PROGRAMMAZIONE I A.A. 2018/2019

Unit 4. Input/Output Functions

Presented By : Gaurav Juneja

UEE1302 (1102) F10: Introduction to Computers and Programming

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

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

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

DEPARTMENT OF MATHS, MJ COLLEGE

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

C-LANGUAGE CURRICULAM

Programming. C++ Basics

Chapter 2: Overview of C. Problem Solving & Program Design in C

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

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

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

EC 413 Computer Organization

CS Programming In C

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

C Overview Fall 2014 Jinkyu Jeong

Differentiate Between Keywords and Identifiers

ET156 Introduction to C Programming

Basic Types and Formatted I/O

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

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

C Programming Review CSC 4320/6320

Basic Types, Variables, Literals, Constants

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

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

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Input/Output Week 5:Lesson 16.1

Introduction to C# Applications

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

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

Syntax and Variables

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

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

C - Basic Introduction

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

Data Type Fall 2014 Jinkyu Jeong

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

Fundamentals of Programming Session 4

from Appendix B: Some C Essentials

Introduction to C Language

CMPE-013/L. Introduction to C Programming

The Design of C: A Rational Reconstruction (cont.)

INTRODUCTION 1 AND REVIEW

Fundamental of C programming. - Ompal Singh

Chapter 2, Part I Introduction to C Programming

Number Systems, Scalar Types, and Input and Output

Topic 6: A Quick Intro To C

The Design of C: A Rational Reconstruction: Part 2

Transcription:

Introduction to the C Programming Language Michael Griffiths Corporate Information and Computing Services The University of Sheffield Email m.griffiths@sheffield.ac.uk

Course Outline Part 1 Introduction to C Programming Application development tools Part 2 Structured Program Development and Program Control Part 3 Functions Pointers and Arrays Part 4 Characters and Strings Data Types Structures Part 5 File Processing Further Topics

Outline Introduction Structured program development Application development tools Compiling applications

Course Material C language (clanguage folder) Examples illustrating basics of C programming Case Studies (casestudies folder) Example research problems Data Analysis (patientdatastudy) Running Computational Models Monte Carlo (montecarlo) Numerical analysis Example c programs for basic numerical analysis applications

Development Environment for Building Programs Codeblocks Available on managed windows Free download from MinGW http://www.codeblocks.org/ Available on managed windows If you are a windows user MinGW can be downloaded from https://sourceforge.net/projects/mingw/files/mingw/ You should install the latest version. Information about MinGW is here http://www.mingw.org/

Examples C language Numerical Analysis Examples Scenarios Patient data analysis study Modelling the van-der-poll oscillator Monte-carlo Ising Model Solving the Schrodinger Equation - Numerov method for numerical integration

Introduction Developed late 70 s Used for development of UNIX Powerful If used with discipline ANSI Standard C C90 ANSI/ISO 9899: 1990 C11 ISO/IEC 9899:2011 (formerly C1X) the current standard for the C programming language, replaces the previous C standard, informally known as C99

Program Development Edit Create program and store on system Preprocessor Manipulate code prior to compilation Compiler Create object code and store on system Linker Link object code with libraries, create executable output Loader Execution CPU executes each instruction

Parts of C Language Keywords Functions Operators Values and variables

Keywords This is a list of reserved keywords in C. Since they are used by the language, these keywords are not available for re-definition. auto break case char const continue default do double else enum extern float for goto if inline (since C99) int long register restrict (since C99) return short signed sizeof static struct switch typedef union unsigned void volatile while

Functions Functions enable grouping of commonly used code into a reusable and compact unit. In programs containing many functions main should be implemented as a group of calls to functions undertaking the bulk of the work Become familiar with rich collections of functions in the ANSI C standard library Using functions from ANSI standard library increases portability

Operators Arithmetic operations =, -, /, %, * Assignment operations =, +=. -=, *=, %=, /=,! Increment and decrement (pre or post) operations ++, -- Logical operations, &&,! Bitwise operations, &, ~ Comparison <, <=, >, >=, ==,!=

Values and Variables A variable is a container for a value we can have different types Characters Integer Values Floating-point values Memory locations

Variables Variables of the same type are compared using the comparison operator == Variable declaration using the assignment operator = float myfloat; float fanother=3.1415927; Other types using unsigned and long long double, long int, short int, unsigned short int Precision and range machine dependent

Variable Types Type Size (Bytes) Lower Upper int 4-2 31 +2 31-1 float 4-3.2X10 32 3.2X10 32 double 8-1.7X10 302 1.7X10 302 char 1 - - unsigned char 1 0 255 unsigned short int 2 0 65536 short int 2-32768 32767

Program Structure Collection of Source files header files Resource files Source file layout Function layout Pre-processor directives Program starts with a function called main

program.c Source file layout Pre-processsor directives Global declarations main() {. } function1() { } function2() {. }

Function Layout vartype function(vartypes ) { local variables to function statements associated with function.... }

Invoking the Compiler Compiling FORTRAN Programs g77 o mycode [options] mycode.f Compiling c/c++ Programs gcc o mycode [options] mycode.c

Example - Demonstration Compile the program welcome to C Find the errors (the compiler will help) Add text to the puts statement in empty.c compile and run the code Challenge Add another puts statement to empty.c, compile and run the code

Hello World /*Program1: Hello World*/ #include <stdio.h> main() { printf("welcome to the White Rose Grid!\n"); /*Welcome banner on several lines*/ printf("welcome to the \n \t White Rose Grid!\n"); }

Features of Hello World Lots of comments Enclosed by /* */ Statements terminated with a ; Preprocessor statement #include <stdio.h> Enables functions to call standard input ouput functions (e.g. printf, scanf) Not terminated with a ; Printf uses escape sequence characters e.g. \n newline \t tab character

Standard Conforming Hello World /*Program1: Hello World*/ #include <stdio.h> int main(void) { printf("welcome to the White Rose Grid!\n"); /*Welcome banner on several lines*/ printf("welcome to the \n \t White Rose Grid!\n"); return 0; }

Compilation To compile the program myprog.c using the GNU C Compiler gcc myprog.c o myprog Example compile arith.c Modify program arith.c to test the effect of the decrement and increment operations Modify program arith.c and test the assignment operations

Input and Output To process data computer programs need to input that data Processed data needs to output to the user The puts function can be used to display output puts( Welcome );

Character Input and Output getchar() putchar() Require stdio.h header Work with integer values Stream functions

Input and Output Using stdio.h printf Provides formatted input and output Input for printf is a format specification followed by a list of variable names to be displayed printf( variable %d is %f\n, myint, myfloat); scanf Provided an input format and a list of variables scanf( %d, &myint); Note variable name has & in front Program arith.c is an example of the arithmetic operations, printf and scanf.

Input/Output Demonstration Examples in the folder clanguage/inputoutput Run the examples chario1...3.c Change split the line in chario1.c so that putchar is used chario2..3 demonstrate stream output Run the printf.c example Run scanf1.c and scanf2.c demonstrating string and integer input For the scanf1.c change the input to a float

Example of printf and scanf Example program arith.c Worksheet problem 3, use the decrement (--) and increment operators(++) to modify the variables sum and difference Add a line requesting the user to input a floating point variable called f1 Add a line to multiply the new variable f1 by the variable sum Add a line to display the variable f1 /*Request input from the user*/ printf("enter the first integer\n"); scanf("%d", &i1); /*Read in the integer*/ printf("enter the second integer\n"); scanf("%d", &i2);

Escape characters Escape Sequence Description \n Newline, position cursor at the start of a new line \t Horizontal tab, move cursor to the next tab stop \r Carriage return. Position cursor to the beginning of the current line; do not advance to the next line. \a Alert, sound system warning beep \\ Backslash, print a backslash character in a printf statement \ Double quote print a double quote character in a printf statement.

Format Specifiers for printf and scanf Printf specifier Scanf specifier Data Type long double %Lf %Lf double %f %lf float %f %f unsigned long int %lu %lu long int %ld %ld unsigned int %u %u int %d %d short %hd %hd char %c %c

Practical Examples basic coding Inspect, Compile and run the following Finding a root using the Bisection method Modify the input function and experiment with the bracketing of the root Control statements to be studied next session While statement If statement

Summary Program Structure Defining and using variables and values Displaying output Reading input Compilers and development Environments