Basics of C Programming!
Tags: c, beginners, programming
Published: 2021-12-05 19:37:43

There is nothing worth learning like C programming which ideally provides you with the basic knowledge of other programming languages whereas some term it to be the Latin of other programming languages.
The execution of a C program begins from the main() function whereas running from top to bottom. So you can define a program as:

```
#include <stdio.h>
#include <stdlib.h>
int main(){
}
```

The first two declared statements with "#include" which is an import statement, importing a standard input-output header file plus the standard library header file, and yet the third statement is the main function declared.
**NB: How does a C program work?**
* All valid C programs must contain the main() function.
* Code execution begins from the start of the main() function.
However, we believe that C programming is strongly typed if we try changing the return type of the main function, no error raises because the compiler doesn't treat it as an error since, prior to the 1999 standard, C did permit the return type of a function to be omitted and it would default to int. Then with C++ which is derived from C, so early or pre-standard versions of C++ had the same rule, where omitting the return type is an error.

## **Comments:**

```
/*
Multiple lines commenting
This allows us to take notes for our program
*/
// single of line commenting

```

## **Format Specifiers:**

```
int - represented by %d
char - represented by %c
float - represented by %f
double - represented by %lf
Sample:
int age = 18;
printf("%d", age); // There are many Format Specifiers you can 
use as we are going to see
```

## **Escape sequence:**

```
new line - represented by \n
sample:
printf("\nManuel");
printf("Manuel\'s Name");
```
A sequence of characters that does not represent itself when used inside a character or string literal, but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly. Escape sequences consist of two or more characters, the first of which is the backslash "\"

## **Printf:**
In C programming, printf() is one of the main output functions. The function sends formatted output to the screen.
The printf() is a library function to send formatted output to the screen. The function prints the string inside quotations. To use printf() in our program, we need to include stdio.h header file using the #include <stdio.h> statement.

```
printf("Manuel");
```

## **Scanf:**

```
printf("Enter any Number: ");
scanf("%d", &num2);
printf("You enter %d", num2);
```
**NB:** In C programming, scanf() is one of the commonly used function to take input from the user. 
* The scanf() function reads formatted input from the standard input such as keyboards. Notice, that we have used &num2 inside scanf(). It is because &num2 gets the address of num2, thus the value entered by the user is stored in that address.

## **Variables:**

```
float number1 = 11.6;
 double number2 = 13.8;
 printf("number1 = %f", number1);
 printf("number1 = %.1f", number1); // this prints the variable 
to one decimal place.
 printf("number2 = %lf", number2);
 char letter = 'A';
 printf("%c", letter);
 //The value of a variable can be changed.
 letter = 'K';
 printf("%c", letter);
```
**Rules for naming a variable:**
 * A variable name can only have letters, digits, and underscore.
 * The first character of a variable name must be either a letter or an underscore.
 * There is no rule on how long a variable name can be, however, you may run into problems in some compilers if the variable name is longer than 31 characters.

## **Constant:**

```
const int age = 18;
//Here, age is a constant of int type, here,the constant/variable name is assigned an integer value 18
This will raise an error if we try re-assigning the constant value to 17:
const int age = 18;
age = 17;
printf("%c", age);
```

## **Operators:**

```
•Mathematical: +, -, /, *, %
•Assignment: =, and so on
•Relational: >, >=, <, <=, ==, !=
•Conditional: ?:
•Logical: !, &&, ||
•Bitwise: &, |, ^, <<, >>, ~
•Unary: &, *, ++, --, sizeof
Example:
float num3;
double num4;
printf("Enter a number: ");
scanf("%f", &num3);
printf("Enter another number: ");
scanf("%lf", &num4);
printf("num3 + num4 = %lf", num3+num4);
```

## **Conditional Statement:**
**How if statement works?**
The if statement evaluates the test expression inside the parenthesis ().
If the test expression is evaluated to be true, statements inside the body of if are executed.
If the test expression is evaluated to false, statements inside the body of if are not executed.

```
if(age <= 17) {
    printf("You\'re below 18 years!");
}
```
If the test expression is evaluated to be true, the body print, however, is skipped. But to check for the false, we provide the else.

```
if(age <= 17) {
    printf("You\'re below 18 years!");
}else {
    printf("You\'re above 18 years!");
}
```

## **Loops:**
**NB:** In programming, a loop is used to repeat a block of code until the specified condition is met. however if the test expression is false, the loop terminates (ends). However, the below code indicates curly brackets which are optional but recommendable to define clearly a given block for the loop. 
C programming has three types of loops:
**for loop.**

```
// for (initializationStatement; testExpression; updateStatement) { body }
int i;
for (i=1; i<11; ++i){
    printf("%d", i);
    printf("\n");
}
```
**while loop.**

```
//while (testExpression) { body }
int j=7;
while (j>=5) {
    printf("%d", j);
    printf("\n");
    --j;
}
```
**do…while loop.**

```
//do { body }while (testExpression); 
int total=0;
int k;
do {
    printf("Enter a number: ");
    scanf("%lf", &k);
    total += k;
}while(k != 0);
printf("%d", total);
```

## **Switch Statement:**

```
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &sign);
printf("\n");
printf("Enter two operands: ");
scanf("%lf %lf", &num8, &num9);
switch (sign){
    case '+':
        printf("%.1lf + %.1lf = %.1lf",num8, num9, num8+num9);
        break;
    case '-':
        printf("%.1lf - %.1lf = %.1lf",num8, num9, num8-num9);
        break;
    case '*':
        printf("%.1lf * %.1lf = %.1lf",num8, num9, num8*num9);
        break;
    case '/':
        printf("%.1lf / %.1lf = %.1lf",num8, num9, num8/num9);
        break;
    // operator doesn't match any case constant +, -, *, /
    default:
        printf("Error! operator is not correct");
}
```
## **Functions:**

```
void checkEvenNumber(){
    int num;
    printf("Enter any Number: ");
    scanf("%d", &num);
    if(num%2 == 0) {
        printf("%d is an Even Number", num);
    }else {
        printf("%d is an Odd Number", num);
    }
}
int main(){
    checkEvenNumber();
}
```
When the compiler encounters checkEvenNumber();, control of the program jumps to And, the compiler starts executing the codes inside checkEvenNumber().
The control of the program jumps back to the main() function once the code inside the function definition is executed.