← Back to Index
Research & Engineering Archive

LPC 2. Data & Operations

By Jingnan Huang · January 09, 2025 · 4186 Words

Last Edit: 1/9/25

2.1 Double data type for real numbers
#

2.1.1 Convert Inches to Centimeters
#

// Description: This program convert inches to centimeters
#include <stdio.h>

int main(void){
  // Declare variables
  const double InchesToCm = 2.54;
  double inputInches, outputCm;

  // Prompt user for input
  printf("Enter the number of inches to convert to cm: ");
  scanf("%lf", &inputInches);
  // Convert inches to centimeters
  outputCm = inputInches * InchesToCm;
 
  // Display output in 2 decimal places
  printf("The number of centimeters is %.2lf\n", outputCm);
  return 0;
}
int main(void){
	const double InchesToCm = 2.54;
	InchesToCm = 2.51;
}

这样操作将会报错,因为InchesToCm是一个不可以更改的Constant

What would happen if a number with decimal is stored in an int? 当赋值一个小数给int的时候,小数部分将被 Truncated 截断,只保留整数部分

2.1.2 Summary
#

2.2 Data types and representation
#

2.2.1 Integers
#

Other Integers Representation
#

2.2.2. Floating point or real numbers
#

Two float Representation
#

2.2.3. Characters
#

#include <stdio.h>

int main(void){
  char firstInitial = 'S';
  printf("My first initial is %c.\n", firstInitial);
  return 0;
}

2.2.4. Boolean
#

#include <stdbool.h>
#include <stdio.h>

int main(void){
  bool isRaining = true;
  printf("Is it raining? %d\n", isRaining);
  return 0;
}

ex.
#

bool isPositive = n > 0;
> True
bool isPositive = n;
> True or False,any non-zero number is considered as `true`
bool isPositive = n > 0 != 0;
> True
bool isPositive = n <= 0 != 1;
> n<=0 is 0, 0 != 1 -> 1 or True

2.2.5. Declaring Vs. Initializing Variables
#

#include <stdio.h>

int main(void) {
  int var;
  printf("Value of uninitialized variable \"var\": %d\n", var);
  int var2 = 0;
  printf("Value of initialized variable \"var\": %d\n", var2);
  return 0;
}

2.2.6. Taking in input from the user using scanf
#

Mutiple Numbers in mutiple variables
#

#include <stdio.h>
 
int main(void) {
  int num1 = 0, num2 = 0;
  double dnum1 = 0, dnum2 = 0;
  printf("Enter a number: ");
  scanf("%d %lf %d %lf", &num1, &dnum1, &num2, &dnum2);
 
  printf("Numbers entered: %d %lf %d %lf\n", num1, dnum1, num2, dnum2);
 
  return 0;
}
> 1 1.2 3 3.4
> Enter a number: 1 1.2 3 3.4 Numbers entered: 1 1.200000 3 3.400000

Numbers and Characters
#

#include <stdio.h>
 
int main(void) {
  char idChar;
  int idNum;
  printf("Enter your ID: ");
  scanf("%c %d", &idChar, &idNum);
 
  printf("ID entered: %c%d\n", idChar, idNum);
 
  return 0;
}

> S1321234
> Enter your ID: S1321234 ID entered: S1321234

Take in characters and ignoring leading spaces
#

#include <stdio.h>
 
int main(void) {
  char c1, c2, c3, c4, c5, c6, c7;
 
  printf("Enter license plate letters and numbers: ");
  scanf("%c %c %c %c %c %c %c", &c1, &c2, &c3, &c4, &c5, &c6, &c7);
   
  printf("Licence plate entered: %c%c%c%c-%c%c%c\n", c1, c2, c3, c4, c5, c6,c7);
   
  return 0;
}

Common mistake: Spaces after format specifiers
#

#include <stdio.h>
int main(void) {
  double dnum1 = 0;
  printf("Enter a number: ");
  scanf(" %lf ", &dnum1);
 
  printf("Number entered: %.2lf\n", dnum1);
  return 0;
}

2.3 Operations
#

2.3.1 Basic Arithmetic Operations
#

int x = 10 / 5 * 2;

2.3.2. The more accurate data type is contagious
#

int x = 10 * 5 / 3;
int x = 50 / 3.0;

2.3.3. What happens when we divide by 0?
#

2.3.4. Modulo operator
#

3 % 0 的结果是什么? 会表现出和3/0类似的行为

2.3.5. Assignment operators
#

x = y =z

Complex Assignment Operations
#

2.3.6. Increment and decrement operators
#

j = i;
i = i + 1;

2.3.7. Type casting
#

double x = 3 / 2; // x 的值是 1.0
double x = 3.0 / 2; // 或者
double x = (double) 3 / 2; // x 的值是 1.5
double x = 3 / (int) 2.9; // x 的值是 1.0

2.3.8. sizeof() operator
#

2.4. Math library
#

#include <math.h>
#include <stdio.h>
int main(void) {
  double a = 0, b = 0, c = 0;
  printf("Enter the lengths of the sides: ");
  scanf("%lf %lf", &a, &b);

  c = sqrt(a * a + b * b);
  printf("The length of the hypotenuse is %.2lf\n", c);
  return 0;
}

2.4.2. You can still use integer values
#

2.5. Random numbers
#

2.5.1. Generating a random number
#

#include <stdio.h>
#include <stdlib.h>
int main(void) {
  printf("Random number 1: %d\n", rand());
  printf("Random number 2: %d\n", rand());
  printf("Random number 3: %d\n", rand());
  return 0;
}

2.5.2. Are we generating random numbers?
#

Time overflow Problem
#

Nested rand
#

如果用 srand(rand()) 替代 srand(time(NULL)),是否会让种子变得随机?

2.5.3. Random numbers within a range
#

int random_number = rand() % 6; // 结果范围是 [0, 5]
0 % 5 = 0
1 % 5 = 1
2 % 5 = 2
3 % 5 = 3
4 % 5 = 4
5 % 5 = 0 (循环重复)
int random_number = (rand() % 6) + 1; // 结果范围是 [1, 6]
int random_number = rand() % (MAX - MIN + 1) + MIN;