
What is the usage of const?
# The usage of const is:
1. The common usage of const
int const size; const int size;
These two statements are Declare size as an integer, and its value cannot be modified.
You can initialize it when declaring it.
int const size =10;
Incorrect usage:
const int size =10; char buffer[size];
The error is that size occupies a certain memory block and the C compiler does not know that it is compiling value. Moreover, C defaults to const being externally connected, so it is okay to write it as const.
2.const is used for pointers
(1)
const int *p; int const *p; //含义是指向常量的指针,指针的常量值不可以修改
Both are the same, p is a pointer to an integer that is always on and can be modified The value of a pointer, but the value pointed to by the pointer cannot be modified.
int b = 5; int c = 3; const int *p=&b; *p=6; //错误,不能通过指针修改所指向的值; p=&c; //正确,可以修改指针的值
(2)
int * const p; //含义是常量指针,p的值不可以修改
p is a pointer, and pointer p is a constant. Its value cannot be modified, but the value of the integer pointed to by p can be modified.
int b = 5; int c = 3; int *const p = &b; *p = 6; //正确,可以修改指针所值向的值 p =&c; //错误,不可以修改指针的值
(3)
const int * const p;
Both the pointer itself and the value it points to are constants.
Simple method of judgment:
If const is on the left side of *, then const is used to modify the variable pointed by the pointer, that is, the pointer points to a bit constant;
If const is located on the right side of *, then const modifies the pointer itself, that is, the pointer itself is a constant.
3.const is used for formal parameters of functions
void foo(const int * p)
When const is used for formal parameters, it means that the formal parameters will not be used inside the function. Change. This form is usually used to simulate call-by-value in array-form parameters. This is also the most useful thing about const, limiting the formal parameters of a function so that the function will not modify the data pointed to by the actual parameter pointer. What needs to be noted here is that the function should not be modified, not that it cannot be modified.
4. Other ways to declare constants
In C language, you can use enum type and #define macro to define constants
#define MAX_SIZE 10; int const max_size =10;
Used here# define is better than using const variables.
The former can be used wherever literal constants are allowed, such as declaring the length of an array.
const variables can only be used where variables are allowed.
Recommended tutorial: "cVideo Tutorial"
The above is the detailed content of What is the usage of const?. For more information, please follow other related articles on the PHP Chinese website!
C# and .NET: Understanding the Relationship Between the TwoApr 17, 2025 am 12:07 AMThe relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).
The Continued Relevance of C# .NET: A Look at Current UsageApr 16, 2025 am 12:07 AMC#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.
From Web to Desktop: The Versatility of C# .NETApr 15, 2025 am 12:07 AMC#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.
C# .NET and the Future: Adapting to New TechnologiesApr 14, 2025 am 12:06 AMC# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.
Is C# .NET Right for You? Evaluating its ApplicabilityApr 13, 2025 am 12:03 AMC#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.
C# Code within .NET: Exploring the Programming ProcessApr 12, 2025 am 12:02 AMThe programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.
C# .NET: Exploring Core Concepts and Programming FundamentalsApr 10, 2025 am 09:32 AMC# is a modern, object-oriented programming language developed by Microsoft and as part of the .NET framework. 1.C# supports object-oriented programming (OOP), including encapsulation, inheritance and polymorphism. 2. Asynchronous programming in C# is implemented through async and await keywords to improve application responsiveness. 3. Use LINQ to process data collections concisely. 4. Common errors include null reference exceptions and index out-of-range exceptions. Debugging skills include using a debugger and exception handling. 5. Performance optimization includes using StringBuilder and avoiding unnecessary packing and unboxing.
Testing C# .NET Applications: Unit, Integration, and End-to-End TestingApr 09, 2025 am 12:04 AMTesting strategies for C#.NET applications include unit testing, integration testing, and end-to-end testing. 1. Unit testing ensures that the minimum unit of the code works independently, using the MSTest, NUnit or xUnit framework. 2. Integrated tests verify the functions of multiple units combined, commonly used simulated data and external services. 3. End-to-end testing simulates the user's complete operation process, and Selenium is usually used for automated testing.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function






