1 / 39

Programming In C++

Programming In C++. Spring Semester 2013 Lecture 2. What is Development Environment? What is basic structure? What is input & output?. Turbo C Development System. The Integrated Development System (IDE) The Command-Line Development System. Turbo C Development System.

shiri
Télécharger la présentation

Programming In C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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? Programming In C++, Lecture 2

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

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

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

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

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

  8. Basic Structure of C Programs Your first Program 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 printffunction is in this file. Programming In C++, Lecture 2

  9. Basic Structure of C Programs Your first Program 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

  10. Basic Structure of C Programs Your first Program 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

  11. Basic Structure of C Programs Your first Program 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

  12. Basic Structure of C Programs Your first Program Line 4: printf("Hello World From About\n"); Printfis 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 printfto put a line feed at the end of the line. If there were another printfin 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

  13. Basic Structure of C Programs printf() Function (Output) 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

  14. Basic Structure of C Programs printf() Format Specifier 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

  15. Basic Structure of C Programs List of Format Specifier for printf() %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) Programming In C++, Lecture 2

  16. Basic Structure of C Programs Example of Format Specifier for printf() #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);} Output The winning time in the heat c of event 5 was 27.25. Programming In C++, Lecture 2

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

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

  19. Variables 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'. Programming In C++, Lecture 2

  20. Variables 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 Programming In C++, Lecture 2

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

  22. Variables Variables Type / Data Type Int Integer Type (1,2,3…) Float Decimal Type (1.22,1.50…) Char Character Type (a,b,c…) Programming In C++, Lecture 2

  23. Variables 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) Programming In C++, Lecture 2

  24. Variables 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: intnum; int num1,num2,num3; char ch1,ch2; Programming In C++, Lecture 2

  25. Variables 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. Programming In C++, Lecture 2

  26. Variables 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: intnum=10; char ch1=‘a’; Programming In C++, Lecture 2

  27. Variables #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);} Output The winning time in the heat c of event 5 was 27.25. Programming In C++, Lecture 2

  28. Escape Sequence \n new line \t tab \r carriage return \a alert \\ backslash \” double quote Programming In C++, Lecture 2

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

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

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

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

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

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

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

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

  37. 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);} 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. Programming In C++, Lecture 2

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

  39. Introduction To C Programming Language Quiz • 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? Programming In C++, Lecture 2

More Related