"Ming Jie C language" Chapter 1 study notes

learning target:

Learning "Ming Jie C Language" 3rd Edition
Chapter 1
Initial C Language


Learning Content:

1-1 Display calculation results

1-2 Variables
1-3 Input and display


study-time:

7-9pm, October 22, 2020

Study notes

1-1 Display calculation results

1. Calculate the sum of integers and display the result

#include<stdio.h>  //注意不要和studio混淆,stdio是standard I/O(标准输入输出)的缩写
int main(void)
{
    
    

  printf("%d",15+37);  /*用十进制数显示整数15和37的和*/
  return 0;
  }

Basic program and fixed code

#include<stdio.h>
int main(void)
{

   /*...*/
   return 0;
               }

1. The comment is / /, you can record the meaning you want to express in the form of a comment.

2.printf function: formatted output function

If you want to use the function of a function, you must implement it through a function call.

ex: call the printf function to display the sum of 15 and 37

Function call: printf/function name/ (“%d”/actual parameter/, 15+37/actual parameter/);

The display result is 52.

/*Calling this function not only issues a "display these content" request, and then passes the content you want to display through the argument in parentheses. When there are more than two actual parameters, they need to be separated by commas*/

/* Attention! ! In principle, the sentence must end with a semicolon! */

2. The necessity of line breaks

1-3

#include<stdio.h>
int main (void)
{
    
    
  printf("15与37的和是%d。/n",15+37);
  return 0;
  }

1.%d specifies that the actual parameter should be displayed in the form of a decimal number. This is the conversion specification.
2. \n is a symbol for newline, a special newline character composed of \ and n.
/* The \n will not be displayed on the screen, but an (invisible) line break is entered.

! In most operating environments, after the program is executed, the input result of the program will be followed by a prompt. If you enter a newline character after the program, the prompt will not follow.

Exercise 1-1

  /* 编写一段程序,计算出15减去37的结果,并以“15减去37
的结果时-22。”的格式进行显示。*/
 
#include <stdio.h>
 
int main (void)
{
    
    
    printf("15减去37的结果是%d\n",15-37);
 
    return 0;
 }

Exercise 1-2

/*  换行显示天地人  */
 
#include <stdio.h>
 
int main(void)
{
    
    
	printf("天\n地\n人\n");
	
	return 0; 
}

Exercises 1-3

/*  换行表示喂!您好!再见!  */
#include <stdio.h>
 
int main(void)
{
    
    
	printf("喂!\n\n您好!\n再见。");
	return 0; 
 } 

1-2 variables

1. Assign integer values ​​to two variables and display

#include<stdio.h>
int main(void)
{
    
    
    int vx,vy;
    vx= 57;
    vy = vx + 10;
   printf("vx的值是%d。\n",vx);
   printf("vy的值是%d。\n",vy);
return 0;
}

int vx; /*variable*/
int vy; /*variable*/
declare two variables.
vx = 54;
vy = vx +10;
Assign values ​​to two variables.

2. Initialization and assignment
Initialization: Put the value when generating the variable.
Assignment: Put a value in the generated variable.

The thin = means initialization, and the bold = means assignment.

Exercises 1-4

/*   在int型变量的声明中为变量赋一个实数值的初始值
(如3.14或5.7等)会怎样?   */
 
#include <stdio.h>
 
int main(void)
{
    
    
	int x = 3.14;
	printf("%d",x); 
	return 0;
 } 

1-3 Input and display

1. Format the input function scanf

#include<stido.h>
int main(void)
{
    
    
  int no;
  pritnf("请输入一个整数:");
  scanf("%d",&no);   //和printf不同,此处需要使用&!!!
  printf("您输入的是%d。\n",no);
  return 0;
  }

The difference with the printf function is that when using the scanf function to read, a special symbol & must be added before the variable name!

// printf("%d",no) //scanf("%d",&no)

Exercises 1-5

/*   读取一个整数并显示该整数加上12之后的结果。   */
 
#include <stdio.h>
 
int main(void)
{
    
    
	int no;
	printf("请输入一个整数:");scanf("%d",&no);
	printf("该整数加上12的结果是%d。", no + 12);
	return 0;
 } 

Exercises 1-6

/*   读取一个整数并显示该整数减去6之后的结果   */
 
#include <stdio.h>
 
int main(void)
{
    
    
	int no;
	printf("请输入一个整数:");scanf("%d",&no);
	printf("该整数减去6的结果是%d",no - 6);
	
	return 0; 
 } 

2. Output function puts

#include<stdio.h>
int main(void)
{
    
    
    int n1,n2;
    puts("请输入两个整数。");
    printf("整数1:");scanf("%d",&n1);
    printf("整数2:");scanf("%d",&n2);
    
    printf("它们的和是%d。\n", n1+n2);
   
    return 0;
    }

Puts("...") has basically the same function as printf("...\n").
The puts function can output the string as the actual parameter in order, with a newline at the end.

// There can only be one actual parameter of the puts function. And the display method of the symbol% ​​is different from the printf function.

Exercises 1-7

/*   用puts函数输出天地人   */
 
#include <stdio.h>
 
int main(void)
{
    
    
	puts("天");
	puts("地");
	puts("人"); 
	
	return 0;
 } 

Exercises 1-8

/*  显示读取到的两个整数27与35的乘积。   */
 
#include <stdio.h>
 
int main(void)
{
    
    
	int a,b; 
	
	puts("请输入两个整数。");
	printf("整数1:");
	scanf("%d",&a);
	
	printf("整数2:");
	scanf("%d",&b);
	
	printf("它们的乘积是%d",a * b);
	
	return 0;
}

Exercises 1-9



/*   显示读取到的三个整数的和   */

#include<stdio.h>
int main(void)
{
    
    
int a,b,c;
puts("请输入三个整数");
printf("整数1:");scanf("%d",&a);
printf("整数2:");scanf("%d",&b);
printf("整数3:");scanf("%d",&c);

printf("它们的和是%d",a+b+c);

return 0;
}

Guess you like

Origin blog.csdn.net/weixin_51493740/article/details/109229001