Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 19

CS 1001 A

Programming in C Fall 2011

Instructor Sami Ullah Kashif

What is a Computer?
Computer
A device capable of performing computations and making logical decisions

Computer programs
Sets of instructions that control a computers 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
Strings of numbers giving machine specific instructions Example:
+1300042774 +1400593419 +1200274027

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: 1. Edit
Editor Disk
Program is created in the editor and stored on disk. Preprocessor program processes the code. Compiler creates object code and stores it on disk. Linker links the object code with the libraries, creates a.out and stores it on disk

Preprocessor

Disk

2. Preprocess
3. Compile

Compiler

Disk

Linker

Disk
Primary Memory

4. Link 5. Load 6. Execute Files:


CPU Loader

Loader puts program in memory.

Disk

. . . . . .

Primary Memory

*.cpp, *.c, *.h *.obj, *.exe


. . . . . .

CPU takes each instruction and executes it, possibly storing new data values as the program executes.

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

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 Global declarations 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 wont 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

You might also like