// -- line comment, compiler ignores the line /* * … */ -- block comment, compiler ignores what in between
C has 32 reserved words (keywords). Keywords are those words whose meaning is already defined by the compiler.
auto | double | int | struct |
break | else | long | switch |
case | enum | register | typedef |
char | extern | return | union |
const | float | short | unsigned |
continue | for | signed | void |
default | goto | sizeof | volatile |
do | if | static | while |
Keywords cannot be used as variable names, function names, or constant names.
main
.
main
function.
C source code
[pre-processor statements] [declare global variables and constants] [declare function prototypes] int main(int argc, char *args[]) { [program statements] return 0; } [implementation of functions]
Memory map in execution:
Stack: function local data at runtime |
Heap: dynamic data |
Data: global, static, constant |
Text: Program Functions |
Data Type defines how data is stored in memory
char, int, float, double
array, pointer, struct, union, enum, etc
Each data type has a size: the number bytes in memory
-
Each addressable memory cell holds 8 bits (1 byte) Primary data types (size
of some data type is platform dependent): For example, int
can
be 2 bytes, 4 bytes, 8 bytes or larger
The char
type is stored as an integer using ASCII code. E.g.
'A' is coded as 65, 'a' as 97, '+' as 43.
See an ASCII Table
int a; char c; float f;
Assign values to variables:
a = 2; // assignment statement, compiler assign the value 2 to memory space c = 'a'; // compiler replaces 'a' by 97, then assigns it to the memory space f = 1.41;
Declaration & initialization:
int a = 2; int b = 3; char c = 'a'; float f = 1.41; int x = 9, y = 3;
sizeof
Operator
sizeof
is a unary operator that calculates the sizes of
data typesSyntax:
size_t sizeof(type)
size_t sizeof expression
(size_t
is an unsigned integer)
Examples:
int n = 0; sizeof(int): 4 sizeof n: 4 sizeof &n: 4 double d = 0; sizeof(double): 8 sizeof d: 8 sizeof &d: 4 char c = 'A'; sizeof(char): 1 sizeof c: 1 sizeof &c: 4 int a[10]; sizeof(int [10]): 40 sizeof a: 40 sizeof &a: 4 sizeof a / sizeof a[0]: 10 sizeof a / sizeof *a: 10 typedef struct { int x; double y; char z; char *p; } some_struct; sizeof(some_struct): 24 sizeof q: 24 sizeof &q: 4
(What is 'strange' about the size of some_struct
?)
Constant value: A fixed value used in program, like 3.1415926
#define PI 3.1415926 float r = 4; float area = PI*r*r; float cf = 2*PI*r;
The pre-processor replaces all occurrences of PI
in the source
code with 3.1415926
Constant variable: Variable with initialized value, but not allowed to change, i.e. read-only variable
const float pi = 3.1415926; float r = 4; float area = pi*r*r; float cf = 2*pi*r; pi = 3.14; // this is not allowed by compiler
double, float, long,
int, short, char
.
int b = 2; float f1 = 1.5, f2 = 3.5; f1 = b; // implicit casting, f1 = 2.0, f1 = (float) b; // explicit casting b = (int) f2; // b = 3
The right size of an assignment statement must be an expression. Expressions consists of constants, variables, operators and parentheses. Example:
z = x + y a = (b + 2) * (c + 4);
Be aware of operation issues
Type issues
int i = 1; double x = 2; // 2.0 x = i / 2.0; // i is casted to double before being multiplied, x = 0.5 x = (double) i / 2.0; // equivalent x = i / 2; // i is not promoted to double because 2 is an int x = (double) (i / 2); // so evaluates as this, x = 0.0
Over-flow: values are out of range
int a = INT_MAX; int b = a + 1; // overflow
See Operators in C
Sequence: linear order. Lines are executed in the order given.
if
statement
if (condition) { group1; }
if-else
statement
if (condition) { block1; } else { block2; }
if-else-if
statement
if (condition) { block1; } else if (condition) { block2; } else { block3; }
switch
statement
char c; … switch (c) { case 'A': printf("Excellent"); break; case 'B': printf("Good"); break; case 'C': printf("OK"); break; default: printf("What is your grade anyway?"); }
Another example:
// Program to print the day of the week. #include <stdio.h> int main() { int day; printf("\n Enter any number from 1 to 7 : "); scanf("%d", &day); switch (day) { case 1: printf("\n SUNDAY"); break; case 2: printf("\n MONDAY"); break; case 3: printf("\n TUESDAY"); break; case 4: printf("\n WEDNESDAY"); break; case 5: printf("\n THURSDAY"); break; case 6: printf("\n FRIDAY"); break; case 7: printf("\n SATURDAY"); break; default: printf("\n Wrong Number"); } return (0); }
for
statement
for (count = 1; count <=10; count++) { printf("%d",count); }
while
loop
while (condition is true) { group; }
do-while
loop
do { group; } while (condition is true);
Example of prompt input
#include <stdio.h> int main() { setbuf(stdout, NULL); char c; printf("Enter a character ('*' to quit): "); c = getchar(); while (c != '*') { getchar(); // throw away enter key if (c >= 'a' && c <= 'z') { printf("%c %d %c\n", c, c, c - ' '); } else { printf("Not a lower case letter\n"); } printf("Enter a character ('*' to quit): "); c = getchar(); } return (0); }
continue
statement moves to the next iteration of a loop:
/* output the integers between 100 and 200, not divisible by 3*/ #include <stdio.h> int main(void) { int n; for (n = 100; n <= 200; n++) { if (n % 3 == 0) continue; printf("%d ", n); } printf("\n"); }