c notes

some of these notes may have been created from other online sources.

they are just my home schooling notes.

 

 

BODMAS brackets orders division multiplication addition subtraction

format strings:
As you know, printf uses the character that immediately follows the percent sign to
determine how to print the next argument. However, if it is another percent sign that
follows, the printf routine takes this as an indication that you really intend to display a
percent sign and inserts one at the appropriate place in the program’s output.

you can choose how many places after the decimal point to print using “.number” (.3) does 3 digits.

printf (“%.2f”, variable);

You can not declare or define functions inside a stuct.

taking user input using scanf:

scanf (“%i”, &number);

notice the & before “number”. always place a “&” before the variable when using scanf.

The Type Cast Operator:

f2 = (float) i2 / 100; // type cast operator is (float)

this causes the i2 to be treated as a float during this operation.
it does not permanently change the type.

for loops:
you can nest for loops.
you can initialize more than one variable with the for loop, and check conditions on more than one variable
you just separate them with a comma.
you can leave blank any pa rt of the for loop also.

for ( int a = 1, i = 20; a < 20; a++, i–)
{
some actions
}

while loops:

while ( count <= 5 )
{
printf (“%in”, count);
++count;
}

break; //kills loop
continue; //continues loop but skips statements after the continue word.

If statements:
braces can be used to execute multiple statements in an if statement.
if statements can be nested.

if ( number < 0 )
number = -number;

if ( number < 0 )
{
number = -number;
number++
}

else
number++

if ( expression 1 )
program statement 1;

else if ( expression 2 )
program statement 2;

else
program statement 3;

when you i++ it increments i after the condition is over
when you ++i it increments i before the condition is over

switch( c )
{
case ‘A’:
capa++;
case ‘a’:
lettera++;
default :
total++;
}

switch( i )
{
case -1:
n++;
break;
case 0 :
z++;
break;
case 1 :
p++;
break;
}

condition ? expression1 : expression2
maxValue = ( a > b ) ? a : b;
printf (“Sign = %in”, ( number < 0 ) ? –1 : ( number == 0 ) ? 0 : 1);

advanced array usage:
next_value = sorted_data[(low + high) / 2];

if you initialize fewer elements than the array contains the rest will be initialized as zero.
int counters[5] = { 0, 0, 0, 0, 0 };            // array with initializer list
char letters[5] = { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ };  // array with initializer list

By enclosing an element number in a pair of brackets, specific array elements can be
initialized in any order. For example,
float sample_data[500] = { [2] = 500.5, [1] = 300.0, [0] = 100.0 };

if you set an initializer at a higher number that many will be allocated for the array
float sample_data[] = { [0] = 1.0, [49] = 100.0, [99] = 200.0 };
this array will have 100 buckets because of the initialization of bucket 100.

const
declares that the data which is marked const does not change. attempted change gives an error.
One of the motivations for the const attribute in the language is that it allows the compiler to place
your const variables into read-only memory.

multi-dimensional arrays
Remember, the first index number refers to the row number, whereas the second index
number references the column.

initialization of multi-dimentsional array:

int M[4][5] = {
{ 10, 5, -3, 17, 82 },
{ 9, 0, 0, 8, -7 },
{ 32, 20, 1, 0, 14 },
{ 0, 0, 8, 7, 6 }
};

Note that commas are
required after each brace that closes off a row, except in the case of the final row.The use
of the inner pairs of braces is actually optional. If not supplied, initialization proceeds by
row. the preceding statement could also have been written as follows:

int M[4][5] = { 10, 5, -3, 17, 82, 9, 0, 0, 8, -7, 32,
20, 1, 0, 14, 0, 0, 8, 7, 6 };

Subscripts can also be used in the initialization list, in a like manner to single dimensional arrays.
int matrix[4][3] = { [0][0] = 1, [1][1] = 5, [2][2] = 9 };

To pass an array to a function, it is only necessary to list the name of the array,
without any subscripts, inside the call to the function.
The function which you pass the array to must be defined to take the whole array like below:

int minimum (int values[100])  //array passed with 100 buckets (like declaration of array it is 100 not 101)
{

return minValue;
}

when declaring multi dimentsional arrays you must give a number for the amount of columbs.
int myarray[50][50]
R   C
R = row / C = columbs

keyword static:
variables declared outside of block-scope were implicitly global.
Then I discovered that declaring a variable as static within block-scope
would give it permanent duration, and declaring it outside of block-scope
(in program-scope) would give it file-scope (can only be accessed in that compilation unit).

global variables are variables made outside of any function, which are accessable in any function.
the default value for a global variable is zero

keyword static:
keyword static makes a variable have the same value when a function is called multiple times.
if you declared a variable static it will have the same value next time the function is called.
static does not make it a global variable accessable to all other functions, just the function it
is declared in. static variables have the default value of zero. you can also enter an initial value
for the static variable but it will only be set once the first time the function is entered.
so: “static myvariable = 100;” this will initialize the variable once, when the function is entered.
after that it will retain the value of 100 until it is changed by you.

structures using struct:
you access the members of a struct using a period.

struct date
{
int month;
int day;
int year;
};

date today;

today.month = 10;

————————————————————————————————–
STRINGS:

pointer to a string (not considered a real string)
it is stored as “This is my string�”:

char * mystring = “This is my string”;

char array string
it is stored as “This is my string�”:

char mystring[]  = “This is my string”;

book 189

Leave a Reply