Computers Programming: Course 5
Computers Programming: Course 5
Computers Programming: Course 5
Course 5
Iulian Nstac
Medium level (C, C++, FORTH, etc.) Low level (assembly languages)
programming languages that provide little or no abstraction from a computer's instruction set architecture
2
function main()
other functions
5
Recap C preprocessor
The preprocessor provides the ability for:
inclusion of header files macro expansions conditional compilation
Constants in C Language
Constants can be very useful in C programming whenever you need a value that is repeated during the program Ex: #define PI 3.14159 Declaring a constant allows you to quickly and easily change a value that is used throughout the program simply by changing the initial declaration.
7
Data types
four basic arithmetic type specifiers:
char int float double
optional specifiers:
signed, unsigned short long
8
Type char
Explanation
Smallest addressable unit (8 bits) that can contain basic character set. It is an integer type. Actual type can be either signed or unsigned depending on the implementation. Same size as char, but guaranteed to be signed Same size as char, but guaranteed to be unsigned short signed integer type. At least 16 bits in size
signed char unsigned char short short int signed short signed short int unsigned short unsigned short int int signed int unsigned unsigned int
Same as short, but unsigned Basic signed integer type. At least 16 bits in size Same as int, but unsigned
Type
long long int signed long signed long int unsigned long unsigned long int long long long long int signed long long signed long long int unsigned long long unsigned long long int
Explanation
long signed integer type. At least 32 bits in size
Same as long, but unsigned long long signed integer type. At least 64 bits in size (specified since the C99 version of the standard). Same as long long, but unsigned (specified since the C99 version of the 10 standard).
Type
float
Explanation
Single-precision floating-point format is a computer number format that occupies 4 bytes (32 bits) in computer memory and represents a wide dynamic range of values by using a floating point. Double-precision floating-point format is a computer number format that occupies 8 bytes (64 bits) in computer memory and represents a wide dynamic range of values by using floating point. Extended precision floating-point type. Unlike types float and double, it can be either 80-bit floating point format, or IEEE 754 quadrupleprecision floating-point format if a higher 11 precision format is provided.
double
long double
long double
The 80-bit floating point format was widely available by 1984 after the development of C and similar computer languages, which initially offered only the common 32- and 64-bit floating 12 point sizes.
Notes:
The actual size of integer types varies by implementation. The standard only requires size relations between the data types and minimum sizes for each data type. the long long is not smaller than long, which is not smaller than int, which is not smaller than short.
13
Notes:
char size is always the minimum supported data type, all other data types can't be smaller. The minimum size for char is 8 bit, the minimum size for short and int is 16 bit, for long it is 32 bit and long long must contain at least 64 bit. Many conversions are possible in C.
14
Conversions in C
Implicit conversion
If there are several types of data, then all of them will be converted to the larger one
By assignation
when "equal" operator is involved, then the type of right side result is converted to the type of the left side.
Logic conversion
when logic operators are involved
15
Variables
Variables are simply names used to refer to some location in memory.
Types of variables:
Local variables Global variables
17
External variables
variable defined outside any function block
Register variables
register allocation is the process of assigning a large number of target program variables onto a small number of CPU registers
18
Automatic variable
The term local variable is usually synonymous with automatic variable, since these are the same thing in many programming languages, but local is more general. Most local variables are automatic local variables, but static local variables also exist, notably in C. Automatic variables may be allocated in the stack frame of the procedure in which they are declared; this has the useful effect of allowing recursion and re-entrancy. The specific keyword is auto (not compulsory).
19
External variables
As an alternative to automatic variables, it is possible to define variables that are external to all functions (variables that can be accessed by name by any function). An external variable may also be declared inside a function. In this case the extern keyword must be used, otherwise the compiler will consider it a definition of a local variable, which has a different scope, lifetime and initial value. This declaration will only be visible inside the function instead of throughout the function's module. An external variable can be accessed by all the functions in all the modules of a program. It is a global variable.
20
Register variables
For efficiency, the optimizer will try to allocate some of local variables in processor registers. In most register allocators, each variable is either in a register or in memory. If a variable can not be assigned a register then all of the variable's usage, including its definition, is preceded by a load from memory. The specific keyword is register (not compulsory). The register must be big enough to host that variable.
22
23
Type of associate variable single character string integer unsigned decimal integer pointer float double long double
24
Note:
%5d is associated with an integer that get maximum 5 character positions with right alignment %-5d is associated with an integer that get maximum 5 character positions with left alignment
25
Note:
\" becomes " inside a string
Escape (backslash \) character \n \t \b \v \\ \/ Effect in a string new line tab backspace vertical tabulation backslash character slash
27
ASCII code
The American Standard Code for Information Interchange (ASCII) is a character-encoding scheme originally based on the English alphabet that encodes 128 specified characters (the numbers 0-9, the letters a-z and A-Z, basic punctuation symbols, control codes, and a blank space) into the 7-bit binary integers.
28
29
30
Example ESC 1 2 : 9 0 A B : a b :
Hex Code 1B 31 32 : 39 30 41 42 : 61 62 :
31
2. puts function int puts ( const char * str ); Writes the C string pointed by str and appends a newline character ('\n'). Ex.: printf(Message \n);
puts(Message);
34
3. putchar function int putchar ( int character); Writes a character to the output Ex.: #include <stdio.h> int main() { char c; for (c = 'A' ; c <= 'Z' ; c++) putchar(c); return 0; }
35
4. putch function int putch ( int character); putch displays any alphanumeric characters to the standard output device. It displays only one character at a time.
36
2. gets function char * gets ( char * str ); gets() accepts any line of string including spaces from the standard Input device (keyboard). gets() stops reading character from keyboard only when the enter key is pressed.
38
39
3. getchar function int getchar ( void ); getchar() accepts one character type data from the keyboard. Ex variable_name = getchar();
40
4. getch function int getch ( void ); getch() accepts only single character from keyboard. The character entered through getch() is not displayed in the screen (monitor).
42
5. getche function int getche ( void ); Like getch(), getche() also accepts only single character, but unlike getch(), getche() displays the entered character in the screen.
43
synthesis
Output functions printf puts putchar putch Input functions scanf gets getchar getch getche
44
Expressions
An expression in a programming language is a combination of explicit values, constants, variables, operators, and functions that are interpreted according to the particular rules of precedence and of association for a particular programming language, which computes and then produces another value. This process, like for mathematical expressions, is called evaluation. The value can be of various types, such as numerical, string, and logical.
45
Order of operations
In computer programming, the order of operations (operator precedence) is a rule used to clarify which procedures should be performed first in a given mathematical expression. The operators in C have a strict precedence level.
47
1 2 3 4 5 6 7 8 9 10 11 12 13 14
() [] -> . :: ! ~ - + * & sizeof type cast ++x - -x * / % + << >> < <= > >= == != & ^ | && || ?: = += = *= /= %= &= |= ^= <<= >>= ,
Grouping, scope, array / member access (most) unary operations, sizeof and type casts Multiplication, division, modulo Addition and subtraction Bitwise shift left and right Comparisons: less-than, ... Comparisons: equal and not equal Bitwise AND Bitwise exclusive OR Bitwise inclusive (normal) OR Logical AND Logical OR Conditional expression (ternary) and assignment operators Comma operator 48
Example:
float x; double y; char c; int i; ...
50
Operators in C
Programming languages typically support a set of operators, which differ in the calling of syntax and/or the argument passing mode from the language's functions. C programming language contains a fixed number of built-in operators.
51
1 2 3 4 5 6 7 8 9 10 11 12 13 14
() [] -> . :: ! ~ - + * & sizeof type cast ++x - -x * / % + << >> < <= > >= == != & ^ | && || ?: = += = *= /= %= &= |= ^= <<= >>= ,
Grouping, scope, array / member access (most) unary operations, sizeof and type casts Multiplication, division, modulo Addition and subtraction Bitwise shift left and right Comparisons: less-than, ... Comparisons: equal and not equal Bitwise AND Bitwise exclusive OR Bitwise inclusive (normal) OR Logical AND Logical OR Conditional expression (ternary) and assignment operators Comma operator 52
1. Brackets in C
53