#include <stdio.h> // for printf(), putchar()
int main()
{
enum boolean {FALSE, TRUE};
enum ny {NO, YES};
enum escapes {BELL = '\a', BACKSPACE = '\b', TAB = '\t',
NEWLINE = '\n', VTAB = '\v', RETURN = '\r'};
enum separators {BLANK = ' ', COMMA = ','};
enum months {JAN = 1, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC};
enum digits {ZERO, ONE, TWO, THREE, FOUR,
FIVE, SIX, SEVEN, EIGHT, NINE};
enum chars {CH0 = '0', CH1, CH2, CH3, CH4,
CH5, CH6, CH7, CH8, CH9};
enum digits zero = ZERO;
int one = ONE;
printf("%d, %d\n", zero, one); // 0, 1
zero++, one++;
printf("%d, %d\n", zero, one); // 1, 2
zero += 10;
one += 10;
printf("%d, %d\n", zero, one); // 11, 12
for (zero = ZERO; zero <= NINE; zero++)
{printf("%d%c%c", zero, COMMA, BLANK);} // 0, 1, ..., 9,
putchar(NEWLINE);
for (one = JAN; one <= DEC; one++)
{printf("%d%c%c", one, COMMA, BLANK);} // 1, 2, ..., 12,
putchar(NEWLINE);
enum chars f(void) {return zero;} // `zero' is of type `digits'
enum digits g(void) {return one;} // `one' is an `int'
printf("%d, %d\n", f(), g()); // 10, 13
char c;
int i;
for (c = CH0; c <= CH9; c++) // print as characters
{printf("%c%c%c", c, COMMA, BLANK);} // 0, 1, ..., 9,
putchar(NEWLINE);
for (i = CH0; i <= CH9; i++) // ASCII values
{printf("%d%c%c", i, COMMA, BLANK);} // 48, 49, ..., 57,
putchar(NEWLINE);
return 0;
}
/*
gcc enum.c -o enum
./enum
0, 1
1, 2
11, 12
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
10, 13
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf()
// uninitialized vars
#define SIZE
#define LENGTH 0
int global;
const int cig;
int const icg;
int main()
{
printf("global vars: ");
printf("%d, %d, %d\n", global, cig, icg);
global++;
// cig++; // compile error:
// icg++; // cannot increment read-only vars
global = 1;
// cig = 0; // compile error:
// cig = 1; // cannot assign a value to read-only vars
// icg = 1;
int local;
const int cil;
int const icl;
printf("local vars: ");
printf("%d, %d, %d\n", local, cil, icl);
local++;
// cil++; // error
// icl++;
local = 1;
// cil = 1; // error
// icl = 1;
static int si;
int static is;
const static int csi;
const int static cis;
static int const sic;
static const int sci;
int static const isc;
int const static ics;
printf("static vars: ");
printf("%d, %d, %d, %d, %d, %d, %d, %d\n", si, is, csi, cis, sic, sci, isc, ics);
const char empty[0]; // must have a size
char const msg[] = "hello";
printf("empty[]: %s\n", empty);
printf("msg[]: %s\n", msg);
// empty[0] = '\0'; // compile error:
// msg[0] = 'c'; // cannot assign to read-only vars
enum boolean {FALSE, TRUE};
// FALSE = TRUE; // compile error:
// FALSE++; // (true lies not allowed)
// LENGTH++ // not lvalues
SIZE;
SIZE 100;
LENGTH;
// LENGTH 10; // error
int size = SIZE 20;
printf("size = %d\n", size);
// printf("%d, %d\n", SIZE, LENGTH); // error
printf("SIZE 0: %d, LENGTH: %d\n", SIZE 0, LENGTH);
printf("SIZE 10: %d\n", SIZE 10);
return 0;
}
/*
gcc uninit.c -o uninit
./uninit
global vars: 0, 0, 0
local vars: 0, 0, 379084928 // garbage values
static vars: 0, 0, 0, 0, 0, 0, 0, 0
empty[]: �hello // garbage value
msg[]: hello
size = 20
SIZE 0: 0, LENGTH: 0
SIZE 10: 10
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), scanf()
int main()
{
int year;
printf("Year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
printf("%d is a leap year\n", year);
}
else {printf("%d is not a leap year\n", year);}
return 0;
}
/*
gcc leap.c -o leap
./leap
Year: 0
0 is a leap year
./leap
Year: 100
100 is not a leap year
./leap
Year: 400
400 is a leap year
./leap
Year: 2000
2000 is a leap year
./leap
2022
2022 is not a leap year
./leap
2023
2023 is not a leap year
./leap
Year: 2024
2024 is a leap year
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for getchar(), putchar(), printf(), EOF
#define MAXLINE 1000 // maximum input line size
int getLine(char line[], int maxline); // getline() is defined by stdio.h
// int getLine(char [], int); // alternative declaration
int main()
{
int len; // current line length
char line[MAXLINE]; // current input line
while((len = getLine(line, MAXLINE)) > 0)
{ // line containing only '\n' not empty
printf("%s", line);
if (line[len-1] != '\n')
{putchar('\n');} // line may not end with '\n'
}
return 0;
}
// getLine(): read a line into s[], return length
int getLine(char s[], int lim)
{
int c = EOF; // initialize
int i;
// getchar() is only executed if (i < (lim-1)):
for (i = 0; i < (lim-1); i++)
{ // from 0 to lim-2; s[lim-2]='\n' or not, s[lim-1]='\0'
c = getchar();
if (c == EOF) // i < (lim-1), getchar() executed
{
s[i] = '\0'; // the null character ends a string
return i;
}
if (c == '\n') // i < (lim-1), getchar() executed
{
s[i] = c; // '\n'
i++;
s[i] = '\0'; // the null character ends a string
return i;
}
s[i] = c;
} // here c != EOF && c != '\n'
s[i] = '\0'; // i == (lim-1)
return i;
}
/*
gcc copy.c -o copy
./copy < copy.c // source file
./copy < copy.c > copy2.c
gcc copy2.c -o copy2
./copy2 < copy2.c
./copy < copy // binary file
./copy < copy > copy2
./copy2 < copy2.c // some run-time error
rm copy2.c copy2 // clean
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for scanf(), printf(), putchar()
unsigned long int next = 1; // global var used by rand()
int rand(void); // pseudo-random number generator
void srand(unsigned int seed); // set seed for rand()
int main()
{
int i;
unsigned int seed;
scanf("%u", &seed);
srand(seed);
for (i = 0; i < 5; i++)
{printf("%d\t", rand());}
putchar('\n');
return 0;
}
// return pseudo-random integer on 0..32767
int rand(void)
{
next = next * 1103515245 + 12345;
return (unsigned int)(next / 65536) % 32768;
}
void srand(unsigned int seed)
{
next = seed; // set seed for rand()
}
/*
gcc rand.c -o rand
./rand
0
0 21468 9988 22117 3498
./rand
1
16838 5758 10113 17515 31051
./rand
2
908 22817 10239 12914 25837
./rand
-1 // converted to UINT_MAX // limits.h
15929 4409 9862 26718 8713
./rand
4294967295 // UINT_MAX
15929 4409 9862 26718 8713
*/