C language programming variables and expressions

Posted by Redlightpacket on Sat, 29 Jan 2022 16:29:38 +0100

Reading C language programming
Chapter 2 variables and arithmetic expressions
Author: Geekwyz
This series of articles can help you lay a solid foundation


The following is the main body

In the previous section, I mainly introduced Hello,World! This program, today we look at the chapter of variables and expressions

First, a function is realized: print Fahrenheit temperature (fahr) and Celsius
Conversion formula: c = 5 * (f-32) / 9;

Code implementation:

#include<stdio.h>

int main(){
    /*Realize the conversion between Celsius and Fahrenheit*/
    int c,f; //Define Celsius, define Fahrenheit
    int upper,lower,step;
    
    upper = 300;//Upper temperature limit 300
    lower = 0;//Lower temperature limit 0
    step = 20;//Temperature span
    
    lower = f;
    
    while (upper >= f) {
        c = 5 * (f-32) / 9;//Convert
        printf("%df\t%dc\n",f,c);
        f = f + 20;
    }
    return 0;
}
  • notes

The function of annotation is to explain the function of this program
For example:
/*Realize the conversion between Celsius and Fahrenheit*/
Comments are divided into single line comments and multi line comments:

//This is a single line comment
/*
This is a multiline comment
*/
  • variable

The function of variables is to store data, just like loading dishes on a plate
For example:
int c,f;
int upper,lower,step;
In C language, variables must be declared first and then used. The declaration of variables is usually placed at the beginning of functions.

  • data type

Int means that the variable followed is of integer type, and the corresponding float means that the number is of floating-point type. The value range of float type depends on the specific machine. For int type, it may be 32-bit or 16 bit
Besides int and float, there are some data types
For example: char, character, short integer, long integer, double double double precision floating point

  • Assignment statement
    There are also some statements in this code, such as
 	upper = 300;//Upper temperature limit 300
    lower = 0;//Lower temperature limit 0
    step = 20;//Temperature span

This statement means to assign the value on the right to the variable on the left. The assignment symbol used is=
Remember that in C, the equal sign means assignment

  • Circular statement
    There are such statements in this code
 while (upper >= f) {
        c = 5 * (f-32) / 9;//Convert
        printf("%d\t%d\n",f,c);
        f = f + 20;
}

Such statements are called loop statements:
Its execution order is as follows:
1. First, judge whether the conditions in parentheses are met. If they are met, start to execute next. If not, execute after the while loop
2. Start to execute the statement in {} once, and then return to judge whether the () condition is still satisfied. If so, continue to execute

  • Formatted output of printf

Let's look at this Code:

printf("%d\t%d\n",f,c);

It is found that the first position of the printf() function is a string with
%D this means that the following corresponding f is an integer type, and the second% d also corresponds to c in turn

We can think of% d as a model

\t indicates the meaning of space

Is to add a space between the data output later

\n means line feed

After printing this line, stop the cursor on the next line

This type of \ is called escape

Let me introduce a fun thing

Align the output numbers to the left and right
We just need to print in printf("%d\t%d\n",f,c); Just add the number of% d
For example, printf("%3d",3); The effect is as follows

**3

We use the * sign here to indicate the space
Of course, we can also replace 3 with - 3

  • Accurate calculation

After the above study, we make this program more perfect

#include<stdio.h>

int main(){
    /*Realize the conversion between Celsius and Fahrenheit*/
    int c,f; //Define Celsius, define Fahrenheit
    int upper,lower,step;
    
    upper = 300;//Upper temperature limit 300
    lower = 0;//Lower temperature limit 0
    step = 20;//Temperature span
    
    lower = f;
    
    while (upper >= f) {
        c = 5 * (f-32) / 9;//Convert
        printf("%3df\t<----->\t%3dc\n",f,c);
        f = f + 20;
    }
    return 0;
}

There is a small bug in this program, that is, the calculated value is not very accurate,

For example, 0 degrees Celsius should be - 17.8c instead of 17c

In order to obtain more accurate values, we should use floating-point arithmetic operators instead of integer calculations. Let's modify this program:

#include<stdio.h>

int main(){
    float f,c;
    float upper,lower,step;
    
    upper = 300;
    lower = 0;
    step = 20;
    
    lower = f;
    
    while (upper >= f) {
        c = 5 * (f-32) / 9;
        printf("%ff\t%fc\n",f,c);
        f = f + step;
    }
    return 0;
}

We found that the output result is with decimal places:

This is because integers are divided and rounded, while floating-point numbers are not rounded
Let's take an example

int a = 5;
int b = 2;
int c = 5 / 2;

We declare a and b as integer types, and finally calculate that c is 2

If we write this, we'll get 2.5

float a = 5;
float b = 2;
float c = 5 / 2; 

We can also set the number of decimal places left after the decimal point through% Reserved digits f complete
If we want to keep three significant digits now, we can write this in the output statement

printf("%.3d",0.33333);

This will output 0.333

printf("%6.3f",0.2222);

The output statement occupies 6 words wide and retains 3 significant digits

In addition, the C language printf function supports the following formats

  • %o print octal numbers
  • %x stands for hexadecimal number
  • %s represents a string
  • %%Represents the% itself

Topics: C Back-end