Discuss Scratch
- Discussion Forums
- » Things I'm Making and Creating
- » C Programming Language
- 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.
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?
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?
you forgot the number sign/hashtag at the beginning of the include statementinclude <stdio.h> int main(){printf("bump");return 0;}
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?
i mean C itself doesn't sound too complex
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
- Vaibhs11
-
Scratcher
1000+ posts
C Programming Language
i wonder if it'd be possible to make a C compiler in scratch?very hard.
it'd use an array for the memory
and it'd compile to like… idk what's in machine code instructions to do stuff?i mean C itself doesn't sound too complexreplace
9
10
goto
20
Especially the multi-dimensional lists.
- pkhead
-
Scratcher
1000+ posts
C Programming Language
i think multi-dimensional arrays are just stored as 1D arrays in memory, and you get an index from them using likei wonder if it'd be possible to make a C compiler in scratch?very hard.
it'd use an array for the memory
and it'd compile to like… idk what's in machine code instructions to do stuff?i mean C itself doesn't sound too complexreplace
9
10
goto
20
Especially the multi-dimensional lists.
ok you're right it'd be kinda hard to program a compiler for the entirety of the C programming language
- 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:
so it spits out this
now i just gotta program it some more so it spits out something similar to this:
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
and so far it parsed the following program into tokens:
int main() { return 2; }
keyword
int
identifier
main
symbol
(
symbol
)
symbol
{
keyword
return
integer
2
symbol
;
symbol
}
- constant
- 2
- return command
- 1
- statement list
- 1
- 3
- func
- int
- main
- 5
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
i've started working on the c compiler( - s y m b o l
and so far it parsed the following program into tokens:so it spits out thisint main() { return 2; }now i just gotta program it some more so it spits out something similar to this:keyword
int
identifier
main
symbol
(
symbol
)
symbol
{
keyword
return
integer
2
symbol
;
symbol
}(i didn't add function parameters yet)
- constant
- 2
- return command
- 1
- statement list
- 1
- 3
- func
- int
- main
- 5
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
why constant? Integer is better or even better,
constant
integer
2
Last edited by Vaibhs11 (May 21, 2021 02:19:57)
- pkhead
-
Scratcher
1000+ posts
C Programming Language
i mean a constant integeri've started working on the c compiler( - s y m b o l
and so far it parsed the following program into tokens:so it spits out thisint main() { return 2; }now i just gotta program it some more so it spits out something similar to this:keyword
int
identifier
main
symbol
(
symbol
)
symbol
{
keyword
return
integer
2
symbol
;
symbol
}(i didn't add function parameters yet)
- constant
- 2
- return command
- 1
- statement list
- 1
- 3
- func
- int
- main
- 5
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
why constant? Integer is better or even better,gosh i wish scratch was a collaborative platformconstant
integer
2
that's supposed to be the integer in
return 2;
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.
I never got around to finishing it though.
- Vaibhs11
-
Scratcher
1000+ posts
C Programming Language
Here's a C compiler I started working on a while ago: https://github.com/CrypticOS/crcis that a tiny c compiler?
I never got around to finishing it though.
- pufflegamerz
-
Scratcher
100+ posts
C Programming Language
Here's a C compiler I started working on a while ago: https://github.com/CrypticOS/crcis that a tiny c compiler?
I never got around to finishing it though.
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?
(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
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
do you have any c programs vaibhs?(it's my name but shortened)
(what does that name even mean)
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; }
I couldn't find any code shorteners
- pkhead
-
Scratcher
1000+ posts
C Programming Language
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 putactually now that i think about it, why would anyone want to program in C instead of C++ for practical purposes nowadays?c is easier.
#include <vector>
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 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'; }
c++ also has oop. i also i found a list online of features unique to c++ other than oop
Non-OO features that C++ has that C does not:(also after several years, i have finally figured out what the “Clean” tool on the forums do)
- Templates
- Function overloading
- References
- Namespaces
- You can use structs and enums without writing struct or enum before every declaration or using typedefs
- 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.
- Type safety (even though some would call it weak)
- Exceptions
- Variable declarations in conditionals, C99 only has it in for
Last edited by pkhead (May 24, 2021 14:54:02)
- Vaibhs11
-
Scratcher
1000+ posts
C Programming Language
sir this is a wendy's C programming topicwell 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 putactually now that i think about it, why would anyone want to program in C instead of C++ for practical purposes nowadays?c is easier.at the top and now you have dynamic arrays#include <vector>
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)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.#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 if you don't want to keep putting std::, then put “using namespace std;” on the top)these do exactly the same thing, but you don't need to put * or replace . with -> in the non-references one#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'; }
c++ also has oop. i also i found a list online of features unique to c++ other than oopNon-OO features that C++ has that C does not:
- Templates
- Function overloading
- References
- Namespaces
- You can use structs and enums without writing struct or enum before every declaration or using typedefs
- 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.
- Type safety (even though some would call it weak)
- Exceptions
- 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)*gasp* what does it do?
- Discussion Forums
- » Things I'm Making and Creating
-
» C Programming Language