CSE1301 Computer Programming Lecture 2: Introduction to C Joselito (Joey) Chua

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
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 Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University.
CSE1301 Computer Programming Lecture 4: C Primitives I.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Three types of computer languages
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Introduction to Computers and Programming Class 6 Introduction to C Professor Avi Rosenfeld.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)
The C Programming Lecture 24.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
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.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Introduction to C Programming. A Brief History u Created by Dennis Ritchie at AT&T Labs in 1972 u Originally created to design and support the Unix operating.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Computer Programming I Hour 2 - Writing Your First C Program.
1 Why C? History of C Generic C Program Structure Variables –Type and Name –Assignment Operator Simple I/O –scanf/printf Today’s Material.
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.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
1 Programming a Computer Lecture Ten. 2 Outline  A quick introduction to the programming language C  Introduction to software packages: Matlab for numerical.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
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.
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.
Basic concepts of C++ Presented by Prof. Satyajit De
Computer Science 210 Computer Organization
- Standard C Statements
Chapter 2 - Introduction to C Programming
Chapter 2, Part I 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.
History of ‘C’ Root of the morden language is ALGOL It’s first
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Getting Started with C.
Introduction to C Programming Language
Chapter 2 - Introduction to C Programming
Computer Science 210 Computer Organization
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
Chapter 2 - Introduction to C Programming
Chapter 11 Introduction to Programming in C
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Lecture3.
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
Capitolo 1 – Introduction C++ Programming
Chapter 2 - Introduction to C Programming
Dale Roberts, Lecturer IUPUI
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Introduction to C Programming
Getting Started With Coding
Presentation transcript:

CSE1301 Computer Programming Lecture 2: Introduction to C Joselito (Joey) Chua

Topics Basic structure of a C program C “baby-talk” phrases printf(“Da-da-da”);

0 or 1 Recall:

Machine Language etc...

High-Level Language Compilers and linkers translate a high level program into executable machine code. #include int main() { printf(“Da-da-da”); return 0; } Source code Executable code etc...

Why C? Structured language Standard library exists, allowing portability –See D&D 2/e Appendix B (web links in D&D 3/e) –Forouzan & Gilberg Appendix F Wide availability on a variety of computers Low level activities possible It can produce lean and efficient code Widely used

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) ANSI C American National Standards Institute C (X3J11, 1989) C9X (JTC1/SC22/WG14, ISO/IEC 9899)

Basic Structure of a C Program output “Da-da-da” Instructions: #include int main() { printf(“Da-da-da”); return 0; } C Program: Example: Da-da-da

Basic Structure of a C Program #include int main() { printf(“Da-da-da”); return 0; } C Program: “Skeleton” Example: Da-da-da

Basic Structure of a C Program #include int main() { printf(“Da-da-da”); return 0; } C Program: void main() { } Also: Not recommended Example: Da-da-da

Basic Structure of a C Program #include int main() { printf(“Da-da-da”); return 0; } C Program: main() { return 0; } Also: Assumes int Example: Da-da-da

Basic Structure of a C Program #include int main() { printf(“Da-da-da”); return 0; } C Program: main() { return 0; } Also: Warning messages: “Return value expected” Example: Da-da-da

Basic Structure of a C Program #include int main() { printf(“Da-da-da”); return 0; } C Program: Includes standard input / output library of functions. Read: “Hash-include” Example: Da-da-da

Basic Structure of a C Program #include int main() { printf(“Da-da-da”); return 0; } C Program: Brackets mark the beginning and end of a block of instructions. Example: Da-da-da

Basic Structure of a C Program #include int main() { printf(“Da-da-da”); return 0; } C Program: Instruction (function call) to output “Da-da-da” Example: Da-da-da

Basic Structure of a C Program #include int main() { printf(“Da-da-da”); return 0; } C Program: “Statements” (lines of instructions) end with a semi-colon ( ; ) Example: Da-da-da

Simple C Phrases Example 1: Float let x be equal to 4.5 output the value of x Instructions: #include int main() { float x; x = 4.5; printf(“%f”, x); return 0; } C Program:

Simple C Phrases Example 1: Float #include int main() { float x; x = 4.5; printf(“%f”, x); return 0; } C Program: Declares that x is a variable which can be assigned a numerical value.

Simple C Phrases Example 1: Float #include int main() { float x; x = 4.5; printf(“%f”, x); return 0; } C Program: Assigns the value 4.5 into the variable x. (Remember: “right value is assigned to left variable”)

Simple C Phrases Example 1: Float #include int main() { float x; x = 4.5; printf(“%f”, x); return 0; } C Program: Outputs the value of x. (“ %f ” for float)

Simple C Phrases Example 2: Sum input the value of x input the value of y output “The sum is “ output the value of x + y output a new line Instructions: #include int main() { float x; float y; scanf(“%f”, &x); scanf(“%f”, &y); printf(“The sum is “); printf(“%f”, x + y); printf(“\n”); return 0; } C Program:

x and y are both numeric variables #include int main() { float x; float y; scanf(“%f”, &x); scanf(“%f”, &y); printf(“The sum is “); printf(“%f”, x + y); printf(“\n”); return 0; } C Program: Simple C Phrases Example 2: Sum

Input the values of x and y respectively (note the ampersand!) #include int main() { float x; float y; scanf(“%f”, &x); scanf(“%f”, &y); printf(“The sum is “); printf(“%f”, x + y); printf(“\n”); return 0; } C Program: Simple C Phrases Example 2: Sum

Outputs the string: “The sum is “ #include int main() { float x; float y; scanf(“%f”, &x); scanf(“%f”, &y); printf(“The sum is “); printf(“%f”, x + y); printf(“\n”); return 0; } C Program: Simple C Phrases Example 2: Sum

Outputs the sum x + y #include int main() { float x; float y; scanf(“%f”, &x); scanf(“%f”, &y); printf(“The sum is “); printf(“%f”, x + y); printf(“\n”); return 0; } C Program: Simple C Phrases Example 2: Sum

Outputs a newline #include int main() { float x; float y; scanf(“%f”, &x); scanf(“%f”, &y); printf(“The sum is “); printf(“%f”, x + y); printf(“\n”); return 0; } C Program: Simple C Phrases Example 2: Sum

Simple C Phrases: “if” #include int main() { char name[50]; float mark; printf("What is your name? "); scanf("%s", name); printf("What is your mark? "); scanf("%f", &mark); printf("\n"); printf("Hi, "); printf("%s", name); printf("!\n"); /* The next output depends on the mark. */ if (mark < 50) { printf("Sorry. You failed.\n"); } else { printf("Yey! You passed!\n"); } return 0; } Example 3: Mark

Simple C Phrases: “if” #include int main() { char name[50]; float mark; printf("What is your name? "); scanf("%s", name); printf("What is your mark? "); scanf("%f", &mark); printf("\n"); printf("Hi, "); printf("%s", name); printf("!\n"); Example 3: Mark /* The next output depends on the mark. */ if (mark < 50) { printf("Sorry. You failed.\n"); } else { printf("Yey! You passed!\n"); } return 0; } Declares that name is a variable which can contain a “string” (i.e. words, names, etc.)

/* The next output depends on the mark. */ if (mark < 50) { printf("Sorry. You failed.\n"); } else { printf("Yey! You passed!\n"); } return 0; } Simple C Phrases: “if” #include int main() { char name[50]; float mark; printf("What is your name? "); scanf("%s", name); printf("What is your mark? "); scanf("%f", &mark); printf("\n"); printf("Hi, "); printf("%s", name); printf("!\n"); Example 3: Mark Outputs a “prompt” so the user knows what the program wants.

/* The next output depends on the mark. */ if (mark < 50) { printf("Sorry. You failed.\n"); } else { printf("Yey! You passed!\n"); } return 0; } Simple C Phrases: “if” #include int main() { char name[50]; float mark; printf("What is your name? "); scanf("%s", name); printf("What is your mark? "); scanf("%f", &mark); printf("\n"); printf("Hi, "); printf("%s", name); printf("!\n"); Example 3: Mark Inputs the string content of the variable name. (Note: no ampersand!)

/* The next output depends on the mark. */ if (mark < 50) { printf("Sorry. You failed.\n"); } else { printf("Yey! You passed!\n"); } return 0; } Simple C Phrases: “if” #include int main() { char name[50]; float mark; printf("What is your name? "); scanf("%s", name); printf("What is your mark? "); scanf("%f", &mark); printf("\n"); printf("Hi, "); printf("%s", name); printf("!\n"); Example 3: Mark (The usual. Note the ampersand for floats!)

/* The next output depends on the mark. */ if (mark < 50) { printf("Sorry. You failed.\n"); } else { printf("Yey! You passed!\n"); } return 0; } Simple C Phrases: “if” #include int main() { char name[50]; float mark; printf("What is your name? "); scanf("%s", name); printf("What is your mark? "); scanf("%f", &mark); printf("\n"); printf("Hi, "); printf("%s", name); printf("!\n"); Example 3: Mark Outputs the string name. (Note the “%s”)

/* The next output depends on the mark. */ if (mark < 50) { printf("Sorry. You failed.\n"); } else { printf("Yey! You passed!\n"); } return 0; } Simple C Phrases: “if” #include int main() { char name[50]; float mark; printf("What is your name? "); scanf("%s", name); printf("What is your mark? "); scanf("%f", &mark); printf("\n"); printf("Hi, "); printf("%s", name); printf("!\n"); Example 3: Mark Comments: everything between /* and */ are ignored by the compiler.

Simple C Phrases: “if” #include int main() { char name[50]; float mark; printf("What is your name? "); scanf("%s", name); printf("What is your mark? "); scanf("%f", &mark); printf("\n"); printf("Hi, "); printf("%s", name); printf("!\n"); Example 3: Mark This block of instructions is performed if the comparison is true. /* The next output depends on the mark. */ if (mark < 50) { printf("Sorry. You failed.\n"); } else { printf("Yey! You passed!\n"); } return 0; }

Simple C Phrases: “if” #include int main() { char name[50]; float mark; printf("What is your name? "); scanf("%s", name); printf("What is your mark? "); scanf("%f", &mark); printf("\n"); printf("Hi, "); printf("%s", name); printf("!\n"); Example 3: Mark This block of instructions is performed if the comparison is false. /* The next output depends on the mark. */ if (mark < 50) { printf("Sorry. You failed.\n"); } else { printf("Yey! You passed!\n"); } return 0; }

Summary Basic structure of a C program Simple C “phrases” –Declaring numerical variables (floats) –Declaring string variables –Input (scanf) and Output (printf) –comments –The “if...else...” condition Readings: Forouzan & Gilberg, Chapter 1