/*
 * Library: libcrc
 * File:    precalc/precalc.c
 * Author:  Lammert Bies
 *
 * This file is licensed under the MIT License as stated below
 *
 * Copyright (c) 2008-2016 Lammert Bies
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Description
 * -----------
 * The source file precalc/precalc.c contains routines to generate the lookup
 * tables for the CRC values at compile time. The tables are stored in include
 * files which are then included by the CRC routine source files
 */

#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "precalc.h"


//#define		CRC_POLY_32		0x04C11DB6ul
#define		CRC_POLY_32		0x2083B86Dul

#define		CRC_START_32		0xFFFFFFFFul


/*
 * Functions in this source file with local scope
 */
void init_crc32_tab( uint64_t* crc_tab_precalc ) {

	uint32_t i;
	uint32_t j;
	uint32_t crc;

	for (i=0; i<256; i++) {

		crc = i;

		for (j=0; j<8; j++) {

			if ( crc & 0x00000001L ) crc = ( crc >> 1 ) ^ CRC_POLY_32;
			else                     crc =   crc >> 1;
		}

		crc_tab_precalc[i] = crc;
	}

}  /* init_crc32_tab */

static int	generate_table( const char *typename, const char *filename );

/*
 * Internal table to store the CRC lookup table
 */

uint64_t	crc_tab_precalc[256];

/*
 * int main( int argc, char *argv[] );
 *
 * Precalc is a commandline utility that is called from the Makefile to
 * generate the lookup tables for the CRC routines. Generating these tables at
 * compile time is much more efficient than doing it at runtime. Most compilers
 * will be able to optimize the routines more when the tables are guaranteerd
 * to be constant than when they have been generated on the fly as variable
 * tables.
 */

int main( int argc, char *argv[] ) {

	int retval;

	if ( argc != 3 ) {

		fprintf( stderr, "\nusage: precalc --type file\n" );
		fprintf( stderr, "       where --type is any of --crc64\n\n" );

	//	exit( 1 );
	}

	retval = generate_table( argv[1], argv[2] );

	exit( retval );

}  /* main (libcrc precalc) */

/*
 * static int generate_table( const char *typename, const char *filename );
 *
 * The function generate_table() generates a CRC lookup table of a specified
 * type and stores the generated output in a file. If the function succeeds the
 * value 0 is returned. Another value is an indication of failure and that
 * value is used as the exit value of the program.
 */

static int generate_table( const char *typename, const char *filename ) {

	int a;
	int type;
	int bits;
	FILE *fp;
	const char *tabname;


	tabname = NULL;
	bits    = 1;

	 init_crc32_tab(crc_tab_precalc); 
	 tabname = "crc_tab32"; 
	 bits = 32; 
		



	printf( "const uint%d_t %s[256] = {\n", bits, tabname );

	for (a=0; a<256; a++) {

		switch ( bits ) {

			case  8 : printf( "\t0x%02"  PRIX8  "\x75",   (uint8_t)  (crc_tab_precalc[a] & 0x00000000000000FFull) ); break;
			case 16 : printf( "\t0x%04"  PRIX16 "\x75",   (uint16_t) (crc_tab_precalc[a] & 0x000000000000FFFFull) ); break;
			case 32 : printf(  "\t0x%08"  PRIX32 "\x75l",  (uint32_t) (crc_tab_precalc[a] & 0x00000000FFFFFFFFull) ); break;
			case 64 : printf(  "\t0x%016" PRIX64 "\x75ll",             crc_tab_precalc[a]                          ); break;
		}
		if ( a < 255 ) printf(  ",\n" );
		else           printf(  "\n" );
	}


	return 0;

}  /* generate_table */
 
by

C Language online compiler

Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!

Read inputs from stdin

OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.

#include <stdio.h>
int main()
{
    char name[50];
    printf("Enter name:");
    scanf("%s", name);
    printf("Hello %s \n" , name );
    return 0;
    
}

About C

C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.

Key features:

  • Structured Programming
  • Popular system programming language
  • UNIX, MySQL and Oracle are completely written in C.
  • Supports variety of platforms
  • Efficient and also handle low-level activities.
  • As fast as assembly language and hence used as system development language.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition if-else is used.

if(conditional-expression) {
   // code
} else {
   // code
}

You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to if-else-if ladder.

switch(conditional-expression) {    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
...    
    
default:     
 // code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  // code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {  
 // code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition); 

Arrays

Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.

Syntax

One dimentional Array:

data-type array-name[size];

Two dimensional array:

data-type array-name[size][size];

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

Two types of functions are present in C

  1. Library Functions:

Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,

  1. User defined functions:

User defined functions are the ones which are written by the programmer based on the requirement.

How to declare a Function

return_type function_name(parameters);

How to call a Function

function_name (parameters)

How to define a Function

return_type function_name(parameters) {  
  //code
}