Download Understanding Variables and Declarations in C Programming and more Lecture notes C programming in PDF only on Docsity!
Declarations
declarationsdeclarations declarationsdeclarations
What is an declaration?
how we introduce an identifier
specifies the interpretation and attributes of an identifier
in other words, tells the computer what an identifier means
variable
function
various other things
let’s focus on variables right now
variablesvariables variablesvariables
- variable issizes, the computer needs to know what type each• Because different types of data are different(instead of having to know the address)• For your convenience, you give them nameshold data while your program is running. • So variables are memory regions you can use to so it can reserve the memory you need
back to declarationsback to declarations back to declarationsback to declarations
- you need to tell the compiler: • So all this means that before you can use a variable, its name
its data type
anything else that the compiler needs to know
storage class
right now, we’ll just stick to the first two
declaration rulesdeclaration rules declaration rulesdeclaration rules
- • Each declaration consists of:
a data type
int, double, char…
a list of variable names
remember the rules for identifiers
letters, digits, underscore
can’t start with a digit
case sensitive
optional initialization
ends with a semicolon
simple declaration examplessimple declaration examples simple declaration examplessimple declaration examples
int lower, upper, step;double d;int i; char c;
initialisation and data typesinitialisation and data types initialisation and data typesinitialisation and data types
- • The initializer must be: of the same data type as the variable
of a data type the compiler can convert into the data type of the variable
Good:
double b = 12;double a = 19.38;int i = 3;
Less good:
float f = 12.34;char c = 3.1415;
placement of declarationsplacement of declarations placement of declarationsplacement of declarations
Placement in program is significant
At beginning of statement block
this is what we’ve seen so far
Outside a function - global variable
Parameter declaration - in function declaration
another statement blockanother statement block another statement blockanother statement block
/ / conversion table FtoC1 – print a Fahrenheit-to-Celsius { int main (void)#include <stdio.h> while (fahr <= upper) {fahr = lower;int step = 20;int upper = 300;int lower = 0;int celsius;int fahr; fahr = fahr + step;printf (“%d\t%d\n”, fahr, celsius);celsius = 5 * (fahr – 32) / 9; return 0;} }**
back to variables andback to variables andback to variables andback to variables and
statement blocksstatement blocksstatement blocksstatement blocks
some variable declarationsSo any statement block can start with
but those variables are only available to the code inside the statement block
this is a concept called scope…
function parametersfunction parameters function parametersfunction parameters
We haven’t talked about functions much yet
each time it is invokedParameters are data you give to a function
remember my driving analogy of a function
adjust speed to X mph
the same instructions, no matter what X is each time
remember how we have used printf: printf (“Hello, world!\n”); printf (“i is %d\n”, i); printf (“%d\t%d\n”, fahr, celsius);
we’ll talk about parameters more when we get into functions