CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
 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.
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Structure of a C program
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
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 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Using C Programming Language.  The programs that run on a computer are referred to as software.  You’ll learn key programming methodology that are enhancing.
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.
Chapter 3 Getting Started with C++
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
C Programming language
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
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.
Programming With C.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
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.
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.
Basic Program Construction
CC112 Structured Programming Lecture 2 1 Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer.
Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
CSC 110 – Intro to Computing - Programming
COMPUTER PROGRAMMING. Topic: STRUCTURE OF C PROGRAMMING.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Department of Electronic & Electrical Engineering Statements Blocks{} Semicolons ; Variables Names keywords Scope.
Objective Write simple computer program in C++ Use simple Output statements Become familiar with fundamental data types.
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:
Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
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.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Introduction to C Topics Compilation Using the gcc Compiler
User-Written Functions
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
CSC201: Computer Programming
Introduction to C Language
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.
Getting Started with C.
CS1061 C Prgramming Lecture 11: Functions
Chapter 2 - Introduction to C Programming
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
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
Chapter 2 - Introduction to C Programming
Introduction to C Topics Compilation Using the gcc Compiler
Presentation transcript:

CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004

First Program The best way to get started with C is to actually look at a program. main(){} notes: The word "main" is very important, and must appear once, and only once in every C program. This is the point where execution is begun when the program is run. We will see later that this does not have to be the first statement in the program but it must exist as the entry point.

First Program (continued) notes: Following the "main" program name is a pair of parentheses which are an indication to the compiler that this is a function. The two curly brackets, properly called braces, are used to define the limits of the program itself. The actual program statements go between the two braces and in this case, there are no statements because the program does absolutely nothing. You can compile and run this program, but since it has no executable statements, it does nothing.

Second Program Another sample C program is given below: /*Sample program */ #include int main(){ printf(“C is fun!\n”); return(0); }

Second Program (continued) notes : This program outputs text to the monitor. Notice the semi-colon at the end of the statements. C uses a semi- colon as a statement terminator, so the semi-colon is required as a signal to the compiler that this line is complete. The first printf() outputs a line of text and returns the carriage. printf() is a function used for displaying text. This function is part of the C library. \n is a special newline character (see later). The #include is a pre-processor directive which includes declarations of library functions. The # character must appear as the first character in a line. stdio.h is needed because of the printf().

Second Program (continued) return() is a standard command that causes the program to ‘ return ’ a value. Strictly speaking it is not needed here as it is the last line of main() and the program will terminate anyway. /*... */ indicates a comment which will not be parsed by the compiler. Comments are totally ignored by the compiler, they're just annotations you can add to make it easier to read your code. The comment has the following syntax: A comment begins with the two characters /* (no space between them) and ends with the characters */. A comment can span many lines. Comments cannot be nested.

A Word on Functions Functions are the basic unit of any C program, or any structured programming language for that matter. Functions are called procedures or subroutines in other languages. If a function is well written it may be reusable from one project to the next. Every C program has to have at least one function called main(). This is the first function that is executed. This function may then call other functions. It is conventional to place the main() function at the start of the program followed by all the other functions in the file. It is considered good software engineering practice to keep functions short.

A Word on Functions (continued) A programmer can create his own functions Advantage: the programmer knows exactly how it works Disadvantage: time consuming Programmers will often use the C library functions Use these as building blocks Avoid re-inventing the wheel If a pre-made function exists, generally best to use it rather than write your own Library functions carefully written, efficient, and portable

“Form” of Fucnction A function has the form: return_type function_name (parameters){ } The important thing to note here is the order. Firstly we have the function name (and return type and parameters). Any C function may have parameters, which is a mechanism for passing extra information to a function. Then we have the body of the function enclosed by curly braces {}.

One last program #include main(){ int index; index = 13; printf("The value of the index is %d\n",index); index = 27; printf("The value of the index is %d\n",index); index = 10; } notes: This uses a variable to store a numeric value.

One last program (continued) notes: int index; is used to define an integer variable named "index". The "int" is a reserved word in C, and can therefore not be used for anything else. Variables are locations in memory where a value can be stored int means the variables can hold integers (e.g. -1, 3, 0, 47) there are three statements that assign a value to the variable "index", but only one at a time. The first one assigns the value of 13 to index, and its value is printed out. The second assignment gives index the value 27 and the third 10. the syntax of printf() is discussed later.