CS 214 - C Programming Notes
Author: Mali Payani
Lecturer: Dr Jonathan Sparks
CS 214 - Lecture 1- CH. 2
Intro to the C Language
Background:
Core programming language
Why C?
Powerful
Syntax similar to other languages
Structured high-level language
Efficient
Reusable through libraries
Mature and wide accepted
C's History:
Timeline:
1960s- ALGOL (Algorithm)
1967- BCPL (Martin Richards) Basically Combined Programming Language
1970- B Language (Ken Thompson)
1972- C language (Dennis Ritchie)
1989- ANSI C standardization
1999- Major C standard update
2011- Minor C Update
Basic Parts of a Program:
1. Preprocessor Directives
2. Global Declarations
3. Local Declarations
4. Statements
5. Other Functions
,CS 214 - Lecture 2
Steps to Program:
The basic steps are:
1. Understand: understanding what the problem is will help us to find a solution.
2. Plan (Risk Analysis): finding a solution (flow charts).
3. Code: Implementation
4. Compile
5. Test
6. Implement: Publishing app to the public
Don't forget to documentation. Documentation is for other coders.
Error Types:
Syntax
Prevents a program from compiling
Error message may help solve these.
Misspelling, punctuation, spacing
Logic (a problem with the implementation)
Prevents a program from running properly.
More subtle (painful)-the joy of programming
Example: Start at 1, count by 2's until counter equals 100.
Programs can run with logic errors.
Testing is important here.
#Page 32
Comments
Describes a function or code block's purpose.
Block comment: /* text*/
Line Comments: // (1999 Standard)
Don't overuse.
Can appear anywhere a space can but cannot be nested.
Structure of a C Program: pg. 32
Preprocessor Directives
Global Directives
Local Directives
Statements
Other functions as required
Preprocessor Directives (like a to-do list):
#include <stdio.h>
- Tells the compiler to do something before compiling.
- Appears at the beginning of the code.
- Begins with a '#'
,CS 214 - Lecture 3
#include includes a header file or library (like java's 'import')
Appendix G-pg. 1071: writing your own libraries
Line the # all the way to the lef
No space between # and 'include'
stdio- standard input/output: allows user to write on the screen (printf function)
What are Libraries?
Additional features (mostly functions) which can be available to the program.
C is a small language-libraries extend it as needed. ex. #include <stdio.h>
Standard libraries are available for all. <stdio.h>; printf
o Standard libraries available: Appendix F - pg. 1059
Programmers can create their own so that they themselves or other programmers can access it.
The main() function
int main(void) /*int returns an integer value. It is not required
to type void. All it means is it doesn't receive anything as it's
running.*/
{
declaration section
statement section
}
A main() is required!
The program begins and ends with main()
It calls other functions.
The printf() Function:
printf() is a standard library function from <stdio.h>
You cannot use the printf without <stdio.h>
This library is used for most programs.
Items between parentheses of a function are called parameters or arguments.
ex. printf("Hello world!"); // the string is the parameter or argument.
printf() Control Codes:
"escape sequences" are used for special things.
Control codes begin with a '\'
List of codes: pg. 48
Carriage return returns to next line.
Variable Identifiers:
Name referring to a place in memory where data or objects can be stored or a box that holds something.
A variable has a:
o Name
o Type
o Value
o Address
, Using a variable name accesses the value stored at that memory location.
Identifier Naming: see pg. 37
First character must be alphabetical character or underscore
Must consist only of alphabetical characters, digits, or underscores.
First 63 characters of an identifier are significant.
Cannot duplicate a keyword.
CS 214 - Lecture 4
Author: Mali Payani
Lecturer: Dr Jonathan Sparks
CS 214 - Lecture 1- CH. 2
Intro to the C Language
Background:
Core programming language
Why C?
Powerful
Syntax similar to other languages
Structured high-level language
Efficient
Reusable through libraries
Mature and wide accepted
C's History:
Timeline:
1960s- ALGOL (Algorithm)
1967- BCPL (Martin Richards) Basically Combined Programming Language
1970- B Language (Ken Thompson)
1972- C language (Dennis Ritchie)
1989- ANSI C standardization
1999- Major C standard update
2011- Minor C Update
Basic Parts of a Program:
1. Preprocessor Directives
2. Global Declarations
3. Local Declarations
4. Statements
5. Other Functions
,CS 214 - Lecture 2
Steps to Program:
The basic steps are:
1. Understand: understanding what the problem is will help us to find a solution.
2. Plan (Risk Analysis): finding a solution (flow charts).
3. Code: Implementation
4. Compile
5. Test
6. Implement: Publishing app to the public
Don't forget to documentation. Documentation is for other coders.
Error Types:
Syntax
Prevents a program from compiling
Error message may help solve these.
Misspelling, punctuation, spacing
Logic (a problem with the implementation)
Prevents a program from running properly.
More subtle (painful)-the joy of programming
Example: Start at 1, count by 2's until counter equals 100.
Programs can run with logic errors.
Testing is important here.
#Page 32
Comments
Describes a function or code block's purpose.
Block comment: /* text*/
Line Comments: // (1999 Standard)
Don't overuse.
Can appear anywhere a space can but cannot be nested.
Structure of a C Program: pg. 32
Preprocessor Directives
Global Directives
Local Directives
Statements
Other functions as required
Preprocessor Directives (like a to-do list):
#include <stdio.h>
- Tells the compiler to do something before compiling.
- Appears at the beginning of the code.
- Begins with a '#'
,CS 214 - Lecture 3
#include includes a header file or library (like java's 'import')
Appendix G-pg. 1071: writing your own libraries
Line the # all the way to the lef
No space between # and 'include'
stdio- standard input/output: allows user to write on the screen (printf function)
What are Libraries?
Additional features (mostly functions) which can be available to the program.
C is a small language-libraries extend it as needed. ex. #include <stdio.h>
Standard libraries are available for all. <stdio.h>; printf
o Standard libraries available: Appendix F - pg. 1059
Programmers can create their own so that they themselves or other programmers can access it.
The main() function
int main(void) /*int returns an integer value. It is not required
to type void. All it means is it doesn't receive anything as it's
running.*/
{
declaration section
statement section
}
A main() is required!
The program begins and ends with main()
It calls other functions.
The printf() Function:
printf() is a standard library function from <stdio.h>
You cannot use the printf without <stdio.h>
This library is used for most programs.
Items between parentheses of a function are called parameters or arguments.
ex. printf("Hello world!"); // the string is the parameter or argument.
printf() Control Codes:
"escape sequences" are used for special things.
Control codes begin with a '\'
List of codes: pg. 48
Carriage return returns to next line.
Variable Identifiers:
Name referring to a place in memory where data or objects can be stored or a box that holds something.
A variable has a:
o Name
o Type
o Value
o Address
, Using a variable name accesses the value stored at that memory location.
Identifier Naming: see pg. 37
First character must be alphabetical character or underscore
Must consist only of alphabetical characters, digits, or underscores.
First 63 characters of an identifier are significant.
Cannot duplicate a keyword.
CS 214 - Lecture 4