#include <stdio.h> // for printf()
#include <stdlib.h> // for system()
int main(int argc, char*argv[])
{
if (argc != 2)
{
printf("Usage: ./system command\n");
return 1; // end program, signalling error
}
system(argv[1]);
return 0;
}
/*
gcc system.c -o system
./system
Usage: ./system command
./system date
Sun 03 Sep 2023 11:06:19 AM EEST
./system dir
isupper isupper.c system system.c
./system ls -a
Usage: ./system command
./system "ls -a"
. .. isupper isupper.c system system.c
./system uname
Linux
./system whoami
user
./system w
./system "ls -l"
./system "cat system.c"
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf()
#include <stdlib.h> // for rand(), srand(), RAND_MAX
#include <time.h> // for time_t, time()
#include <limits.h> // for INT_MAX
double frand(void); // floating point rand()
int main()
{
printf("RAND_MAX: %d\n", RAND_MAX);
printf("INT_MAX: %d\n", INT_MAX);
time_t t;
srand((unsigned) time(&t)); // seed rand()
printf("frand(): %lf\n", frand());
return 0;
}
double frand(void) // floating point rand()
{
return (double) rand() / (RAND_MAX + 1.0);
}
/*
gcc frand.c -o frand
./frand
RAND_MAX: 2147483647
INT_MAX: 2147483647
frand(): 0.326507
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 7-9. Functions like isupper() can be implemented to save space or to save time. Explore both possibilities.
#include <stdio.h> // for printf(), getchar()
#include <limits.h> // for UCHAR_MAX
unsigned char ISUPPER[UCHAR_MAX];
int isUpper(unsigned char c);
int main() // time efficient, requires space for array
{
int c;
for (c = 0; c < UCHAR_MAX; c++)
{ // initialize array
if (c >= 'A' && c <= 'Z')
{ISUPPER[c] = 1;}
else {ISUPPER[c] = 0;}
}
printf("Give an unsigned char: ");
c = getchar();
printf("isUpper('%c'): %d\n", c, isUpper(c));
return 0;
}
int isUpper(unsigned char c)
{
return ISUPPER[c]; // one operation
}
/*
gcc isupper.c -o isupper
./isupper
Give an unsigned char: a
isUpper('a'): 0
./isupper
Give an unsigned char: B
isUpper('B'): 1
./isupper
Give an unsigned char: 1
isUpper('1'): 0
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), getchar()
#include <limits.h> // for UCHAR_MAX
unsigned char ISUPPER[UCHAR_MAX];
#define isUpper(c) ISUPPER[c]
int main() // macro for isupper()
{
int c;
for (c = 0; c < UCHAR_MAX; c++)
{ // initialize array
if (c >= 'A' && c <= 'Z')
{ISUPPER[c] = 1;}
else {ISUPPER[c] = 0;}
}
printf("Give an unsigned char: ");
c = getchar();
printf("isUpper('%c'): %d\n", c, isUpper(c));
return 0;
}
/*
gcc isuppm.c -o isuppm
./isuppm
Give an unsigned char: a
isUpper('a'): 0
./isuppm
Give an unsigned char: B
isUpper('B'): 1
./isuppm
Give an unsigned char: 1
isUpper('1'): 0
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), getchar()
int isUpper(unsigned char c);
int main() // space efficient, requires time for function call
{
int c;
printf("Give an unsigned char: ");
c = getchar();
printf("isUpper('%c'): %d\n", c, isUpper(c));
return 0;
}
int isUpper(unsigned char c)
{
return c >= 'A' && c <= 'Z'; // two operations
}
/*
gcc isupps.c -o isupps
./isupps
Give an unsigned char: a
isUpper('a'): 0
./isupps
Give an unsigned char: B
isUpper('B'): 1
./isupps
Give an unsigned char: 1
isUpper('1'): 0
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), getchar()
#define isUpper(x) ((unsigned)((unsigned char)(x) - 'A') <= 'Z'-'A')
int main() // macro for isupper()
{
int c;
printf("Give an unsigned char: ");
c = getchar();
printf("isUpper('%c'): %d\n", c, isUpper(c));
return 0;
}
/*
gcc isuppsm.c -o isuppsm
./isuppsm
Give an unsigned char: a
isUpper('a'): 0
./isuppsm
Give an unsigned char: B
isUpper('B'): 1
./isuppsm
Give an unsigned char: 1
isUpper('1'): 0
*/