# C Programming

## main()

* All C programs should contain a **main()** function that follows the format

{% code overflow="wrap" lineNumbers="true" %}

```c
<optional return value type> main(<optional argument>) {
  <optional procedure statements or functional calls>;
}
```

{% endcode %}

{% code overflow="wrap" lineNumbers="true" %}

```c
// argc integer holds the number of arguments 
// argv holds the input arguments (strings)
// Name of the program is always stored at offset argv[0]
<optional return value type> main(int argc, char * argv[]){
}
```

{% endcode %}

## Functions

* Functions are self-contained bundles of code that can be called for execution by **main()**

{% code overflow="wrap" lineNumbers="true" %}

```c
// C Function format
<optional return value type> function name (<optional function argument>) {
}
```

{% endcode %}

{% code overflow="wrap" lineNumbers="true" %}

```c
// Simple example
#include <stdio.h>
#include <stdlib.h>

int foo() {
    return 8;
}
int main(void){
    int val_x;
    val_x = foo();
    printf("The value returned is: %d\n", val_x);
    exit(0);
}
```

{% endcode %}

## Variables

* Used in programs to store pieces of information that may change and may be used to dynamically influence the program.

| Variable Type | Use                                                 | Typical Size                                                                                                              |
| ------------- | --------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
| int           | Stores a signed integer value such as 314 or -314   | <ul><li>8 bytes for 64-bit machines</li><li>4 bytes for 32-bit machines</li><li>2 bytes for 16-bit machines<br></li></ul> |
| float         | Stores a signed floating-point number such as 3.234 | 4 bytes                                                                                                                   |
| double        | Stores a large floating-point number                | 8 bytes                                                                                                                   |
| char          | Stores a single character                           | 1 byte                                                                                                                    |

## printf

* Prints out to the screen

{% code overflow="wrap" lineNumbers="true" %}

```c
// Trwo forms of the printf command:
printf(<string>);
printf(<format string>), <list of variables/values>);
```

{% endcode %}

| Format Type | Meaning       | Example                   |
| ----------- | ------------- | ------------------------- |
| %n          | Print nothing | printf("test %n");        |
| %d          | Decimal value | printf("test %d, 123);    |
| %s          | String Value  | printf("test %s", "123"); |
| %x          | Hex value     | printf("test %x", 0x123); |
| %f          | Float         | printf("test %f", 1.308); |

{% code overflow="wrap" lineNumbers="true" %}

```c
// Format string example code
#include <stdio.h>

int main(void){
  double x = 23.5644;
  //Total width of 5 with 2 values after the floating point
  printf("The value of x is %5.2f\n", x);
  // Total width of 4 with 1 value after the floating point 
  printf("The value of x is %4.1f\n", x);
  
  return 0;
}
```

{% endcode %}

## scanf

* Generally used to get input from the user

{% code overflow="wrap" lineNumbers="true" %}

```c
// scanf format
scanf(<format string>, <list of variables/values>);

// Example - reads an integer from the user and stores it in a variable called number
scanf("%d", &number);

// You must use & before any variable with scanf
```

{% endcode %}

## strcpy/strncpy

* One of the most dangerous functions used in C.
* Purpose is to copy each character in the source string into the destination string.&#x20;
* Dangerous because there is no checking of the source's size before it is copied over to the destination.
  * If the source is is larger than space allocated for the destination, overflow conditions are likely.

{% code overflow="wrap" lineNumbers="true" %}

```c
// Format
strcpy(<destination>, <source>);
```

{% endcode %}

* **strncpy** is a safer alternative

{% code overflow="wrap" lineNumbers="true" %}

```c
// strncpy format
strncpy(<destination>, <source>, <width>);
```

{% endcode %}
