The Language
Where did it come from?
-Seed language was the Basic Combined Programming Language(BCPL) in 1967
-BCPL refined into a language called B
-Dennis Ritchie enhanced B to form C in 1972
-The American National Standards Institute(ANSI) completed C standards in 1988
Why C?
-C is a powerful and flexible language.
-C is a popular language preferred by professional programmers.
-C is a portable language.
-C is a language of few words, containing only a handful of terms keywords.
-C is modular.
-Structured language.
Forming a C Program
Whitespace
-spaces, tabs, line endings, and blank lines.
-irrelevant to the compiler.
-should be used in specific places to make your program more readable.
Outline Form
-Your program should resemble an outline with main topics to the left and subordinate topics to the right.
-Statements end with semicolon ";"
-Where the line ends is usually not important.
-However, line breaks are not allowed inside of quotations.
Comments
-Comments in C are shown by /* and */
-Everything between /* and */ is ignored by the compiler.
-Comments are very important to the understandability of a program.
-Make sure you end all of your comments.
Directives
-Directives tell the preprocessor to do things
-Directives always begin with #
-Directives end at the end of the source line
-The #include in the example program tells the preprocessor to insert the code found in the stdio.h file at that location.
Statements
-A statement is a HLL instruction that performs a specific operation.
e.g. quiz = 20;
-All single statements end with a semicolon and may extend over multiple lines.
e.g. printf("A perfect quiz is %d points.\n", quiz);
Assignment statement
-A statement is a complete direction instructing the computer to carry out some task.
e.g. x = 2 + 3;
Compound statement
-A block, group two or more C statement enclosed in braces
e.g. { printf("Hello");
printf("Class"); }
Functions
-A function is a set of instructions that performs an operation
-A function will always have a name that can be used to reference (or call) the function.
-The function name will be followed by the argument list in parentheses (sometimes there will be no arguments and so the parentheses will be empty).
Values in C
Numeric values
-Real numbers
-Integral number (or whole numbers)
Character values
-ASCII code is an integer value for characters
-The character 'A' is the same as 65 in ASCII
-You can add characters
'A' + ' ' = 'a' 65 + 32 = 97
Special ASCII Characters
-Special characters have a backslash \ followed by a printable character
\0 = Null \t = Horizontal tab
\a = Audible alarm \v = Vertical tab
\b = Backspace \' = Apostrophe
\f = Form feed \" = Quote
\n = New line \? = Question mark
\r = Carriage Return \\ = Backslash
Integral Data Types
- Basic types
§ Character type char
§ Integer type int
§ Floating type float
§ Valueless type void
- Both can be signed (default) or unsigned
- Type int can be short, long, or neither
- Type float can be double, long double
- Signed allows the value to be negative
- Unsigned allows only positive (and zero)
The char Data Type
- Eight bits long
- Used to store characters, but really numbers
- Range for signed char values is -128 to 127
- Range for unsigned char values is 0 to 255
- Some valid char declarations
char letter;
unsigned char index;
char TopGrade = ‘A’, BottonGrade = ‘F’;
char TopGrade = 65, BottonGrade = 70;
The int Data Type
- Variable length
- Type long int at least as big as short int
- The word int can be implicit
- Some valid int declarations
int currentPage, lastPage;
short age;
unsigned volts = 110;
unsigned volts = ‘n’;
unsigned long nationalDebt;
signed short variance;
Floating Point Data Types
-Stored in scientific notation
o Mantissa of significant digits
o Exponent power of 10
-Expressed in standard or scientific notation
o Can use 7146.0 or 7.146e3
-Three flavors (float, double, long double)
o Type double greater than or equal to float
o Type long double greater than or equal to double
Floating Point Forms
-A value with a decimal stored as double
-Explicitly assign type
o Use F or f for a float (3.806e-3F or 0.003806F)
o Use L or l for a long double
-Some valid floating point declarations
float wageRate = 12.75;
double area, volume;
long double humongous;
String Data
- A string is a set of characters
- String values indicated by quotes
o For example, “This is a string”
- The quotes are not part of the string
printf(“This is a string”);
results in
This is a string
More String Data
- Adjacent strings are concatenated
o So, “This is just” “one string.” would be stored as This is just one string.
- Remember, no line endings inside a string
- Quote marks within the string are special characters and are indicated by \”
printf(“Her name\nis \”Henrietta.\””);
Her name
is “Henrietta.”
Variables
- Represent a location or locations in memory
- Values are placed there to be used in a program
- Values can change, thus are “variable”
- Variable names are used to reference
Variable Names
- Length depends on compiler (at least 31)
- Only alpha-numeric (A to Z, a to z, and 0 to 9) characters and the underscore ( _ )
- Must begin with alpha or underscore
- No blank space between variable names
- C is case-sensitive
o So Total is different from total and toTal
- No reserved words (also called keywords)
Variable Declarations
- All variables must be declared before used
datatype variable [= initialization]
int frequency;
int StartValue = 14;
- A declaration does a number of things
o Tells the compiler how to store the value
o Defines it, memory is allocated for it
o May initialize it, giving it an initial value
Simple Output
- Most common output function is printf()
- Uses codes to represent data placement
o Character values represented by %c
o Integer values represented by %d
o Float values represented by %f
o ASCII values for characters can be printed by using the integer code %d
Conversion Characters
%s string of character
%c character
%d decimal integer
%f floating-point numbers
%e exponential notation
%o octal integer
%x hexadecimal integer
%% percent sign (%)
Example of Output
Program :
#include
void main(void)
{
printf(“Here is the character %c,\n”, ‘X’);
printf(“and the numbers %d and %f.\n”, 46, 12.345);
printf(“Now we will print %d as a number.\n”, ‘X’);
printf(“Things will really be messed up if we confuse\n”
“data types, such as %f and %d.\n”, 46, 12.345);
}
Here is the character X,
and the numbers 46 and 12.345.
Now we will print 88 as a number.
Things will really be messed up if we confuse
data types, such as -2.19317e-73 and 16424.
Expression
- Anything that evaluates to a numeric value
- consist of a single item, simple variable, literal constant or symbolic constant
e.g.
2 + 3;
or
rate * 5;
Operators
- An operator is a symbol that performs a specific mathematical or logical operation
- Types of operators
· arithmetic
· assignment
· equality
· relational
· logical
· Increment and Decrement operators
Arithmetic Expressions
- Anything reducible to a single value
- Consists of values and/or variables connected by arithmetic operators
- Precedence determines what operator gets done first
- Associativity dictates the order if two operators have the same precedence
RULES OF OPERATOR PRECEDENCE
1. Parentheses ( )
2. Multiplication *
Division /
Modulus %
3. Addition +
Subtraction -
Integer Arithmetic
- Result is integer
- Be careful result is not too big
o For example, 32000 * 10 can result in -7680
- Division truncates any fractions
o The result of 3 / 2 is 1, not 1.5
- Remainder (or modulo or modulus)
o Only good with positive integers
o The result of 14 % 10 is 4, 3 % 10 is 3
Mixed Arithmetic
- Result is the highest data type used
- Highest data type takes most memory
o Floats are higher than Integers
Assignment Operator | Sample Expression |
---|---|
+= | c+=7 |
-= | d-=4 |
*= | e*=5 |
/= | f/=3 |
%= | g%=9 |
Equality
To determine whether two items are equal to one another.
C equality | standard algebraic |
---|---|
== (x == 100) | |
!= (5 != x) |
Relational
C provides a set of relational that allows you to compare numbers.
Operator Example
> x>100
>= x>=20
Logical
C provides a set of logical that are used to combine expressions into logical expression
Operator
&& logical AND
II logical OR
Increment & Decrement
++ ++a = increment a by 1 then use the new value of a in the expression in which a resides
++ a++ = use the current value of a in the expression in which a resides, then increment a by 1
-- --a = decrement a by 1 then use the new value of a in the expression in which a resides
-- a-- = use the current value of a in the expression in which a resides, then decrement a by 1
0 comments:
Post a Comment