Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of simple programming in C language

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the method of simple programming in C language". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn "what is the method of simple programming in C language"?

We put all the C elements on an easy-to-read memo.

Dennis Ritchie Dennis Ritchie worked for Bell Labs at Hubert Lab in 1972, and he and his team invented Unix a few years ago. After creating an enduring operating system (still in use today), he needed a good way to program these Unix computers so that they could be used to perform new tasks. This seems strange now, but at the time there were relatively few programming languages, Fortran, Lisp, Algol, and B were all popular, but they were not enough for what the Bell Labs researchers wanted to do. Dennis Ritchie showed a trait that came to be known as the programmer's main feature: creating his own solution. He calls it the C language, and nearly 50 years later, it is still widely used.

Why should you learn C language?

Today, there are many languages that provide programmers with more features than C. The most obvious is the C++ language, a language named in a rather explicit way, which is built on top of C, creating a good object-oriented language. However, many other languages exist for a good reason. Computers are good at consistent repetition, so anything predictable can be built into a programming language, which means less work for programmers. Why use two lines of statements in C when you can convert an int into a long with one sentence in the C++ language (long x = long (n);)?

However, C language is still useful today.

First of all, C language is a rather simple and direct language. There are no high-level concepts other than the basics of programming, largely because C is actually one of the foundations of modern programming languages. For example, one of the features of C is an array, but it does not provide a dictionary (unless you write one yourself). When you learn the C language, you will learn the basic components of programming, which can help you recognize the improvements and careful design of today's programming languages.

Because C is a minimized programming language, your application is likely to get a performance improvement that is not seen in many other programming languages. It's easy to fall into penny-pinching when you think about how fast your code can execute, so it's important to ask if you need to provide more speed for a particular task. With C, you need to tangle less in each line of code than Python or Java. C language programs run very fast. This is a good reason why the Linux kernel is written in the C language.

Finally, C is easy to get started, especially if you are running Linux, you can already run C code because the Linux system contains the GNU C library (glibc). In order to write and build a C program, all you need to do is install a compiler, open a text editor, and start coding.

Start learning the C language

If you are running Linux, you can install a C compiler using your package manager. On Fedora or RHEL:

$sudo dnf install gcc

On Debian and its derivative systems:

$sudo apt install build-essential

On macOS, you can install Homebrew and use it to install GCC:

$brew install gcc

On Windows, you can use MinGW to install a minimal set of GNU utilities that contain GCC.

Verify the GCC you have installed on Linux or macOS:

Gcc-versiongcc (GCC) x.y.zCopyright (C) 20XX Free Software Foundation, Inc.

On Windows, provide the full path to the EXE file:

PS > C:\ MinGW\ bin\ gcc.exe-- versiongcc.exe (MinGW.org GCC Build-2) x.y.zCopyright (C) 20XX Free Software Foundation, Inc.C syntax

C is not a scripting language. It is a compiled language, which means that it is processed by the C compiler to produce a binary executable. This is different from scripting languages such as Bash or hybrid languages such as Python.

In C, you can create functions to perform the tasks you want to do. By default, a function named main is executed.

Here is a simple "hello world" program written in C:

# include int main () {printf ("Hello world"); return 0;}

The first line contains a header file called stdio.h (standard input and output), which is basically free-to-use, very rudimentary C code that you can reuse in your own programs. Then you create a function called main that consists of a basic output statement. Save the text to a file called hello.c, and then compile it using GCC:

$gcc hello.c-output hello

Try to run your C language program:

$. / helloHello world$ return value

This is part of the Unix philosophy in which a function returns something after execution: nothing on success and something else (for example, an error message) on failure. These returns are usually represented by numbers (or integers to be exact): 0 indicates no error, and any number greater than 0 indicates some unsuccessful state.

It is wise that Unix and Linux are designed to remain silent when running successfully. This is to allow you to execute a series of commands on the assumption that no errors or warnings will interfere with your work, so that you can always be prepared for successful execution. Similarly, functions in the C language are designed to be error-free.

You can see this with a small modification to make your program look like a failure:

Include int main () {printf ("Hello world"); return 1;}

Compile it:

$gcc hello.c-output failer

Now use a built-in Linux test to run it. The & & operator executes the second part of a command only if it is successful. For example:

$echo "success" & & echo "it worked" successit worked

On failure, | | the test executes the second part of a command.

$ls blah | | echo "it did not work" ls: cannot access' blah': No such file or directoryit did not work

Now, try your program, and when it succeeds, it doesn't return 0; it returns 1:

$. / failer & & echo "it worked" String is: hello

The program was successfully executed, but the second command was not triggered.

Variables and types

In some languages, you can create variables without specifying the type of data the variables contain. These languages are so designed that the interpreter needs to run some tests on a variable to see what kind of data type the variable is. For example, var=1 defines an integer, and when you create an expression to add var to something, Python knows that it is obviously an integer. It also knows that when you connect hello and world, the word world is a string.

The C language will not do any of these identifications and surveys for you; you must define your variable types yourself. There are several variable types, including int, char, float, and boolean.

You may also notice that there is no string type here. Unlike Python and Java and Lua and other programming languages, C does not have a string type, but treats a string as an array of characters.

Here is some simple code that creates an char array variable and then uses printf to print the array variable and a simple piece of information to your screen:

# include int main () {char var [6] = "hello"; printf ("Your string is:% s\ r\ n", var);}

You may notice that this code example provides six characters of space for a five-letter word. This is because there is a hidden Terminator at the end of the string, which takes up a byte in the array. You can run it by compiling and executing the code:

$gcc hello.c-- output hello$. / hellohello function

Like other programming languages, C functions accept optional parameters. You can pass parameters from one function to another by defining the type of data you want the function to accept:

# include int printmsg (char a []) {printf ("String is:% s\ r\ n", a);} int main () {char a [6] = "hello"; printmsg (a); return 0;}

This method of simply decomposing a function into two functions is not very useful, but it demonstrates how to run the main function by default and how to pass data between functions.

Conditional statement

In real programming, you usually want your code to make judgments based on the data. This is done using conditional statements, of which the if statement is the most basic.

To make the sample program more dynamic, you can include the string.h header file, which, as the name implies, contains code to check for strings. Try to test whether the string passed to the printmsg function is greater than 0 using the strlen function from the string.h file:

# include # include int printmsg (char a []) {size_t len = strlen (a); if (len > 0) {printf ("String is:% s\ r\ n", a);} int main () {char a [6] = "hello"; printmsg (a); return 1;}

As implemented in this example, this condition can never be untrue, because the supplied string is always hello, and its length is always greater than 0. The last thing this reimplemented echo command needs to do is to accept input from the user.

Command parameter

The code contained in the stdio.h file provides two parameters each time the program starts: a count of how many items are contained in the command (argc), and an array containing each item (argv). For example, suppose you issue this fictional command:

$foo-I bar

Argc is 3g.argv. The content is:

Argv [0] = foo

Argv [1] =-I

Argv [2] = bar

Can you modify the sample C language program to accept argv [2] as a string instead of the default hello?

Imperative programming language

C language is an imperative programming language. It is not object-oriented and has no class structure. Experience with C can teach you a lot about how to deal with data and how to better manage the data generated by your code at run time. Use C more often, and you will eventually be able to write libraries that can be used in other languages, such as Python and Lua.

To learn more about C, you need to use it. Look for useful C headers in / usr/include/ and see what small tasks you can do to make C useful to you. In the process of learning, use Jim Hall from FreeDOS to write a memo of the C language. It places all the basic elements on a double-sided memo, so when you practice, you can immediately access all the elements of C language grammar.

Thank you for your reading, the above is the content of "what is the method of simple programming in C language". After the study of this article, I believe you have a deeper understanding of what is the method of simple programming in C language. the specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report