/* * based on https://github.com/chmike/fpsqrt * Christophe Meessen 1993 */ #include <stdio.h> #include <stdint.h> #include <math.h> #define SHIFT (1 << 16) int32_t sqrtF2F(int32_t x) { uint32_t q = 0; uint32_t b = (1 << 30); uint32_t r = x; while( b > 0x40 ) { uint32_t t = q + b; if( r >= t ) { r -= t; q = t + b; // equivalent to q += 2*b } r <<= 1; b >>= 1; } q >>= 8; return q; } int main(int argc, char* argv[]) { for (int32_t i = 0; i < 1 * SHIFT; i += 64) { int32_t result = sqrtF2F((int32_t) (i)); double in = ((double) i) / SHIFT; double out = ((double) result) / SHIFT; printf("sqrt(%.4f) = %.4f\n", in, out); int32_t test = sqrt(in) * SHIFT; if (result != test) { printf("error\n"); break; } } return 0; }
#include <stdio.h> #include <stdlib.h> #define LINHA 3 #define COLUNA 4 void matriz_ponteiro(int **mtr, size_t linhas, size_t colunas) { for (int lin = 0; lin < linhas; lin++) { for (int col = 0; col < colunas; col++) { printf("\t%d", mtr[lin][col]); } printf("\n"); } } int main () { int **mtr = malloc(LINHA * sizeof(*mtr)); int cont = 0; for (int lin = 0; lin < LINHA; lin++) { mtr[lin] = malloc(COLUNA * sizeof(*mtr[lin])); for (int col = 0; col < COLUNA; col++) { mtr[lin][col] = cont++; } } matriz_ponteiro(mtr, LINHA, COLUNA); }
//#include <stdio.h> void getAlphabet(char *alphabet) { char symbol = 'a'; int i = 0; while (symbol <= 'z') { alphabet[i++] = symbol++; } } void itmo (int ********snt) { ********snt = 118; } int main() { char arr[26]; getAlphabet(arr); /*int i; for (i = 0; i < 26; i++) { printf("%c ", arr[i]); }*/ int snt = 0; int* snt1 = &snt; int** snt2 = &snt1; int*** snt3 = &snt2; int**** snt4 = &snt3; int***** snt5 = &snt4; int****** snt6 = &snt5; int******* snt7 = &snt6; itmo(&snt7); //printf("\n%d", snt); return 0; }
#include <stdio.h> void alphabetPrinter() { printf("Lower case alphabet: \n"); for (char i = 'a'; i <= 'z'; i++) { printf("%c", i); } printf("\nUpper case alphabet: \n"); for (char i = 'A'; i <= 'Z'; i++) { printf("%c", i); } } int main() { printf("It's C99, so let's display alphabet with my rules!\n\n"); alphabetPrinter(); return 0; }
#include <stdio.h> typedef struct { int id; char nome[201]; char endereco[101]; char cidade [31]; } Pessoa; void preencherPessoas(Pessoa pessoas[], int qtde) { for (int i = 0; i < qtde; i++) { printf(" Digite o ID do funcionário: "); scanf("%d", &pessoas[i].id); printf(" Digite a nome do funcionário: "); scanf("%s", pessoas[i].nome); printf(" Digite o endereco do funcionário: "); scanf("%s", pessoas[i].endereco); printf(" Digite o cidade do funcionário: "); scanf("%s", pessoas[i].cidade); printf(" DADOS DO FUNCIONARIO DE ID %d CADASTRADOS! \n", pessoas[i].id); } } int main() { printf("Insira a quantidade de funcionários que serão cadastrados: "); int qtde; scanf("%d", &qtde); printf("\nQTD: %d", qtde); Pessoa pessoas[qtde]; preencherPessoas(pessoas, qtde); } //https://pt.stackoverflow.com/q/363861/101
#include <stdio.h> #include <stdlib.h> int main(){ char *ponteiro = malloc(100); printf("String: "); scanf("%99s", ponteiro); free(ponteiro); } //https://pt.stackoverflow.com/q/45796/101
#include <stdio.h> int main() { int b = 3; int arr[] = {6,7,8,9,10}; int *ptr = arr; *(ptr++)+=123; printf("%d, %d\n", *ptr,*(++ptr)); return 0; }
#include <unistd.h> #include <stdio.h> void my_putchar(char c) { write(1, &c, 1); } int my_strlen(char *str) { int i; i = 0; while ( str[i] != '\0' ) { i = i + 1; } return (i); } void my_putnbr(int n) { if (n > -2147483648 ) { if (n < 0) { n = -n; my_putchar(45); } if (n > 9) { my_putnbr(n / 10); my_putnbr(n % 10); } if (n <= 9 && n >= 0) my_putchar(n + 48); } else if (n == -2147483648) write(1, "-2147483648", 11); } void my_putnbr_base(int n, char *base) { int tb; tb = my_strlen(base); if (n < 0) { n = -n; my_putchar(45); } if (n >= 9) { my_putnbr_base(n / tb, base); my_putchar(base[n % tb]); } if (n <= 9 && n >= 0) my_putchar(base[n]); } int main(void) { my_putnbr_base(345678, "0123456789ABCDEF"); my_putchar('\n'); my_putnbr_base(35, "01"); my_putchar('\n'); my_putnbr_base(34565, "!))\\n"); my_putchar('\n'); my_putnbr_base(-35, "01"); my_putchar('\n'); my_putnbr_base(-35, "0123456789"); my_putchar('\n'); my_putnbr_base(0, "01"); my_putchar('\n'); }
#include <stdlib.h> #include <stdint.h> #define MEM_BLOCK_SIZE (512) #define MEM_BLOCK_COUNT (8) #if __WORDSIZE == 64 #define MIN_BLOCK_SIZE 8 #elif __WORDSIZE == 32 #define MIN_BLOCK_SIZE 4 #else #define MIN_BLOCK_SIZE 2 #endif #if MEM_BLOCK_SIZE < MIN_BLOCK_SIZE #error "Block size is too small" #endif static uint8_t mem_pull[(MEM_BLOCK_COUNT * MEM_BLOCK_SIZE) + 1]; static void *block_free; static void *block_last; // extern functions can be included using other way. // For example, as include or weak, or using set callback functions, etc. extern void alloc_block_failed_callback(); extern void rtos_enter_critical(); extern void rtos_exit_critical(); void alloc_init() { block_last = &(((uint8_t *)mem_pull)[MEM_BLOCK_COUNT * MEM_BLOCK_SIZE]); block_free = mem_pull; uint32_t block_count = MEM_BLOCK_COUNT; // Link all free blocks void *mem = mem_pull; while (--block_count != 0U) { void *block = &((uint8_t *)mem)[MEM_BLOCK_SIZE]; *((void **)mem) = block; mem = block; } *((void **)mem) = NULL; } /** * Allocate memory block of @ref MEM_BLOCK_SIZE bytes * @return Allocated memory block pointer or NULL */ void * alloc_block() { void * block = block_free; if (block != NULL) { block_free = *((void **)block); } else { alloc_block_failed_callback(); } return block; } /** * Free memory block allocated using @ref alloc_block * @note Free memory is not part of test task */ void free_block(void * p_block) { if ((p_block < (void*)mem_pull) || (p_block >= (void*)block_last)) { return; } *((void **)p_block) = block_free; block_free = p_block; } // ============================================================================= #include <stdio.h> int main(void) { alloc_init(); for (int q = 0; q < MEM_BLOCK_COUNT + 1; q++) { uint8_t * p_block = alloc_block(); printf("block addr = %p\n", p_block); } } void alloc_block_failed_callback(void * arg) { printf("No memory\n"); } void rtos_enter_critical() { } void rtos_exit_critical() { }
#include <stdio.h> void main(){ unsigned int array[3] = {10, 20, 30}; unsigned int*p1,*p2, *p3; p1 = &array[0]; p2 = &array[1]; p3 = &array[2]; printf("p1 = %d\n", p1); p1 = p1+3; printf("p1+3 = %d\n", p1); printf("p2 = %d\n", p2); ++p2; printf("++p2 = %d\n", p2); printf("p3 = %d\n", p3); --p3; printf("--p3 = %d\n", p3); }
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more