Learning syntax in C programming will make you understand how to code C in a better way, making you less prone to syntax errors when coding. Below are some conventions in writing C programming that you need to know:
- Characters Syntax in C. The letters used in C are alphabet uppercase letters (A - Z), lowercase letters (a-z), digits (0-9), and special characters in C (constants, variables, operators, expressions). The syntax could be a combination of escape sequence characters, such as '\n' (newline).
- Naming Syntax. There are rules when naming an identifier in C such as variable, array or records.
- The identifier can only consist of letters and digits, and the first character cannot be digits; otherwise the compiler won't treat it as a value.
- The letters can be uppercase or lowercase; most programmer stick with lowercase but it's up to you.
- Case-sensitive, which means a variable named var1 is different than when it is named Var1.
- Using underscore characters is permitted.
- Some rules before using the variables. Declaration associates a group of variables with a specific data type. All variables must be declared before they can appear in executables statements. A declaration consists of a data type, followed by one or more variables names, ending with a semicolon. Example declarations of variables:
- int name, x, a, var1, var2, var_3; /* variable */
- char *value1,character; /* array */
- Expressions in C. The expression in C is a piece of single data, like number or character. It could be a combination of characters by operators; it could also be a logical condition, true or false. Example of expressions:
- a + b;
- c = d;
- result = x + y;
- a == b
Operators in C. Operators are used with operands to build expressions. Following is an expression containing two operands and one operator.
1 + 2
C contains the following operator groups :
- Operator Precedence.
6. Comments. A comment in C starts with a /* and ends with */.
Example comment in C:
/* hi, im comments, do use me when explaining your code (just for your reference) */
7. Statements. C has three types of statement.
- assignment =
- selection (branching)
These are all the rules syntax when you are programming in C. Just make sure when you declare a variable, name it according to what it is intended to do in the program, so you know its purpose when you recode again. Also the expression shouldn't be too complex like writing all in one bracket; try to separate it to make it clearer. That's it! Just be careful with your code or you will find a bug that resulted from your own mistake when naming the variable.