Introduction to C Programming

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 2. Today’s Lecture Software Categories Software Categories System Software System Software Application Software Application.
Advertisements

Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 1 – Introduction to Computers and C++ Programming Outline 1.6 Machine Languages, Assembly Languages,
COSC 120 Computer Programming
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
Three types of computer languages
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
If You Missed Last Week Go to Click on Syllabus, review lecture 01 notes, course schedule Contact your TA ( on website) Schedule.
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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
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.
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 1 Introduction to Programming. Computer Hardware CPU Memory –Main or primary –Secondary or auxiliary Input device(s) Output device(s)
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Introduction to C/C++ Programming This lecture has major focus on programming, compilation.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 1 Introduction to Computers and Programming.
Why Program? Computer – programmable machine designed to follow instructions Program – instructions in computer memory to make it do something Programmer.
Chapter Introduction to Computers and Programming 1.
COMPUTER SCIENCE I C++ INTRODUCTION
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
History of C and C++ C++ evolved from C ANSI C C++ “spruces up” C
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Computer Engineering 1 nd Semester Dr. Rabie A. Ramadan 2.
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice:
Programming With C.
Lecture #5 Introduction to 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.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
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.
"Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.
Introduction to C 2008/09/29: Lecture 7 CMSC 104, Section 0101 John Y. Park 1.
CMSC 104, Lecture 111 Introduction to C Topics l Compilation l Using the gcc Compiler l The Anatomy of a C Program l 104 C Programming Standards and Indentation.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
CS-303 Introduction to Programming
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Introduction to Programming Lesson 3. #include #include main ( ) { cout
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 1: Introduction to Computers and Programming.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
12/06/ :04:41 CSC Alliance — 1 Kimera Richard Phone: INSTITUTE OF COMPUTER SCIENCE DEPARTMENT.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 1.
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
Introduction to C Topics Compilation Using the gcc Compiler
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 1: Introduction to computers and C++ Programming
Basic Elements of C++.
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.
Objectives Identify the built-in data types in C++
Computer Engineering 1nd Semester
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Topics Compilation Using the gcc Compiler
Basic Elements of C++ Chapter 2.
Introduction to C Topics Compilation Using the gcc Compiler
2008/09/29: Lecture 7 CMSC 104, Section 0101 John Y. Park
Introduction to C Topics Compilation Using the gcc Compiler
Programs written in C and C++ can run on many different computers
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Programming
Presentation transcript:

Introduction to C Programming Lecture 2

Today’s Lecture Software Categories System Software Application Software Introduction to ‘C’ Language History Evolution Justification Development Environment of ‘C’

There are two main categories of software System software Application Software

Technology Without An Interesting Name TWAIN Technology Without An Interesting Name

Evolution of programming languages The lack of portability between different computers led to the development of high-level languages—so called because they permitted a programmer to ignore many low-level details of the computer's hardware Details of procedural, non-procedural will follow in the lectures

How people used to program Machine Language….. Damn! It was difficult Assembly Language…. Remember ADD? Required too much user involvement To much to remember Less semantic C Language B Language.. Bell Labs Improved to C Language Is a compiled language

ANSI C

Tools of the trade Editor Interpreter and Compilers Debuggers

Integrated Development Environment (IDE) It contains Editor Compilers Debugger Linkers Loaders

Loader puts program in memory. 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   . Disk CPU Program is created in the editor and stored on disk.

#include <iostream.h> main ( ) { cout << “ Welcome to NIIT “; }

Variable X Variable

Variable Pic of the memory 25 10323 name of the variable

Variable Variable starts with Character Underscore _ (Not Recommended)

Variable Small post box X

Variable Variable is the name of a location in the memory e.g. x= 2;

Variable In a program a variable has: Name Type Size Value

Assignment Operator = x = 2 X 2

Assignment Operator L.H.S = R.H.S. X+ 3 = y + 4 Wrong Z = x +4 x +4 = Z Wrong

10 X X = 10 ; X = 30 ; 30 X

X = X + 1; X 10 + 1 = 11 X

Data type i int i ; //Declaration line

Writing C program

Examples of C/C++ compilers of today: Compiler converts human readable language to a language which is understandable by the operating system/hardware Examples of C/C++ compilers of today: Visual C++ GCC/G++ DJGPP (open source for windows like GCC) Borland C Turbo (obsolete and not recommended)

3 Stages of Compilation Stage 1: Preprocessing Performed by a program called the preprocessor Modifies the source code (in RAM) according to preprocessor directives (preprocessor commands) embedded in the source code Strips comments and white space from the code The source code as stored on disk is not modified.

3 Stages of Compilation (con’t) Stage 2: Compilation Performed by a program called the compiler Translates the preprocessor-modified source code into object code (machine code) Checks for syntax errors and warnings Saves the object code to a disk file, if instructed to do so (we will not do this). If any compiler errors are received, no object code file will be generated. An object code file will be generated if only warnings, not errors, are received.

Object code: It is machine language code containing various calls specific to operating system… e.g the object code written by compiler is not only hardware dependent but also operating system dependent. So if you have linux and windows both operating systems then object file of compiled by one Operating System (OS) will not get executed on the other OS

3 Stages of Compilation (con’t) Stage 3: Linking Combines the program object code with other object code to produce the executable file. The other object code can come from the Run-Time Library, other libraries, or object files that you have created. Saves the executable code to a disk file. On the Linux system, that file is called a.out. If any linker errors are received, no executable file will be generated.

Program Development Using gcc Editor Source File pgm.c Preprocessor Modified Source Code in RAM Compiler Program Object Code File pgm.o Other Object Code Files (if any) Linker Executable File a.out

#include <iostream.h> //This is pre-processor directive void main ( ) //this tells the starting point of your program { int x ; int y ; int z ; x = 10 ; y = 20 ; z = x + y ; cout << " x = " ; //print the text on monitor cout << x ; cout << " y = " ; cout << y ; cout << " z =x + y = " ; cout << z ; }

int x, y, z ; int x; int y; int z ;

Data Types int short long float double char

Arithmetic operators Plus + Minus - Multiply * Divide / Modulus %

Arithmetic operators i + j x * y a / b a % b

% = Remainder 5 % 2 = 1 2 % 2 = 0

4 / 2 = 2 5 / 2 = ?

Precedence Highest: ( ) Next: * , / , % Lowest: + , -

Another C++ Program /***************************************** ** File: proj1.c ** Author: Joe Student ** Date: 9/15/01 ** SSN: 123-45-6789 ** Section: 0304 ** E-mail: jstudent22@umbc.edu.pk ** ** This program prompts the user for two integer values then displays ** their product. ***********************************************/

Another C Program (con’t) #include <stdio.h> int main( void ) { int value1, value2, product ; cout<<“Enter two integer values: ; cin>>value1>>value2; product = value1 * value2 ; cout<<“Product = ”<<product ; return 0 ; }

Tokens The smallest element in the C language is the token. It may be a single character or a sequence of characters to form a single item.

Tokens are: Tokens can be: Numeric constants Character constants String constants Keywords Names (identifiers) Punctuation Operators

Numeric Constants Numeric constants are an uninterrupted sequence of digits (and may contain a period). They never contain a comma. Examples: 123 98.6 1000000

Character Constants Singular! One character defined character set. Surrounded on the single quotation mark. Examples: ‘A’ ‘a’ ‘$’ ‘4’

String Constants A sequence characters surrounded by double quotation marks. Considered a single item. Examples: “UMBC” “I like ice cream.” “123” “CAR” “car”

Keywords Sometimes called reserved words. Are defined as a part of the C language. Can not be used for anything else! Examples: int while for

Names Sometimes called identifiers. Can be of anything length, but on the first 31 are significant (too long is as bad as too short). Are case sensitive: abc is different from ABC Must begin with a letter and the rest can be letters, digits, and underscores. Must follow the standards for this course!

Punctuation Semicolons, colons, commas, apostrophes, quotation marks, braces, brackets, and parentheses. ; : , ‘ “ [ ] { } ( )

Operators There are operators for: assignments mathematical operations relational operations Boolean operations bitwise operations shifting values calling functions subscripting obtaining the size of an object obtaining the address of an object referencing an object through its address choosing between alternate subexpressions