Last active
April 1, 2020 10:57
-
-
Save elowy01/a6fbf247719922f1aa3d986f023fe4b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Environment variables: | |
| C_INCLUDE_PATH=/opt/gdbm-1.8.3/include | |
| export C_INCLUDE_PATH | |
| / Link path: | |
| LIBRARY_PATH=/opt/gdbm-1.8.3/lib | |
| export LIBRARY_PATH | |
| // hello world | |
| #include <stdio.h> | |
| int main() { | |
| printf("Hello, World!\n"); | |
| return 0; | |
| } | |
| / Now, we can compile: | |
| gcc main.c // it will produce a file named a.out that can be executed by doing ./a.out | |
| // | |
| # Print a float | |
| int main() { | |
| float slope = 0.2; | |
| printf("Slope: %f\n", slope); | |
| return 0; | |
| } | |
| # Print an int | |
| int main() { | |
| int anumber = 1; | |
| printf("%d\n", anumber); | |
| return 0; | |
| } | |
| ################################### | |
| ##### Structs: | |
| // A variable declaration with structure declaration. | |
| struct Point | |
| { | |
| int x, y; | |
| } p1; // The variable p1 is declared with 'Point' | |
| // A variable declaration like basic data types | |
| struct Point | |
| { | |
| int x, y; | |
| }; | |
| int main() | |
| { | |
| struct Point p1; // The variable p1 is declared like a normal variable | |
| } | |
| ## Structure members can be initialized using curly braces ‘{}’. For example, following is a valid initialization. | |
| struct Point | |
| { | |
| int x, y; | |
| }; | |
| int main() | |
| { | |
| // A valid initialization. member x gets value 0 and y | |
| // gets value 1. The order of declaration is followed. | |
| struct Point p1 = {0, 1}; | |
| } | |
| # How to access structure elements? | |
| # Structure members are accessed using dot (.) operator. | |
| #include<stdio.h> | |
| struct Point | |
| { | |
| int x, y; | |
| }; | |
| int main() | |
| { | |
| struct Point p1 = {0, 1}; | |
| // Accessing members of point p1 | |
| p1.x = 20; | |
| printf ("x = %d, y = %d", p1.x, p1.y); | |
| return 0; | |
| } | |
| # What is designated Initialization? | |
| # Designated Initialization allows structure members to be initialized in any order. This feature has been added in C99 standard. | |
| #include<stdio.h> | |
| struct Point | |
| { | |
| int x, y, z; | |
| }; | |
| int main() | |
| { | |
| // Examples of initialization using designated initialization | |
| struct Point p1 = {.y = 0, .z = 1, .x = 2}; | |
| struct Point p2 = {.x = 20}; | |
| printf ("x = %d, y = %d, z = %d\n", p1.x, p1.y, p1.z); | |
| printf ("x = %d", p2.x); | |
| return 0; | |
| } | |
| # What is an array of structures? | |
| # Like other primitive data types, we can create an array of structures. | |
| #include<stdio.h> | |
| struct Point | |
| { | |
| int x, y; | |
| }; | |
| int main() | |
| { | |
| // Create an array of structures | |
| struct Point arr[10]; | |
| // Access array members | |
| arr[0].x = 10; | |
| arr[0].y = 20; | |
| printf("%d %d", arr[0].x, arr[0].y); | |
| return 0; | |
| } | |
| ######### | |
| #goog tutorial on arrays: | |
| https://www.programiz.com/c-programming/c-arrays |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment