Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
Outline 2.1 Introduction 2.2 Basics of C Programs
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 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 C Programming
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
C programming Language and Data Structure For DIT Students.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
COMPUTER SCIENCE I C++ INTRODUCTION
Chapter 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Chapter 2: Using Data.
EPSII 59:006 Spring Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Khalid Rasheed Shaikh Computer Programming Theory 1.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
© 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.
An overview of C Language. Overview of C C language is a general purpose and structured programming language developed by 'Dennis Ritchie' at AT &T's.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
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.
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.
CSCE 206 Structured Programming in C
Chapter 1: Introduction to computers and C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Chapter 2 - Introduction to C Programming
By: Syed Shahrukh Haider
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to CS Your First C Programs
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
Introduction to C++ Programming
Chapter 2: Introduction to C++.
Introduction to Java Applications
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
C – Programming Language
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Introduction to C Programming
Presentation transcript:

Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

What is Development Environment? What is basic structure? What is input & output?

Programming In C++, Lecture 2 Turbo C Development System The Integrated Development System (IDE) The Command-Line Development System

Programming In C++, Lecture 2 Turbo C Development System The Integrated Development System (IDE) It is a screen display with windows and pull down menus. The program listing, its output, error messages and other information are displayed in separate windows. You can use menu selections to invoke all the operations necessary to develop your program including editing, compiling, debugging, Linking and program execution.

Programming In C++, Lecture 2 Turbo C Development System The Command-Line Development System This is traditional Command-Line system, in which editing, compiling, linking, debugging, and executing are invoked from the DOS command line as separate activities, performed by separate programs. This system is relatively difficult to learn and complex to operate.

Programming In C++, Lecture 2 Turbo C Development System PROGRAM EXECUTION PROCESS DIAGRAM MyPro.c Header file Compiler MyPro.obj Library Files Linker MyPro.exe Source File Executable File

Programming In C++, Lecture 2 /* A first C Program*/ #include void main() { printf("Hello World \n"); } Output: Hello World Your first Program Basic Structure of C Programs

Programming In C++, Lecture 2 Your first Program Basic Structure of C Programs Line 1: #include As part of compilation, the C compiler runs a program called the C preprocessor. The preprocessor is able to add and remove code from your source file. In this case, the directive #include tells the preprocessor to include code from the file stdio.h. This file contains declarations for functions that the program needs to use. A declaration for the printf function is in this file.

Programming In C++, Lecture 2 Your first Program Basic Structure of C Programs Line 2: void main() This statement declares the main function. A C program can contain many functions but must always have one main function. A function is a self-contained module of code that can accomplish some task. Functions are examined later. The "void" specifies the return type of main. In this case, nothing is returned to the operating system.

Programming In C++, Lecture 2 Your first Program Basic Structure of C Programs Line 2: void main() Function Declaration: Return type Name (Accept) If have nothing to returned we use void key word & if have nothing to accept leave the braces empty like () or use void key word in the braces.

Programming In C++, Lecture 2 Your first Program Basic Structure of C Programs Delimiter: Following the function definition are the braces, which signals the beginning and ending of the body of the function. The Opening brace “{” indicates that a block of code that forms a distinct unit is about to begin. The Closing brace “}” terminates the block code. Line 3: { This opening bracket denotes the start of the program. Line 5: } This closing bracket denotes the end of the program.

Programming In C++, Lecture 2 Your first Program Basic Structure of C Programs Line 4: printf("Hello World From About\n"); Printf is a function from a standard C library that is used to print strings to the standard output, normally your screen. The Linker looks in the CS.lib file, find the sections of the file containing printf() and sections to be linked with the source program. The "\n" is a special format modifier that tells the printf to put a line feed at the end of the line. If there were another printf in this program, its string would print on the next line. Statement Terminator A statement in C Language is terminated with semicolon “;”.

Programming In C++, Lecture 2 printf() Function (Output) Basic Structure of C Programs The printf() function gives the programmer considerable power to format the printed output. or The printf() function allows us to send output on screen and see the output of that program

Programming In C++, Lecture 2 printf() Format Specifier Basic Structure of C Programs The format specifier tells the printf() where to put a value in a string and what format to use in printing the values. Then what will be the method of writing a character or floating point number? Why not simply put the decimal integer into the original string as compared to the format specifier?

Programming In C++, Lecture 2 List of Format Specifier for printf() Basic Structure of C Programs %csingle character %sstring %iinteger %dsigned decimal integer %f floating point(decimal notation) %efloating point (exponential notation) %uunsigned decimal integer %xunsigned hexadecimal integer %ounsigned octal integer |prefix used with %d, %u, %x, %o to specify long integer (for example %|d)

Programming In C++, Lecture 2 Example of Format Specifier for printf() Basic Structure of C Programs #include void main() { int event =5; char heat=‘c’; float time=27.25; printf(“The winning time in heat %c“, heat); printf(“of event %d was %.2f.”,event,time); } Output The winning time in the heat c of event 5 was

Programming In C++, Lecture 2 Variables A variable is a space in the computer memory set aside for a certain kind of data and given a name for easy reference. Variables are used so that the same space in memory can hold different values at different times.

Programming In C++, Lecture 2 Variables The Programming language C has two main variable types Local Variables Global Variables Constants Variables

Programming In C++, Lecture 2 Local Variables Local variables scope is confined within the block or function where it is defined. Local variables must always be defined at the top of a block. When a local variable is defined - it is not initialised by the system, you must initialise it yourself. When execution of the block starts the variable is available, and when the block ends the variable 'dies'. Variables

Programming In C++, Lecture 2 Global Variables Global variable is defined at the top of the program file and it can be visible and modified by any function that may reference it. Global variables are initialised automatically by the system when you define them Variables

Programming In C++, Lecture 2 Constants Variables The term constant means that it does not change during the execution of program. Variables

Programming In C++, Lecture 2 Variables Type / Data Type IntInteger Type (1,2,3…) FloatDecimal Type (1.22,1.50…) CharCharacter Type (a,b,c…) Variables

Programming In C++, Lecture 2 Variables Type / Data Type IntInteger Type (1,2,3…) FloatDecimal Type (1.2200,1.5078…) CharCharacter Type (a,b,c…) (1 byte) Variables

Programming In C++, Lecture 2 Variables Definition The definition consists of the type of the variable followed by the name of the variable. C program all variables must be defined. Defining a variable tells the compiler to set aside an appropriate amount of memory to store. More then one variables of the same type names separated with commas. For Example: int num; int num1,num2,num3; char ch1,ch2; Variables

Programming In C++, Lecture 2 Variables Declaring Variables Declaration, by contrast, specifies the variable’s name and data type, but does not set aside any memory for the variable. Variable declaration are important in multifile programs where a variable that is defined in one file must be referred to in a second file. Variables

Programming In C++, Lecture 2 Variables Initializing A variable is given a value at the same time of its defining. It is possible to combine a variable definition with an assignment operator. For Example: int num=10; char ch1=‘a’; Variables

Programming In C++, Lecture 2 #include void main() { int event =5; char heat=‘c’; float time=27.25; printf(“The winning time in heat %c“, heat); printf(“of event %d was %.2f.”,event,time); } Output The winning time in the heat c of event 5 was Variables

Programming In C++, Lecture 2 Escape Sequence \nnew line \ttab \rcarriage return \aalert \\backslash \”double quote

Programming In C++, Lecture 2 Operators Decision Making Equality operators == != Relational operators < > <= >=

Programming In C++, Lecture 2 Assignment operators = += -= *= /= %= Operators

Programming In C++, Lecture 2 Increment/ decrement operators ++++a ++a a --a-- Operators

Programming In C++, Lecture 2 Arithmetic Operators +Addition - Subtractions * Multiplication /Division % Remainder Operators

Programming In C++, Lecture 2 Expressions An expression is a sequence of operators and operands that specifies computation of a value. For example : a+b

Programming In C++, Lecture 2 Keywords Keywords are standard identifiers that have standard predefined meaning in C "Keywords" are words that have special meaning to the C compiler They cannot be used as variable or function names.

Programming In C++, Lecture 2 Comments Comments are added to make a program more readable to you. Everything that is inside /* and */ is considered a comment and will be ignored by the Compiler For example: /* pintf (“Test line for comments”); */

Programming In C++, Lecture 2 Basic Structure of C Programs The scanf() Function (Input) The scanf() function allows you to accept input from users via keyboard. scanf() requires the use of an ampersand (&) sign before the variable name. It is unreliable because it does not handle human errors very well. But for simple programs it is good enough and easy-to- use. The scanf() function uses the same placeholders as printf(): int uses %d float uses %f char uses %c character strings use %s

Programming In C++, Lecture 2 Example scanf() Function (Input) #include void main() { int event ; char heat; float time; printf(“Type event number, heat letter and time:”); scanf(“%d %c %f”,&event, &heat, &time); printf(“The winning time in heat %c“, heat); printf(“of event %d was %.2f.”,event,time); } Output Type event number, heat letter and time: 5 c The winning time in the heat c of event 5 was

Programming In C++, Lecture 2 Basic Structure of C Programs The getchar() Function (Input) getchar ( ) is used to get a character from console, and display to the screen OR getchar ( ) function will accept a character from the console or from a file, displays immediately while typing and we need to press Enter key for proceeding. void main() { char ch; ch = getchar(); printf("Input Char Is :%c",ch); }

Programming In C++, Lecture 2 Introduction To C Programming Language What is output function? What are input functions? What is the difference between IDE & Command Line Development System? What are variables? What are constant variables? What we use as Statement Terminator? Why we use void? What are keywords? Quiz