Added book examples
This commit is contained in:
18
C19/define_example.c
Normal file
18
C19/define_example.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#define UP 1
|
||||
#define DOWN 2
|
||||
|
||||
int main()
|
||||
{
|
||||
int i = 1;
|
||||
|
||||
if (i == UP )
|
||||
{
|
||||
// Do something
|
||||
}
|
||||
|
||||
if ( i == DOWN)
|
||||
{
|
||||
// Doe something else
|
||||
}
|
||||
return 0;
|
||||
}
|
22
C19/enum_example.c
Normal file
22
C19/enum_example.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
enum DOW { sun, mon, tue, wed, thu, fri, sat } ;
|
||||
|
||||
// Main () - execution starts here
|
||||
int main (void)
|
||||
{
|
||||
enum DOW day;
|
||||
|
||||
/* Code that get a value for 'day' */
|
||||
day = tue;
|
||||
|
||||
switch (day)
|
||||
{
|
||||
case sun : printf ("Sunday\n") ; break ;
|
||||
case mon : printf ("Monday\n") ; break ;
|
||||
case tue : printf ("Tuesday\n") ; break ;
|
||||
/* etc. */
|
||||
}
|
||||
return (0); // Exit indicating success
|
||||
}
|
26
C19/static_variable_example.c
Normal file
26
C19/static_variable_example.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void DisplayHelloWorld (void)
|
||||
{
|
||||
static int k = 0; // Counter for how many times the function is called
|
||||
|
||||
printf ("Hello World\n");
|
||||
|
||||
// Increment counter and display value
|
||||
k = k + 1;
|
||||
printf ("I have now said this %d times\n",k);
|
||||
}
|
||||
// Main () - execution starts here
|
||||
int main (void)
|
||||
{
|
||||
int i;
|
||||
|
||||
// Loop calling out function 10 times
|
||||
for ( i =0 ; i < 10 ; i++ )
|
||||
{
|
||||
DisplayHelloWorld();
|
||||
}
|
||||
|
||||
return (0); // Exit indicating success
|
||||
}
|
Reference in New Issue
Block a user