Difference between revisions of "C language"
Jump to navigation
Jump to search
(→Basics) |
|||
| Line 7: | Line 7: | ||
===Basics=== | ===Basics=== | ||
;Objects, Functions, Types and Pointers | ;Objects, Functions, Types and Pointers | ||
| − | * '''object''': storage used to represent values. (Per ISO/IEC 9989:2018: "a region of data storage in the execution environment, the contents of which can represent values. A '''variable''' is an example of an object. An object with '''type''' <code>int</code> contains and integer value. | + | * '''object''': storage used to represent values. (Per ISO/IEC 9989:2018: "a region of data storage in the execution environment, the contents of which can represent values. A '''variable''' is an example of an object. An object with '''type''' <code>int</code> contains and integer value. E.g. why types are important: the ''text'' number 1 in IEEE 754 (ASCII, roughly) is specified by 0x3f80000, which a little over a billion as a decimal. |
| − | E.g. why types are important: the ''text'' number 1 in IEEE 754 (ASCII, roughly) is specified by 0x3f80000, which a little over a billion as a decimal. | ||
* '''functions''': are not objects, but do have types. A function is defined by both its return type and the number and types of its parameters. | * '''functions''': are not objects, but do have types. A function is defined by both its return type and the number and types of its parameters. | ||
* '''types''': | * '''types''': | ||
Revision as of 18:06, 18 March 2022
Elements of C
Sources
- Effective C by Robert Seacord
- 21st Century C
Basics
- Objects, Functions, Types and Pointers
- object: storage used to represent values. (Per ISO/IEC 9989:2018: "a region of data storage in the execution environment, the contents of which can represent values. A variable is an example of an object. An object with type
intcontains and integer value. E.g. why types are important: the text number 1 in IEEE 754 (ASCII, roughly) is specified by 0x3f80000, which a little over a billion as a decimal. - functions: are not objects, but do have types. A function is defined by both its return type and the number and types of its parameters.
- types:
- pointers: an address or location in memory where an object or function is stored. Pointer types derive from the referenced function. or object type. E.g.,
*charis called a pointer to char. - Because objects and functions are different things, object pointers differ from function pointers
- In the swap program below the
mainfunction declares two variables of typeint, using the combination of a type (int), an identifier (a,b) and a value (21,17). Assigning the values is also called initialization
- parameters: objects declared as part of a function declaration that acquire value on entry to the function
- argruments: comma-separated expressions included in the function call expression (whether values or pointers)
- pass-by-refernce
#include <stdio.h>
void swap(int *pa, int *pb) {
int t = *pa;
*pa = *pb;
*pb = t;
// int t = a;
//a = b;
//b = t;
printf("swap: a = %d, b = %d\n", *pa,*pb);
}
int main(void){
int a = 21;
int b = 17;
swap(&a, &b);
printf("main: a = %d, b = %d\n", a, b);
return 0;
}
The commented lines in the swap function show the original program, which failed to swap main's values, because they were being passed by value rather than reference. The corrected version (above) declares and dereferences pointers with the indirection operator (*).
- pass-by-value:
void increment(void) {
static unsigned int counter = 0;
counter++;
printf("%d", counter);
}
int main(void) {
for (int i = 0; i < 5; i++) {
increment();
}
return 0;
}