programming in c++ spring semester 2013 lecture 2 programming in c++, lecture 2

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

Upload: sydnee-lemons

Post on 15-Dec-2015

224 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++

Spring Semester 2013Lecture 2

Programming In C++, Lecture 2

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

Programming In C++, Lecture 2

• What is Development Environment?

• What is basic structure?

• What is input & output?

Page 3: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

Turbo C Development System

• The Integrated Development System (IDE)

• The Command-Line Development System

Page 4: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

Turbo C Development SystemThe 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.

Page 5: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

Turbo C Development SystemThe 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.

Page 6: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

Turbo C Development SystemPROGRAM EXECUTION PROCESS DIAGRAM

MyPro.cHeader

file

Compiler

MyPro.objLibrary

Files

Linker

MyPro.exe

Source File

Executable File

Page 7: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

/* A first C Program*/#include <stdio.h>

void main()

{ printf("Hello World \n");

}

Output:Hello World

Your first Program

Basic Structure of C Programs

Page 8: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

Your first Program

Basic Structure of C Programs

Line 1: #include <stdio.h>

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.

Page 9: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

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.

Page 10: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

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.

Page 11: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

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.

Page 12: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

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 TerminatorA statement in C Language is terminated with semicolon “;”.

Page 13: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

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.

orThe printf() function allows us to send output on screen and see the output of that program

Page 14: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

printf() Format SpecifierBasic 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?

Page 15: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

List of Format Specifier for printf()Basic Structure of C Programs

• %c single character• %s string• %i integer• %d signed decimal integer• %f floating point(decimal notation)• %e floating point (exponential notation)• %u unsigned decimal integer• %x unsigned hexadecimal integer• %o unsigned octal integer• | prefix used with %d, %u, %x, %o to specify

long integer (for example %|d)

Page 16: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

Example of Format Specifier for printf()Basic Structure of C Programs

#include <stdio.h> 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);

}OutputThe winning time in the heat c of event 5 was 27.25.

Page 17: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

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.

Page 18: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

VariablesThe Programming language C has two main variable types• Local Variables• Global Variables• Constants Variables

Page 19: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

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

Page 20: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

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

Page 21: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

Constants VariablesThe term constant means that it does not change during the execution of program.

Variables

Page 22: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

Variables Type / Data Type• Int Integer Type (1,2,3…)• Float Decimal Type (1.22,1.50…)• Char Character Type (a,b,c…)

Variables

Page 23: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

Variables Type / Data Type• Int Integer Type (1,2,3…)

• Float Decimal Type (1.2200,1.5078…)

• Char Character Type (a,b,c…) (1 byte)

Variables

Page 24: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

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

Page 25: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

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

Page 26: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

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

Page 27: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

#include <stdio.h> 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);

}OutputThe winning time in the heat c of event 5 was 27.25.

Variables

Page 28: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

Escape Sequence

\n new line\t tab\r carriage return\a alert\\ backslash\” double quote

Page 29: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

OperatorsDecision Making

Equality operators==!=

Relational operators<><=>=

Page 30: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

Assignment operators

=+=-=*=/=%=

Operators

Page 31: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

Increment/ decrement operators

++ ++a++ a++-- --a-- a--

Operators

Page 32: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

Arithmetic Operators

+ Addition- Subtractions

* Multiplication/ Division% Remainder

Operators

Page 33: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

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

Page 34: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

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.

Page 35: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

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 CompilerFor example:

/*pintf (“Test line for comments”);

*/

Page 36: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

Basic Structure of C ProgramsThe 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

Page 37: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

Example scanf() Function (Input) #include <stdio.h> 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);

}OutputType event number, heat letter and time: 5 c 27.25The winning time in the heat c of event 5 was 27.25.

Page 38: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

Programming In C++, Lecture 2

Basic Structure of C ProgramsThe getchar() Function (Input)

getchar ( ) is used to get a character from console, and display to the screen ORgetchar ( ) 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);

}

Page 39: Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2

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