Professional Documents
Culture Documents
Programming in C Fall 2011
Programming in C Fall 2011
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
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
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
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++
Preprocessor
Disk
2. Preprocess
3. Compile
Compiler
Disk
Linker
Disk
Primary Memory
Disk
. . . . . .
Primary Memory
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
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
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