Introduction to C: Difference between revisions

From Applied Science
(Created page with "Because the discipline is an introduction, the algorithms studied doesn't use the most advanced features of C. A lot of things aren't studied in this course, just the most elementary to understand how a program is executed. Due to the similarities between mathematics and even spoken language, you just have to read a code carefully to understand the order of the operations. <div style="text-align:center;">'''A short summary about the C language'''</div> * '''Keywords....")
 
No edit summary
Line 7: Line 7:
* '''Keywords.''' They are reserved words, have a fixed and unchangeable meaning. For example: a function or variable cannot be named 'while', because 'while' already has a fixed meaning. The exception is text, we can print the word 'while' on screen because in this case, the word is treated as a char sequence, not as a command. The same applies for symbols such as commas and semicolons, the meaning is fixed and it must be used as pre-determined by the language's syntax.
* '''Keywords.''' They are reserved words, have a fixed and unchangeable meaning. For example: a function or variable cannot be named 'while', because 'while' already has a fixed meaning. The exception is text, we can print the word 'while' on screen because in this case, the word is treated as a char sequence, not as a command. The same applies for symbols such as commas and semicolons, the meaning is fixed and it must be used as pre-determined by the language's syntax.


* '''The assignment operation.''' In the very beginning, to read a = 2 as ''"a is equal to two"'' is ok, because the variable 'a' has the value two after the assignment. But when we do arithmetic operations, then we have to read the assignment operation properly: ''"the value two is assigned to the variable a".''
* '''The assignment operation.''' In the very beginning, to read a = 2 as ''"a is equal to two"'' is ok, because the variable 'a' has the value two after the assignment. But when we do arithmetic operations, then we have to read the assignment operation properly: ''"the value two is assigned to the variable a".''<br /><br />'''For example:'''<br />a = b + c;<br /><br />This is read right to left, first the operation ''''b + c'''' is processed, then the resulting value is assigned to the variable ''''a'''.
 
* '''Variable declaration.''' It would be boring to program having to memorize memory positions all the time. To provide a more comfortable way of using the computer's memory the programming language exposes for us the variable's names, much easier to understand and to memorize than numerical codes.  The var needs a type, else it would be confusing to have all variables with just one type for all. An analogy: it'd be confusing to store liquids in boxes and it'd be a waste to use a very large box to store a very small object.
 
<div style="margin-left: 1.5em;">
/* Variable declaration */<br />
int var;<br />
float var2;<br />
double var3;
 
/* Variable declaration including assignment of some value */<br />
int var = 2;<br />
float var2 = 4.5;
 
/* Precision loss, the digits of the non integer part are lost */<br />
int var = 2.5;
 
/* Mixing types. The type with the highest precision dominates the expression. In this example, float has preference over the integer 2. The result of this operation is 2.5 */<br />
float a;<br />
float b = 5;<br />
int c = 2; <br />
a = b/c;
 
/* Special case of dominance: even though the var "a" is float, the result  is going to be 2.0 because the quotient operation is done first and with  integers, the assignment operation is done later. */<br />
float a;<br />
int b = 5;<br />
int c = 2;<br />
a = b/c;
</div>
 
* '''The comma ','.''' In the same way we separate examples of a list with commas, variables are separated with commas in a program. They are also used to separate parameters and arguments of a function. The compiler doesn't "see" blank spaces, therefore int a,b,c; declares 3 variables of the int type, the absence or not of blank spaces before or after the commas is a matter of readability.

Revision as of 20:30, 14 April 2022

Because the discipline is an introduction, the algorithms studied doesn't use the most advanced features of C. A lot of things aren't studied in this course, just the most elementary to understand how a program is executed. Due to the similarities between mathematics and even spoken language, you just have to read a code carefully to understand the order of the operations.


A short summary about the C language


  • Keywords. They are reserved words, have a fixed and unchangeable meaning. For example: a function or variable cannot be named 'while', because 'while' already has a fixed meaning. The exception is text, we can print the word 'while' on screen because in this case, the word is treated as a char sequence, not as a command. The same applies for symbols such as commas and semicolons, the meaning is fixed and it must be used as pre-determined by the language's syntax.
  • The assignment operation. In the very beginning, to read a = 2 as "a is equal to two" is ok, because the variable 'a' has the value two after the assignment. But when we do arithmetic operations, then we have to read the assignment operation properly: "the value two is assigned to the variable a".

    For example:
    a = b + c;

    This is read right to left, first the operation 'b + c' is processed, then the resulting value is assigned to the variable 'a.
  • Variable declaration. It would be boring to program having to memorize memory positions all the time. To provide a more comfortable way of using the computer's memory the programming language exposes for us the variable's names, much easier to understand and to memorize than numerical codes. The var needs a type, else it would be confusing to have all variables with just one type for all. An analogy: it'd be confusing to store liquids in boxes and it'd be a waste to use a very large box to store a very small object.

/* Variable declaration */
int var;
float var2;
double var3;

/* Variable declaration including assignment of some value */
int var = 2;
float var2 = 4.5;

/* Precision loss, the digits of the non integer part are lost */
int var = 2.5;

/* Mixing types. The type with the highest precision dominates the expression. In this example, float has preference over the integer 2. The result of this operation is 2.5 */
float a;
float b = 5;
int c = 2;
a = b/c;

/* Special case of dominance: even though the var "a" is float, the result is going to be 2.0 because the quotient operation is done first and with integers, the assignment operation is done later. */
float a;
int b = 5;
int c = 2;
a = b/c;

  • The comma ','. In the same way we separate examples of a list with commas, variables are separated with commas in a program. They are also used to separate parameters and arguments of a function. The compiler doesn't "see" blank spaces, therefore int a,b,c; declares 3 variables of the int type, the absence or not of blank spaces before or after the commas is a matter of readability.