UMBC CMSC 104 – Section 01, Fall 2016

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Introduction to C Creating your first C program. Writing C Programs  The programmer uses a text editor to create or modify files containing C code. 
Introduction to C Programming
Introduction to C Programming CE Lecture 1 Introduction to C.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
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
Introduction to C. A Brief History Created by Dennis Ritchie at AT&T Labs in 1972 Originally created to design and support the Unix operating system.
CHAPTER 1: INTORDUCTION TO C LANGUAGE
Introduction to C/C++ Programming This lecture has major focus on programming, compilation.
Computer Science 210 Computer Organization Introduction to C.
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.
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:
Creating your first C++ program
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.
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!
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.
Introduction to C CMSC 104, Section 4 Richard Chang 1.
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.
CMSC 1041 Introduction to C Creating your first C program.
Agenda Computer Languages How to Write a Simple C Program
Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004
CMPE13Cyrus Bazeghi 1 Chapter 11 Introduction to Programming in C.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Introduction to C Programming
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
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.
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.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Introduction to C Topics Compilation Using the gcc Compiler
CSCE 206 Structured Programming in C
User-Written Functions
Computer Science 210 Computer Organization
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
CSC201: Computer Programming
Introduction to C Language
Chapter 2 - Introduction to C Programming
C Programming Hardik H. Maheta.
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.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Welcome In The World Of ‘C’.  TEXT BOOK: Programming in ANSI ‘C By: E Balagurusamy. TMH  Reference Books: 1) The ‘C Programming Language By: Kernighan.
Computer Science 210 Computer Organization
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
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
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
Creating your first C program
Chapter 2 - Introduction to C Programming
Programming Fundamentals Lecture #3 Overview of Computer Programming
2008/09/29: Lecture 7 CMSC 104, Section 0101 John Y. Park
Your first C and C++ programs
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Programming
SPL – PS1 Introduction to C++.
Presentation transcript:

UMBC CMSC 104 – Section 01, Fall 2016 Introduction to C

Notes & Announcements Grades for Project 1 Posted As for all projects, points posted are out of 75. Average was ~78% If you submitted late, I took off half the points. Next time it will be a zero. Project 2 will be posted tonight Two parts – a free response and a programming problem. As always, start early!

History of C Developed in 1972 by Dennis Ritchie Quickly adopted into UNIX – much of the UNIX OS was re-written in C Most of Linux is still written in C The American National Standards Institute (ANSI) began working on “C Standard” in 1983. It was ratified in 1989 and referred to as ANSI C This is the version of C we will be using Further revised in 1999 (C99) and 2011 (C11) Added new data types and features to the standard

C Today

Why Start With C? Lower Level Required for advanced Computer Science Closer to the hardware More control, potentially Forces understanding of what the computer is doing Required for advanced Computer Science Want to know how Linux works? Better know C…

C vs… C vs C++ C vs Java/C# C vs Python & Perl C++ is basically C with additional functionality Most C programs will compile with a C++ compiler C vs Java/C# C is a “lower level” language C programs compile to machine code Java/C# programs compile to an intermediary language (aka “bytecode”) and require an interpreter C vs Python & Perl C is a compiled language, Python & Perl are scripting languages Scripting languages are compiled on-the-fly at runtime Difficult or impossible to deploy scripted code without providing source

Writing C Programs A programmer uses a text editor to create or modify files containing C code. Code is also known as source code. A file containing source code is called a source file. After a C source file has been created, the programmer must invoke the C compiler before the program can be executed (run).

Using the C Compiler at UMBC Invoking the compiler is system dependent. At UMBC, we have two C compilers available, cc and gcc. For this class, we will use the gcc compiler as it is the compiler available on the Linux system.

Invoking the gcc Compiler At the prompt, type gcc -ansi -Wall pgm.c where pgm.c is the C program source file. -ansi is a compiler option that tells the compiler to adhere to the ANSI C standard. -Wall is an option to turn on all compiler warnings (best for new programmers).

The Result : a.out If there are no errors in pgm.c, this command produces an executable file, which is one that can be executed (run). The gcc compiler names the executable file a.out . To execute the program, at the prompt, type ./a.out Although we call this process “compiling a program,” what actually happens is more complicated.

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 whitespace 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.

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

A Simple C Program /* Filename: hello.c Author: Brian Kernighan & Dennis Ritchie Date written: ?/?/1978 Description: This program prints the greeting “Hello, World!” */ #include <stdio.h> int main ( ) { printf (“Hello, World!\n”) ; return 0 ; }

Anatomy of a C Program program header comment preprocessor directives (if any) int main ( ) { statement(s) return 0 ; }

Program Header Comment A comment is descriptive text used to help a reader of the program understand its content. All comments must begin with the characters /* and end with the characters */ These are called comment delimiters The program header comment always comes first. Look at the class web page for the required contents of our header comment.

Preprocessor Directives Lines that begin with a # in column 1 are called preprocessor directives (commands). Example: the #include <stdio.h> directive causes the preprocessor to include a copy of the standard input/output header file stdio.h at this point in the code. This header file was included because it contains information about the printf ( ) function that is used in this program.

int main ( ) Every program must have a function called main. This is where program execution begins. main() is placed in the source code file as the first function for readability. The reserved word “int” indicates that main() returns an integer value. The parentheses following the reserved word “main” indicate that it is a function.

The Function Body A left brace (curly bracket) -- { -- begins the body of every function. A corresponding right brace -- } -- ends the function body. The style is to place these braces on separate lines in column 1 and to indent the entire function body 2 to 4 spaces.

printf (“Hello, World!\n”) ; This line is a C statement. It is a call to the function printf ( ) with a single argument (parameter), namely the string “Hello, World!\n”. Even though a string may contain many characters, the string itself should be thought of as a single quantity. Notice that this line ends with a semicolon. All statements in C end with a semicolon.

return 0 ; Because function main() returns an integer value, there must be a statement that indicates what this value is. The statement return 0; indicates that main() returns a value of zero to the operating system. A value of 0 indicates that the program successfully terminated execution. Do not worry about this concept now. Just remember to use the statement.

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

Another C Program (con’t) #include <stdio.h> int main() { int value1, value2, product; printf(“Enter two integer values:\n“); scanf(“%d%d”, &value1, &value2); product = value1 * value2; printf(“Product = %d\n”, product); return 0; }

Good Programming Practices C programming standards and indentation styles are available on the 104 course homepage. You are expected to conform to these standards for all programming projects in this class. (This will be part of your grade for each project!) The program just shown conforms to these standards, but is uncommented (later). Subsequent lectures will include more “Good Programming Practices” slides.

Project 2 Programming Write a C program, called campusid_hello.c, that prompts the user to input three initials, scans the three initials, and then outputs "Hello, XYZ!", where X,Y, and Z are the three input initials, respectively.

Compiling Your Program

Compiling Your Program

Running Your Program

Running Your Program

Resources I’ve pasted sample code files from today’s lecture in the following location: http://userpages.umbc.edu/~reicht1/classes/cmsc104/fall16/samples/ You can download hello_world.c as a starting template using: curl -O http://userpages.umbc.edu/~reicht1/classes/cmsc104/fall16/samples/hello_world.c