C Programming Languages

What is the best programming language and why is it C?

I'll start:
>your favorite functional meme language interpreter and VM are implemented in C

Other urls found in this thread:

pragprog.com/book/jgade/test-driven-development-for-embedded-c
twitter.com/NSFWRedditGif

It's not 1989 anymore grandpa, we're C++17 now.

I bet you don't even know assembly do you?

I know some of C++ and I can tell you that:
> You can create a high level interface and work on it and work at low level at the same time.
> Support multithreading
> It's multiparadigm (OOP && Generic Programming)

Have you tried C++++? It's even better than c++

>best programming language
only kids who don't know shit about programming would ever say that.

FUCK YOU SHITFUCKER I FUCK YOU I HAVE PHD FROM IIT BOMBAY IN C PROGRAMMING

You'r right...

>mfw C++ returns C before incrementing

I hope you mean C11?
C++17 is still better better because of its move semantics. Somebody shoul write C++, the good parts.

However there is Rust and its even better because today it got rid of its drop flags in its nightly(see Reddit).
That means once it gets untagged unions C can slowly retire. What a time to be alive, i thought it would never happen.

>That means once it gets xyz C can slowly retire
people have been saying that shit for years now, but every language failed at replacing C, and most likely will fail for the next 20 years

Most C users would consider most C novices a security risk. Not C interfaces to hardware mind you,
C users that start at writing C code, at least for their first decade.

C interfaces as a common denominator likely will never go away, although lifetime and borrowchecking semantics are certainly needed here mid-term.
Although standardizing a common c documentation format for this would maybe enough.

Kernels most likely will go the microkernel way and leave most hardware to userspace deamons or applications directly, because its more efficient, needs fewer reboots and causes less interupts.
There really is no need to maintain a giant C codebase anymore in a few years. Sure that doesn't mean C will go away, you will 20 find linux kernels everywhere even in 20 years.

But its just not something you should start a new project on if rust and c++ continue to improve. (finally we already waited 20 years for this mind you and you can probably find a job writing C until you retire.)

> C
> because you don't have to memorize a massive bunch of libraries, you just learn to write code and then program whatever you need. and it runs fast.

What do you mean? Do you not import open source libraries?

perfect for counting in loops my dude

>if rust and c++ continue to improve
still a hypothetical, it has never happened before, what makes you think it ever will
c will always be the king of its niche

Actually, that's funny to remember, that when UNIX got moved from ASM to C, it's binary size increased 1,5 times.

Hey Sup Forums i started learning programming as a hobby. I tried python and java, but i quickly got bored of them. Now i started with c++ and it looks good so far. I want to make multiplatform programs and c++ with qt is very nice to use. Do you think it's worth it or am i just wasting my time? Also why would i use c or rust? As in, what can i make with them? Drivers and kernel modules look interesting, but i'd prefer to make something useful. Should i consider learning c instead of c++? If so, why? Thanks.

I reinvent the wheel every time.

In the current year, unless you are working on low-level kernel/hypervisor code or legacy embedded systems, you really should be using a higher level language than C.

#include
#include
#include

std::vector implements {"anvils", "tweezers", "the pelvis of a dead dog", "a used burlap sack"};
while (true)
{
for (const auto &e : implements)
{
std::cout

>I really enjoy writing code in a language used in the last century that can crash from printing out some strings.... but do it quite quickly!

>I'm a bad programmer, therefore it's a bad language!

You forgot to define a main() function, and your indentation is horrible.

Also:
#include
#include
#include

int main() {
std::vector implements {"anvils",
"tweezers",
"the pelvis of a dead dog",
"a used burlap sack"};
while (true) {
for (const auto &e : implements) {
std::cout

>I use a language I learned at university in the 1980s and I haven't moved on because objects, standard libraries that increase productivity and code stability, lambdas and multicore processors don't exist...

>I think it's perfectly acceptable to create a Hello World program that takes up more than half a megabyte of disk space

int i = 0;
while (1) {
printf("%d \n", i)
i++;
i = i % 4;
}

wtf?
doesn't this generate the following sequence
i=0,3,0,3,0,3...

am I just retarded?

Nope, it's an infinite loop, i starts at 0, each time it goes through the loop it increments by 1, but the mod statement means that when it reaches 4, it resets to 0. So it goes 0,1,2,3,0,1,2,3,0,1,2,3...

Jesus wept

Why do people who worship C always post examples of trivial programs like FizzBuzz and Hello World? Do they ever write anything of significance?

#include
#include

int main()
{
while(1)
fork();
}

> still rocking those carved stone wheels

>stdlib

Here's a challenge for you. Write a program in C that will go to a Sup Forums thread and print out the top 5 posts with the most number of replies.

I'd never use C for a task like that. I'd probably learn Python and use that.

Right tools for the job and all that shit.

So C is only useful for generating small hello world binaries?

C is useful for optimized low level stuff.

Using it for menial tasks like that is crazy and overly complicated.

...

As for what optimized low level stuff means, just look at what OP said.

It's perfect for that.

>microkernel
>more efficient
dumb

So you spend most of your time writing VMs and kernels? You must be one 1337 hacker.

No, although I'd like to experiment with microcontrollers one of those days, and that's why I'm learning C.

Wrong.

C++ mangles function names in the debug symbols, making debugging annoying. If you don't need the C++ abstractions (hint: you never do), just use C as its a simpler language. Simpler to read, simpler to understand, and a simpler language means you can better trust the compiler to work correctly.

Source: I'm a firmware developer at a very large computer hardware company.

And you're proclaiming that a language that you are "learning" is the best?

Let me guess, are you gonna start educating me about how lisp is wonderful and how it should be applied to everything?

No. I just find it strange that someone who barely knows how to write a FizzBuzz in the language is going around claiming that it is the best out there.

Anyway, we're always learning.

>because objects
Sometimes it seems like people don't realize how much of a crutch and a waste objects can be.

I had a guy at work tell me that, for a data task that didn't need any objects at all, creating thousands of ruby objects wasn't slowing it down. He wanted the objects so that he could concatenate first and last names with a ruby method. Fucking ruby programmers.

The best language is literally every Lisp derivative, with the possible exception of Clojure
But even Clojure is better than C

I'm not saying C is bad
But Lisp...
You will never beat it

fucking lispfags

Out of curiosity, when you're writing C, do you use C++ to test it? I've found some sensible-sounding advice that says that's the way to go, but it seems really counterintuitive.

>Yeah who needs encapsulation and type safety checks! Just use strcat! Who cares if we write past the end of the allocated space!

What do you mean test it? We write the code, run the debugger, step through it and make sure the hardware is doing its thing, then release an image to the validation team and they run all sorts of automated tests on the hardware and try to catch bugs in the hardware design or the firmware.

Most non-trivial programs in any language are too big to be reasonably posted on Sup Forums, it's better to just link to github or something. Especially since C development practices usually involve multiple files. And anything with GUIs or the like is non-portable, little point in posting the source to demonstrate a point.

Is there a library in either of those languages that gives you access to the Sup Forums API? Otherwise I'd have to resort to copy-pasting the whole thread into a text file and parsing it. Parsing data and doing calculations isn't hard, getting the data off the internet in the first place is.

It's used for a lot of low-level programming, most Unix operating systems are written in C, as are the standard programs that come with them. It's also a popular IR for compilers - a lot of people writing a compiler for a new language would rather parse a source file in the new language, compile it to a subset of C, and call a C compiler on that, rather than write the whole backend themselves. It's also fairly popular for games and stuff where performance is important - though it's partially been superseded by C++, and a lot of "game programming" is really just doing scripting in some other language for game engines written in C.

What is the intended connotation of this?

Anyone who wants to do any sort of low-level stuff like that should have some knowledge of C, even if it's not their preferred language. Even high level languages like Java and Python require a runtime environment written in something like C or assembly. So if you're going to be writing purely userland applications, C might not be that important, but if you write compilers, device drivers, embedded systems stuff, or just anything where speed is a priority, that's where C is useful.

You can use popen and wget.

I can't hear you over all this clean fucking code

>I can't hear you over this overhead
ftfy

>If you don't need the C++ abstractions (hint: you never do)
If you're writing code that lends itself naturally to the OOP approach, C++ classes can be beneficial, OOP in C is pretty annoying. Although Java seems better for OOP anyway, C++ tries too hard to be a do-everything language.

Any high-level language that protects you from buffer overflows was initially implemented in something low-level like C or assembly. Which means that it is possible to do safe string handling in a language like C, it's just a lot of work to implement it the first time.

Besides, their argument wasn't that you should use C for string-heavy tasks, rather it was that people shouldn't use OOP unless the situation actually calls for it.

Modern machines have no trouble running a lisp interpreter, if it tickles your autism that badly you can precompile

>What is the intended connotation of this?

Stuff like this: pragprog.com/book/jgade/test-driven-development-for-embedded-c

If I recall descriptions other people have given for this book correctly, it teaches you to write tests in C++ to test your C code. I don't know if TDD is considered meme-tier to C programmers. Parts of the description to that book sound nice, others sound like marketing-bait:

> With his years of training, coaching, and practicing TDD in C, C++, Java, and C# he will lead you from being a novice in TDD...

Shit like that makes me think he might not know what the fuck he's talking about, and he's just treating everything like a nail because he has a hammer. Would rather hear from somebody who uses C at his job.

In most languages strings are objects. Doing operations with strings is simple because they have predefined methods that encapsulate all the details. Nobody wants to fucking mess around with char*'s and constantly checking bounds.

Let's just keep tacking even more shit and it'll be even better!

No. C++ is great for specializing in a certain field, but it's too complicated for anyone (or at least most people) to completely master and understand.

Whereas C is great (some would say perfect) because it's simple and elegant. It took the complexity of assembly and abstracted/generalized it to run anywhere and be readable by most people with minimal learning. It's the backbone and/or basis of all major languages to date, and it remains among the top used languages. No language has ever been as successful and revolutionary in its goals.

C++ is just a C library/extension that got out of hand such that it needed a different compiler and standard.

>tfw STILL have no idea how the fuck pointers work

you mean including the lisp runtime in my program which forces me to license my program as whatever the interpreter was licensed as?

let's face it, lisp is trash for anything but CS masturbation
>be readable by most people with minimal learning.
if only that were true

the truth is no one knows how to program so it results in unreadable messes

Points are a reference to a point in memory, they are like a variable but do not initialize memory or store anything.

This wasn't a C or ruby problem - it was meant to highlight how much people rely on objects when they shouldn't. More specifically, we were looking at a slow running report that was written with rails, and he wanted to continue to make objects out of what was returned from the ORM to get string concatenation, instead of having the database take care of that because rails programmers are all afraid of the big bad database man.

DHH, an author of, and very outspoken advocate for rails literally has this to say about databases:

>I don’t want my database to be clever!
>I consider stored procedures and constraints vile and reckless destroyers of coherence.
>No, Mr. Database, you can not have my business logic.
>Your procedural ambitions will bear no fruit and you’ll have to pry that logic from my dead, cold object-oriented hands...
>I want a single layer of cleverness: My domain model.

And so rails developers shy away from the thought of trusting a database to do anything.

>the truth is no one knows how to program so it results in unreadable messes
Fair enough, but you have to admit that C code is less complex than C++ code in general. There's literally less syntax to cope with.

Oh yes, C++ is simply unreadable for me.

C I can eventually understand if I focus hard enough.

...

You realize that strings are objects too right?

But what's the use of them? How do they help me?

What if you need two variables to match in value?

Normally you would create another variable with an identical value but now you have taken twice the space for the same amount of information.

If you created a pointer to the first variable you get the same result with less resources and the pointer is dynamically updated as well, no need to update the second variable to the new value each time.

In C they are the only way of dealing with data that is unknown size at compile time. They are also the foundation of arrays and strings (though these can be done better with a fairly simple library).

In sepples they are as good as redundant unless you're doing genuinely low level code. If you really need a pointer you can also just use unique_ptr which is a lot easier.

Pointers are what every variable is under the hood. They basically give you more control over the program at a lower level, meaning more efficiency and/or simplicity in a program.

So in this scenario, it's just optimization?
And in this one it's just to prevent some kind of overflow?

>And in this one it's just to prevent some kind of overflow?
Yes and no.

Imagine you're writing a plain text editor. You decide to store all the text in a buffer so you do:
#define SIZE 256000
char text[SIZE];


the question is what do you set size to? No matter what you do there's always the possible of a bigger text file being used. The problem is you just can't pick a size at compile time that you know will be big enough (and reserving large chunks of memory is pant-on-head retarded anyway).

Pointers let you grow the buffer as it's used. So when it fills up you can just reserve more memory.

A pointer is like a link on a webpage, or a shortcut to a file on your computer.

So basically, something like
int a = 5;
int * ptr = &a;

mean that ptr holds the ADDRESS in memory of the 'a' variable. (The & means "take the address of this variable). So if you try to printf() the pointer, you'll probably get some big number corresponding to the address, rather then the value 5 itself. But you can DEREFERENCE the pointer with the * operator to get the value stored at the address.

Main use of pointers is to implement pass-by-reference semantics (C is purely pass-by-value language).

So if a function has to MODIFY a struct, it's best to pass it as a pointer, otherwise the function will make a copy of the original, then throw it away when it returns. They're likewise useful for functions operating on very large structs, even if they don't modify it - otherwise they'll have to copy the whole big struct, which can waste a lot of memory.

Additionally, pointers are the way arrays are passed as arguments, if you write a function taking an array as argument, it just converts it to a pointer to the first element, and you can do "pointer arithmetic" to access the other elements.

Although in addition to understanding pointers, you should know there's basically 3 types of memory allocation:

global variables, and static local variables - allocated when program starts, deallocated when program closes, alive for the entire time program is running

non-static local variables: allocated when function is called, deallocated when the function exits
(this means if you call a function, which declares a local variable, and returns a pointer to that variable, you'll have issues if you try to dereference the variable later on, since the variable it points to is deallocated)

Is this pretty much the best book for getting into C?

thank you for the explanations

see It's just there for infinite fuckery

No, the 2nd edition is.

After that you'll probably want to read something else since C evolved since then.

Different user but thank you very much for the comprehensive explanation

(cont)
finally there's heap memory, which is allocated with malloc() and deallocated with free(). So the programmer is in direct control of this memory. And it's accessible ONLY with pointers, so a call to malloc(20) returns a POINTER to the beginning of a block of 20 bytes on the heap. And you should free() that memory before the pointer goes out of scope, or is pointed to something else (you can reassign pointers just like you can reassign variables), or you'll end up with a "memory leak" (meaning memory is not deallocated and given back to the OS, but is inaccessible to the program; C has no garbage collection).

This may seem like a bunch of confusing nonsense, but probably the best way to get used to pointers is to practice, try solving random problems in pure C, and eventually you'll come across a situation where pointers are the best way of doing it. For example, you pretty much cannot do i/o without pointers, since scanf() requires a pointer as its second argument, because it has to modify memory before returning it. (Though in practice, you'll end up using &var, rather than creating an explicit extra pointer variable). And likewise, if you want to simulate OOP in C, you'll use structs to hold member variables, but structs cannot hold functions, so instead of calling

Object object = new Object

as in C++, you might do something like

struct Object * object = new_object()

where new_object() is a function that malloc's space for a new struct, assigns values to its members, and returns a pointer to it.

well duh c isn't as efficient as asm but it's kinda close and it's way more productive. C isn't my favourite language but that's a dumb comment.

Nothing is going to be as efficient as assembly, the whole point of asm is it's basically a "word for word translation" of machine code, so nothing can ever beat highly optimized assembly code. Assembly is still used in some cases where speed and small binary sizes are of critical importance, but it's very hard to code in, and a compiler can generate shitty assembly far faster than a human can. And on top of that, C is portable, so you don't need to rewrite the whole operating system every time you get a new computer.

>so you don't need to rewrite the whole operating system every time you get a new computer.
this, im pretty sure ken and ritchie did not care at all about efficiency

the entire point of C was to be a "portable assembler", after all

They probably did care about efficiency, having compilers generate at least half-decent machine code was a priority even from the first FORTRAN compiler in the 1950s. And C actually is fairly efficient, since it's about as low-level as you can go while still being a "high level" language.

i still think it was a very secondary goal, at least

they wanted a portable time-sharing operating system, so they made C

>the entire point of C was to be a "portable assembler", after all
No, the point of C was to get a language working on their cheap computer that was less awful than assembler. Portability was mostly an accident.