Everything You Need To Know To Start With C
Everything You Need To Know To Start With C
You have to compile C files with a compiler (for instance gcc) to create an
executable file.
Comments
#cisfun
Comments
Begins with /* and ends with */
Syntax:
Example:
Aewe\
Declaration:
Example:
Structures
A complex data type declaration that Example
defines a physically grouped list of
variables to be placed under one name Creation of a new type called new_struct
in a block of memory
Functions:
Example
The entry point | main
Program starts with the entry point. In C it’s the main function
Syntax
Blocks
Declarations Statements
Contains:
● Blocks
● Instructions (assigning values,
computing)
● Control structures (conditional
statements, loops)
Blocks | Example
Instructions, Expressions
#cisfun
Instructions, expressions
Expression, followed by ;
; (semicolon) is a statement terminator (indicates the end of one logical entity)
Types of expressions:
Example:
Strings
“string” (double quotes)
Example:
When using strings, the computer creates an array of chars, containing the same
number of cells as the length of the string + 1, and then fills the array with the
ASCII codes of each letter of the string. In the extra cell (the last one) the
computer will store the ASCII code of the special character ‘\0’
Arithmetic operators
+ addition, - subtraction, * multiplication, / division, % modulo (integer remainder)
++ increment, -- decrement
Basic expressions | example
Using parentheses with operators
Affectations
Change the content of a variable (update the value in memory)
Syntax:
Examples:
If the expression is another affectation, then its value is the value affected to the
previous variable
Affectations | elements of arrays and structures
Syntax:
Examples:
Variables assignment | example
Comparisons
< Less than expression operator expression
<= Less or equal to
The value of a comparison is 0 if false, and
== Equal to
something else if true.
>= Greater or equal
to Examples:
> Greater than
!= Different than
Logical operators
|| OR, && AND, ! NOT
expression1 && expression2 - if one of the 2 expressions is false, then the whole
expression is false
!expression - if the expression is false, then the whole expression is true. If the
expression is true, then the whole expression is false.
0 1 0 1
0 1
0 0 0 0 0 1
1 0
1 0 1 1 1 1
Can you solve this?
Logical operators | examples
Binary operators
Bitwise operations
| OR, & AND, << LEFT SHIFT, >> RIGHT SHIFT, ^ XOR
~ NOT
Example:
expression1 += expression2, is equivalent to:
expression1 = expression1 + expression2
+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
Comma operator
expression1, expression2
Example:
Example:
sizeof
Unary operator that evaluates to the size in memory of the variable or type, in
bytes
Example:
&
&var_name
Example:
Can you solve this?
Control structures
#cisfun
if if … else
The while loop lets you repeat a block until a specified expression becomes false.
for loops
The for statement lets you repeat a block a specified number of times. The block
of a for statement is executed zero or more times until an optional condition
becomes false. You can use optional expressions within the for statement to
initialize and change values during the for statement's execution.
If used with an expression, the expression becomes the return value of the
function
The type of expression must match the return type of the function