B. Ramamurthy University at Buffalo

Slides:



Advertisements
Similar presentations
An Introduction to Programming By :- Vishal Hirani B.Tech II year (CSE)
Advertisements

Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
CSE1301 Computer Programming Lecture 4: C Primitives I.
C workshop Yuli Kaplunovsky - Today - Introduction to C Recommended book: The C programming Language / Kernighan & Ritchie.
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.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
Introduction to C Language
Computer Science 210 Computer Organization Introduction to C.
CIS Computer Programming Logic
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Programming With C.
Engineering Computing I Chapter 1 – Part A A Tutorial Introduction.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Fundamental Programming: Fundamental Programming Introduction to 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.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Khalid Rasheed Shaikh Computer Programming Theory 1.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
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.
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.
Introduction to C Topics Compilation Using the gcc Compiler
Last week: We talked about: History of C Compiler for C programming
CSCE 206 Structured Programming in C
Computer Science 210 Computer Organization
BASIC ELEMENTS OF A COMPUTER PROGRAM
GC211Data Structure Lecture2 Sara Alhajjam.
ITEC113 Algorithms and Programming Techniques
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.
Revision Lecture
Introduction to C Topics Compilation Using the gcc Compiler
Getting Started with C.
Lecture 13 & 14.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Introduction to C Programming Language
Lecture2.
B. Ramamurthy University at Buffalo
Computer Science 210 Computer Organization
B. Ramamurthy University at Buffalo
B. Ramamurthy University at Buffalo
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to Programming
פרטים נוספים בסילבוס של הקורס
CS1100 Computational Engineering
B. Ramamurthy University at Buffalo
Programming Funamental slides
פרטים נוספים בסילבוס של הקורס
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
C Programming Getting started Variables Basic C operators Conditionals
Introduction to C Topics Compilation Using the gcc Compiler
Let’s start from the beginning
Unit 3: Variables in Java
DATA TYPES There are four basic data types associated with variables:
Course Outcomes of Programming In C (PIC) (17212, C203):
Presentation transcript:

B. Ramamurthy University at Buffalo bina@buffalo.edu The C Language B. Ramamurthy University at Buffalo bina@buffalo.edu Amrita-UB-MSES-CSE524-2013-3 5/10/2013

Introduction About C language: “.. features economy of expression..”; written for Unix Operating system (1978) The C Language since then has taken a life of it own and has become the foundation for many modern languages. It has also become a language of choice for RTOS. Reference: The C Programming Language by Kernighan & Ritchie (available online) We will learn C by repeated spiral mode hands-on exposure to various elements of the language We will also try to work on the Linux system and another system called Nexos (Next generation embedded operating system) at the CSE department at UB. Amrita-UB-MSES-CSE524-2013-3 5/10/2013

Program Structure A C program is a collection of functions with at least one function called “main” Here is the classical example that has become a metaphor for a first program in any language. Hello World: lets compile it and see what happens. #include <stdio.h> int main () { printf(“Hello World \n”); return 0; } Amrita-UB-MSES-CSE524-2013-3 5/10/2013

Processing the C program Save the program in a file called “hello.c” Compile it using an appropriate compiler CC or cc or gcc or g++ (where G/g stands for “gnu” organization) Compiler parses the input, checks for syntax correctness and if syntax is correct generates code; This code is further linked and loaded to genenerate the executable. Source code  Compile  loader/linker  executable code Object code Amrita-UB-MSES-CSE524-2013-3 5/10/2013

C program structure Program Directives (#include libraries) functions statements Variables/constants Different types of statements Sequential, assignment, selection, iterative, input/output Amrita-UB-MSES-CSE524-2013-3 5/10/2013

Variables and arithmetic expressions #include <stdio.h> /* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; } Amrita-UB-MSES-CSE524-2013-3 5/10/2013

Lets analyze the program #include directive Comment // single line comment /* multiple line comment */ 3. Main function 4. Variable declarations {variable type, variable name} int step; 5. Initialization: step= 20; 6. Statements: computations; arithmetic operations {+, -, *, /, %} 7. Repeat computation using a “while loop” 8. Condition for repetition 9. Output results using “printf” 10. Semicolon (;) as a terminator for statements Amrita-UB-MSES-CSE524-2013-3 5/10/2013

Variable types int: integer; for representing whole numbers float : floating point or real numbers; for representing fractional numbers, vary large and very small numbers (32 bits) double: double precision real number; double the size of float (64 bits) char: single ASCII (American Standard Code of Information Interchange) character long: longer integer Amrita-UB-MSES-CSE524-2013-3 5/10/2013

Variable names A sequence of characters used for identifying an entity/item used in the program Example: partNum voltage portNum myName ECUNum Amrita-UB-MSES-CSE524-2013-3 5/10/2013

Assignment statement Assignment operator = Syntax Variable = expresion; partNum = 84560; double temp = 89.5; int age = 78; pay = salary + bonus; Amrita-UB-MSES-CSE524-2013-3 5/10/2013

Arithmetic operators Addition + Subtraction – Multiplication * Division / Modulus % Precedence of operators: *, /, % + - Left to right associavity Override precedence using ( ) Amrita-UB-MSES-CSE524-2013-3 5/10/2013

Arithmetic expression celsius = 5 * (fahr-32) / 9; 3 + 5 – 10 * 2 / 3 % 4 =3 + 5 -20 /3 % 4 = 3 + 5 – 6% 4 = 3+5 – 2 = 8 – 2 = 6 Amrita-UB-MSES-CSE524-2013-3 5/10/2013

Iteration/repetition While loop Syntax: Initialize condition; while (condition) { statements; update condition; } Execution semantics Amrita-UB-MSES-CSE524-2013-3 5/10/2013

If..else: selection Often we need to make choices in the execution path. If ..else statement if (sensedTemp > refTemp) //cool the room else if (sensedTemp < refTemp) // heat the room Amrita-UB-MSES-CSE524-2013-3 5/10/2013

Multi-way selection Case or switch statement: switch (grade) { case ‘A’ : printf (“Very good \n”); break; case ‘B’ : printf (“Good\n”); break; case ‘C’ : printf(“not bad\n”); break; case ‘F’: printf(“Bad\n”); default: printf(“Grade out of range \n”); } Amrita-UB-MSES-CSE524-2013-3 5/10/2013

Putting it all together Lets solve the problem below using C. Consider the number game shown in the next few slides. Amrita-UB-MSES-CSE524-2013-3 5/10/2013

The Number Game (1) 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 1/17/2019

The Number Game (2) 2 3 6 7 10 11 14 15 18 19 22 23 26 27 30 31 1/17/2019

The Number Game (4) 4 5 6 7 12 13 14 15 20 21 22 23 28 29 30 31 1/17/2019

The Number Game (8) 8 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31 1/17/2019

The Number Game (16) 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 1/17/2019

Analysis What is theory /concept behind this game? How did I arrive at the number you guessed? How can I automate this process? What is the data and what is the algorithm? How can we convey these to a computing machine? While a computer talks binary, we humans write programs in languages such as Java, C#, C++, Basic etc. Binary numbers (1’s and 0’s) is the number system used by the computer systems. We humans use decimal number system that has 10 distinct symbols (0,1,2,3,4,5,6,7,8,9) Your task: Write a C program to computerize this game. 1/17/2019

2 3 6 7 10 11 14 15 18 19 22 23 26 27 30 31 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 4 5 6 7 12 13 14 15 20 21 22 23 28 29 30 31 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 8 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31 1/17/2019