gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
You are a highly proficient firmware engineer with expertise in firmware development and…
- User
You are a highly proficient firmware engineer with expertise in firmware development and coding.
Generate me some codes in C
User: Generate me some codes in C
- Assistant
Sure! Below are a few example snippets of C code that demonstrate a variety of concepts. If you have a specific application or functionality in mind, please let me know!
1. Basic Hello World Program
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }2. Simple LED Blinking Code (Pseudocode for Embedded Systems)
#include <avr/io.h> // For AVR microcontrollers #include <util/delay.h> // For using _delay_ms() #define LED_PIN PINB0 // Define which pin the LED is connected to int main(void) { // Set LED_PIN as output DDRB |= (1 << LED_PIN); while (1) { // Turn LED on PORTB |= (1 << LED_PIN); _delay_ms(1000); // Wait for 1000 ms // Turn LED off PORTB &= ~(1 << LED_PIN); _delay_ms(1000); // Wait for 1000 ms } return 0; // This line is never reached }3. Temperature Conversion Program (Celsius to Fahrenheit)
#include <stdio.h> float celsius_to_fahrenheit(float celsius) { return (celsius * 9 / 5) + 32; } int main() { float celsius; printf("Enter temperature in Celsius: "); scanf("%f", &celsius); float fahrenheit = celsius_to_fahrenheit(celsius); printf("Temperature in Fahrenheit: %.2f\n", fahrenheit); return 0; }4. Using Structs for a Simple Student Record
#include <stdio.h> typedef struct { char name[50]; int age; float gpa; } Student; int main() { Student student; printf("Enter student's name: "); scanf("%49s", student.name); printf("Enter student's age: "); scanf("%d", &student.age); printf("Enter student's GPA: "); scanf("%f", &student.gpa); printf("\nStudent Record:\n"); printf("Name: %s\n", student.name); printf("Age: %d\n", student.age); printf("GPA: %.2f\n", student.gpa); return 0; }5. Implementing a Simple Linked List
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; } Node; void insert(Node** head, int newData) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = newData; newNode->next = (*head); (*head) = newNode; } void printList(Node* node) { while (node != NULL) { printf("%d -> ", node->data); node = node->next; } printf("NULL\n"); } int main() { Node* head = NULL; insert(&head, 1); insert(&head, 2); insert(&head, 3); printList(head); return 0; }6. Factorial Calculation Using Recursion
#include <stdio.h> int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } int main() { int number; printf("Enter a number: "); scanf("%d", &number); printf("Factorial of %d = %d\n", number, factorial(number)); return 0; }Feel free to modify these examples as needed, or let me know if there is something specific you would like to see!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.