First knowledge of C language series

What is c language

C language is a general computer programming language, which is widely used in the bottom development. C language is a process oriented computer programming language, which is different from c++, Java and other object-oriented programming languages.

The first c language program

#include <stdio.h>
//The use of library functions should include header files.
int main()//The main function is the entry of a project, and there is only one main function in a program.
{
	
	printf("This is my first c Language program");//printf statement is a library function provided by c language for output.
	return 0;//return 0; Indicates that main is normally executed, and non-0 indicates that the main function is abnormal.
}

This is my first c language program, which outputs "this is my first c language program". To call the printf() function, you need to include the header file <stdio h> , main function is the entry of c language program, and there is only one function in a project.

data type

char character data type
Short short
int shaping
Long long integer
long long longer shaping
float single precision floating point number
Double double precision floating point number

Why do we have so many types? What is the difference between them? The reason is that we need to more richly express various values in life. For example, we need to use char character data type to define a person's name, and int integer to define a person's age. The difference between them can be found through code. Their length and precision are different.

#include <stdio.h>
int mian()
{
	printf("%d\n", sizeof(char));
	printf("%d\n", sizeof(int));
	printf("%d\n", sizeof(short));
	printf("%d\n", sizeof(long));
	printf("%d\n", sizeof(long long));
	printf("%d\n", sizeof(float));
	printf("%d\n", sizeof(double));
	printf("%d\n", sizeof(long double));
	
      return 0;
	}

Variables and constants

Why distinguish between variables and constants? Because we sometimes need variables in our life, such as defining values that will change such as age, height and weight, we need to use variables, and we can use constants when defining constant values such as blood type and gender.

1. variable definition method

The variable definition method is data type variable name = variable value, for example, int a =20; char b = "pig"; Etc.

2. naming rules of variables

There are also requirements for variable naming in c language. The rules are as follows

1. only letters (case sensitive), numbers, and underscores (_) form.
2. cannot start with a number
3. no more than 63 characters in length
4. the variable name cannot use keywords (such as int char, etc.)

3. classification of variables

global variable

#include<stdio.h>
int a =20;
int main()
{

return 0;
}

At this point, variable a is a global variable

local variable

#include<stdio.h>
int a =20;
int main()
{
int b = 20;
return 0;
}

In this case, a is a global variable and b is a local variable. Can the boys see any difference? Variables defined outside the main function are called global variables, and variables defined inside the function are called local variables

4. scope and life cycle of variables

  • Scope

Scope is a programming concept. Generally speaking, the name used in a piece of program code does not always effectively limit the availability of the name. The scope of the code is the scope of the name.

1. the scope of global variables is the whole project
2. the scope of a local variable is the local range of the variable

  • life cycle
    The life cycle of a variable refers to the period between the creation of a variable and its destruction

The life cycle of a local variable is from the beginning of the scope entry life cycle to the end of the scope exit life cycle.
The life cycle of a global variable is the life cycle of the entire program.

It can be said that the scope of a variable determines the life cycle of the variable.

  • notes
    If the global variable and the local variable have the same name, the local variable takes precedence
#include<stdio.h>
int a =20;
int main()
{
int a = 40;
printf("%d\n",a);
return 0;
}

Because local variables take precedence, the code outputs a value of 40.

5. constant

The definition methods of variables and constants in c language are different. The definition methods of constants in c language can be divided into the following categories.

  • Literal constant
  • const decorated constant
  • #define defined indicator constants
  • enumeration constant

Literal constant

#include <stdio.h>
int main()
{
20;
"abd";
return 0;
}

const decorated constant

#include<stdio.h>
int main()
{
const a = 10;
//a = 20; error
return 0;
}

The above code variable a is modified by const to become a constant variable, so the value of a cannot be changed. Note that the variable modified by const is essentially a variable, but has a constant attribute.

#include<stdio.h>
int main()
{
const a =10;
//int arr[a] = {} error prompt: constant is required when defining array yes
return 0;
}

define defined indicator constants

#define MAX 1000

int main(){	
	int a = MAX;
	printf("%d\n", a);
	//MAX = 2000;
	int arr[MAX];

	return 0;
}

enumeration constant

#include <stdio.h>
enum sex{
man,
woman
};
int main(){
printf("%d\n",man);
printf("%d\n",woman);
return 0;
}

Enum constants need to use enum statements to list all the possibilities of a thing. Each possibility is an enum constant. By default, the enum constant starts from 0 and increases by 1.

Last words

I am a Xiaobai who has just started to learn. I hope you can point out my problems. If you don't understand any problems, you can leave a message to me. As long as I can solve them, I will gladly help you. I hope we can work together to make progress together. In the future, I will continue to share the knowledge I have learned. Don't forget to praise the brothers here! Thank you!

Tags: C

Posted by yandoo on Fri, 03 Jun 2022 23:59:29 +0530