Introduction to Computing Lecture 01: Introduction to C

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

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

Presented By : Gaurav Juneja

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

ET156 Introduction to C Programming

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

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

Chapter 1 & 2 Introduction to C Language

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

ET156 Introduction to C Programming

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

A Fast Review of C Essentials Part I

C: How to Program. Week /Mar/05

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

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

6.096 Introduction to C++ January (IAP) 2009

Chapter 2 - Introduction to C Programming

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

BLM2031 Structured Programming. Zeyneb KURT

Data Types and Variables in C language

CMPE-013/L. Introduction to C Programming

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

Introduction to the C Programming Language

C Language, Token, Keywords, Constant, variable

VARIABLES AND CONSTANTS

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

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

Syntax and Variables

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

DEPARTMENT OF MATHS, MJ COLLEGE

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

Programming. C++ Basics

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

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

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

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Programming and Data Structures

Fundamental of Programming (C)

C Overview Fall 2014 Jinkyu Jeong

INTRODUCTION 1 AND REVIEW

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

CMSC 246 Systems Programming

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso

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

Fundamentals of Programming

Procedures, Parameters, Values and Variables. Steven R. Bagley

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

Preview from Notesale.co.uk Page 6 of 52

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

Data types, variables, constants


ANSI C Programming Simple Programs

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

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

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

printf( Please enter another number: ); scanf( %d, &num2);

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

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

Basic Elements of C. Staff Incharge: S.Sasirekha

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

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Differentiate Between Keywords and Identifiers

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

Lecture 02 C FUNDAMENTALS

AMCAT Automata Coding Sample Questions And Answers

Fundamental of C programming. - Ompal Singh

LECTURE 02 INTRODUCTION TO C++

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

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

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.

Programming in C. What is C?... What is C?

Programming in C and C++

Computers Programming Course 5. Iulian Năstac

UEE1302 (1102) F10: Introduction to Computers and Programming

from Appendix B: Some C Essentials

CHW 469 : Embedded Systems

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

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

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

Tokens, Expressions and Control Structures

Programming. Data Structure

Fundamentals of Programming. Lecture 3: Introduction to C Programming

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

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

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

Programming in C and C++

Introduction to Computing Lecture 09: Functions (Part II)

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

Lecture 2: C Programming Basic

C Programming Multiple. Choice

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

Variables and Constants

Chapter 15 - C++ As A "Better C"

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

Chapter 2: Introduction to C++

Tutorial No. 2 - Solution (Overview of C)

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Transcription:

Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering ozbek.nukhet@gmail.com

Topics Introduction to C language Basic Components of C

From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: talking to humans, easy to understand in plain (English) language Program: talking to computer (compiler) can be regarded as a formal expression of an algorithm

What is a program? A program is a way of solving some problem-a set directives (or instructions) stating how to go about achieving to some desired results Thus program must provide a precise list of instructions stating what you want done, which someone or something could follow in order to achieve the task

Programming Language Terms A computer program is a precise list of instructions, or statements, to solve a particular problem, specified in a programming language Effectively, the computer obey or executes the statements in the program, creating and manipulating objects, following the algorithm expressed in the program It is useful to have some idea of what is going on behind the scenes in getting a program to execute

How does a program get executed? The programs you write are expressed in a programming language and stored in the computer as a sequence of printable characters in a file-called the source file In this source form, the program cannot be directly executed by the computer The language and instructions that the computer understands and obeys are very different: * expressed as sequences of binary digits/bits (0 s and 1 s) * very low-level and general purpose Source file has to be translated into a form that the computer can execute

Compilers and linkers translate a high level program into executable machine code. High-Level Language #include <stdio.h> int main() { printf( Hello ); } return 0; 10100110 01110110 00100110 00000000 11111010 11111010 01001110 10100110 11100110 10010110 11001110 00101110 10100110 01001110 11111010 01100110 01001110 10000110 etc... Source code Executable code

Why C? Structured language Standard library exists, allowing portability Wide availability on a variety of computers Low level activities possible It can produce lean and efficient code Widely used Has great influence on many other popular languages

History of C CPL Combined Programming Language (Barron et al., 1963) BCPL Basic CPL (Richards, 1969) B (Thompson, 1970) C K&R C (Ritchie, 1972) (for use in UNIX operating system) ANSI C American National Standards Institute C (X3J11, 1989) C9X (JTC1/SC22/WG14, ISO/IEC 9899)

C++? Derived from C Object-oriented functionality with C-like syntax Nearly a superset of C -> C++ compilers can compile C code

Basic Structure of a C Program Example: it s too easy to learn C Algorithm: output it s too easy to learn C C Program: #include <stdio.h> int main() { printf( it s too easy to learn C ); } return 0;

Basic Structure of a C Program Example: it s too easy to learn C C Program: #include <stdio.h> Skeleton int main() { printf( it s too easy to learn C ); } return 0;

Basic Structure of a C Program Example: it s too easy to learn C Also: void main() { C Program: #include <stdio.h> int main() { printf( it s too easy to learn C ); } } return 0; Not recommended

Basic Structure of a C Program Example: it s too easy to learn C Also: C Program: #include <stdio.h> main() { int main() { printf( it s too easy to learn C ); } return 0; } return 0; Assumes int

Basic Structure of a C Program Example: it s too easy to learn C Also: main() { C Program: #include <stdio.h> int main() { printf( it s too easy to learn C ); } return 0; } return 0; Warning messages: Return value expected

Basic Structure of a C Program Example: it s too easy to learn C C Program: #include <stdio.h> Includes standard input / output library of functions. Read: Hash-include int main() { printf( it s too easy to learn C ); } return 0;

Basic Structure of a C Program Example: it s too easy to learn C C Program: #include <stdio.h> Brackets mark the beginning and end of a block of instructions. int main() { printf( it s too easy to learn C ); } return 0;

Basic Structure of a C Program Example: it s too easy to learn C C Program: Instruction (function call) to output it s too easy to learn C #include <stdio.h> int main() { printf( it s too easy to learn C ); } return 0;

Basic Structure of a C Program Example: it s too easy to learn C Statements (lines of instructions) end with a semi-colon (;) C Program: #include <stdio.h> int main() { printf( it s too easy to learn C ); } return 0;

C Language Elements in Miles-to-Kilometers Conversion Program

Preprocessor Directives Begin with # C language defines only a small number of operations C implementations contain collections of useful functions and symbols called libraries C can be expanded with additional libraries, programmers can add their own libraries

Preprocessor Directives #include directive gives a program access to a library (Read: hash-include) #include <stdio.h> #define directive instructs the preprocessor to replace each occurrence of statement used in #define with the supplied value Use #define statements for data that changes rarely

Syntax for #include directive #include <standard header file> #include <stdio.h> #include <math.h>

Syntax for #define directive #define NAME value #define MILES_PER_KM 0.62137 #define PI 3.141493 #define MAX_LENGTH 100

Comments Start with /* and end with */ Provide supplementary information making it easier to understand the program Comments are ignored by the compiler

Function main Every C program must have a main function Valid forms are int main(void) int main(void) int main()

Function main Code betwen { and } is called function body int main(void) { } function body

Reserved Words All appear in lowercase Have special meaning in C and cannot be used for other purposes

ANSI C Reserved Words auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

Standard Identifiers Identifiers from standard library like printf, scanf Can be redefined and used by the programmer BUT IT IS NOT RECOMMENDED! If you redefine a standard identifier, you cannot use it for its original purpose

User-Defined Identifiers Variable names, function names, etc MILES_PER_KM miles, kms

Rules for User-Defined Identifiers 1. Must consist only of letters, digits, and underscores letter_1 -> VALID inches -> VALID Hello -> VALID TWO*FOUR -> INVALID joe s -> INVALID

Rules for User-Defined Identifiers 2. Can not begin with a digit 1letter -> INVALID 3. A C reserved word cannot be used as an identifier double -> INVALID 4. An identifier defined in a C standard library should not be redefined RECOMMENDATION

Uppercase and Lowercase Letters Identifiers in C are case-sensitive Rate, rate and RATE are different identifiers Use your own style, but as a recommendation Use all uppercase letters in constant definitions (#define statements) Use all lowercase or first letters uppercase, other lower case for other identifiers Miles or miles

Choosing Identifier Names A program that looks good is easier to read and understand Most programs are examined by someone other than the original programmer and generally more time is spent on the maintenance of the programs than time spent is writing the original program A neatly stated and clear program is easier to understand

Choosing Identifier Names It is a good practice to choose meaningful names for user-defined identifiers Use salary instead of s or sal, etc. If an identifier consists two or more words use an underscore between words Use dollars_per_hour instead of dollarsperhour

Choosing Identifier Names Choose identifiers long enough to convey the meaning, but avoid excessively long names because of possible typing errors Avoid using similar names for different identifiers that may cause confusion For example, do not use LARGE and large, xcoord and x_coord together