You are on page 1of 2

CS1AC16 VARIABLES AND USER INTERACTION

A variable is a container in which data is stored in computer memory. Its size depends on its type. All
variables must have names. The names may consist of letters, numbers and underscores but cannot
start with a number and cannot be C keywords.
Variables must be declared before they can be used. It is done in the following way:
Typename variable_name;
Example: int counter; char choice;
Variables can be set and changed using = operator: counter = 0; choice = 'a'. They can also be set during
declaration: int counter = 0;
Variables of the same type can be declared in the same line, separated by commas.
You can perform mathematical operations on numbers on the right hand side of the = operator:
Counter = 3 + 5;
You can also use variables on the right hand of the equation:
Counter = other + 3;

Printf is used for displaying output. It sends a formatted sequence of ASCII characters to standard
output, usually the screen but it can also be a file.
Printf uses special tag character % to identify a format operator that tells printf how to convert
a variable:
 %c – a character
 %s – a string of characters
 %d or %i – signed decimal integer
 %f – floating point number, %.3f displays a number with 3 decimal places – this can be adjusted
as necessary
 %E or %e – scientific notation
 %x – unsigned hexadecimal integer
These are called format specifiers.
Escape characters: /n for blank line, /t for tab. Use double slashes if you want to print them:
printf("//n") prints /n to the screen.
You can also use ACSII control characters with %c:
 13 – carriage return (returns to the start of the same line and overwrites it
 8 – backspace

Scanf is used to read characters from the user. It uses the same format specifiers as printf. The syntax is:
Scanf("%d", &var) - the ampersand indicates the address where the input is to be stored.
Scanf("%c", &letter) - takes a character from the user and puts it in the char variable (the
variable must be declared beforehand).

With printf, you work with variables that already exist. Scanf brings a value from the user and must put
it somewhere.

Strings – implemented as arrays of characters in C. C does not pass the string around, but the address of
the start of the string. To know where the string ends, it must have a null character at the end – that's
why the size of the string you create must be 1 larger than the number of characters you need.
Example: char hello[6]; - declaring the string
hello = "Hello"; - setting the value
Variable modifiers
Ints, chars and doubles may have their characteristics modified using:
 Long and short – modify the size of the variable
 Signed and unsigned – change how the number is considered for negative values

Data type qualifiers – if the code is to be portable for different CPUs, we need to know the size of the
native CPU.
Limits.h defines constants that a program can use to find information about its native size: INT_MAX,
INT_MIN, LONG_MAX, LONG_MIN, SHRT_MAX, SHRT_MIN.

Sizeof is a built-in C function that returns the number of bytes that are used to store a variable in
memory.

You might also like