1 ICS103 Programming in C Lecture 2: Introduction to C (1)

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
TDBA66, VT-03 Lecture - Ch. 21 A complete C-program Display Fig. 2.1 and comment on different things such as Preprocessor directives Header files Identifiers.
1 CS 201 Introduction to C (1) Debzani Deb. 2 Outline Overview of C General form of a C program C Language Elements.
Software Development Method & C Language Elements H&K Chapter 1-2
Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Structure of a C program
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
Topic 2 – Introduction to the C Programming Language.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Guide To UNIX Using Linux Third Edition
C programming Language and Data Structure For DIT Students.
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
Computer Science 210 Computer Organization Introduction to C.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CPCS 202 Chapter 2 – Input/Output
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
(1 - 2) C Language Elements H&K Chapter 2 Instructor - Andrew S. O’Fallon CptS 121 (August 28, 2015) Washington State University.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Programming With C.
King Saud University College of applied studies and community services CSC 1101 Computer Programming I Lecture 2.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to Programming
ELE118 Introduction to Programming
CC112 Structured Programming Lecture 2 1 Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Overview of C. C—a high-level programming language developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories. We will discuss: –the elements of a.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CISC105 – General Computer Science Class 2 – 6/7/2006.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
An overview of C Language. Overview of C C language is a general purpose and structured programming language developed by 'Dennis Ritchie' at AT &T's.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
INTRODUCTION TO PROGRAMING System Development Mansoura October 2015.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 1.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Lecture2.
CSCE 206 Structured Programming in C
Chapter 2 - Introduction to C Programming
What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 2 - Introduction to C Programming
Lecture2.
Compiled and ready to run Memory Stack /*
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to CS Your First C Programs
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Lecture3.
C programming Language
Chapter 2 - Introduction to C Programming
Presentation transcript:

1 ICS103 Programming in C Lecture 2: Introduction to C (1)

2 Outline Overview of C  History & Philosophy  Why C?  What’s Missing? General form of a C program C Language Elements  Preprocessor Directives  Comments  The “main” function  Variable Declarations and Data Types  Executable Statements  Reserved Words  Identifiers

3 History & Philosophy C is developed in 1972 by Dennis Ritchie at the AT&T Bell Laboratories for use with the Unix. The most commonly used programming language for writing system software. Machine independent: by minimal change in source code, can be compiled in a wide variety of platform and operating system.

4 Why C? Many, many companies/research projects do all their programming in C. Looks good on your resume. Small, compact code. Produces optimized programs that run faster. Low-level access to computer memory via machine addresses and pointers. Low level (BitWise) programming readily available. Can be compiled on a variety of computers.

5 What’s Missing? Poor error detection which can make it difficult to use for the beginner  No automatic garbage collection.  No bounds checking of arrays and allocated memory segments.  No exception handling. No native support for multithreading and networking, though these facilities are provided by popular libraries No standard libraries for graphics and several other application programming needs

6 A Simple, Example C Program /* helloworld.c */ #include int main(void) { printf("Hello World!\n"); return(0); } Every C program has a main function. printf is also the name of a function This program can use the printf function, because of the line #include in the source code.

7 General Form of a C program Executable statements are translated into machine language and eventually executed. Executable statements perform computations on the declared variables or input/output operations. Preprocessor directives are instructions to C Preprocessor to modify The text of a C program before compilation. Every variable has to be declared first.

8 Preprocessor Directives /* Converts distances from miles to kilometers */ #include /* printf, scanf definitions */ #define KMS_PER_MILE 1.609/* conversion constant */ int main(void) { double miles,//distance in miles kms;//equivalent distance in kilometers //Get the distance in miles printf("Enter the distance in miles> "); scanf("%lf", &miles); //Convert the distance to kilometers kms = KMS_PER_MILE * miles; //Display the distance in kilometers printf("That equals %f kilometers.\n", kms); return (0); }

9 Preprocessor Directives Preprocessor directives are commands that give instructions to the C preprocessor. Preprocessor is a system program that modifies a C program prior to its compilation. Preprocessor directives begins with a #  Example: #include or #define

10 #include #include is used to include other source files into your source file. The #include directive gives a program access to a library. Libraries are useful functions and symbols that are predefined by the C language (standard libraries).  Example: You must include stdio.h if you want to use the printf and scanf library functions.  # include insert their definitions to your program before compilation.

11 #define The #define directive instructs the preprocessor to replace each occurrence of a text by a particular constant value before compilation. #define replaces all occurrences of the text you specify with value you specify  Example: #define KMS_PER_MILES 1.60 #define PI

12 Comments /* Converts distances from miles to kilometers */ #include /* printf, scanf definitions */ #define KMS_PER_MILE 1.609/* conversion constant */ int main(void) { double miles,//distance in miles kms;//equivalent distance in kilometers //Get the distance in miles printf("Enter the distance in miles> "); scanf("%lf", &miles); //Convert the distance to kilometers kms = KMS_PER_MILE * miles; //Display the distance in kilometers printf("That equals %f kilometers.\n", kms); return (0); }

13 Comments Comments provide supplementary information making it easier for us to understand the program, but are ignored by the C compiler. Two forms of comments:  /* */ - anything between them will be considered a comment, even if they span multiple lines.  // - anything after this and before the end of the line is considered a comment. Comments are used to create Program Documentation  Information that help others read and understand the program. The start of the program should consist of a comment that includes programmer’s name, date of the current version, and a brief description of what the program does. Always Comment your Code!

14 The “main” Function /* Converts distances from miles to kilometers */ #include /* printf, scanf definitions */ #define KMS_PER_MILE 1.609/* conversion constant */ int main(void) { double miles,//distance in miles kms;//equivalent distance in kilometers //Get the distance in miles printf("Enter the distance in miles> "); scanf("%lf", &miles); //Convert the distance to kilometers kms = KMS_PER_MILE * miles; //Display the distance in kilometers printf("That equals %f kilometers.\n", kms); return (0); }

15 The “main” Function The heading int main(void) marks the beginning of the main function where program execution begins. Every C program has a main function. Braces ({,}) mark the beginning and end of the body of function main. A function body has two parts:  declarations - tell the compiler what memory cells are needed in the function  executable statements - (derived from the algorithm) are translated into machine language and later executed by the compiler.

16 Variables and Data Types /* Converts distances from miles to kilometers */ #include /* printf, scanf definitions */ #define KMS_PER_MILE 1.609/* conversion constant */ int main(void) { double miles,//distance in miles kms;//equivalent distance in kilometers //Get the distance in miles printf("Enter the distance in miles> "); scanf("%lf", &miles); //Convert the distance to kilometers kms = KMS_PER_MILE * miles; //Display the distance in kilometers printf("That equals %f kilometers.\n", kms); return (0); }

17 Variables Declarations Variable – The memory cell used for storing a program’s data and its computational results  Variable’s value can change.  Example: miles, kms Variable declarations –Statements that communicates to the compiler the names of variables in the program and the kind of information they can store.  Example: double miles Tells the compiler to create space for a variable of type double in memory with the name miles.  C requires you to declare every variable used in the program.

18 Data Types Data Types: a set of values and a set of operations that can be performed on those values  int: Stores integer values – whole numbers 65,  double: Stores real numbers – numbers that use a decimal point or 1.23e5 (which equals )  char: An individual character value. Each char value is enclosed in single quotes. E.g. ‘A’, ‘*’. Can be a letter, a digit, or a special symbol  Arithmetic operations (+, -, *, /) and compare can be performed in case of int and double. Compare can be performed in char data.