Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2."— Presentation transcript:

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

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

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

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

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

6 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

7 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

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

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

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

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

12 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 “;”.

13 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

14 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?

15 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)

16 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 27.25.

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

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

19 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

20 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

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

22 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

23 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

24 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

25 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

26 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

27 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 27.25. Variables

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

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

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

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

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

33 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

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

35 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”); */

36 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

37 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 27.25 The winning time in the heat c of event 5 was 27.25.

38 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); }

39 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


Download ppt "Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2."

Similar presentations


Ads by Google