Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Do we define data types by defining classes?

#1
Ostronomos Offline
Also,  I am curious to know why the compiler requires separate compilation for its programs and what is the need to write our own header files? Is it to avoid clashes between entities such as variables when defining them between multiple files?
Reply
#2
confused2 Offline
Computers (microprocessors) use simple instructions along the lines of:

Load register A from memory location [12345]
Load register B from memory location [12346]
Add register B to register A
Move the contents of register A to memory location [12347]

To do anything useful the microprocessor has to carry out (often) millions of these very simple (machine code) instructions.
The compiler creates a version of the text that you write (your 'program') in the simple codes the microprocessor actually uses.

You don't need to write your own headers unless you are sharing either all or part of one program with another- to start you just need to include the standard headers
#include <stdio.h>
^^ this enables you to write stuff to the screen, it is quite complex and will usually call functions provided by the operating system.

There's a very good tutorial site here:
https://www.w3schools.com/c/index.php
Reply
#3
Ostronomos Offline
Does the compiler give an error message if one fails to properly define and declare the variable in proper separate compilation format? I believe the preprocessor is used for safely dividing a class definition into multiple header files correct?
Reply
#4
confused2 Offline
You can compile and run programs here:
https://www.w3schools.com/c/c_variables.php

I tried this where myNum is declared but yourNum isn't

#include <stdio.h>
int main() {
int myNum;
myNum=24;
yourNum=99;
printf("%d", yourNum);
printf("%d", myNum);
return 0;
}

Their compiler gave this output:
prog.c: In function ‘main’:
prog.c:6:3: error: ‘yourNum’ undeclared (first use in this function)
6 | yourNum=99;
| ^~~~~~~
prog.c:6:3: note: each undeclared identifier is reported only once for each function it appears in

You can play with this more yourself - it is free!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Defining GOD Ostronomos 1 104 Aug 27, 2023 09:18 PM
Last Post: Magical Realist
  Like LSD: dreaming is brain preference for weak relationships + Two curiosity types C C 0 116 Jan 15, 2021 08:23 PM
Last Post: C C



Users browsing this thread: 1 Guest(s)