Skip to content

Instantly share code, notes, and snippets.

@RedToor
Last active September 28, 2016 20:40
Show Gist options
  • Save RedToor/537a472980861e5d99a53f2cf60c2732 to your computer and use it in GitHub Desktop.
Save RedToor/537a472980861e5d99a53f2cf60c2732 to your computer and use it in GitHub Desktop.
C Language in one file
/*
C Language in one file
by RedToor - sep, 27' 2016
C - gcc review.c -o review.o -fpermissive
X - ./review.o
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#undef FILE_SIZE
#define FILE_SIZE 42
#ifndef START
#define START "C Programming"
#endif
#define Message(a, b) \
printf("\n{Message}" #a " and " #b " : Hi!\n")
#define subtract(a,b) ((a)-(b))
#define port 80
static int count = 5;
typedef int integer;
void exit(int status);
void func(void);
void *ptr;
union Data {
int i;
float f;
char str[20];
};
typedef struct MyStructure {
int id:1;
char Name[3];
int Age;
} MyStructure;
int add(int a, int b){
return a + b;
}
typedef int (*compare_func)(int a, int b);
int compare(int a, int b, compare_func compare_algorithm)
{
int result = compare_algorithm(a, b);
return result;
}
int max(int a, int b)
{
int result;
if (a > b){result = a;}else{result = b;}
return result;
}
int min(int a, int b)
{
int result;
if (a < b){result = a;}else{result = b;}
return result;
}
void func( void ) {
static int i = 5;
i++;
printf("{static Storage Class} i is %d and count is %d\n", i, count);
}
int main(int argc, const char* args[]){
const int portconst = 1000;
printf("\n(C) Language structure.\n\n");
printf("File :%s\n", __FILE__ );
printf("Date :%s\n", __DATE__ );
printf("Time :%s\n", __TIME__ );
printf("Line :%d\n", __LINE__ );
printf("ANSI :%d\n", __STDC__ );
printf("Integer Types\n");
printf("char : %dBytes (-128 to 127 or 0 to 255)\n",sizeof(char));
printf("unsigned char : %dBytes (0 to 255)\n",sizeof(unsigned char));
printf("signed char : %dBytes (-128 to 127)\n",sizeof(signed char));
printf("int : %dBytes (-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647)\n",sizeof(int));
printf("unsigned int : %dBytes (0 to 65,535 or 0 to 4,294,967,295)\n",sizeof(unsigned int));
printf("short : %dBytes (-32,768 to 32,767)\n",sizeof(short));
printf("unsigned short : %dBytes (0 to 65,535)\n",sizeof(unsigned short));
printf("long : %dBytes (-2,147,483,648 to 2,147,483,647)\n",sizeof(long));
printf("unsigned long : %dBytes (0 to 4,294,967,295)\n",sizeof(unsigned long));
printf("Floating-Point Types\n");
printf("float : %dBytes (1.2E-38 to 3.4E+38)\n",sizeof(float));
printf("double : %dBytes (2.3E-308 to 1.7E+308)\n",sizeof(double));
printf("long double : %dBytes (3.4E-4932 to 1.1E+4932)\n",sizeof(long double));
int a=10;
int b=10;
if (a==b){printf("The variable 'a' is equals to 'b' variable\n");}
for(int c=0; 2 > c;c++){printf("{for} The variable 'c' is %i\n",c);}
while(1){printf("{while} into bucle\n");break;}
switch(2) {
case 1:
printf("{switch} This A is part of outer switch\n" );
break;
case 2:
printf("{switch} This A is part of outer switch\n" );
break;
}
do {
printf("{do while} value of a: %d\n", a);
a = a + 1;
}while( a < 12 );
int cnum=2;ptr = &cnum;printf("{void *ptr} pointer Void as integer : %i\n",*((int*)ptr));
float cfloat=10.10;ptr = &cfloat;printf("{void *ptr} pointer Void as float : %f\n",*((float*)ptr));
char cchar[]="a";ptr = &cchar;printf("{void *ptr} pointer Void as char : %c\n",*((char*)ptr));
printf("{#define} Global Constant %i\n", port);
printf("Constant Keyword %i\n", portconst);
struct MyStructure *LocalStructure;
strcpy(LocalStructure->Name,"Red");
LocalStructure->Age=53;
printf("{typedef struct MyStructure} Set 'Name' %s, 'Age' %i and 'id' %d variables \n",LocalStructure->Name,LocalStructure->Age,sizeof(LocalStructure));
free(LocalStructure->Name);
free(LocalStructure);
printf("{function add} add funcion 8+8:%i\n",add(8,8));
printf("{function subtract} add funcion 4-8:%i\n",subtract(4,8));
integer number = 10;
printf("{typedef int integer} My own type variable %i\n",number);
enum boolean
{
NO,
YES
};
printf("The Boolean variable is true: %d false: %d\n", YES, NO);
int compA = compare(8, 90, max);
printf("{Pointer functions} the highest number between 8 and 90 is %d\n", compA);
while(count--) {
func();
}
int d;
d = (2 == 1) ? 20 : 30;
printf( "{ternary operator} Value of 'd' is %d\n", b );
LABEL:
printf("{goto} In label section");
goto NEXT;
goto LABEL;
NEXT:
Message("Red","TOOR");
int n[3];
for ( int i = 0; i < 3; i++ ) { n[i] = i + 100; }
for (int j = 0; j < 3; j++ ) {
printf("{array} Element[%d] = %d\n", j, n[j] );
}
char mystring[] = "Hello";
char myvar[strlen(mystring)];
strcpy(myvar, mystring);printf("{strcpy} the strings copied %s\n", myvar);
strcat(myvar, mystring);printf("{strcat} the strings copied %s\n", myvar);
printf("{strcmp} the diference is %d\n",strcmp(mystring,"Hellod"));
union Data data;
data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");
printf("{union} data.i : %d\n", data.i);
printf("{union} data.f : %f\n", data.f);
printf("{union} data.str : %s\n", data.str);
char str2[100];
printf( "{gets} Enter a value :");
gets( str2 );
printf( "{puts} You entered: ");
puts( str2 );
char str3[100];
int i;
printf("{scanf} Enter a value :");
scanf("%s %d", str3, &i);
printf("\n{printf} You entered: %s lenght %d", str3, i);
int c;
printf("\n{getchar} Enter a value :");
c = getchar( );
printf("\n{putchar} You entered: %c",putchar(c));
FILE *fp;
fp = fopen("test.txt", "w+");
fprintf(fp, "testing\n");
fputs("testing\n", fp);
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment