The char data type, review of the modulus operator

A variable declaration also functions as a definition for the variable, because it causes memory to be allocated to store the variable’s data type, and assigns this memory address a name. When a variable is declared it is assigned what is known as a junk value, which is whatever happened to be there from when the memory address was used last.

#include <stdio.h>

int main(void){
 
  int a;
  int b;
  
  /*
   * output the junk values
   * stored in variables 
   * a and b
   */
  printf("a = %d and b = %d\n", a, b);
  
  
  return 0;
  
}

An assignment statement, as we have seen earlier, takes the value to the right of the equal sign and stores in the variable on the left of the equal sign. The equals sign is thus called the assignment operator because it assign the value on the right to the variable on the left.

Whenever we print the value of a variable using the printf() function, we must provide the printf() function with at least two arguments inside the parenthesis, separated by a comma. The first argument is the control string, because it controls how the other arguments will be presented as output. The control string is a character string enclosed in double quotes. The control string is sometimes also referred to as the format string, since again it formats the output. The second argument is the variable whose value we wish to display.

The control string must have the proper conversion specifier in order to display the variable’s data properly. Note that because conversation specifiers always starting with a % character, if we want to output a % character we must use the sequence %%.

#include <stdio.h>

int main(void){
  
  /*
   * declare a char variable
   * called a
   */
  char a;
  /*
   * declare an int variable
   * called x
   */
  int x;
  
  /*
   * store 'b' in variable
   * a
   */
  a = 't';
  
 /*
  * store 451 in 
  * variable x
  */
  x = 451;
  
  /*
   * display output
   */
  printf("%%c is for chars, %%d is for ints\n");
  printf("a = %c, x = %d\n", a, x);
  
  return 0;
  
}

The char data type is used to store a single character of information. A character, also referred to as a character constant, is a symbol enclosed between two single quotation marks. Care should be taken to ensure that single and not double quotation marks are used. Along with letters, mathematical symbols and numbers can be stored in a char variable as well.

 #include <stdio.h>

int main(void){
  
  char letter1 = 'D';
  char letter2 = 'x';
  
  /*
   * don't confuse the character
   * constants for numbers
   * with their numeric values
   */
  char number1 = '7';
  char number2 = '4';
  
  
  printf("Four char values: %c %c %c %c\n", letter1, number1, letter2, number2);
  
  return 0; 
    

Remember, the division operator always produces an integer result with the operands are of type int. To calculate the remainder, we need to use the modulus operator.

#include <stdio.h>

int main(void){
    
  int apples = 73;
  int students = 31;
  
  
  /*
   * calculate how many apples 
   * are available per student
   */
  int applesPerStudent = apples / students;
  
  /*
   * calculate how many apples are 
   * left over
   */
  int applesLeftOver = apples % students;
  
  printf("We can give %d apples to each student.\n", applesPerStudent);
  Printf("with %d apples left over.\n", applesLeftOver);
  
  
  return 0;
  

So far we have mostly been working with binary operators, so called because they operate on two operands. There are some operators that are unary, meaning that they only use one operand. The unary minus operator is the most familiar of these, it’s simply a minus sign that toggles the value of the operand to its opposite.

 #include <stdio.h>

int main(void){
  
  
  double balance = 49.99;
  
  double book = 11.99;
  double meal = 29.99;
  double shirt = 39.99;
  
  
  printf("Your balance is %f\n", balance);
  
  balance = balance - book;
  
  printf("balance has changed by %f\n", -book);
  
  balance = balance - meal;
  
  printf("balance has changed by %f\n", -meal);
  
  balance = balance - shirt;
  
  printf("balance has changed by %f\n", -shirt);
  
  printf("Your balance is %f\n", balance);
  
  return 0; 
}

The unary minus operator when applied to a variable results in the inversion of the sign of the value stored in the variable; thus, positive becomes negative and negative becomes positive.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s