Understanding Variables and Declarations in C Programming, Lecture notes of C programming

An introduction to variables and declarations in c programming. It explains what variables are, their declaration process, and the importance of declaring variables. The document also covers the rules for declaring variables, simple declaration examples, initialization, data types, and placement of declarations.

Typology: Lecture notes

2013/2014

Uploaded on 12/29/2014

wsabren1..
wsabren1.. 🇮🇶

4

(5)

11 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Declarations
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

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