Skip to content

Commit c0d3e3d

Browse files
authored
Update C_syntax.md
complete for a good start
1 parent 30badbd commit c0d3e3d

File tree

1 file changed

+107
-11
lines changed

1 file changed

+107
-11
lines changed

LearningC/C_Language_Shorts/C_syntax.md

Lines changed: 107 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,47 @@
33
C is a language with static strong typing,
44
This means that you cannot change a variable's type or size once you've declared it.\
55
You can only use variables you previously declared (or included from other files)
6-
### Index
6+
7+
## Instructions and blocs
8+
>[!IMPORTANT]
9+
> every instruction ends with a **semi-colon** **;**\
10+
> a bloc contains instructions and is delimted by **{** and **}**\
11+
> variables defined inside a block, are not known outside of this bloc\
12+
> ==> there will be a separate part on **scope of variables**
13+
14+
>[!TIP]
15+
> Start using a good coding style for readability right from the start
16+
> examples:
17+
> ```
18+
> for(;;){// loop other ....
19+
> ...
20+
> }
21+
> if () {// condition1
22+
> ...
23+
> }
24+
> else {// condition2
25+
> ...
26+
> }//endif
27+
>
28+
> ```
29+
>
30+
31+
>[!NOTE]
32+
> Use comments to document immediately
33+
34+
## Index
735
836
> [Data Types](#variable-types)
937
> > numbers \
1038
> > arrays \
11-
> > struct
39+
> > struct \
40+
> > union
1241
>
1342
>
1443
> [Operators](#operators)
1544
>
1645
> [Conditional Statements](#conditional)
17-
> > if / then / else \
46+
> > if / else \
1847
> > switch / case
1948
>
2049
> [Loop Statements](#loop)
@@ -75,12 +104,32 @@ If you want to know how many bits they are, you can call the _sizeof() function_
75104
Records of homogenous data. Technically it's like a **list**\
76105
The classic example would be **char\* argv[]**\
77106
which holds the list of parameters/arguments passed to a program.
107+
```
108+
char buffer[26] = "the dark side of the moon";
109+
buffer[2] : would be "e", because indices start at 0
110+
```
78111
79112
### Struct
80113
When we need several variables to describe an entitiy, we use **struct** (records of heterogenous data)
114+
```
115+
struct{
116+
float x = 0.707;
117+
float y = 0.707;
118+
} point; // defines point as a struct containing 2 real values (coordinates)
119+
point.x is 0.707 (sqrt(2)/2 ;-))
120+
```
81121
82122
### Unions
83-
Sometimes we need alternatives. A **union** could consist of a number and the text "Not Defined"
123+
Sometimes we need alternatives. A **union** could consist of a number and the text "Not Defined" \
124+
Both variables are stored in the same memory location, so I can have one or the other \
125+
Perfect in this case:
126+
127+
```
128+
union test{
129+
char* status;
130+
int value ;
131+
};
132+
```
84133
85134
### Type defined
86135
You can define your own types with **_typedef_**
@@ -90,7 +139,13 @@ banaba bigNum = 1329:
90139
```
91140
92141
### Other/Any
93-
void: no variable/value
142+
```
143+
void : no variable/value
144+
<datatype>* p : is a pointer to variable of type <datatype>
145+
pointer : an address in memory
146+
*pointer : value stored at this address
147+
&<variable> : memory address of <variable>
148+
```
94149
95150
## Operators
96151
@@ -129,7 +184,8 @@ a <op> b, where <op> os one of:
129184
```
130185
A.a where A is a struct
131186
returns elemement a
132-
*A.a == A->a : where a is a pointer to a struct
187+
*A.a == A->a : where A is a pointer to a struct
188+
returns elemement a
133189
A[i] where A is an array
134190
returns the i-th element of A
135191
```
@@ -139,10 +195,13 @@ A[i] where A is an array
139195
a<op>b, where <op> os one of:
140196
& (and) | (or) ^ (Xor) ~ (not)
141197
```
198+
These are extremely important for configuring µControllers,\
199+
Ther will be an exercise just for this topic
200+
142201
### logic
143202
```
144203
a<op>b, where <op> os one of:
145-
&& (and) || (or) ^ (Xor) ! (not)
204+
&& (and) || (or) ^ (xor) ! (not)
146205
```
147206
148207
## Functions
@@ -182,18 +241,55 @@ When you want to create a function you put the prototype at the beginning of the
182241
Conditional statements are only executed under certain conditions
183242
184243
### if-then-else
185-
244+
```
245+
if (condition1)
246+
{}
247+
else if (condition2)
248+
{}
249+
...
250+
else
251+
{}
252+
```
186253
### switch-case
254+
```
255+
switch (<test>):
256+
case (confition1):
257+
...
258+
break;
259+
case (confition2):
260+
...
261+
break;
262+
case (confition3):
263+
...
264+
break;
265+
...
266+
default:
267+
...
268+
break;
269+
```
187270
188271
## Loop
189272
Loops run several times depending on an exit condition
190273
191274
### while-do
192-
275+
```
276+
while (<condition>) do {}
277+
only if condition true
278+
checks condition BEFORE execution
279+
do {} while (<condition>)
280+
at least once
281+
checks condition AFTER execution
282+
```
193283
### for
194-
284+
```
285+
for(iterator;end_condition;increment){}
286+
for(int i = 0; i<10; i++){}
287+
```
195288
### break and continue
196-
289+
```
290+
break;// get out of the loop (if some condition is met)
291+
continue;// go to next iteration of the loop (if some condition is met)
292+
```
197293
198294
## Preprocessor Directives
199295
They start with a hash **#** and should be declared at the top of the file.

0 commit comments

Comments
 (0)