C
C is a low-level programming language which allows you to construct programs writing in a syntactical form. When compiled (typically using cc (short for C compiler) or gcc (GNU C compiler)), the C code will be converted into machine-readable code to execute the program.
Contents
Overview
Basic programs can be broken down into 3 main categories: variables, loops, and If/Else statements.
Basic Formatting
Each C program follows a general format.
Includes
Includes are calls from within a C program which reference a library - a database of predefined functions. They are used to provide a standardised set of functions that programmers can use rather than reinventing the wheel with each program.
Includes in C follow this syntax:
<syntaxhighlight lang="c">
//searches for library.h in the default directory of libraries
//searches for library.h in the defined path </syntaxhighlight> |
A few includes are necessary for every C program - namely stdio.h (a library defining functions to deal with basic input and output). By convention, includes are normally placed at the beginning of a program, although it is not necessary.
The main() Function
The concept of functions is beyond the scope of this introductory article, but simply put, a function is a block of code that is executed each time it is called and must return a value. There are many applications for functions, but the most important function is main().
The main() function is where the main code of a program is placed - when a program runs, main() is where the program starts executing code.
As such, a typical program may look like this:
<syntaxhighlight lang="c">
int main() { printf(%s, "Hello, world!\n"); return 0; } </syntaxhighlight> |
Variables
A variable is a value that stores data that can be edited, modified, and used at a later time. To declare a variable in the C language your first declare its type and then the variable name. Some of the basic variable types are:
int iName; float fName; double dName; char cName; |
Integer or int variables can store whole numbers while floats and doubles can hold integer values with decimal places. A char type variable can only hold a single character. while C itself does not have a string variable type you can create a array of characters refereed to as a CString to accomplish the same task.
Loops
In C there are three types of loops that allow the user to accomplish a repetitive task without repeating numerous lines of code. These three basic loops are called the for loop, the while loop, and the do while loop. Each loop has their own purpose for being used and normally follow the same syntax. All loops are based of an equation and if that equation does not evaluate to true then the looping will not halt. A for loop is good for a repetitive task that you know how many times you want to repeat, while a while loops is normally used when how many times you need to loop is unknown like when you are reading a text document. A do while loop is almost the same as a while loop except for one difference, it runs its code at least once before checking if it should stop looping
Examples:
<syntaxhighlight lang="c"> int i; for(i = 0; i < 10; i++) //integer "i" equals 0. when "i" is less then 10 increment "i" by one { //code to repeat 9 times } </syntaxhighlight> |
<syntaxhighlight lang="c"> char myChar; while(myChar != 'c') //While "myChar" does not equal "c" continue to loop { scanf("%c", &myChar); //get input from the user and put it into variable "myChar" } </syntaxhighlight> |
<syntaxhighlight lang="c"> do //loop at least once { x = x + 1; //variable x equals itself plus one (if x equals 0 then x = 0 + 1) } while(x < 2); //check to see if condition to stop looping is met </syntaxhighlight> |
If/Else
If/Else statements are used when you need some way to control the flow of execution of your code. These statements are just like asking questions and depending upon if the answer is true or false the program may execute differently.
Example:
<syntaxhighlight lang="c"> if(1 == 1) //if 1 equals 1 execute the true code block { printf("This is the true code block"); //execute the true code block }else{ //if the statement is not true printf("This is the false code block"); //execute the false code block } </syntaxhighlight> |
C Visit the Compiled Languages Portal for complete coverage.
|