Chapter 1. Introduction to C
Introduction
Variables
A variable is an space in memory. To define a variable in C is something like this.:
int weight = 30;
// data_type varioable_name
It is important to say that C is Strongly typed, this means that each variable or constant must have its data type and it cannot change once variable is declared.
Variables in C are mutable by default, so its value could be changed throughout the program
Constants in C are something like this
const double PI = 3.14
A constant is written using UPPER CASE.
Data types
It declares variables and constants. It defines the type and length from associated data in variables.
Data types in C
// Using integers
int
// Using decimals
float // 4 bytes
double // 8 bytes
// Characters
char
// Unsigned
// With this type we'll be able to change data storage
unsigned
Basic Structure
All program in C must have the next structure
libraries: Always imports the standard librarystdio.hmainmethod that always has a data type to return
#include <stdio.h>
int main()
{
printf("Hola mundo"); // This comes from stdio.h
return 0;
}
Run your first program
To run a C program, first we need to compile it, so we have to run a command to compile it from terminal. In Linux, to do this is using gcc <your-program>.c -o <your-program>
For example
gcc helloWorld.c -o helloWorld
# then you can run t=your program with this
./helloWorld
Print Values
In this case we are using stdio.h to print values in CLI. To index a value we will use something like this: %c:
In C, to define char data types, you must write that inside of ', instead of ":
int main()
{
char letter = 'A';
printf("Grettings: %c", letter);
}
Print multiple values
#include <stdio.h>
int main() {
int a = 0, b = 1;
printf("In statistics, the probability of something is between %d to %d", a, b);
return 0;
}
Read Values
To read values is similar to print, we need an indicator to express that we are going to enter a value:
int main()
{
int entrada_salida;
printf("Enter your favorite number: ");
scanf("%d", &entrada_salida);
return 0;
}
Now to print result we add:
int main()
{
int input_exit;
printf("Enter your favorite number: ");
scanf("%d", &entrada_salida);
printf("Your favorite number is: %d", input_exit);
return 0;
}
Identifiers
| Identifier | Data type to be applied |
|---|---|
%d or %i |
int |
%li |
long integer |
%fl |
float |
%u |
Unsigned integers |
%f |
For floating-point numbers |
%lf |
For double precision in floating-point numbers |
%c |
char |
%s |
strings |
%o |
octal integers |
%x or %X |
hexadecimal numbers |
%p |
for pointers |
%% |
Print a percentage % symbol |
Operators
In C programming language exists all operators that exist in other programming language
Arithmetic operators
| Operator | Definition |
|---|---|
+ |
Sum |
- |
Rest |
* |
Times |
/ |
Division |
% |
Modulus |
= |
equal |
Each operator follows the sign rules, for example
int main() {
float a = 10, b = -10;
// If we rest this, this will converted in a sum
// If we multiply, this will multiple the signs
float res;
res = a * b;
printf("The result is %fl", res);
// this will be -100
return 0;
}
Relational operators
| Operator | Relation |
|---|---|
| == | Same |
!= |
Different |
< |
bigger than |
> |
less than |
| => | equal or bigger than |
| <= | equal or less than |
Logical operator
| Operator | Meaning |
|---|---|
| || | or |
&& |
and |
! |
negation |
Change the datatype of a number
To change the data type, we only need to multiply the number with the datatype that we want to redefine:
For example
int main() {
int a = 5,b = 2;
float c;
// If we divide a / b the result must be 2.5, but both are integers
c = ((float) a) / b
printf("%fl\n", c);
return 0;
}
Also works if we multiply the whole equation for that datatype:
int main() {
int a = 5,b = 2;
float c;
// If we divide a / b the result must be 2.5, but both are integers
c = (float) a / b
printf("%fl\n", c);
return 0;
}