You are viewing the article How to Learn Coding at Tnhelearning.edu.vn you can quickly access the necessary information in the table of contents of the article below.
wikiHow is a “wiki” site, which means that many of the articles here are written by multiple authors. To create this article, 54 people, some of whom are anonymous, have edited and improved the article over time.
This article has been viewed 4,034 times.
The C programming language is one of the oldest programming languages. This language was developed in the 70s, but it is still very powerful today thanks to its low-level language characteristics. Learning C is also a great way to educate yourself about more complex languages; in addition, the knowledge gained will be useful in most programming languages and can help you with application development. To learn how to get started programming in C, see Step 1 below.
Steps
Ready
- For Windows, try Microsoft Visual Studio Express or MinGW.
- For Mac, XCode is one of the best C compilers.
- For Linux, one of the most popular choices is gcc.
- C mainly consists of functions, and within these functions you can use variables, conditional statements, and loops to store and manipulate data.
#include <stdio.h> int main () { printf ( "Hello, World! n " ); getchar (); return 0 ; }
[1] X Research Source
- The #include directive is executed before the program starts and loads the libraries containing the functions you need. In this example, stdio.h allows us to use printf() and getchar() functions.
- The command { int main() tells the compiler that the program is running a function called “main” and that it should return an integer when it terminates. All C programs run a “main” function.
- { } indicates that everything inside them is part of the function. In this case, they signify that everything inside is part of the “main” function.
- The printf() function displays the content in parentheses on the user’s screen. The double quotes ensure that the string inside is printed literally. The string n tells the compiler to move the pointer to the next line.
- ; denotes the end of a line. Most lines of C code must end with a semicolon.
- The getchar() command tells the compiler to wait for keyboard input before moving on. This is useful because many compilers will run the program and close the window immediately. As such, this command will keep the program from closing until a key is pressed.
- The return 0 (return) statement indicates the end of the function. Notice how the “main” function is an int function. This means that it will need an integer to be returned when the program terminates. The number “0” indicates that the program executed correctly; If ra returns any other number then the program has encountered an error.
- To comment in C, put /* at the beginning of the comment section and end it with */ .
- You can take notes on all but the most basic of your code.
- You can use the comments section to quickly remove sections of code without deleting. Simply enclose the code you want to remove with comment tags and then compile. If you want to add the code back, remove these tags.
Using variables
- Some of the more common variables include int , char , and float . Each variable will store a different type of data.
float x ; character name ; _ int a , b , c , d ;
- Note that you can declare multiple variables on the same line, as long as they are of the same type. You just need to separate the variable names with commas.
- Like many lines in C, each line declaring a variable needs to end with a semicolon.
#include <stdio.h> int main () { int x ; printf ( "Enter a number:" ); scanf ( "%d" , & x ); printf ( "You entered %d" , x ); getchar (); return 0 ; }
- The string "%d" tells scanf to look for integers in user input.
- The & before the variable x tells scanf where to find the variable to replace it, and stores the integers in the variable.
- The last command printf reads the input integer back to the user.
x = 3 * 4 ; /* set "x" to 3 * 4, or 12 */ x = x + 3 ; /* adds 3 to the original value of "x", and sets the new value as the variable */ x == 15 ; /* checks whether "x" is equal to 15 */ x < 10 ; / * checks if the value of " x " is less than 10 * / _ _
Use conditional statements
- TRUE and FALSE in C will work differently than you might be used to. The TRUE statement always ends with a non-zero number. When you do the comparison, if the result is TRUE then “1” will be returned. If the result is FALSE, return “0”. Understanding this point will help you understand how IF statements are handled.
> /* greater than */ < /* less than */ >= /* greater than or equal to */ <= /* less than or equal to */ == /* equals */ != /* not equal to */
10 > 5 TRUE 6 < 15 TRUE 8 >= 8 TRUE 4 <= 8 TRUE 3 == 3 TRUE 4 != 5 TRUE
#include <stdio.h> int main () { if ( 3 < 5 ) printf ( "3 is less than 5" ); getchar (); }
#include <stdio.h> int main () { int age ; printf ( "Please enter your current age: " ); scanf ( "%d" , & age ); if ( age <= 12 ) { printf ( "You're a kid! n " ); } else if ( age < 20 ) { printf ( "Being a teenager is great! n " ); } else if ( age < 40 ) { printf ( "You're still young! n " ); } else { printf ( "The older the ginger, the spicier it gets. n " ); } return 0 ; }
[2] X Research Source
- The program takes data from the user and passes it through IF statements. If the metric meets the first statement, the first printf statement is returned. If it fails to respond to the first statement, it is passed through ELSE IF statements until it finds a match. If it doesn’t match any statements, it will go through the ELSE statement at the end.
Learn Loops
- There are three main types of loops: FOR, WHILE, and DO … WHILE.
#include <stdio.h> int main () { int y ; for ( y = 0 ; y < 15 ; y ++ ;){ printf ( "%d n " , y ); } getchar (); }
- In the above program, y is set to 0, and the loop continues running as long as the y value is less than 15. Each time the y value is printed, the y value will be added by 1 and the loop will be repeated. again. When y = 15, the loop will be broken.
#include <stdio.h> int main () { int y ; while ( y <= 15 ){ printf ( "%d n " , y ); y ++ ; } getchar (); }
- The y++ instruction will add 1 to the variable y each time the loop is executed. When the variable y reaches 16 (remember, this loop will continue running as long as the y value is less than or equal to 15), the loop is stopped.
#include <stdio.h> int main () { int y ; y = 5 ; do { printf ( "Loop is running! n " ); } while ( y != 5 ); getchar (); }
- This loop will display the message even though the condition is FALSE. The variable y is set to 5 and the WHILE loop is set to run when y is not 5, so the loop terminates. The message is printed from when the condition is unchecked until the end.
- The WHILE loop in the DO…WHILE setup must be terminated with a semicolon. This is the only time a loop ends with a semicolon.
Using functions
- The main() line at the top of all the examples above is a function, for example getchar()
- Functions are essential to making code efficient and readable. Make good use of functions to organize your program.
int plus ( int x , int y );
- This will create a function that adds two integers ( x and y ) together and then returns the sum which is also an integer.
#include <stdio.h> int plus ( int x , int y ); int main () { int x ; int y ; printf ( "Enter the two numbers you want to add together: " ); scanf ( "%d" , & x ); scanf ( "%d" , & y ); printf ( "The sum of your numbers is %d n " , plus ( x , y ) ); getchar (); } int plus ( int x , int y ) { return x + y ; }
- Note that the outline is still at the top of the program. This tells the compiler what to expect when the function is called and what the result should be. This is only necessary if you want to define a function at the end of the program. You can put the add() (plus) function before the main() function and the result will be the same without the outline.
- The actual function of the function is defined at the end of the program. The main() function collects integers from the user and then sends them to the add() function for processing. The add() function performs the addition function and then returns the result to main()
- At this point add() has been defined, which can be called anywhere in the program.
Continue to dig deeper
- Attend some hack-a-thons if possible. These are events where groups and individuals come up with programs and solutions, and often spur creativity within deadlines. You can meet a lot of good programmers this way, and hack-a-thon competitions are often held around the globe.
Advice
- Always add notes to your program. This section not only makes its source code visible to others, but also helps you remember what you’re writing and why you’re writing it. Right at the time of coding, you may know well what you are writing it for, but after two or three months, you probably won’t remember much about the purpose and reason for knowing the code.
- Always remember to end a statement like printf(), scanf(), getch() etc with a semicolon (;) but never insert it after a control statement like ‘if’, ‘while’ or ‘for’.
- When you get a syntax error while compiling, if you’re confused, look up the error you’re getting on Google (or another search engine). Chances are someone has had the same problem as you and posted a solution.
- Your source code needs to have the *.c extension for the compiler to understand that it’s a C source file.
- Has iron grinding makes perfect. The more you practice writing programs, the better you will become. So start with simple and short programs until you become more proficient and confident then you can move on to more complex programs.
- Try to learn how to build logic. It helps to solve various problems while coding.
wikiHow is a “wiki” site, which means that many of the articles here are written by multiple authors. To create this article, 54 people, some of whom are anonymous, have edited and improved the article over time.
This article has been viewed 4,034 times.
The C programming language is one of the oldest programming languages. This language was developed in the 70s, but it is still very powerful today thanks to its low-level language characteristics. Learning C is also a great way to educate yourself about more complex languages; in addition, the knowledge gained will be useful in most programming languages and can help you with application development. To learn how to get started programming in C, see Step 1 below.
Thank you for reading this post How to Learn Coding at Tnhelearning.edu.vn You can comment, see more related articles below and hope to help you with interesting information.
Related Search: