Discuss Scratch

Vaibhs11
Scratcher
1000+ posts

C Programming Language

Discuss the C programming language.

Designed by Dennis Ritchie in 1972 at Bell Labs as a successor to the B language, C is a procedural language, fast and reliable which has become once the most used language and has inspired countless other languages such as Java, C#, JS, etc.

Share tips, knowledge, projects and questions.

Last edited by Vaibhs11 (Nov. 15, 2023 15:10:44)

Vaibhs11
Scratcher
1000+ posts

C Programming Language

#include <stdio.h>
int main(){printf("bump");return 0;}

Last edited by Vaibhs11 (May 17, 2021 04:33:45)

pkhead
Scratcher
1000+ posts

C Programming Language

um i only know c++

is c just basically c++ except with no classes and no new or delete operator instead you use like malloc or something and said function stands for “memory allocate”
and instead of iostream there's stdio.h and you use printf
and for functions with no parameters you just put “void” in the parentheses?

Vaibhs11 wrote:

include <stdio.h>
int main(){printf("bump");return 0;}
you forgot the number sign/hashtag at the beginning of the include statement

Last edited by pkhead (May 17, 2021 03:07:08)

pkhead
Scratcher
1000+ posts

C Programming Language

i wonder if it'd be possible to make a C compiler in scratch?
it'd use an array for the memory
and it'd compile to like… idk what's in machine code instructions to do stuff?
replace
9
10
goto
20
i mean C itself doesn't sound too complex
Vaibhs11
Scratcher
1000+ posts

C Programming Language

pkhead wrote:

i wonder if it'd be possible to make a C compiler in scratch?
it'd use an array for the memory
and it'd compile to like… idk what's in machine code instructions to do stuff?
replace
9
10
goto
20
i mean C itself doesn't sound too complex
very hard.
Especially the multi-dimensional lists.
pkhead
Scratcher
1000+ posts

C Programming Language

Vaibhs11 wrote:

pkhead wrote:

i wonder if it'd be possible to make a C compiler in scratch?
it'd use an array for the memory
and it'd compile to like… idk what's in machine code instructions to do stuff?
replace
9
10
goto
20
i mean C itself doesn't sound too complex
very hard.
Especially the multi-dimensional lists.
i think multi-dimensional arrays are just stored as 1D arrays in memory, and you get an index from them using like

ok you're right it'd be kinda hard to program a compiler for the entirety of the C programming language
Vaibhs11
Scratcher
1000+ posts

C Programming Language

bump
Vaibhs11
Scratcher
1000+ posts

C Programming Language

bump
pkhead
Scratcher
1000+ posts

C Programming Language

i've started working on the c compiler
and so far it parsed the following program into tokens:
int main() {
return 2;
}
so it spits out this
keyword
int
identifier
main
symbol
(
symbol
)
symbol
{
keyword
return
integer
2
symbol
;
symbol
}
now i just gotta program it some more so it spits out something similar to this:
  1. constant
  2. 2
  3. return command
  4. 1
  5. statement list
  6. 1
  7. 3
  8. func
  9. int
  10. main
  11. 5
(i didn't add function parameters yet)
then after that i need to program it so more to spit out something similar to assembly
then program it so the program executes the assembly
Vaibhs11
Scratcher
1000+ posts

C Programming Language

pkhead wrote:

i've started working on the c compiler
and so far it parsed the following program into tokens:
int main() {
return 2;
}
so it spits out this
keyword
int
identifier
main
symbol
(
symbol
)
symbol
{
keyword
return
integer
2
symbol
;
symbol
}
now i just gotta program it some more so it spits out something similar to this:
  1. constant
  2. 2
  3. return command
  4. 1
  5. statement list
  6. 1
  7. 3
  8. func
  9. int
  10. main
  11. 5
(i didn't add function parameters yet)
then after that i need to program it so more to spit out something similar to assembly
then program it so the program executes the assembly
( - s y m b o l
why constant? Integer is better or even better,
constant
integer
2
gosh i wish scratch was a collaborative platform

Last edited by Vaibhs11 (May 21, 2021 02:19:57)

pkhead
Scratcher
1000+ posts

C Programming Language

Vaibhs11 wrote:

pkhead wrote:

i've started working on the c compiler
and so far it parsed the following program into tokens:
int main() {
return 2;
}
so it spits out this
keyword
int
identifier
main
symbol
(
symbol
)
symbol
{
keyword
return
integer
2
symbol
;
symbol
}
now i just gotta program it some more so it spits out something similar to this:
  1. constant
  2. 2
  3. return command
  4. 1
  5. statement list
  6. 1
  7. 3
  8. func
  9. int
  10. main
  11. 5
(i didn't add function parameters yet)
then after that i need to program it so more to spit out something similar to assembly
then program it so the program executes the assembly
( - s y m b o l
why constant? Integer is better or even better,
constant
integer
2
gosh i wish scratch was a collaborative platform
i mean a constant integer
that's supposed to be the integer in
return 2;
right now i'm just programming the compiler with the intention of compiling only that program
then when it's finished i add more features
since there is only one value type in that program (int), i just made that the default type for now

the program is supposed to be compiling the list of tokens to an “abstract syntax tree” because the guide said so. so some of the numbers in the list are actually pointers to items on the same list.

and also i programmed a block which would parse what's enclosed inside curly braces, and since there could be curly braces the function is recursive. however since there are no local variables in scratch and custom blocks can't return anything, i had to implement a stack myself using a list
and since i can't create lists during runtime i had to implement a dynamic memory heap myself like memalloc() and free() so i could store the dynamic array of statements in each function occurrence what do you call it

i mean i'd had to program both of those at some point anyway because of how C works
Vaibhs11
Scratcher
1000+ posts

C Programming Language

#include <stdio.h>
int main(){printf("bump");return 0;}

Last edited by Vaibhs11 (May 22, 2021 12:59:51)

pufflegamerz
Scratcher
100+ posts

C Programming Language

Here's a C compiler I started working on a while ago: https://github.com/CrypticOS/crc
I never got around to finishing it though.
Vaibhs11
Scratcher
1000+ posts

C Programming Language

pufflegamerz wrote:

Here's a C compiler I started working on a while ago: https://github.com/CrypticOS/crc
I never got around to finishing it though.
is that a tiny c compiler?
pufflegamerz
Scratcher
100+ posts

C Programming Language

Vaibhs11 wrote:

pufflegamerz wrote:

Here's a C compiler I started working on a while ago: https://github.com/CrypticOS/crc
I never got around to finishing it though.
is that a tiny c compiler?

I guess, depends on what you mean by tiny.
pkhead
Scratcher
1000+ posts

C Programming Language

do you have any c programs vaibhs?
(what does that name even mean)

i haven't programmed anything in C, but i have programmed some things in C++
and i do know that you use stdio.h's printf instead of std::cout
and that there are no classes and no namespaces
and that you can't put methods in structs
and that there is no operator overloading
oh yeah there are also no references, only pointers

actually now that i think about it, why would anyone want to program in C instead of C++ for practical purposes nowadays?
Vaibhs11
Scratcher
1000+ posts

C Programming Language

pkhead wrote:

actually now that i think about it, why would anyone want to program in C instead of C++ for practical purposes nowadays?
c is easier.

Last edited by Vaibhs11 (May 24, 2021 05:22:04)

Vaibhs11
Scratcher
1000+ posts

C Programming Language

pkhead wrote:

do you have any c programs vaibhs?
(what does that name even mean)
(it's my name but shortened)
not much, but here's one of my repl's
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int gridlinecheck(const char agrid[9]) {
  const char samp[2] = "XO";
  if (agrid[0]==agrid[3]&&agrid[3]==agrid[6]) {
    if (agrid[0]==samp[0]) {
      return 1;
    } else if (agrid[0]==samp[1]) {
      return 2;
    }
  } else if (agrid[1]==agrid[4]&&agrid[4]==agrid[7]) {
    if (agrid[1]==samp[0]) {
      return 1;
    } else if (agrid[1]==samp[1]) {
      return 2;
    }
  } else if (agrid[2]==agrid[5]&&agrid[5]==agrid[8]) {
    if (agrid[2]==samp[0]) {
      return 1;
    } else if (agrid[2]==samp[1]) {
      return 2;
    }
  } else if (agrid[0]==agrid[1]&&agrid[1]==agrid[2]) {
    if (agrid[0]==samp[0]) {
      return 1;
    } else if (agrid[0]==samp[1]) {
      return 2;
    }
  } else if (agrid[3]==agrid[4]&&agrid[4]==agrid[5]) {
    if (agrid[3]==samp[0]) {
      return 1;
    } else if (agrid[3]==samp[1]) {
      return 2;
    }
  } else if (agrid[6]==agrid[7]&&agrid[7]==agrid[8]) {
    if (agrid[6]==samp[0]) {
      return 1;
    } else if (agrid[6]==samp[1]) {
      return 2;
    }
  } else if (agrid[0]==agrid[4]&&agrid[4]==agrid[8]) {
    if (agrid[0]==samp[0]) {
      return 1;
    } else if (agrid[0]==samp[1]) {
      return 2;
    }
  } else if (agrid[2]==agrid[4]&&agrid[4]==agrid[6]) {
    if (agrid[2]==samp[0]) {
      return 1;
    } else if (agrid[2]==samp[1]) {
      return 2;
    }
  } else {
    return 0;
  }
}
int main() {
  char grid[9] = "123456789", *corx = "X", *x = "X";
  int player = 1, tile, result, numerified;
  for (int m=0;m<10;) {
    result = gridlinecheck(grid);
    system("clear");
    for(int i=0;i<9;i++) {
      printf("[%c]", grid[i]);
      if(i==2||i==5||i==8){
        printf("\n");
      }
    }
    if (m!=9&&result==0) {
      printf("player %d: tile index to place %c (1-9): ",  player, *corx);
      scanf("%d", &tile);
      numerified = grid[tile - 1] - '0';
      if(numerified==tile) {
        grid[tile - 1] = *corx;
        if(*corx==*x) {
          corx = "O";
          player = 2;
        } else {
          corx = "X";
          player = 1;
        }
        m++;
      } else {
        printf("invalid move!\n");
        sleep(1);
      }
    } else if (m < 10&&result > 0) {
      printf("player %d wins!\n", result);
      exit(0);
    } else if (m==9&&result==0) {
       printf("draw!\n");
       m++;
    }
  }
  return 0;
}
It's a tic tac toe game
I couldn't find any code shorteners
pkhead
Scratcher
1000+ posts

C Programming Language

Vaibhs11 wrote:

pkhead wrote:

actually now that i think about it, why would anyone want to program in C instead of C++ for practical purposes nowadays?
c is easier.
well c++ has useful standard libraries, like vector. you could just download a dynamic array library for C from online (or write it yourself which would take longer), but in c++ you just put
#include <vector>
at the top and now you have dynamic arrays

and c++ has a library for a string class, while in C you can only handle strings as arrays of characters (or maybe there's like a library online)
#include <iostream>
#include <vector>
#include <string>
int main() {
  std::vector<std::string> vectorThing; // make a std::vector on the stack with template type std::string
  vectorThing.push_back("Hello"); // add string "Hello" to the end
  vectorThing.push_back("World!"); // add string "World" to the end
  std::cout << vectorThing[0] + vectorThing[1] << '\n'; // you can concatenate two strings together using the overloaded plus operator
  return 0;
}
and c++ has stuff to make your code more organized like namespaces or whatever (std a standard namespace). and references are easier to understand than pointers.
(and if you don't want to keep putting std::, then put “using namespace std;” on the top)
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void references(vector<string> &someVec) {
  someVec.push_back("foo");
  someVec.push_back("bar");
}
void pointers(vector<string> *someVec) {
  (*someVec).push_back("foo");
  (*someVec).push_back("bar");
}
void pointerClasses(vector<string> *someVec) {
  someVec->push_back("foo");
  someVec->push_back("bar");
}
int main(int argc, char* argv[]) {
  vector<string> thingy;
  string arg(argv[1]);
  // std::string also overloaded the == operator to comparing strings
  if (arg == "references") {
    references(thingy);
  } else if (arg == "pointers") {
    pointers(&thingy);
  } else if (arg == "pointerClasses") {
    pointerClasses(&thingy);
  }
  cout << thingy[0] << '\n' << thingy[1] << '\n';
}
these do exactly the same thing, but you don't need to put * or replace . with -> in the non-references one

c++ also has oop. i also i found a list online of features unique to c++ other than oop

stackoverflow wrote:

Non-OO features that C++ has that C does not:
  1. Templates
  2. Function overloading
  3. References
  4. Namespaces
  5. You can use structs and enums without writing struct or enum before every declaration or using typedefs
  6. Even if you don't define your own classes, using C++'s string and container classes is still often more convenient and safe to work with than c-style strings and arrays.
  7. Type safety (even though some would call it weak)
  8. Exceptions
  9. Variable declarations in conditionals, C99 only has it in for
(also after several years, i have finally figured out what the “Clean” tool on the forums do)

Last edited by pkhead (May 24, 2021 14:54:02)

Vaibhs11
Scratcher
1000+ posts

C Programming Language

pkhead wrote:

Vaibhs11 wrote:

pkhead wrote:

actually now that i think about it, why would anyone want to program in C instead of C++ for practical purposes nowadays?
c is easier.
well c++ has useful standard libraries, like vector. you could just download a dynamic array library for C from online (or write it yourself which would take longer), but in c++ you just put
#include <vector>
at the top and now you have dynamic arrays

and c++ has a library for a string class, while in C you can only handle strings as arrays of characters (or maybe there's like a library online)
#include <iostream>
#include <vector>
#include <string>
int main() {
  std::vector<std::string> vectorThing; // make a std::vector on the stack with template type std::string
  vectorThing.push_back("Hello"); // add string "Hello" to the end
  vectorThing.push_back("World!"); // add string "World" to the end
  std::cout << vectorThing[0] + vectorThing[1] << '\n'; // you can concatenate two strings together using the overloaded plus operator
  return 0;
}
and c++ has stuff to make your code more organized like namespaces or whatever (std a standard namespace). and references are easier to understand than pointers.
(and if you don't want to keep putting std::, then put “using namespace std;” on the top)
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void references(vector<string> &someVec) {
  someVec.push_back("foo");
  someVec.push_back("bar");
}
void pointers(vector<string> *someVec) {
  (*someVec).push_back("foo");
  (*someVec).push_back("bar");
}
void pointerClasses(vector<string> *someVec) {
  someVec->push_back("foo");
  someVec->push_back("bar");
}
int main(int argc, char* argv[]) {
  vector<string> thingy;
  string arg(argv[1]);
  // std::string also overloaded the == operator to comparing strings
  if (arg == "references") {
    references(thingy);
  } else if (arg == "pointers") {
    pointers(&thingy);
  } else if (arg == "pointerClasses") {
    pointerClasses(&thingy);
  }
  cout << thingy[0] << '\n' << thingy[1] << '\n';
}
these do exactly the same thing, but you don't need to put * or replace . with -> in the non-references one

c++ also has oop. i also i found a list online of features unique to c++ other than oop

stackoverflow wrote:

Non-OO features that C++ has that C does not:
  1. Templates
  2. Function overloading
  3. References
  4. Namespaces
  5. You can use structs and enums without writing struct or enum before every declaration or using typedefs
  6. Even if you don't define your own classes, using C++'s string and container classes is still often more convenient and safe to work with than c-style strings and arrays.
  7. Type safety (even though some would call it weak)
  8. Exceptions
  9. Variable declarations in conditionals, C99 only has it in for
sir this is a wendy's C programming topic

pkhead wrote:

(also after several years, i have finally figured out what the “Clean” tool on the forums do)
*gasp* what does it do?

Powered by DjangoBB