Week 1 Lecture 2SE1SA51 Introducing C SE1SA5 Sue Walmsley.

Slides:



Advertisements
Similar presentations
Module 6: Introduction to C Language ITEI102 Introduction to Programming Structure of C++ Program - Programming Terminologies - Microsoft Visual Studio.
Advertisements

Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2.
Chapter 3: Beginning Problem Solving Concepts for the Computer Programming Computer Programming Skills /1436 Department of Computer Science.
Lecture 2 Introduction to C Programming
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.
Introduction to C Programming
Structure of a C program
Three types of computer languages
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
Introduction to C Programming
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
Computer Engineering 1 nd Semester Dr. Rabie A. Ramadan 2.
Programming With C.
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.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
Computer Programming I Hour 2 - Writing Your First C Program.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
1 MT258 Computer Programming and Problem Solving Tutorial 03.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
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.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
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.
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.
© Copyright by Deitel 1 Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple.
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
Chapter 1.2 Introduction to C++ Programming
Computer Science 210 Computer Organization
Chapter 1: Introduction to computers and C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
CSC201: Computer Programming
Introduction to C Language
Chapter 2 - Introduction to C Programming
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Computer Engineering 1nd Semester
C Short Overview Lembit Jürimägi.
Chapter 2 - Introduction to C Programming
Computer Science 210 Computer Organization
Introduction to the C Language
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
פרטים נוספים בסילבוס של הקורס
Chapter 2 - Introduction to C Programming
פרטים נוספים בסילבוס של הקורס
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Programming Fundamentals Lecture #3 Overview of Computer Programming
Introduction to C Topics Compilation Using the gcc Compiler
Programs written in C and C++ can run on many different computers
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Presentation transcript:

Week 1 Lecture 2SE1SA51 Introducing C SE1SA5 Sue Walmsley

Week 1 Lecture 2SE1SA52 Outline Why C? Standard C libraries A Program Comments Compiling and Running Variables Input/Output Data type qualifiers

Week 1 Lecture 2SE1SA53 Why C? C is a classic language which forms the foundation of many other programming languages C is widely available, under many operating systems As you study C (and then C++) you will learn the theory of programming and this will help you appreciate other languages  You will study C++ later this year Those following degree courses in the School of Systems Engineering (SSE) will go on to use both C and C++, and many of you will also utilise other languages

Week 1 Lecture 2SE1SA54 Standard C libraries ANSI C defines a number of standard libraries The libraries have “header files” which normally have the extension “.h” For example, most C programs use: stdio.h  Contains input and output functions, types and macro definitions.

Week 1 Lecture 2SE1SA55 Other libraries include: ctype.h string.h math.h stdlib.h time.h limits.h float.h Look these up and find out what they contain Hint: before writing something complicated check if there is a library function that does it

Week 1 Lecture 2SE1SA56 A Program #include /* this program produces three lines of output */ int main ( ) { printf("This module is called: \n"); printf("\t Programming \n"); printf("This program is written in: "); printf("C\n"); return 0; /* returns successfully */ }

Week 1 Lecture 2SE1SA57 Notes on the program Save it as name.c C always need a main( ) function  This one returns an int 0 Lines starting # are instructions to the preprocessor Names of library files must be in angular brackets The braces { and } contain statements to be executed In C semicolons ( ; ) are used to terminate statements C is free format – line breaks and white space can go between items C is case sensitive Int is different to int

Week 1 Lecture 2SE1SA58 Comments and coding standards In C comments are delimited by /* and */ Use comments to make your program more easily understood by other people and yourself Commercial firms often have standards for coding and these will include how to comment

Reproduced from the PowerPoints for C How to Program, 4/e by Deitel and Deitel Reproduced by permission of Pearson Education, Inc. (selected and adapted by SMW) Phases of C Programs: 1.Edit 2.Preprocess 3.Compile 4.Link 5.Load 6.Execute Program is created in the editor and stored on disk. Preprocessor program processes the code. Loader puts program in memory. CPU takes each instruction and executes it, possibly storing new data values as the program executes. Compiler creates object code and stores it on disk. Linker links the object code with the libraries Loader Primary Memory Compiler Editor Preprocessor Linker Primary Memory Dis k CPU Dis k Compiling and Running

Week 1 Lecture 2SE1SA510 Variables A variable is like a container in a C program in which a data value can be stored in the computer memory Variable names:  Consist of letters, numbers and underscores ( _ )  CANNOT start with a number  CANNOT be C keywords For example:  Num1, num1, _answer

Week 1 Lecture 2SE1SA511 Example of variable use int num1, num2; int sum; num1 = 2; num2 = 5; sum = num1 + num2; num1num2sum

Week 1 Lecture 2SE1SA512 Basic types Data typeDescriptionExample charA single byte, holds one character 'A' intAn integer whole number 12 floatA floating point number using 32 bits doubleMore accurate, using 64 bits Look up bits and bytes

Week 1 Lecture 2SE1SA513 Data types and printing them #include /* illustrates printf */ int main ( ) { int num =12; char letter; float fNum; letter = 'A'; fNum = 12.34; printf ("num is %d \n", num); printf ("letter is %c \n", letter); printf ("fNum is %f \n", fNum); return 0; }

Week 1 Lecture 2SE1SA514 Output num is 12 letter is A fNum is

Week 1 Lecture 2SE1SA515 Using scanf to read values into variables int num =12; char letter; float fNum; printf ("please enter a character: "); scanf ("%c", &letter); printf ("please enter a float: "); scanf ("%f", &fNum); printf ("num is %d \n", num); printf ("letter is %c \n", letter); printf ("fNum is %f \n", fNum); return 0; Note with scanf you must use the ampersand (&) with basic data types (such as int). In this case & is the “addressof” operator and tells the program to store the data in the variable name that follows.

Week 1 Lecture 2SE1SA516 Output please enter a character: A please enter a float: num is 12 letter is A fNum is

Week 1 Lecture 2SE1SA517 Formatting Output (int) int num =12; printf ("num using %d is %d \n", num); printf ("num using %1d is %1d \n", num); printf ("num using %2d is %2d \n", num); printf ("num using %3d is %3d \n", num); printf ("num using %4d is %4d \n", num); printf ("num using %05d is %05d \n", num); printf ("num using %06d is %06d \n", num); printf ("-num using %4d is %4d \n", -num);

Week 1 Lecture 2SE1SA518 Output num using %d is 12 num using %1d is 12 num using %2d is 12 num using %3d is 12 num using %4d is 12 num using %05d is num using %06d is num using %4d is -12

Week 1 Lecture 2SE1SA519 Formatting Output (floats) float fNum = ; printf ("fNum using %8.0f is %8.0f end\n", fNum); printf ("fNum using %-8.0f is %-8.0f end\n", fNum); printf ("fNum using %8.2f is %8.2f end\n", fNum); printf ("fNum using %-8.4f is %-8.4f end\n", fNum);

Week 1 Lecture 2SE1SA520 Output fNum using %8.0f is 12 end fNum using %-8.0f is 12 end fNum using %8.2f is end fNum using %-8.4f is end Note the rounding in the third line.

Week 1 Lecture 2SE1SA521 Data type qualifiers #include int main ( ) { short int small; long int tall; printf ("Max int:\t %d\n", INT_MAX); printf ("Max long int:\t %d\n", LONG_MAX); printf ("Max shortint:\t %d\n", SHRT_MAX); printf ("Min int:\t %d\n", INT_MIN); printf ("Min long int:\t %d\n", LONG_MIN); printf ("Min short int:\t %d\n", SHRT_MIN); return 0; }

Week 1 Lecture 2SE1SA522 Output Max int: Max long int: Max shortint: Min int: Min long int: Min short int:

Week 1 Lecture 2SE1SA523 sizeof operator printf ("sizeof small %d\n", sizeof (small)); printf ("sizeof tall %d\n", sizeof (tall)); printf("sizeof short int %d\n", sizeof (short int)); printf("sizeof float %d\n", sizeof (float)); printf("sizeof double %d\n", sizeof (double)); -- sizeof small 2 sizeof tall 4 sizeof short int 2 sizeof float 4 sizeof double 8

Week 1 Lecture 2SE1SA524 Summary Standard C libraries Programs and comments Compiling and Running Variables Input/Output Data type qualifiers Next week –will include  External, static, register variables  Casting  Arrays  Constants and enumeration  Preprocessor directives 