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.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
 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
Structure of a C program
CMT Programming Software Applications
 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.
Chapter 2: Introduction to C++.
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Programming Introduction to C++.
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.
Chapter 01: Introduction to Computer Programming
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Introduction to C++ Programming Introduction to C++ l C is a programming language developed in the 1970's alongside the UNIX operating system. l C provides.
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
© 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.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Introduction to C++ Programming COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 2 Introduction to C++ l C is a programming language developed.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
 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:
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.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2 - Introduction to C Programming
Basic Elements of C++.
Revision Lecture
Mr. Shaikh Amjad R. Asst. Prof in Dept. of Computer Sci. Mrs. K. S
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Basic Elements of C++ Chapter 2.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2: Introduction to C++.
Programming Introduction to C++.
Chapter 2 - Introduction to C Programming
Variables in C Topics Naming Variables Declaring Variables
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Presentation transcript:

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 http://www.ustudy.in/node/10273 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

#include<stdio.h> #include<conio.h> void main( ) { Structure of C Program To include header files in our program Header File Contains built-in-functions. These are also called pre-defined or already developed functions 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 Return type of main function. It is used to determines whether the program is executed successfully or not. Void means nothing File name(standard input output ) Function Name Starting point of program execution 2 main function Body Begin Parenthesis shows function. It is used to pass parameters/arguments 3 Program Body Body End Each statement must end with semicolon. A statement without semicolon generates syntax error. 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(“ “)

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

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

Now press enter to go back to the source code of the program 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

Now see the following sample program.

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

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

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

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

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

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

Non - executable statements 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. */

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.

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

C++ compiler directives The #include directive 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

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

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

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.

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.

Alphabets from A to Z or a to z The digits from 0 to 9 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

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

How many bytes I am eating? integer data short 2 bytes int 2 bytes(16 bit system) 4 bytes (32 bit system) long 4 bytes Floating point data float double 8 bytes long double 10 bytes Character char 1 byte Boolean bool

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

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

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

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

New Computer Scientists Thank You New Computer Scientists