1 / 34

Program

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 Fundamentals I. BSCS Semester1. Diagrammatic Representation of Program Execution Process. Execution of C Program.

dougal
Download Presentation

Program

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

  2. Programming Fundamentals I BSCS Semester1

  3. Diagrammatic Representation of Program Execution Process

  4. Execution of C Program • Preprocessing • Using a Preprocessor program to convert C source code in expanded source code. "#includes" and "#defines" statements will be processed and replaced actually source codes in this step. • Compilation • Using a Compiler program to convert C expanded source to assembly source code. • Assembly • Using a Assembler program to convert assembly source code to object code. • Linking • Using a Linker program to convert object code to executable code. Multiple units of object codes are linked to together in this step. • Loading • Using a Loader program to load the executable code into CPU for execution. http://www.ustudy.in/node/10273

  5. Structure of C Program Header File Contains built-in-functions. These are also called pre-defined or already developed functions To include header files in our program Preprocessor Directives Angle or pointed brackets #include<stdio.h> #include<conio.h> void main( ) { printf(“Welcome to Computer Scientists”); getch(); } h stands for header file 1 File name(standard input output ) Return type of main function. It is used to determines whether the program is executed successfully or not. Void means nothing Function Name Starting point of program execution 2 main function Parenthesis shows function. It is used to pass parameters/arguments Body Begin Program Body 3 Each statement must end with semicolon. A statement without semicolon generates syntax error. Body End Get character function. It get a character at runtime. But we use it to stay screen to see output printf stands for print function. It will print the output on the monitor String or message to be displayed on monitor. It must be enclosed in double quotes(“ “)

  6. Simple Steps to write and run a program in C. • Go to this path C:\TC\BIN then double click on short to open C language. • Now save your program by choosing a meaningful program file name. e.g. welcome.c is a file name for a program that will display welcome message at runtime. • Now write your program as explained in the previous slide. (translation of algorithm to a c program).

  7. 4. Press Alt+F9 key to compile program. If your program is error free then following screen will appear:

  8. 5. Now press Ctrl+F9 key to run the program. Following screen shows the output of the program: Now press enter to go back to the source code of the program

  9. Now see the following sample program.

  10. Formatted Input Statement • scanf() • The scanf function allows you to accept input from standard in, which for us is generally the keyboard. • Syntax:      scanf(”control string”, &variable01, &variable02, …); • Example:        scanf(”%d”, &number); • E.g. Program • #include <stdio.h> • int main() • { int a, b, c; • printf("Enter the first value:"); • scanf("%d", &a); • printf("Enter the second value:"); • scanf("%d", &b); • c = a + b; • printf("%d + %d = %d\n", a, b, c); • return 0; • } http://computer.howstuffworks.com/c6.htm

  11. Formatted Output Statement • Printf() : ( Output function ) • It displays information on screen. • It returns the number of characters printed. • It displays the text you put inside the double quotes. • It requires the backslash character(escape sequence) to display some special characters. • It can display variables by using the % conversion character. • Printf format: a string argument followed by any additional arguments.                                    • Syntax : •          printf("control string",list); •            where control string gives the format of data to be displayed, list contains the list of variables, constants, array names to be printed. The general form is • %w.p data type •                           where % - conversion specification indicator •                                      w - width of output data(optional) •                                      p - Number of digits after decimal point or number of characters to to displayed from a string •                                      data type - type of output data or conversion character • Printing Integer Numbers : • The general format of control string to output an integer is • %wd •       where w - width of output data(optional) •                 d - conversion character for integer

  12. Rules : • If there is no width then the output number will be printed as such. • If the output number width is greater than the width specified then it will be printed in full irrespective of the width. • The number printed is right justified. This can be changed by left by placing a minus sign after % character. • If the width specified is greater than the width of the output number, then the unused position is left blank. • The unused leading blanks in the output can be made to zero by placing a zero before the width w. • To print numbers with + or - sign, the sign should be placed before the variable. • Example : • Assume x=2000; • Format                                               Output • printf("%d",x);                                    2000   // Rule 1 • printf("%3d",x);                                  2000   // Rule 2 • printf("%5d",x)                                      2000     // Rule 4 • printf("%-5d",x);                                2000        // Rule 3 • printf("%07d",x);                               0002000     // Rule 5 • printf("%4d",-x);                                -2000    // Rule 6

  13. Printing Real Numbers : • The general format of control string to output a real number is •                  %w.p f or e • where  w - width of output data(optional) •  p - number of digits after decimal point • f - conversion character for floating point without exponent • e - conversion character for floating point with exponent • Rules : • If no decimal place count(p) is placed, the default is 6 places. • If a number contains more than 6 decimal places, the result will be rounded to 6 places. • The integer part of a number is right justified and the decimal part is left justified. • By placing a - sign after % character the integer part can be made to left justified. • To print a number with + or - sign the sign should be placed before the variable. • Example : • Assume x=123.4678; • Format                                               Output • printf("%8.4f",x);                                123.4678 • printf("%f",x);                                      123.467800  • printf("%e",x);                                     1.234678e+02 • printf("%-8.2f",x);                                123.47  (left justified) • Printing strings : • The general format of control string to output a string is • %w.ps • where   w - width of the string(optional) • p - number of characters to be printed from the beginning • s - conversion character for string

  14. Rules : • If the width of the string is greater than the width specified, the string will be printed in full. • The output is right justified. • By placing a - sign after % character it can be changed to left justified. • Example : • Assume college="mspvl"; • Format                                               Output • printf("%s",college);                           mspvl • printf("%10s",college);                       mspvl • printf("%3s",college);                         mspvl • printf("%5.2s",college);                     ms

  15. Another Sample Program • Program: • #include<stdio h> • void main() • { • int number; • printf(”Enter an integer: \n”); //prompt message • scanf(”%d”, &number); //read message • printf(”The number you entered is: %d”, number); • getch(); • } • Output:    Enter an integer:    25    The number you entered is: 25 • Rules: •  The ampersand symbol, &, is very important. • It’s an operator specifying the variable name’s address. • Omitting it, might result into unexpected results. • We dont use ampersand(&) symbol in front of the string_name.

  16. Comments Non - executable statements Comments are used for program documentation Two formats Single Line Comments Multi Lines Comments // This program is used to show the Welcome Message. /* This program is used to show the square of even numbers from 10 to 100. */

  17. Tokens Tokens are individual words and punctuation marks in passage of text. In C, program the smallest individual units are known as C Tokens. C has Six types of Tokens. The Tokens are shown in figure. C programs are written using these tokens and the syntax of the language.

  18. General form of a C++ program // Program description #include directives int main() { constant declarations variable declarations executable statements return 0; }

  19. C++ compiler directives • The #includedirective tells the compiler to include some already existing C++ code in your program. • The included file is then linked with the program. • There are two forms of #include statements: #include <iostream> //for pre-defined files#include "my_lib.h" //for user-defined files

  20. C++ keywords • Each keyword has a predefined purpose in the language. • Do not use keywords as variable and constant names!! • Exmples: bool, break, case, char, const, continue, do, default, double, else, extern, false, float, for, if, int, long, namespace, return, short, static, struct, switch,typedef, true, unsigned, void, while etc etc..

  21. C++ identifiers • Variable: Location in memory where value can be stored • An identifier is a name for a variable, constant, function, etc. • Series of characters (letters, digits, underscores) • Cannot begin with digit • Are Case sensitive • Cannot have special characters in them. • Examples of valid identifiers: First_name, age, y2000, y2k • Examples of invalid identifiers: 2000y, X=Y, J-20, ~Ricky,*Michael • Identifiers are case-sensitive. For example: Hello, hello, WHOAMI, WhoAmI, whoami are unique identifiers. • http://www.ustudy.in/node/2897

  22. Programming Style C++ is a free-format language, which means that: • Extra blanks (spaces) or tabs before or after identifiers/operators are ignored. • Blank lines are ignored by the compiler just like comments. • Code can be indented in any way. • There can be more than one statement on a single line. • A single statement can continue over several lines. In order to improve the readability of your program, use the following conventions: • Start the program with a header that tells what the program does. • Use meaningful variable names. • Document each variable declaration with a comment telling what the variable is used for. • Place each executable statement on a single line. • A segment of code is a sequence of executable statements that belong together. • Use blank lines to separate different segments of code. • Document each segment of code with a comment telling what the segment does.

  23. Variables • As a programmer, you will frequently want your program to "remember" a value. For example, if your program requests a value from the user, or if it calculates a value, you will want to remember it somewhere so you can use it later. The way your program remembers things is by using variables. For example: • int b; This line says, "I want to create a space called b that is able to hold one integer value." A variable has a name (in this case, b) and a type (in this case, int, an integer). You can store a value in b by saying something like: • b = 5; You can use the value in b by saying something like: • printf("%d", b); In C, there are several standard types for variables: • int - integer (whole number) values • float - floating point values • char - single character values (such as "m" or "Z") • We will see examples of these other types as we go along.

  24. Rules to Declare an Identifier (variable) • Alphabets from A to Z or a to z • The digits from 0 to 9 • Underscore(_) can be used • The first character of an identifier can not be a digit • The name of an identifier can not be a reserve word • No space allowed in the name of identifier Valid Name: A Student_Name _Fname Pi Inalid Name: $Sum //special ch. 6StName // 1st letter digit F name // no space allowed int // reserve word

  25. Identifier (variable) Declaration Syntax Data-Type Space Variable-Name(Indentifier); e.g. intfrstNumber; char choice; float divide; long output; Identifier (variable) Initialization Syntax Data-Type Space Variable-Name(Indentifier) = Value; e.g. intfrstNumber=10; char choice=‘y’; float divide=0.0;

  26. How many bytes I am eating?

  27. Assignment Operator (=) • = (assignment operator) • Assigns value to variable • Binary operator (two operands) • Example: sum = variable1 + variable2;

  28. Memory Concepts • Variable names • Correspond to actual locations in computer's memory • Every variable has name, type, size and value • When new value placed into variable, overwrites previous value

  29. 45 45 72 45 72 first first second first second 117 sum Memory Concepts Variable Identifier • cin >> first; • Assume user entered 45 • cin >> second; • Assume user entered 72 • sum = first + second;

  30. Arithmetic • Rules of operator precedence • Operators in parentheses evaluated first • Nested/embedded parentheses • Operators in innermost pair first • Multiplication, division, modulus applied next • Operators applied from left to right • Addition, subtraction applied last • Operators applied from left to right

  31. Thank You New Computer Scientists

More Related