Chapter 3 data and C (notes and assignments)

Posted by php4om on Mon, 24 Jan 2022 20:38:30 +0100

3.1 example program

platinum.c procedure

/* platinum.c -- your weight in platinum */
#include<stdio.h>
int main(void)
{
	float weight; /*Your weight*/
	float value; /*Platinum price of equal weight*/
	
	printf("Are you worth your weight in platinum?\n");
	printf("Let's check it out.\n");
	printf("Please enter your weight in pounds:");
	
	/*Get user input*/
	scanf("%f",&weight);
	/*Suppose the price of platinum is $1700 per ounce*/
	value = 1700.0 * weight * 14.5833;
	printf("Your weight in platinum is worth $%.2f.\n",value);
	printf("You are easily worth that!If platinum prices drop,\n");
	printf("eat more to maintain your value.\n");
	
	return 0; 
 } 

3.11 new elements in the program

Integer type variable (int) / / previous examples appear

Floating point type variable (float) / / use% f in printf() to process floating point values

Scanf() function / / used to read keyboard input,%f indicating that scanf() function reads the input floating-point number, & weight is a variable that assigns the input value to the name. The scanf () function uses the & symbol to indicate where the variable was found

This procedure embodies interactivity

3.2 variable and constant data

3.3 data: data type keyword

Table 3.1 # data type keywords of C language

Keywords initially given by K & RKeywords added by C90 standardKeywords added by C99 standard

int / / indicates integer type

signed_ Bool Boolean (true or false)
longA variant that provides the basic integer type_ Complex complex
short_ Imaginary imaginary number
unsigned
char specifies letters and other characters (#% ¥ *), smaller integers
float number with decimal pointvoid
double

3.3.1 integer and floating point numbers

Floating point number: e notation 3.16E7 represents 3.16 * 10 ^ 7, and 7 is called the index of 10

3.4 basic data type of c voice

3.4.1 int type

  • Declare int variable:

Declaration creation variable: int erns; int hogs,cows,goats;

Provided value: 1 cows = 112;    

2. Get the value through the function (e.g. scanf()).

  • initialize variable

Add the assignment operator (=) and the value to be assigned to the variable after the variable name

  • int type constant

Integer constant or integer face value

  • Print int value

%d indicates that printf () prints and displays a value in integer type

  • Octal and hexadecimal (in C language, a specific prefix indicates which hexadecimal is used)

Ox or ox prefix indicates hexadecimal

The O prefix indicates octal

  • Display octal and hexadecimal

Decimal display number, using% d

Octal display digits, using% o

Hexadecimal display number, using% x

To display the hexadecimal prefixes o, ox and ox, you must use% #o,% #x and% #x respectively.

bases.c procedure

/*basecs.c--Print decimal number 100 in decimal, octal and hexadecimal*/
#include<stdio.h>
int main(void)
{
	int x = 100;
	
	printf("dec = %d; octal = %o; hex = %x\n",x,x,x);
	printf("dec = %d; octal = %#o; hex = %#x\n",x,x,x);
	
	return 0;
}

3.4.2 other integer types

  • short int or short
  • long int or long
  • long long int or long long long
  • unsigned int or unsigned is only used for non negative values
  • C90, add unsigned long int or unsigned long and unsigned short int or unsigned short C99, add unsigned long long int or unsigned long long

3.4.3 characters used: char type

The char type is used to store characters

char grade = 'A';

//A single character enclosed in single quotes is called a character constant

  • Single quotes apply only to characters, numbers, and punctuation (three ways to represent characters)
  1. Use ASCII code (for example, the ASCII value of beep character is 7, char beep = 7;)
  2. Use a special symbol sequence -- escape sequence to represent some special characters.
Escape sequence
Escape sequencemeaning
\aAlarm (ANSI C)
\bBackspace
\fPage change
\nLine feed
\renter
\tHorizontal tab
\vvertical tab
\\Backslash (\)
\'Single quotation mark
\"Double quotation mark
\?question mark
\0oo

Octal value (oo) must be a valid octal number,

That is, each o can represent a number from 0 to 7)

\xhh

Hexadecimal number (hh) must be a valid hexadecimal number,

That is, each h can represent a number in 0~f)

Constant writing example of int series type
typehexadecimaloctal number systemdecimal system
char\x41\101N.A.
int0x41010165
unsigned int0x41u010165
long0x41L0101L65L
unsigned long0x41UL0101UL65UL
long long0x41LL0101LL65LL
unsigned long long0x41ULL0101ULL65ULL

charcode.c procedure

/* charcode.c-Displays the code number of the character*/
#include<stdio.h>
int main(void)
{
	char ch;
	
	printf("Please enter a character.\n");
	scanf("%c",&ch); /*User input character*/
	printf("The code for %c is %d.\n",ch,ch);
	
	return 0;
}

3.4.4 _Bool type

_ Bool type is used to represent Boolean values, i.e. logical values true (1) and false (0)

3.4.5 portable type: stdint H and inttypes h

3.6altnames.c procedure

/*altnames.c --Portable integer type name */
#include <stdio.h>
#include <inttypes. h> / / support portable types
int main(void)
{
	int32_t me32;  //me32 is a 32-bit signed integer variable
	
	me32 = 45933945;
	printf("First, assume int32_t is int:");
	printf("me32 = %d\n",me32);
	printf("Next, Let's not make any assumptions.\n ");
	printf("Instead,use a \"macro\"from inttrypes.h:");
	printf("me32 = %" PRId32 "\n",me32);
	
	return 0;
}

3.4.6 float, double and long double

Example of notation
numberScientific notationExponential notation
10000000001.0*10^91.0e9
1230001.23*10^51.23e5
322.563.2256*10^23.2256e2
0.0000565.6*10^-55.6e-5

3.7 showf_pt.c procedure

/* showf_pt.c -- Values of type float are displayed in two ways */
#include <stdio.h>
int main(void)
{
	float aboat = 32000.0;
	double abet = 2.14e9;
	long double dip = 5.32e-5;
	
	printf("%f can be written %e\n",aboat,aboat);
	//The next line requires the compiler to support C99 or its related features
	printf("And it's %a in hexadecimal, powers of 2 notation\n",aboat);
	printf("%f can be written %e\n",abet,abet);
	printf("%Lf can be written %Le\n",dip,dip);
	
	return 0;
}

3.4.7 types of plural and imaginary numbers

  • Three plural types: float_Complex,double_Complex,long double_Complex
  • Three types of imaginary numbers: float_Imaginary,double_Imaginary,long double_Imaginary

. if it contains complex H header file, which can be replaced by complex_ Complex, replace with Imaginary_ Imagine, you can also replace the square root of - 1 with I

3.4.8 other types

Arrays, pointers, structures, and unions

3.7 examples of escape sequences

Listing 3.10 demonstrates how backspace (\ b), horizontal tab (\ t), and carriage return (\ r) work

3.10 escape.c procedure

/* escape.c -- Using escape sequences */
#include <stdio.h>
int main(void)
{
	float salary;
	
	printf("\aEnter your desired monthly salary:"); //1
	printf(" $______\b\b\b\b\b\b\b");//2
	scanf("%f",&salary);
	printf("\n\t$%.2f a month is $%.2f a year.",salary,salary * 12.0);//3
	printf("\rGee!\n");//4
	
	return 0;
	
}

3.11 programming exercises

Topics: C