Professional Documents
Culture Documents
2 - Variables PDF
2 - Variables PDF
Fredy Cuenca
Arquitecture of memory
• The memory assigned to a program that is loaded can be divided into
four segments: text (or code), data, stack, and heap.
Stack
Heap
Data
Stack
Heap
Stack
The heap is to store dynamically generated variables. This is the only segment that
Heap
can grow at runtime
The data segment is to store global and static variables
Data
Stack The stack segment is to store local variables and information about function calls
The heap is to store dynamically generated variables. This is the only segment that
Heap
can grow at runtime
The data segment is to store global and static variables
Data
number1 = 4;
number2 = 10*number1;
...
number1 number2
1 byte
Variables
number1 = 4;
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
number2 = 10*number1;
...
number1 number2
1 byte
Variables
number1 = 4;
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
number2 = 10*number1;
0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0
...
number1 number2
1 byte
Printing with format
• The function printf sends formatted output to the screen; it can
carry an arbitrary number of parameters
int x = 100;
long y = 2094L;
unsigned int z = 34U;
About floating-point constants
• Float numbers may have a suffix: f or L. The abscence of a suffice
indicates that the constant is double
• When you use exponential notation (with e or E) the constant is
considered a floating point number
float x = 3E4;
float y = 12e-4f;
Controlling the output field width
• Floating-point numbers can be formatted as follows:
%[width][.precision][modifier]f
• By default, values are right-aligned. A minus sign (–) right after the
symbol % will print left-aligned data.
Arithmetic operators
Operator Operations Order of evaluation (Precedence)
() Parentheses Evaluated first. If the parentheses are nested, the innermost expression is
evaluated first. If there are several pairs of parentheses on the same level,
they’re evaluated left to right
* Multiplication
/ Division Evaluated second. If there are several, they’re evaluated left to right
Remainder
%
+ Addition Evaluated third. If there are several, they’re evaluated left to right
- Substraction
float d = 10 / 3.0f;
printf("%f", d)
Explicit type conversión (casting)
Operator Operations
== equal to
Equality
operators not equal to
!=
> is greater than
+- left to right
<myVar> += <something>;
++a Increment a by 1, then use the new value of a in the expression in which a resides
a++ Use the current value of a in the expression in which a resides, then increment a by 1
--b Decrement b by 1, then use the new value of b in the expression in which b resides
b-- Use the current value of b in the expression in which b resides, then decrement b by 1
Reading user’s input
The function scanf (library stdio.h) reads data from the keyboard.
Syntax:
address operator
format control string variable name
scanf("%d", &age)
Characters
{
puts("Do you want to continue?");
scanf("%c", &answer);
getchar();
}
Constants
• Constants are declared as follows:
• The compiler will check that constants values are not modified
throughout the program
Enumerations
Enumerations allow defining a new data type with a small range of values
Syntax:
floor(x) Returns the largest integer that isn’t greater than x as type double
ceil(x) Returns the smallest integer that isn’t less than x as type double
fabs(x) Returns the absolute value of x
log(x) Returns the natural logarithm (base e) of x
log10(x) Returns the logarithm to base 10 of x
exp(x) Returns e^x
sqrt(x) Returns the sqare root of x
pow(x, y) Returns x^y