Skip to content

Instantly share code, notes, and snippets.

@Robotawi
Last active September 1, 2022 14:28
Show Gist options
  • Save Robotawi/2919e6fcdf1dfcabc56048f27aa2df76 to your computer and use it in GitHub Desktop.
Save Robotawi/2919e6fcdf1dfcabc56048f27aa2df76 to your computer and use it in GitHub Desktop.
Summary of C language Handbook @ https://thevalleyofcode.pages.dev/c-handbook.pdf
#include <stdio.h>
// This gist summarizes the content of the C language Handbook https://thevalleyofcode.pages.dev/c-handbook.pdf
// The order of the following topics is according to their appearance in the book
int main(int argc, char *argv[])
{
printf("Printed messages are of the format\n");
printf("------------------------------\n");
printf("Topic: related knowledge or example\n");
printf("------------------------------\n");
// Overflow problems unsigned char max value is 255, but we assign to it 256
unsigned char i = 255;
i += 10;
printf("Type overflow: example with a char value > 255: %u\n", i);
// Overflow problems int max value is 2147483647, but we assign to it a huge number
int j = 32000;
j += 50000000000000;
printf("Type overflow: example with an int value > 2147483647: %d\n", j);
// The following two declaration and initialization will generate overflow warning
char k = 1000;
char l;
l = 10000;
// Define constants
const int cnst = 100;
#define cnsty 150 // no type, =, or ; are needed
// Operators
// binary operators work with two operands
// arithmetic operators = + - * / % are binary operators
// unary operators take only one operand +a, -a, ++a, --a
// logical operators are ! && and ||
// Compound assignment operators are += *= -= /=
// Ternary operator a>b ? c : d if a>b is true, c is executed, else d is executed
// Conditionals
// be careful to use ==, not = for comparision
// the following is a wrong case that you should not do
int a = 0;
if (a = 0)
{
// never invoked because 0 number always equates to a false, but any other number equates to true
printf("Conditionals: Inside if\n");
}
// Control flow uses for, while, do while, switch
// Arrays contain elements of the same type
// Strings are special kind of array of char.
/* NOTICE: String char[] size must be 1 more than the content as it needs a null terminator */
// two ways to initialize a string
char first_name[8] = {'M', 'o', 'h', 'a', 'm', 'e', 'd'};
char second_name[7] = "Raessa";
printf("C string is a char[]: Name is: %s %s\n", first_name, second_name);
// Pointers
int my_int = 10;
printf("Pointers: Memory address of my_int is %p\n", &my_int);
int *my_int_address = &my_int;
printf("Pointers: The value at my_int_address is %u\n", *my_int_address);
// Ww can iterate over an array with pointers
int numbers[5] = {10, 20, 30, 40, 50};
printf("Pointers/Arrays: Second position in the numbers array, printed with pointer, is %u\n", *(numbers + 1));
// Functions in C can not have default parameters, not like C++
/* I/O needs #include<stdio.h> */
// Streams types in C are stdin, stdout, and stderr
printf("I/O: printf writes to stdout by default\nI/O: printf needs placeholders to print values of variables. %%c for char, %%s for string, %%d for int, %%f for float, and %%p for pointer\n");
int age = 0;
printf("I/O: What is your age? ");
scanf("%d", &age); // scanf needs the address where to save the input
printf("I/O: scanf read your input as %d and it also needs a placeholder with the type of input, and the address where save the input\n", age);
printf("I/O: This is the statement that read your input age: scanf(\"%%d\",&age)\n");
// Static variables
printf("Static variables: retain their value during the program even if declared inside the scope of a function\n");
// Type definition
printf("Type definition: the 'typedef' keyword is used to define names for our types as names of other existing types\n");
printf("Type definition: typedef is used as 'typedef existing_type desired_type_name'. Example typedef int awesome_int\n");
typedef int awesome_int;
// using typedef and enum to define types
printf("Enumerated Types: We can use typedef and enum to define types that take custom values\n");
printf("Enumerated Types: The syntax is 'typedef enum {value1, value2, value3} TypeName;'\n");
typedef enum
{
red,
green,
blue
} Color;
Color default_color = red;
if (default_color == red)
{
printf("Enumerated Types: default_type is red! Have a look at line 100 in the code\n");
}
// Structures
printf("Structures: Using 'struct' we can create complex data structures using basic C types\n");
// One way to declare an object from a struct
struct Person
{
char *job;
} Mohamed;
Mohamed.job = "Engineer";
// Another way to do the same
struct Person good_name;
good_name.job = "Programmers";
printf("Structures: The object named Mohamed of type Person has a job member/field of %s\n", Mohamed.job);
// Taking command line parameters
if (argc > 1)
{
for (int i = 1; i < argc; i++)
{
printf("Taking command line parameters: The provided input number %d = %s\n", i, argv[i]);
}
}
else
{
printf("Taking command line parameters: If you want, provide input parameters and their values will be printed\n");
}
// Header files
printf("Header files: header files from the std library are included with brackets, like #include <stdio.h>\n");
printf("Header files: your custom header are included with quotes, such as #include \"myfile.h\"\n");
// Preprocessor
printf("Preprocessor: all lines starting with # are taken care of by the preprocessor\n");
//#define
printf("#define: can define both symbolic constants and macros. The difference is that macros accepts arguments, like functions\n");
printf("#define: this is how a symbolic constant is defined '#define PI 3.14'\n");
printf("#define: this is how a macro is defined '#define POWER2(x) (x * x)'\n");
#define PI 3.14
#define POWER2(x) (x * x)
printf("#define: POWER2 is a macro. The value of POWER2(5) = %d. Have a look at line 153 to know how to do\n", POWER2(5));
printf("#define: does not end with a ';' because it is taken care of by the preprocessor, not the compiler\n");
printf("#ifdef: checks if a specific symbolic constant or a macro defined\n");
printf("#ifndef: does the opposite, which is checking if a specific symbolic constant is not defined\n");
printf("#ifndef: it is common to use #ifndef #define and #endif in .h header files as include guards\n");
return 0;
}
/*
Differences from C++
The string is a char[]
Functions can not have default parameters
Struct object needs keyword 'struct' when declared
*/
@Robotawi
Copy link
Author

Robotawi commented Sep 1, 2022

Compiling and running with no inputs

gcc revise_c_langauge.c -o revise 
./revise

Gives the following output

Printed messages are of the format
------------------------------
Topic: related knowledge or example
------------------------------
Type overflow: example with a char value > 255: 9
Type overflow: example with an int value > 2147483647: -2009228032
C string is a char[]: Name is: Mohamed Raessa
Pointers: Memory address of my_int is 0x7ffe9ccec12c
Pointers: The value at my_int_address is 10
Pointers/Arrays: Second position in the numbers array, printed with pointer, is 20
I/O: printf writes to stdout by default
I/O: printf needs placeholders to print values of variables. %c for char, %s for string, %d for int, %f for float, and %p for pointer
I/O: What is your age? 32
I/O: scanf read your input as 32 and it also needs a placeholder with the type of input, and the address where save the input
I/O: This is the statement that read your input age: scanf("%d",&age)
Static variables: retain their value during the program even if declared inside the scope of a function
Type definition: the 'typedef' keyword is used to define names for our types as names of other existing types
Type definition: typedef is used as 'typedef existing_type desired_type_name'. Example typedef int awesome_int
Enumerated Types: We can use typedef and enum to define types that take custom values
Enumerated Types: The syntax is 'typedef enum {value1, value2, value3} TypeName;'
Enumerated Types: default_type is red! Have a look at line 100 in the code
Structures: Using 'struct' we can create complex data structures using basic C types
Structures: The object named Mohamed of type Person has a job member/field of Engineer
Taking command line parameters: If you want, provide input parameters and their values will be printed
Header files: header files from the std library are included with brackets, like #include <stdio.h>
Header files: your custom header are included with quotes, such as #include "myfile.h"
Preprocessor: all lines starting with # are taken care of by the preprocessor
#define: can define both symbolic constants and macros. The difference is that macros accepts arguments, like functions
#define: this is how a symbolic constant is defined '#define PI 3.14'
#define: this is how a macro is defined '#define POWER2(x) (x * x)'
#define: POWER2 is a macro. The value of POWER2(5) = 25. Have a look at line 153 to know how to do
#define: does not end with a ';' because it is taken care of by the preprocessor, not the compiler
#ifdef: checks if a specific symbolic constant or a macro defined
#ifndef: does the opposite, which is checking if a specific symbolic constant is not defined
#ifndef: it is common to use #ifndef #define and #endif in .h header files as include guards

Running with command line inputs such as

./revise C language quickly

Gives this output

Printed messages are of the format
------------------------------
Topic: related knowledge or example
------------------------------
Type overflow: example with a char value > 255: 9
Type overflow: example with an int value > 2147483647: -2009228032
C string is a char[]: Name is: Mohamed Raessa
Pointers: Memory address of my_int is 0x7ffd0c11551c
Pointers: The value at my_int_address is 10
Pointers/Arrays: Second position in the numbers array, printed with pointer, is 20
I/O: printf writes to stdout by default
I/O: printf needs placeholders to print values of variables. %c for char, %s for string, %d for int, %f for float, and %p for pointer
I/O: What is your age? 32
I/O: scanf read your input as 32 and it also needs a placeholder with the type of input, and the address where save the input
I/O: This is the statement that read your input age: scanf("%d",&age)
Static variables: retain their value during the program even if declared inside the scope of a function
Type definition: the 'typedef' keyword is used to define names for our types as names of other existing types
Type definition: typedef is used as 'typedef existing_type desired_type_name'. Example typedef int awesome_int
Enumerated Types: We can use typedef and enum to define types that take custom values
Enumerated Types: The syntax is 'typedef enum {value1, value2, value3} TypeName;'
Enumerated Types: default_type is red! Have a look at line 100 in the code
Structures: Using 'struct' we can create complex data structures using basic C types
Structures: The object named Mohamed of type Person has a job member/field of Engineer
Taking command line parameters: The provided input number 1 = C
Taking command line parameters: The provided input number 2 = language
Taking command line parameters: The provided input number 3 = quickly
Header files: header files from the std library are included with brackets, like #include <stdio.h>
Header files: your custom header are included with quotes, such as #include "myfile.h"
Preprocessor: all lines starting with # are taken care of by the preprocessor
#define: can define both symbolic constants and macros. The difference is that macros accepts arguments, like functions
#define: this is how a symbolic constant is defined '#define PI 3.14'
#define: this is how a macro is defined '#define POWER2(x) (x * x)'
#define: POWER2 is a macro. The value of POWER2(5) = 25. Have a look at line 153 to know how to do
#define: does not end with a ';' because it is taken care of by the preprocessor, not the compiler
#ifdef: checks if a specific symbolic constant or a macro defined
#ifndef: does the opposite, which is checking if a specific symbolic constant is not defined
#ifndef: it is common to use #ifndef #define and #endif in .h header files as include guards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment