Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Software Technology

Programming: Formatting and Variables


Variables:
Variables are just as they are im algebra: a letter or word that holds a changeable numerical value
(for the most part, some variables hold many numbers, or a string of letters). A variable can be
created and destroyed, and there are many different types of variables.
To initialise, or create, a variable, write the type of variable you want.
For example, to create an integer called x, write:
int x;
One can also assign the variable a value when initialising it:
int x = 0;
The value of the variable can be changed at any point.
A variable can also be local or global, global meaning it can be used from anywhere in the
program, and local meaning it can only be used in the function where it is initialized. This does
not really matter for now, but will become important later.
Variable Types:
Variables are data types in the C language that are essentially names given to a storage
area that our program can manipulate. In C, each variable has a specific type which determines
the size and layout of the variable's memory, the range of values that can be stored within that
memory; and the set of operations that can be applied to the variable. Upper and lowercase
letters are distinct because C is case-sensitive.
Type

Description

Use

char

A single octet (one byte); integer type

letter

int

The most natural type of integer for the


machine, at least 16 bits

normal whole
number

Floating point variable

number

Double precision variable

same as int, but


bigger

Variable that does not return value

nothing (executing
code)

Same as int.

Decimal number

float
double
void
Unsigned int

boolean

A variable with only two possible values; true


or false. Intended to represent truth values of
logic and Boolean algebra.

true or false

Here is a reference list of all the various codes for printing different variable types:
code for
use in
printf

variable
type

format

int

decimal (base ten) number

int

octal number (no leading '0' supplied in printf)

x or X

int

hexadecimal number (no leading '0x' supplied in printf; accepted if


present in scanf) (for printf, 'X' makes it use uppercase for the digits
ABCDEF)

ld

long

decimal number ('l' can also be applied to any of the above to change
the type from 'int' to 'long')

unsigned

decimal number

lu

unsigned
long

decimal number

char

single character

char pointer

string

float

number with six digits of precision

float

number with up to six digits of precision

float

number with up to six digits of precision, scientific notation

lf

double

number with six digits of precision

lg

double

number with up to six digits of precision

le

double

number with up to six digits of precision, scientific notation

White space:
All C compilers ignore any white space (anything that does not have text) this includes blank
lines (enter key) spaces, and tabs. However, that does not mean that one should not format code
properly, or that there is not a correct way to go about that formatting.

Incorrectly formatted code will


compile and run fine, but as you can
see, it almost impossible to read and
understand. This is an example of
what a program will look like without
any comments or white space.

Correctly formatted code will look much more like this, it is less dense and far easier to read and
understand. One can read this section of code, and even without and comments, it is possible to
gain some understanding of what this is meant to accomplish. Comments would make this even
better, but good formatting is even more important.
Rules (or Grammar) of Code
; The semicolon is like a period in English, and notates that you are at the end
of a statement or command.
One should press enter after every ; so that only one statement or
command is performed per line of code. Also a blank line should be placed between
sections (paragraphs) or code.
or tab is used to visually display the levels of organization of code. This
sounds complicated, but it is really easy when you start doing it. Basically, each tab stop
is the next level of a function or loop.

Comments:
Comments are a crucial component to all code, for while computers understand code
exceptionally, people can process English must more efficiently. A comment is a separate section
of a program that the computer will ignore upon compilation.
Comments can be defined in two ways:
// Anything after double forward slashes will be ignored by the
compiler (computer).
Example:
#include <stdio.h>
main(){
printf("hello world\n");
} // This function will print the phrase "hello world" on the screen
/*______ */ where anything between the stars will be ignored
Example:
#include <stdio.h>
main(){
printf("hello world\n");
}/*This function will print the phrase "hello world" on the screen*/
Note: All comments will appear in green text.

You might also like