For Windows, try Microsoft Visual Studio Express or MinGW. For Mac, XCode is one of the best C compilers. For Linux, gcc is one of the most popular options.
C is essentially comprised of functions, and in these functions you can use variables, conditional statements, loops to store and manipulate data.
The #include command occurs before the program starts, and loads libraries that contain the functions you need. In this example, stdio. h lets us use the printf() and getchar() functions. The int main() command tells the compiler that the program is running the function called “main” and that it will return an integer when it is finished. All C programs run a “main” function. The {} indicate that everything inside them is part of the function. In this case, they denote that everything inside is a part of the “main” function. The printf() function displays the contents of the parentheses on the user’s screen. The quotes ensure that the string inside is printed literally. The \n sequence tells the compiler to move the cursor to the next line. The ; denotes the end of a line. Most lines of C code need to end with a semicolon. The getchar() command tells the compiler to wait for a keystroke input before moving on. This is useful because many compilers will run the program and immediately close the window. This keeps the program from finishing until a key is pressed. The return 0 command indicates the end of the function. Note how the “main” function is an int function. This means that it will need an integer to be returned once the program is finished. A “0” indicates that the program has performed correctly; any other number will mean that the program ran into an error.
To comment in C place /* at the start of the comment and */ at the end. Comment on all but the most basic parts of your code. Comments can be used to quickly remove parts of your code without deleting them. Simply enclose the code you want to exclude with comment tags and then compile. If you want to add the code back, remove the tags.
Some of the more common variable types include int, char, and float. Each one stores a different type of data.
Note that you can declare multiple variables on the same line, as long as they are the same type. Simply separate the variable names with commas. Like many lines in C, each variable declaration line needs to end with a semicolon.
The “%d” string tells scanf to look for integers in the user input. The & before the variable x tells scanf where to find the variable in order to change it, and stores the integer in the variable. The final printf command reads back the input integer to the user.
TRUE and FALSE work differently in C than what you might be used to. TRUE statements always end up equaling a nonzero number. When you perform comparisons, if the result is TRUE then a “1” is returned. If the result is FALSE, then a “0” is returned. Understanding this will help you see how IF statements are processed.
The program takes the input from the user and takes it through the IF statements. If the number satisfies the first statement, then the first printf statement is returned. If it does not satisfy the first statement, it is taken through each ELSE IF statement until it finds one that works. If it doesn’t match any of them, it goes through the ELSE statement at the end.
There are three main types of loops: FOR, WHILE, and DO. . . WHILE.
In the above program, y is set to 0, and the loop continues as long as the value of y is less than 15. Each time the value of y is printed, 1 is added to the value of y and the loop is repeated. Once y = 15, the loop will break.
The y++ command adds 1 to the y variable each time the loop is executed. Once y hits 16 (remember, this loop goes as long as y is less than or equal to 15), the loop breaks.
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 does not equal 5, so the loop terminates. The message was already printed since the condition is not checked until the end. The WHILE loop in a DO. . . WHILE set must be ended with a semicolon. This is the only time a loop is ended with a semicolon.
The main() line at the beginning of all of the above examples is a function, as is getchar() Functions are essential to efficient and easy-to-read code. Make good use of functions to streamline your program.
This will create a function that adds two integers (x and y) and then returns the sum as an integer.
Note that the outline is still located at the top of the program. This tells the compiler what to expect when the function is called and what it will return. This is only necessary if you want to define the function later in the program. You could define add() before the main() function and the result would be the same without the outline. The actual functionality of the function is defined at the bottom of the program. The main() function collects the integers from the user and then sends them to the add() function to be processed. The add() function then returns the results to main() Now the add() has been defined, it can be called anywhere in the program.
Attend some hack-a-thons if possible. These are events where teams and individuals have time limits to come up with programs and solutions, and often foster a lot of creativity. You can meet a lot of good programmers this way, and hack-a-thons happen regularly across the globe.