Logo  

CS256 - Fundamentals of Structured Programming

Displaying exercises/e3/solution/e3.txt

Exercise #3

Edit this file and add your answers to each question below the question. 

1) Complete Unix command to copy the func.c file located in the h2 sub-
   directory located in the cs256 accounts home directory to your current
   working directory:
 
  cp ~cs256/h2/func.c .
 
2) Complete command to read the online manual for the scanf C function:
 
  man scanf
 
3) Answer the following, given the following ls listing:
  -rw-r----- 1 root sys 215 Apr 29  2019 x.c

 a) Size of the file: 215
 b)     Type of file: regular file
 c)Group permissions: read only (r--)
 d)       File owner: root

4) Define the C grammar for the following:

 a) A block statement:
 
   { statements... }
 
 b) An if statement:

   if (expression) statement
 
 c) An if-else statement:

   if (expression) statement1
   else statement2
 
 d) A while loop:

   while (expression) statement

Note: Note how statement does not have a ; after it.  While many statements
      require the ; to terminate them (much like a period at the end of a
      sentence,) not all do, such as the block statement ({...}).

5) Give a data literal example of each data-type:

 a)  char: 'A', '\n'	<- Note that is is enclosed in single quotes ('')!
 b)   int: 128, -1, 0, 1L, 100UL
 c) float: 5.0, .5, 10e100

6) If the variable M holds some value that represents miles, define a
  variable called K and assigns it number of miles in M converted to kilometers
  (Hint: there are 1.609344 kilometers / mile):
 
  double K = M * 1.609344;
 
7) Read the man page for getchar and tell:
 a) What is the return data-type for the function:

   int
 
 b) What is the return value for the the function: 

   The  next character from stdin and returns it as an unsigned char cast to an
   int, or EOF on end of file or error

8) Write an example scanf() function that would read an integer followed
  by two floating point numbers.  Define the variables used in the scanf() as
  well:
 
  int i;
  float f1, f2;
  
  scanf("%d %f %f", &i, &f1, &f2);

 
9) Define a Boolean expression for the variable 'x' that would be true
  when x is in the range 10-100 or it is 300:
 
  (x >= 10 && x <= 100) || x == 300

Note: == is equality test, not =, which is assignment.  Also always use ()'s
      when the order of operations is unclear.  ()'s don't cost you anything,
      use them liberally.