[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

C programming thread

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 121
Thread images: 8

File: images (1).jpg (5KB, 227x222px) Image search: [Google]
images (1).jpg
5KB, 227x222px
I'm relatively new to C and I love it.

Any tips or challenges?
>>
>>55660889
>any tips
Make sure you know how to use debugging tools like gdb and valgrind
>>
>>55661032
I have gdb but have no idea how to use it, guess I look up something
>>
>>55660889
>Any tips
Don't fall for the C89 meme. Always use the latest standard.
>>
>>55661117
Yeah, I got that covered
>>
Hey, bit like OP here, I've got some decent experience down with Python and C# and I'd like to get started learning C, any advice?
>>
Save yourselves a lot of pain and suffering, and just use c++. Why would you want to go out of your way to make your life harder, when c++ adds functionality that makes your life easier? Especially when its backwards compatible with c functions. For example, parsing dynamically allocated c strings, the fucking bane of my existance, is rendered almost trivial in c++. malloc(arraySize * sizeof(char*));? Fuck that shit, c++ let's you just use 'new.'
>>
>>55661308
Can C++ be used as close to the metal as C can?
>>
>>55661308
C++ is a horrible clusterfuck of a language. C does C's job a lot better than C++ ever will.
>adds functionality that makes your life easier
That is one of C++'s problems: too much shit. There are umpteenth ways to do something, so it leads to programmers subsetting the language. One person's C++ is completely different from another person's C++.
>Especially when its backwards compatible with c functions
Another advantage of C. It has a very simple ABI, so practically any other language can call its functions. That is why C is the language of choice for libraries.
Also, in terms of language features, there are many features that C has but C++ doesn't. It is in no way a superset of C.
>malloc(arraySize * sizeof(char*));? Fuck that shit
You just suck.
>>
>>55661334
>backwards compatible
Not that you'd want to, but you could literally write pure c as a c++ program. So, I'd say yes. It can do everything c can, and more. Hince the ++.
>>
>>55660889
NEVER use GCC
>>
>>55661373
Why not?
>>
>>55661372
It's not 1980 anymore. C has evolved since then.
Here is a little test.
In C++, create a static int array of 10000 elements, where they are all zero, and the 10000th element is 1, without using any initialisation code.
Here is the C solution.
static int array[10000] = {
[9999] = 1,
};
>>
After using C++ instead. C is a total meme.
>>
>>55661424
Start*
>>
>>55661402
You're not understanding. I could take the code you just wrote and embed it in a c++ program and it would work just the same. C++ is C, you can use it just like C, but it also has more things that make what was a pain in C easier. But only if you want to use them.
>>
>>55661444
>I could take the code you just wrote and embed it in a c++ program and it would work just the same
No you can't. It's literally a feature that C has but C++ doesn't.
It MAY be supported as a compiler extension, but I don't know.

How about something more useful?
How do I make a guarantee that two pointers in C++ don't alias?
>>
>>55661132

It's going to be quite a bit different from what you are accustomed to. It's a small, terse language, in which you have complete control over your machine, or in most cases, as much control as the OS will allow you to have. My advice is to think of a project that you'd consider C worthy, and is something you think you could write in a week or so. Then, find yourself some time, and work on it. If you continually find yourself saying "this feels like a step backwards" than you are using C for the wrong application.
>>
>>55661481
Idk I guess just check if the value of pointerA == pointerB? I mean, how would you do it in c?
>>
>>55661533
I had function arguments more in mind.
Anyway, I was pointing out the fact the meme++ doesn't even have restrict pointers.
void my_fn(int *restrict a, int *restrict b)
{
/* a and b don't alias.
* The compiler can now make much better optimisations
* knowing that
*/
}
>>
>>55661561
Actually, upon a little further investigation, it seems that g++, and many other c++ compilers, support __restrict__. Which does the same thing as its C99 counterpart.
>>
>>55661444
>C++ is C, you can use it just like C
There are a few things that are valid in C but not in C++ (don't remember anymore but it was something about void pointers).
>>
>>55661717
Non-standard shit.
>>
>>55660889
command line arguments are nice
>>
>>55661732
Who cares? There's nothing wrong with using something outside of the standard if its useful. There are only a few scenarios where you're required to stay strictly withing the standard and use functionality only c has, and in those situations, sure maybe C would be a slightly more advantageous choice. But in most general cases, it's just more trouble than it's worth. I mean, you could apply the same logic to writing all code in assembly. Why don't we just do that for circuit level control? Because there are better things that make programming easier and more intuitive. Same with C++.
>>
>>55661308
Please don't listen to this guy. C++ is shitloads more difficult to parse than C and will effectively lock you in to a specific toolchain if you do decide to use it. If you want to work with other code that is written in C++, please be my guest, but there are far better approaches to OOP in other languages is that's what you are after.
>>
>>55661824
If you allow non-standard shit to be judged as part of a language, then the whole concept of comparing lagnguages breaks down.
I could extend C, C++ or ANY language in any way I want (which doesn't affect conforming programs) and say that it is better than anything.
>>
File: BjarneStroustrup.jpg (41KB, 432x324px) Image search: [Google]
BjarneStroustrup.jpg
41KB, 432x324px
What did he mean by this?

https://www.youtube.com/watch?v=KlPC3O1DVcg
>>
>>55660889

I'm taking this course, currently on chapter 3 and stuck on link lists..

http://2016-aalto-c.mooc.fi/en/Module_1/index.html
>>
Is K&R a meme for learning C as far as being outdates?
>>
>>55662953
No, not really. Treat it more like the C documentation or reference manual than an educational textbook though. If C is your first programming language than you're going to want to supplement your learning with a book that's more newfag friendly, but if you already have experience in a language like C++ then K&R should serve you fine.
>>
>>55662953

IMHO it's the best start.

Just don't try to apply every funky stuff you see there. You can do a lot of "dirty hacks" in C, but that doesn't mean you should.
>>
>>55661372
>you could literally write pure c as a c++ program

Utterly wrong.
>>
>>55661308
>>55661334
>>55661353
>>55661402
>>55661424

As long as you avoid virtual functions, exceptions, and other shitty bloat, and you just use it as "C with classes", AND you are working on a project that benefits from an object-oriented approach (game engine is one I can immediately think of) then C++ is the better choice.
>>
>>55662953
I thought it was just a reference manual at first and tried a "real, educational textbook" like Head First C
made it into like the third chapter writing stupid number guessing games before I wanted to kill myself from boredom

If you turn off your computer, open up a paper notebook, and get some coffee, K&R is far more useful. Covers entire chapters of "learn programming!!" books in a couple of pages.
>>
>>55663429
>game engine is one I can immediately think of
Game engines do NOT benefit from OOP. In fact, OOP's terrible performance hurts it.
>As long as you avoid X, Y, Z
Exactly as I said, it leads to shitloads of subsets of C++.
>>
>>55660889
>Any tips or challenges?
Graduate middle school, abandon C, and code something useful with Javascript or HTML.
>>
>>55663498
At least put some effort into your bait.
>>
The truly redpilled approach: high-level language with excellent FFI support for C, like Chicken Scheme.
>>
>>55663483
But OOP is just a different way for a programmer to "visualize" and manage code. It is still more or less the same binary that C would produce, although admittedly with a bit of bloat added in support of certain C++ features.

Using C++ like that does create subsets, which genuinely sucks and is a big problem for open source projects with public contribution, but in a proprietary industry setting you just hand new employees a reference guide for your company's/team's subset and call it a day.
>>
>>55663724
>It is still more or less the same binary that C would produce
No it won't. Compilers are filled with magic and fairy dust that will optimise your program into the most efficient program possible.
OOP has huge issues with cache locality, because you're spreading all of your information over several objects.
>>
>>55661334
Yes and no. It is possible but on low mem devices objects are hell to maintain
>>
>>55662953
no, k&r teaches proper C style.
>>
File: 1468430691079.png (1MB, 1587x1600px) Image search: [Google]
1468430691079.png
1MB, 1587x1600px
Fuck, I've made 5 separate attempts at learning this shit, the Head First C book is just sitting there on my desk now

I feel like I'm just tossing code into a void going through video tutorials and that book I got is barely helpful since it assumes that I know Java for some fucking reason.
Is there a list beginner projects I can work on (preferably with ramping difficulty)? If I know one thing it's that I learn better by applying and figuring things out as I go
>>
>>55663954
>Hello world
>Fizzbuzz
>Knight's tour
>Prove P = NP
>>
>>55663954
DCPU-16 emulator.

https://raw.githubusercontent.com/gatesphere/demi-16/master/docs/dcpu-specs/dcpu-1-7.txt
>>
>>55661117
it's pretty hard when most of the good books about C are still in C89
>>
>>55662953
It's honestly my favourite textbook. Concise but still very educational. Great exercises. My only issue with it is that it assumes you know some stuff about how computers work on a low level (only basic shit like twos complement)
>>
>>55664059
C doesn't guarantee that integers use twos complement. I don't think K&R would have made that assumption.
It's been quite a long time since I've read it though, so I might be wrong.
>>
>>55661107
RMS has a good tutorial for it.

I'm not the anon who suggested it, but I can't agree enough that gdb is important for learning C. Also, when you're using it, run
tui enable


That'll make your life much easier.
>>
>>55664074
There are some exercises like 'Explain why the above doesn't work for 128 on an 8 bit twos-complement machine'
>>
>>55664100
I prefer ddd over TUI tbqh.
>>
Write a device driver for a printer or webcam. By the time you're done you'll have a firm grasp on both C and hardware level programming
>>
>>55664144
Thinking about it, that's actually a really good question. It's important that people understand that you cannot always take the absolute value of the most negative number.
But I do see your point. However, I don't think K&R was ever indented as a beginner's book. It clearly expects you to know a little bit about programming before going in. Even high-level languages expect you to know about twos complement.
>>
>>55664249
hello grandpa
>>
>>55664249
>:D
How could you have been a developer for 25 years when you're clearly a 15 year old?
>>
>>55663999
>learning programming from books
Doing it wrong, you should only read K&R to get the basics. After that, you should study real code and make your own projects.
>>
>>55664175
I'm interested in learning to interface simple hardware, like webcams or lcd displays. how should i approach this? Just looking at the datasheet is confusing me.
>>
>>55664896
Start with something simple like reading and writing to pins on a parallel port. You can pin out directly into a LED and make the LED turn on and off.

It's relatively easy on Windows, but even more so on Linux.

http://tldp.org/HOWTO/Module-HOWTO/
>>
>>55661308
new is the past,now you have smart pointers
>>
>>55664937
>unironically using meme pointers
>>
>>55665026
>smart pointers are a meme
That's it, I'm leaving /g/ forever. /g/ can't program for shit.
>>
>>55665037
>dude, do you know what would make this fast memory access operator cool?
>make it an object for all those extra cache misses


>DUUUUDE
>>
>>55665061
>cache miss
You obviously don't know how smart pointers work.

Smart pointers (lets stick to shared_ptr here to keep it simple) are basically just a reference counter, that increases on entering scope and decreases on leaving scope.

When dereferencing the pointer, there are no additional steps other than some encapsulation code that the compiler will inline and optimise away.
>>
>>55661308
nice try bjarne

enjoy your bloated, huge "superset" of C
>>
>>55662953
I'm reading it right now. It's not a beginner's book in that it doesn't introduce basic concepts from CS and whatnot, the book assumes you know a lot of shit and I like it for that. You will learn C and you will like it.
>>
>>55661999
He says C is obsolete because he thinks C and C++ should've merged into a single language. In his view, C++ does everything C can do, but the "plus plus" in C++ helps with working at a higher level of abstraction, which makes doing more complex things easier (as he explained with his example about matrices).

Maybe be more specific than just "this", since "this" could be any part of the video.
>>
>>55661308
I think that whole "new" problem has been taken care of since c99. Also, you could just set up a single line preprocessor macro for a lot of the syntax based changes that you want to make for yourself.

I am not anywhere near C handy but C is thus far much more compelling to me both in performance and the "feel" of it as it runs in my machine. I have an ear problem so it's amazing to be able to get such a light runtime.
>>
>>55661107
gdb isn't that complicated at all. There's 6 keywords and they're all pretty straight forward. Aside from that there are the flags and extensions to consider but if you've got a good grasp of your own program you really shouldn't need gdb to do more than run through small seg faults. Of course, that carries the assumption that you are coding based on the design implementation K&R and the rest have/had in place for it [C].
>>
How do I compile and run a C program inside emacs without using M-x shell-session? Is there some other way?
>>
>>55661308
For example, parsing dynamically allocated c strings, the fucking bane of my existance, is rendered almost trivial in c++.
malloc(arraySize * sizeof(char*));? Fuck that shit, c++ let's you just use 'new.'

>malloc(arraySize * sizeof(char*));
and exactly what's wrong with it? few data types is too much for you?

>>55661402
>where they are all zero
>without using any initialisation
>>55660889
>Any challenges?

write function that interweaves elements in multiple linked lists
so that i1->i2->i3-in ; j1->j2->j3-jn ; k1->k2->k3->kn
becomes
i1->j1->k1->i2->j2->k2->i3->j3->k3->in->jn->kn etc.
>>
>>55669257
M-x gcc ..
M-x eshell ..
>>
>>55669641
well M-x gcc doesn't work, but doing eshell and invoking gcc from there works.
>>
>>55669754
M-c compile gcc ...
>>
>>55669858
M-x
>>
>>55669891
got it. Running the compiled program in eshell works.
>>
>>55669257
C-c C-c
but you have to bind it to compile command.
>>
>>55661308
what if your area of interest is embedded stuff
>>
>>55664186
wasn't K&R mostly written for FORTRAN programmers?
>>
>>55660889
Safely handle a string without rewriting the entire string library and maintain portability.

hint: you can't
>>
>>55669257
learn how to write a makefile and do this >>55669999
>>
File: 43l2czA.jpg (100KB, 500x713px) Image search: [Google]
43l2czA.jpg
100KB, 500x713px
>>55661032
I have never once in my 10+ years programming C needed gdb or valgrind to debug a program.
>>
>>55661402
That's just syntactic sugar. The core of the language hasn't changed. C is simply too old.
>>
>>55671507
I'm actually thankful for that.
C libraries are always so easy to understand because they don't make use of things like namespaces.
In fact, they always work the same way.
Create a little context pointer and have black box functions do operations on the data in the pointer.
>>
>>55671507
Doesn't matter.
You can't do this in c++.
int *x = malloc(sizeof(int));
>>
File: 1434596334339.png (250KB, 720x1110px) Image search: [Google]
1434596334339.png
250KB, 720x1110px
>>55662953
Can confirm it's only a meme if you've never programmed before. But other than that it's god tier, so far the best quality over quantity tech book I've ever read. Short but extremely detailed and to the point. Picked up a used copy for $15, best I've spent in a long time.
>>
>>55671595
That's no feature unique to C.

>>55671622
Too verbose.
>>
>>55671676
But it doesn't relegate the minding of a data table to class distributions. That's literally a whole half to programming, managing memory so that the most important data is at the front. You can do that in c++ too but it'll be placing all data at the end if you use `new`. So instead of worrying about stuff like that, and how new hardware makes it negligible ( it doesn't ), you can use a macro in C and get the same effect you would with new except that now you also get to put the data at the front if you want without having to access the class before you can reach your desired function.
>>
>>55661117
There's not much of a reason to use C11 over C99. Most of the appealing features of C11 were library features that don't have much support.
>>
>>55661308
C++ has some features that make getting things to work a huge pain in the ass. That's why I try to use C when I can.

>>55670100
I don't know why people think you can't use C++ on embedded. It works fine if you don't do anything stupid, like bloat the code with thousands of template functions.
>>
>>55671622
That's bad style. Do this instead:
int *x = malloc(sizeof(*x));


You can't fuck up if you do it this way.
>>
>>55672847
There is no reason to NOT to use C11 over C99.
Both Clang and GCC support C11, and even default to them.
Sure, C11 doesn't add a huge amount to the language like C99 did, but there are several little things which make it nicer.
>>
>>55672970
It's not valid c++
>>
>>55672975
>There is no reason to NOT to use C11 over C99.
Compatibility. It can compile on older versions of GCC on older computers.
>>
>>55672988
>using malloc at all in C++
I know I need to add a cast to int* on the return value of malloc. Is that all that makes it illegal in c++?
>>
>>55673008
That's a stupid reason.
Why not write it in K&R C, so you can be compatible with the first version of GCC?
GCC 5.1, which was the first version to default to C11, was released over a year ago. You should expect most people to have at least that. Even then, earlier versions support C11, it's just not enabled by default.
Also, if you're going to limit yourself to the annoying and ridiculously slow release schedule of Red Had and Debian, you'll basically never get to use ANY new software.
>>
>>55673064
No.

The original claim was that everything c is valid c++ which is not true.
>>
>>55673069
Whatever, I don't like to use something new and shiny unless I absolutely have to do it and there's no other way to do it.
>>
>>55673093
Do you still use CDE?
>>
>>55673105
I meant more in the programming sense.
>>
>>55673122
Is COBOL your favourite language?
>>
>>55671462
>programming C with any worthwhile results whatsoever
>not taking advantage of *db or valgrind
pick at least one, pajeet
>>
>>55673143
C is better than it.
>>
>>55673158
Fucking noob plebeian, using that newfangled shit. Everyone worth his salt knows that anything newer than PL/I is superfluous cruft.
>>
>>55673086
I wasn't commenting on the original claim though. It just bothers me when people use malloc in the non-ideal way.

>>55672975
Well there are very good reasons to avoid C11's optional libraries that aren't supported by most popular compilers, but other than that there's no reason to choose C99 over C11. It's not like C89, where some compilers refuse to give support for anything else.
>>
How would I create a program in C where there is a window with two buttons, click on left the screen closes, click on right and 'click' is printed on that window? I'm slowly implementing algorithms but I just don't understand where to go from there. Does everybody just use some specific library for program development?
>>
File: minor_disturbance.png (112KB, 410x310px) Image search: [Google]
minor_disturbance.png
112KB, 410x310px
>>55673093
>le stable meme
Plebianist detected
>>
>>55673231
goto >>/g/sqt/ ;
>>
>>55661117
This, OP. Invest in compound literals.
>>
>>55673231
GTK. You'd probably be best off using Qt, even if that means having to go to C++.
>>
>>55673231
lrn2toolkit
>>
>>55673247
Thanks mate.
>>
>>55673231
>Does everybody just use some specific library for program development?
Yes.
They are also all a pain in the ass.
>>
>>55673272
none of them will ever be as bad as Xlib at least
>>
>>55673263
>You'd probably be best off using Qt, even if that means having to go to C++.

Can't stress it enough.

It's the difference between a well supported, well documented, well engineered semi-standard in embedded and certain other industries and a rigged together, poorly documented clusterfuck which is barely maintained to a point that it's high time for a sane fork and refactor or rewrite effort with insane owners.
With GTK you are basically the GNOME devs bitch, yet their newest design fallacies aren't documented.
>>
>>55673285
Haven't used Athena, so I can't say anything about that.
>>
>>55661489
What would you say is/are the correct scenario/s where C is the right choice as a programming language?
>>
LOL I created this thread yesterday, I didn't know it still existed
>>
>>55673395
Microcontroller programming with very little code that don't support threads or dynamic memory allocation and very simple games.
Although even in those niches there are other and potentially better choices.
>>
File: XL7i6NR.jpg (336KB, 900x1200px) Image search: [Google]
XL7i6NR.jpg
336KB, 900x1200px
>>55673147
It's called "reasoning about your code". It makes you a better programmer. It's why compiled > interpreted languages.
>>
>>55673147
to be fair, sometimes Valgrind just refuses to work with some libraries. I tried to run an OpenGL 1.0 game I made with Valgrind. It wasn't able to render a single frame in 5 minutes.
Thread posts: 121
Thread images: 8


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.