Variables in C have the same meaning as variables in algebra. That is, they represent some unknown, or variable, value.
x = a + b
Variables in C may be given representations containing multiple characters. But there are rules for these representations.
Variable names (identifiers) in C
May only consist of letters, digits, and underscores
May be as long as you like, but only the first 31 characters are significant
May not begin with a digit
May not be a C reserved word (keyword)
Reserved Words (Keywords) in C
auto break float for sizeof static
case char goto if
const continue int long struct switch
Default do register return
double else short signed typedef union
enum extern unsigned void volatile while
Naming Conventions
C programmers generally agree on the following conventions for naming variables.
Begin variable names with lowercase letters
Use meaningful identifiers
Separate “words” within identifiers with underscores or mixed upper and lower case.
Be consistent!
C is case sensitive.
Use all uppercase for symbolic constants (used in #define preprocessor directives).
Note: symbolic constants are not variables, but make the program easier to read.
Examples:
#define PI 3.14159
#define AGE 52
What Does a Variable Have?
Every variable has:
a name (e.g., number of students),
an address (i.e., a location in memory, such as 123456),
a data type (e.g., int, float, char), and
a value (which may be undefined, also known as garbage). The value is also known as the contents of the variable — that is, the value is the contents of the variable’s memory location.
Compile Time and Run-time
Events that occur while a program is being compiled are said to happen at compile time.
Events that occur while a program is running are said to happen at runtime.
For example, the address of a variable is chosen at compile time, while its value often is determined at runtime.
Declaring Variables
Before using a variable, you must give the compiler some information about the variable; i.e., you must declare it.
The declaration statement includes the data type of the variable.
Examples of variable declarations:
int age ;
float area ;
When we declare a variable
Space is set aside in memory to hold a value of the specified data type
That space is associated with the variable name
That space is associated with a unique address
Unless we specify otherwise, the space has no known value.
Visualization of the declaration
int age ;
Notes About Variables.
You must not use a variable until you somehow give it a value.
You can not assume that the variable will have a value before you give it one.
Some compilers do, others do not! This is the source of many errors that are difficult to find.
Assume your compiler does not give it an initial value!
Variables may be given initial values, or initialized, when declared.
Displaying Variables.
Variables hold values that we occasionally want to show the person using the program.
We have a function called printf( ) that will allow us to do that.
The function printf needs two pieces of information to display things.
How to display it
What to display
printf( “%f\n”, diameter );
The name of the function is “printf”.
The %f is known as a placeholder: it holds the place of the value of the variable that we actually want to output.
Inside the parentheses are:
print specification, where we are going to display:
a floating point value (“%f”)
We want to have the next thing started on a new line (“\n”).
We want to display the contents of the variable diameter.
Code
|
Format
|
%c
|
character
|
%d
|
signed integers
|
%i
|
signed integers
|
%e
|
scientific notation, with a lowercase
"e"
|
%E
|
scientific notation, with a uppercase
"E"
|
%f
|
floating point
|
%g
|
use %e or %f, whichever is shorter
|
%G
|
use %E or %f, whichever is shorter
|
%o
|
octal
|
%s
|
a string of characters
|
%u
|
unsigned integer
|
%x
|
unsigned hexadecimal, with lowercase
letters
|
%X
|
unsigned hexadecimal, with uppercase
letters
|
%p
|
a pointer
|
%n
|
the argument shall be a pointer to an
integer into which is placed the number of characters written so far
|
%%
|
a '%' sign
|
scanf (“%f”, &diameter);
The scanf( ) function also needs two items:
The input specification “%f”. (Never put a “\n” into the input specification.)
The address of where to store the information. (We can input more than one item at a time if we wish, as long as we specify it correctly.)
Notice the “&” in front of the variable name. It says to use the address of the variable to hold the information that the user enters.
Comments should explain why you are doing something, not what you are doing it.
Reading Multiple Variables with a Single scanf
C allows inputting multiple variables per scanf statement. At runtime, when the user types in the input values, they can separate the individual input values
by blank spaces, and/or
by tabs, and/or
by carriage returns (newlines).
Blank spaces, tabs and carriage returns, as a group, are known as white space.
Arithmetic Operators in C
Name Operator Example
Addition + num1 + num2
Subtraction - initial - spent
Multiplication * fathoms * 6
Division / sum / count
Modulus % m % n
If both operands of a division expression are integers, you will get an integer answer. The fractional portion is thrown away.
Examples : 17 / 5 = 3
4 / 3 = 1
35 / 9 = 3
Division By Zero.
Division by zero is mathematically undefined.
If you allow division by zero in a program, it will cause a fatal error. Your program will terminate execution and give an error message.
Non-fatal errors do not cause program termination, just produce incorrect results.
Arithmetic Operators Rules of Operator Precedence
Operator (s) Precedence & Associativity
( ) Evaluated first. If nested (embedded), innermost first. If on same level, left to right.
* / % Evaluated second. If there are several, evaluated left to right.
+ - Evaluated third. If there are several, evaluated left to right.
= Evaluated last, right to left.
What is a Constant?
In mathematics, a constant is a value that cannot change.
In programming, a constant is like a variable, except that its value cannot change.
There are two categories of constants:
literal constants, whose values are expressed literally;
named constants, which have names.
A literal constant is a constant whose value is specified literally:
int literal constants
(e.g., 2, 1, 7,98, -1)
float literal constants
(e.g., 2.4, 0.6, -14.7)
char literal constants
(e.g., ’B’, ’4’, ’?’)
Named Constants
A named constant is a constant that has a name.
A named constant is exactly like a variable, except that its value is set at compile time and CANNOT change at runtime.
A named constant is exactly like a literal constant, except that it HAS A NAME.
In a named constant declaration, we indicate that it’s a constant via the const attribute, and we MUST initialize it:
const float pi = 3.14;
When you embed numeric literal constants in the body of your program, you make it much harder to maintain and upgrade your program.
No comments:
Post a Comment