Learned C here's my version of Hello,World!

#include <stdio.h>

int main() {

printf("Thank you @drenal couldn't do it without ya!");
return 0;
}

Comments

G
@Enlapse there is a situational difference.

Using brackets to declare an array is useful when there is a set number of elements already, or if you don't know the number then using empty brackets and immediately declaring the elements.

Code:
int numbers[10];

int numbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9,  10};

However, sometimes you can't set that, or you may need to change the size of the array throughout the program.

For example, the printf(const char* text, ...) statement in the C library cannot use const char text[] because it can't immediately set the data in the array, and in run-time the program won't know what to do (this is different than in main() because the functions have to be compiled before main has to)

Or, if you are creating a snake game and need to dynamically increase the array for each number of food pellets that have been eaten, you can't recreate the array to keep track of each segments x coordinate. Instead you would do something along the lines of this:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int main()
{
    bool eatPellet = false;
    int pelletsEaten;
    int* segmentCoordinateX = (int)malloc(sizeof(int));
    
    while (1) // always true so don't worry about it
    {
        if (eatPellet ==  true)
            pelletsEaten++;
        segmentCoordinateX = realloc(segmentCoordinateX, pelletsEaten * sizeof(int));
    }
}
 
  • Like
Reactions: 1 person
Coding part are easy, come up with the algorithm is the hardest part for me. I was never figured out how to swap values of 2 variables until I read the answer which make me feel dumb of how easy that actually was.
 
Since I had this ready and only needed to tweak a few things to fit the occasion, I might as well post this:P
$nomod51
#include <REG420.h>
org 0000h
ljmp main

org 0003h
ljmp INT0_ISR

org 30h
reti
org 70h
; LCD Pins
en equ p2.2
rs equ p2.0
rw equ p2.1

key equ 0x30
counter equ 0x31
AddN equ 0x32
KeyFL equ 20h.0

main:
CALL INIT_LCD
setb ex0
setb ea
setb it0
mov p1,#0xf0
WaitKey:
clr KeyFL

jnb KeyFL,$
mov a,key
mov dptr,#asciitable
movc a,@a+dptr
call SENDCHAR
;call Send_Lcd
jmp WaitKey
;*****************************************************************************

;=============================================
INIT_LCD: CLR RW
CLR RS
CLR EN
MOV DPTR,#INIT_TABLE
INITLOOP: MOV A,#0
MOVC A,@A+DPTR
JZ EXIT
MOV P0,A
CALL LCD_DELAY
INC DPTR
JMP INITLOOP
EXIT: RET

LCD_DELAY:SETB EN
MOV R6,#40
LP1: MOV R7,#138
DJNZ R7,$
DJNZ R6,LP1
CLR EN
RET

;-----------------------------------------------

SENDCHAR: MOV P0,A
SETB RS
CALL LCD_DELAY
RET

INT0_ISR: mov b,#0xfe
mov AddN,#0
KLoop: mov p1,b
call key_delay
mov a,p1
cjne a,b,K1
mov a,b
rl a
mov b,a
mov a,AddN
add a,#4
mov Addn,a
cjne a,#37,Kloop
jmp NoKey
K1: call Scan
mov a,Key
add a,AddN
mov Key,a

setb KeyFL
call debounce
clr ie0
reti

nokey: mov Key,#0xff
call debounce
clr ie0
reti

Scan: jb p1.4,R1_C0
mov Key,#0x00
ljmp ScExit
R1_C0: jb p1.5,R2_C0
mov Key,#0x01
ljmp ScExit
R2_C0: jb p1.6,R3_C0
mov Key,#0x02
ljmp ScExit
R3_C0: jb p1.7,ScExit
mov Key,#0x03
ScExit: ret
;_____________________________________________________________________________
debounce:
wait_d: mov p1,#0xf0
lcall key_delay
mov a,p1
cjne a,#0xf0,wait_d
lcall key_delay
mov p1,#0xf0

ret
;_____________________________________________________________________________
key_delay:mov r6,#1
lcd_ad1a: mov r7,#255
djnz r7,$
djnz r6,lcd_ad1a
ret

;_____________________________________________________________________________
asciitable: db 'YoulearnednothingbutDOsuckevenmore:.P'
INIT_TABLE: DB 38H,38H,38H,38H,06H,0EH,01H,80H,0
end


Enjoy the mess that is my programming!:rofl2:
 

Blog entry information

Author
DRAGONBALLVINTAGE
Views
72
Comments
62
Last update

More entries in Personal Blogs

More entries from DRAGONBALLVINTAGE

General chit-chat
Help Users
  • No one is chatting at the moment.
    Psionic Roshambo @ Psionic Roshambo: https://imgur.com/gallery/kUTOYMV