Sunteți pe pagina 1din 23

CS 111

Fundamentals of Computer Programming

Lecture # 1
Basic Introduction
Instructor: Lec Razia Sharif
Sequence
• Why study programming language
• What is computer and its organization
• Types of Programming Languages
• History of C and C++
• Basics of a Typical C++ Environment
• Errors
• Identifiers in C/C++
• Basic C/C++ Program Structure
• Dissection of program
Why study programming
languages?
• Programming languages are important for
students in all disciplines of engineering because
they are the primary tools of the central
activity of any science.

3
Why study programming
languages? (cont.)
• To improve your ability to develop effective
algorithms and to improve your use of existing
programming language.
• To increase your vocabulary of useful
programming constructs.
• To allow a better choice of programming
languages.
• To make it easier to learn a new language.

4
What is a Computer?
• Computer
– A device capable of performing computations and making
logical decisions
• Computer programs
– Sets of instructions that control a computer’s processing of
data
• Hardware
– Various devices comprising a computer
• Examples: keyboard, screen, mouse, disks, memory, CD-ROM, and
processing units
• Software
– Programs that run a computer
Computer Organization
• Six logical units in every computer:
– Input unit
• Obtains information from input devices (keyboard, mouse)
– Output unit
• Outputs information (to screen, to printer, to control other devices)
– Memory unit
• Rapid access, low capacity, stores input information
– Arithmetic and logic unit (ALU)
• Performs arithmetic calculations and logic decisions
– Central processing unit (CPU)
• Supervises and coordinates the other sections of the computer
– Secondary storage unit
• Cheap, long-term, high-capacity storage, stores inactive programs
Types of Programming Languages
• Machine languages
– Combination of 1 and 0 giving machine specific instructions
– Example:
00000 00001 00010 00110 00000 100000
100011 00011 01000 00000 00001 000100
000010 00000 00000 00000 10000 000000
• Assembly languages
– English-like abbreviations representing elementary computer
operations (translated via assemblers)
– Example:
LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY
• High-level languages
– Similar to everyday English, use mathematical notations
(translated via compilers)
– Example:
grossPay = basePay + overTimePay
Some High-Level Languages
• Java used to
– Create web pages with dynamic and interactive content
– Develop large-scale enterprise applications
– Enhance the functionality of web servers
– Provide applications for consumer devices (such as cell phones, pagers and
personal digital assistants)
• FORTRAN
– Used in scientific and engineering applications
• COBOL
– Used to manipulate large amounts of data
• Pascal
– Used to teach structured programming
• C/C++
– Middle-level language
– Provides facilities for both high level as well as low level programming
History of C and C++
• C++ evolved from C
– C evolved from B
• ANSI C
– Established worldwide standards for C programming
• C invented by Dennis Ritchie of AT&T Bell Labs in the 1970s
• C++ by Bjarne Stroustrup of the same labs in the early 1980s
• C++ “spruces up” C
– Provides capabilities for object-oriented programming
• Objects are reusable software components that model things in the real world
• Object-oriented programs are easy to understand, correct and modify
• C is a subset of C++
Basics of a Typical C++ Environment
Phases of C++ Programs: Editor Disk
Program is created in
the editor and stored
on disk.

1. Edit Preprocessor Disk


Preprocessor program
processes the code.

2. Preprocess Compiler Disk


Compiler creates
object code and stores
it on disk.

3. Compile Linker links the object


code with the libraries,
Linker Disk creates .exe and
stores it on disk
4. Link Primary
Memory
Loader
5. Load Loader puts program
in memory.

6. Execute Disk ..
..
..

Files: Primary
Memory
CPU
  CPU takes each
instruction and
*.cpp, *.c, *.h executes it, possibly
storing new data
values as the program
.. executes.
*.obj, *.exe ..
..
Errors
• Syntax errors
– reported by the compiler
• Linker errors
– reported by the linker
• Execution/Run-time errors
– reported by the operating system
• Logic errors
– not reported
Identifiers in C/C++
• A name assigned to:
– A constant
– Variable
– Function
– User defined data type (will be discussed in the next course)
Identifiers in C/C++ …
• Rules
– Can be from one to several characters long
– First 1024 characters are significant
– May consist of alphanumeric and underscore characters
– May start with any letter of alphabet, or with an underscore
– Are case sensitive
– No special characters are allowed
– Spaces are not allowed as part of the identifier name
– Keywords cannot be used as identifiers
– Any other reserved words by the language (such as names of
library functions) shall not be used as identifiers
Identifiers in C/C++ …
• Conventions
– Use identifier names that reflect the meaning or usage
of the items being named

– THISISACONSTANT or THIS_IS_A_CONSTANT
– thisisavariable or this_is_a_variable
– thisIsAFunction
– ThisIsAUserDefinedDataType
Basic C/C++ Program Structure
/**********************************************************
* Header Comments
**********************************************************/
Pre-processor directives
int main()
{
declarations and executable statements
return 0;
}//end block of main

// comments required by some organizations!


/*****************************************************
* Sum two numbers
******************************************************/
#include <stdio.h> //declares standard I/O library
void main() //must have one and only one function named main
{
//declare integer variables
int num1, num2, sum;
printf(“Enter two integers: “);
//input values for two variables from keyboard
scanf(“%d %d”, &num1, &num2)
sum = num1 + num2;
//output sum of two numbers to the screen
printf(“The sum is: %d “, &sum);
}//end block of main
Our First C/C++ Program
/* Program #1 - A first C/C++ program.

Enter this program, then compile and run it.


*/

#include <stdio.h>

// main() is where program execution begins.


void main()
{
printf(“Hello, World!!!\n");
}
Dissection of Program
Comments have two forms in C/ C++
– //Single line comments
– /*Multi-line comments*/

/* Program #1 - A first C/C++ program.

Enter this program, then compile and run it.


*/
• This is a comment (C style)
• Ignored by the compiler i.e. can write anything and the
compiler won’t see it at all
• Multiline or single or mixed with code.
• Why use comments?
Dissection of Program
void main()
{
program statements
}
• Every C program has to have this Function;
execution begins here even if other functions there;
operating system calls main( )
– () means takes no arguments;
– {…} signify start and end of function
Dissection of Program
printf(“Hello, World!!!“);

• Think of printf as the monitor screen (console


output)
• Prints the entire string within quotes to the screen
• This whole line is a statement
• C/C++ Statement that ends with a “;”
Dissection of Program
#include <stdio.h>

• Tells preprocessor to include contents of the text file


“stdio” with your source code before compiling
• “stdio” has details about things that handle input and
output e.g. printf()
• “stdio” is called an include file or header file
• This is called an include directive or preprocessor
directive
#include files
– <filename.h> //Standard C library header file
– <filename> //Standard C++ library files
• Files found in the directory defined by the INCLUDE
environment variable

– “myfile.h” //Your files


• Files found in the current directory
THANKS

S-ar putea să vă placă și