Adding relevante parts of the C language in to a mini manual. To have the information resumed and updated :
A basic file in C it uses the .c extension : mini_manual.c // any coding editor can create this file
A C program consist of this structure :
----------------------
#include <stdio.h> // system Libraries which are compiled
#include "standard_variables.h" // user librarys which are not compiled
// A library is a file that contain mainly other functions or variables.
char msg1; // Global variables outside main
int main() A function called main which is where the compiler\program uses as reference to know where things are or start.
{
char msg2; // local variables inside main
printf("Hello World!"); // Function call to display a msg
#include "menu.c" A include file to use funcionality directly.
// For example our menu is hevy complex and we don't want to edit all our projects that have a menu. We can just use\reuse the same menu include. When we edit the menu.c all our projects will be updated. We just need 1 updated in a file since all others link to that file. But in this case it will depend a bit on our planning. Lets say a change is needed for one project but not the others.
return 0;
}
---------------------------------
// As seen in previous example we have our libraries on the top of the file
#include <stdbool.h> // C99
#include <stdio.h> // header file type is "pre-processor" it is performed before .c files and uses .h extension in this case is a compiled system lib
#include "../includes/standard_variables.h" // user header file in a path above inside the includes folder. assumes a 2 folder structure (progs includes) comes out of 1º folder to include a file in the "includes" folder.
// And variables that can be inside or outside main
char msg[]; // Declare a string in C dinamic size
char msg1; // single character C assume 1 character if no size is give
char msg2[10]; // assumes 10 max characters long string
int myAge = 43; // Integer number no floating only positive and negative values - 2,147,483,648 to 2,147,483,647
float floating = 1.5 // floating values also called decimal
Short Byte and Boolen are not available but probably there are some kind of library to do this. If you plan to work with high amount of numbers may need better declaration because program may become to large in therms of memory use.
short value // Numbers from -32,768 to 32,767
byte value // Numbers from -128 to 127
boolean value // 1 to 0
Array
int int_array[3] = {"5", "8", "9"};
char string_array[3] = {"h", "k", "m"};
Arrays in C are not tha valuable since they don't allow mixed data.
Still a good aspect is that they can improve performance since they don't allow this.
Mix data
Structure declaration of mixed values .
struct My_Structure
{
int my_num; // Member (int variable)
char my_letter; // Member (char variable)
float float1;
}; // End the structure with a semicolon is a type of variable and not a fucntion
C does not suport object for mixed values this is as far you will get
File Manipulation
FILE *fptr
fptr = fopen(filename, mode);
FILE *fptr;w - Writes to a filea - Appends new data to a filer - Reads from a file
fptr = fopen("filename.txt", "w"); // Create a file
fclose(fptr); // Close the file
Librarys
#include <math.h> // Math functions library. it extends the basic funcionality of C width more functions.
ceil(1.4) // rounds up
floor(1.4) // rounds down
Loops
Repeats the same action X times.
Loop example in this case 5 times.
int i = 0; //
while (i < 5)
{
printf("%d\n", i); // \n is a symbol to represent new line
i++;
}
Pointers
Are important because they improve the performance, this is the reason why C is knowed to achive high level of performance. Because they allow us to manipulate the data in the computer's memory, this can reduce the code.
int* ptr_my_age = &my_age; // Pointer is declared widtht the asterisk afther the variable type. The pointer variable stores the address of myAge. Width the pointes we are not accessing the variable but the adress to the variable.
This is my guessing ... if we do not want to change a variable just to access it we gain performance by using a pointer. In this way also a constant should increase performace because is a variable that does not change. But in C we have pointers so we can achieve the same effect.
Pointers in a way are better then constants. Because if lather you decided that a variable is no longer static you have to change your code. While if you have a pointer you need to change less code.
Function
Functions in C most have the same return type. If will return a int it is a int function. If returns string is a string function.
int function_sum(int k) //
{
if (k > 0)
{
return k + sum(k - 1);
}
else
{
return 0;
}
}
typedef unsigned char byte; // think this only works on header files. It allow to set new rules for the program. it creates a 256 positive values string.
#include <errno.h>
No comments:
Post a Comment