[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
C programming
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 140
Thread images: 10
File: brainkiller.png (153 KB, 333x354) Image search: [Google]
brainkiller.png
153 KB, 333x354
Where do I start?
>>
>>54756296
Terry Davis, HolyC will teach you pretty quickly
>>
>>54756296
string operator = /* Find this answer out */
printf("%s is the basis for all structural data", data, operator);
>>
>>54756296
>C

don't bother, there are literally no C jobs last time I checked
>>
>>54756349
Dont care about a job
>>
>>54756296
int main() { ....
>>
https://en.wikibooks.org/wiki/C_Programming
>>
>>54756338
There are no strings in C fucking noob. >>54756349
Ignore autistic people that can't socialize properly so don't get jobs. I'm literally debugging a Linux kernel by fucking with the C code and getting paid $$ by a mid size company.

OP, there is only 1 book you need:

The C Programming language second edition, ANSI C. By Denise Ritchie and Brian Kernighan

Only textbook I actually care about. It's short, to the point, fucking amazing. Just buy it or find one of the gazillion ebooks out there. It's from the like fucking 80s
>>
>>54756296
It's the language with the highest success/atrociousness ratio.
C programming a modern approach is good because it explains a lot of the pitfalls of C, and C has many.
>>
>>54756296
open a .pdf reader (for the K&R book) and two terminal windows (one for text editing, another for compiling).
>>
>>54756506
>The C Programming language second edition, ANSI C. By Denise Ritchie and Brian Kernighan

No, stop the meme

>while(*d++ = *s++);

No, just no.
>>
>>54756506
HAH! i wrote that shitty code on purpose! Now who's the shitty n00b :)))))))))))))))))
>>
Sams Teach Yourself C in 24 Hours
>>
What IDE/Text editor to use if I want to write C in linux? no vim/emacs please I can't handle that right now

anything with autocomplete?
>>
>>54756569
Yeah it's not perfect, but you're going to encounter legacy code that does that. Every neet thinks it's all about writing perfect maintainable code, but the real world is dealing with other people and older standards.

Besides, that book gives me the tools needed to understand your line of code (or, in this case, it's compiler/architecture independent.)
>>
>>54756616
Gedit is comfy. Or else np++ through wine.

I only use visual Studio when doing .NET development for autocomplete because .net languages were designed for autocomplete. C wasn't really, you don't need it. As horrible as it is, grepping through the linux kernel looking for the correct variable name/function is a lot more valuable than the tracing of the stack
>>
>>54756616
Notepad++

doesn't have an error detector as far as i've seen, however.
>>
>>54756630
>Besides, that book gives me the tools needed to understand your line of code (or, in this case, it's compiler/architecture independent.)

But it does not give you the tools to avoid all the pitfalls. It's always been memed as the definitive bible on C, but that book is the single greatest culprit for buffer overflows, ever.
>>
>>54756659
>>54756661
how do you all feel about C in Netbeans or Eclipse?
>>
>>54756616
i don't use them but you can try sublimetext or atom
notepad++ does work on wine btw
>>
>>54756569
this. there are much better books out there, like "Pointers on C"
>>
C Primer Plus is solid
>>
Not OP.

I just bought Programming in C (4th Edition) by Stephen G. Kochan. Literally a few minutes ago.

How did I do?
>>
Not OP, but how is K N King's C Programming: A Modern Approach?
>>
>>54756962
Pretty good, it's the way to go together with "Pointers on C".
>>
>>54756926
I really liked that book. Do all the examples and all the exercises (like, write them out, compile them, get them to run). You'll learn a lot.
>>
>>54756296
Are you totally new to programming? If so check out C Programming Absolute Beginners Guide 3rd Edition by Greg Perry. Solid intro book even if it does look a little childish.
>>
>>54756926
Very good book. Probably one of the best if you already have a little experience in another language such as visual basic. Doesn't have to be much experience, just a basic understanding that you write code then compile it to get an executable, understanding your computer has temporary (volatile) memory (RAM) and long term (non-volatile) memory (HDD/SSD), you can store things in RAM super quick compared to a HDD, etc.

It is a great book. Kochan write good.
>>
>>54756296
You shouldn't begin with it but you should eventually read Expert C Programming Deep C Secrets
>>
>>54756569
>while(*d++ = *s++);

They actually have that in the book? Wow, just wow.
>>
>>54757507
Can you explain why that is bad?
>>
>>54757507
That code triggers me so much
I bet K & R though they were clever when they thought of it.
Still better than the glibc strcpy
http://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/x86_64/strcpy.S;h=3f90c0020a25f2899e5cf74b6077df2f4cbfaada;hb=HEAD
>>
>>54757518
Buffer overflows, people being incredibly stupid, causing 99% of code execution vulns
http://insecure.org/stf/smashstack.html
>>
>>54757518
Difficult to read. It's easy when it's done in a small program, but when you have tens of thousands of lines of code written like this, it's going to be hard to maintain the code. But, in the context of his time, they probably had to do this because of memory and data storage limitations.
>>
>>54756349
>Don't bother learning to play the violin. The jobs are taken.
>Don't bother with gardening your own roses. It's a terrible investment.
>No point in learning Russian. It takes way too long to reach the level of a two-way interpreter.
>Don't bother swimming. The competition for the Olympic team is hard and you're too old to start anyway.
>>
>>54757518
Two reasons:
- one is stylistic, because of terseness and because the operator precedence are not obvious, but this is a minor objection
- the second one is a semantic one: this function does copy only well formed null terminating strings, it can create all sorts of havoc if used incorrectly, and is a prime example of why C is such an insecure language
>>
>>54757690
all of those things are true tho
>>
>>54757551
>I bet K & R though they were clever when they thought of it.

They still do, probably.

>Null terminated strings

Literally a (multi) billion dollar mistake.
>>
>>54757803
if you're not terminating your strings correctly, you shouldn't be using strcpy anyway
That's what strncpy is for.
>>
>>54756296
K & R... Historical reasons. Plus it'll impress other programmers you have it even though you'll never use it or read it.

C in a Nutshell - Prinz and Crawford. For doing your shit with. Covers C11 and C99.
>>
>>54757824
Null terminating string are just a bad idea.
Are there any other languages beside C that use them?
>>
>>54757690
Though these are all true, C knowledge is still highly sought after in fields where speed matters.
Hell, some people in my company program in Assembly.
>>
>>54757853
C99 and C11 are such minimal additions to C, they don't require their own books.

>C99
oh wow some gnu extensions got standardized, you can use strncpy portably now
>C11
oh wow there's a mulithreading library, and it's optional
oh there's a bool type now, cool i guess
>>
>>54757891
>oh there's a bool type now, cool i guess

Let me guess, it's implemented like this:

typedef char bool;
#define TRUE 1
#define FALSE 0
>>
>>54757758
Truth is not the highest level of understanding m8.
>>
File: c.jpg (25 KB, 510x385) Image search: [Google]
c.jpg
25 KB, 510x385
>>54757891
>>
>>54756296
This website is the bomb for c.
http://c.learncodethehardway.org/book/
>>
so..

there's no string

is there a way to print out a char array, besides using a loop?
char arr[]="Some message here"
>>
>>54758048
yes, printf
>>
>>54758048
Nigga, look up pointers and that's all you'll ever need to know about C. Almost everything is an abstraction built on top of pointers.
>>
>>54758025
That books is absolute shit
http://hentenaar.com/dont-learn-c-the-wrong-way
>>
>>54758025
zed shaw is such a faggot
>>
>>54758156
>Not learning programming languages the hard way

Why even get up in the morning
>>
>>54758025
>>54758098
It is funny because neither Learn C the Hard Way nor K&R are good ways to learn C.
A much better resource is either "C Primer Plus" or "C Programming A Modern Approach".
>>
>>54758298
true. some other good books are
>Programming in C by Kochan
>C Programming Absolute Beginner's Guide by Perry
>Beginning C by Horton
>Head First C by Griffiths
>>
>>54756788
No, just no
>>
>>54758098
The main critisim seems the be the auther attitude( wich seem irrelevent to me) and the fact that the auther does not give the reader everthing he needs to know ( instead insisting that the reader do his own research). I liked it because it teaches you how to find answers and to think critically. Mind you I started the tutorial as a complete novice and I'm not saying I'm still not one, but Never did I feel lost or out of options. also keep in mind the title " learn c the hard way"
>>
>>54756616
Sublime is baller.
>>
File: 8Z0bjzV.png (76 KB, 1058x794) Image search: [Google]
8Z0bjzV.png
76 KB, 1058x794
>>54757686
>>54757730
Oh, I assumed if the code was hard for me to understand, it means I need to gain more fluency in C. In other words, I need to bring myself up to their level, not force them to simplify their code down to babby mode that children can understand.

I thought that if I didn't understand every little order of operations trick and idiosyncrasy out there in industry, people would smugly say "Lol learn2C newfag". I spend an unhealthy amount of time studying complex declarations like pic related. This thread makes me think I'm not as retarded as I thought.

My biggest difficulty in reading kernel code right now is the amount of constants and macro definitions fucking everywhere. I remember watching a video where Linus Torvalds said he can read C code just as if he's reading English and immediately know what the code is doing. I'd like to be at that level someday.
>>
File: mBtMXjR.png (83 KB, 1058x794) Image search: [Google]
mBtMXjR.png
83 KB, 1058x794
>>54758730
That image isn't even correct. They're pointers to functions that return pointers to functions that return void.

http://c-faq.com/decl/spiral.anderson.html
>>
>>54758730
How do you know he's not lying?
>>
>>54758800
Linus? I guess we don't.

I found the video.

>I don't know, maybe I'm autistic or something.

https://www.youtube.com/watch?v=S5S9LIT-hdc
>>
>>54758730
>This thread makes me think I'm not as retarded as I thought.

C has something like 18 level of operator precedence. Now, for the expert programmers this is a plus, but it is the root of unreadable declaration like that.
There are languages without operator precedence where you (if they had the right semantics) could do such a thing without effort.
C is cleverly retarded.
>>
void (*(*f[])())()

declare f as array of pointer to function returning pointer to function returning void
>>
File: animath.jpg (23 KB, 640x480) Image search: [Google]
animath.jpg
23 KB, 640x480
i = 1;
i = ++i++;
printf("%d\n",i);
>>
>>54758902
So, you have to basically parse the expression from inside out?
>>
>>54756296
Start with the Greeks.
>>
>>54758938
>error: lvalue required as increment operand
>>
>>54758942
Wow, amazing how you've figured this out. Most C programmers don't figure this out in years.
>>
>>54758938
doesn't compile
>>
Figure out the basics from any C book/website, start working on Project Euler and google new stuff as you go.
>>
>>54759001
Learning Scheme helps.
>>
>>54758850

he was trying to explain what it's like to program to non-programmers in that video. it was a throw-away line that you've read too much into. he wasn't saying he could take any inscrutable mess of code and instantly understand it.
>>
http://www.tutorialspoint.com/compile_c_online.php

#include <stdio.h>

int main()
{
int i = 1;
printf("%d\n",++i + i++);

return 0;
}


Prints 5.

I thought first the postfix increment would increment i from 1 to 2, then return the value before it was incremented, which is 1.

Then the prefix increment would increment 2 to 3, so you'd have 3 + 1 = 4.

Why does the postfix rule only apply to stuff like return(i++) or array[i++]? The postfix rules are the biggest bullshit to understand about C.
>>
>>54759136
It's time to read the C standard.
>>
File: 1460834085307.jpg (95 KB, 621x646) Image search: [Google]
1460834085307.jpg
95 KB, 621x646
>>54759136
>>54759154
#include <stdio.h>

int main()
{
int i = 1;
printf("%d\n",i++ + ++i);

return 0;
}


If you switch the order it prints 4. I'm tripping balls over here.
>>
>>54759136
It's undefined behavior, the compiler can do whatever the fuck it wants in that situation and it's correct.
>>
>>54758938
>The expression ++i++ is evaluated as ++(i++) which is illegal in C as the postfix increment returns a value and prefix incrementing on a that value makes no sense.

>What you have is somewhat equivalent to: ++(5) which is obviously illegal as you can apply prefix increment on something that's not a l-value.
>>
>>54759136
>>54759193 is correct, if the compiler caused the code to rm -rf --no-preserve-root / it would still be correct
But what's happening is the i++ is increasing it to 2 and then then ++I is increasing i to 3 and then adding them
>>
>>54759192
>If you switch the order it prints 4. I'm tripping balls over here.

Welcome to C. I don't think this kind of code shows up in real life, it's there just to show the poor lit corners of the language. Can experienced C programmer confirm?
>>
File: Screenshot_2016-05-26_21-14-46.png (23 KB, 409x626) Image search: [Google]
Screenshot_2016-05-26_21-14-46.png
23 KB, 409x626
>>54759192
>>
>>54759280
Unfortunately, you're wrong
http://www.cl.cam.ac.uk/~pes20/cerberus/notes50-survey-discussion.html
>>
>>54759280
>Can experienced C programmer confirm?

According to the language spec, the result is undefined. There is no definition whether the arguments of an arithmetic operator (+ - * /) are evaluated from left to right, or right to left -- the implementation is free to do it either way. The only operators with a specifically defined order are && and ||, which are always evaluated left to right.
>>
>>54759360
Amazing stuff.
>>
>>54759360
Where did you find this?
>>
>>54759549
http://www.open-std.org/JTC1/SC22/WG14/www/docs/n2012.htm
>>
>>54756296

Start with A and then do B before getting to C.
>>
>>54756506
>There are no strings in C fucking noob
the standard says you're retarded
>>
>>54757507
looks pretty standard and straightforward
>>
>>54757686
>Difficult to read
only if you don't know C
>>
>>54759136
>>54759192
Undefined behaviour. Any result is meaningless.
>>
>>54757730
>because the operator precedence are not obvious
it's obvious if you know the language; only """web developers""" expect to read and write code without knowing the language
>only well formed null terminating strings
that's the only kind of string
>>
>>54757965
you guessed wrong
>>
>>54758382
there's also the fact that the author doesn't know C, by his own admission; he just hunted for random information to write the book
>>
>>54759741
>that's the only kind of string
For writing serious exploits
>>
>>54759798
for everything
>>
>>54759824

no.
>>
>>54759938
yes
>>
>>54759952
You are going senile, Brian.
>>
>>54756296
Realize that everything you do in C essentially revolves around manipulating buffers of memory. Things like "strings" make more sense when you look at everything as just chunks of memory. Pointers and arrays become a lot easier to reason about when you're thinking in terms of locations of memory and ranges of bytes from a location.
>>
>>54759977
You don't know c, rajesh.
>>
>>54759938
One of the reasons that Pascal lost out to C was because the model of strings with a length at the beginning made them almost unusable in practice.
>>
>>54760004

We are not using teletypes anymore, grandpa.
>>
>>54760043
>One of the reasons that Pascal lost out to C was because the model of strings with a length at the beginning made them almost unusable in practice.

Do you have a source for that claim?
>>
>>54760068
The first char of a Pascal string holds the size, and a char only goes up to 255, so a Pascal string was limited to 255 characters.
>>
>>54760068
In Pascal the number of elements in each array type is determined at compile-time and cannot be changed during the execution of the program. Hence, it is not possible to define an array whose length depends in any way on program data
Pascal has neither array initialization nor a means of determining arbitrary array sizes at compile time
Pascal has no support for variable-length arrays, and so any set of routines to perform string operations is dependent on a particular string size
And yes, arrays of characters are the same as strings in both C and Pascal
>>
>>54760047
The year is 2016, shit stain.
>>
>>54760130

That seems to me like a problem with the specific implementation of Pascal strings, not an indictment of length-delimited strings period.

The use of a safe string library like https://github.com/msteinert/bstring seems like an almost prerequisite for starting a new project in C unless you just plain don't need strings.
>>
>>54760190
You shouldn't use teletypes in CURRENT_YEAR.
>>
>>54760242
You shouldn't be a pedophile.
>>
>>54760146
>And yes, arrays of characters are the same as strings in both C and Pascal

But Pascal strings are not null terminated.
They are something like
struct{
uint length;
char* string;
}
>>
>>54760224
It's not a specific implementation, it's part of the language standard that the size is one byte.
Besides, it would break compatibility with all other pascal programs if you changed the size because that would also change the array index of every element by one and it would be impossible to link with object code from other compilers.
The idea of having a set size for the length of the string is fundamentally flawed because if it is too small then nobody can make strings larger than the arbitrarily defined limit and if it is too large then it will cause too much overhead for short strings and if it's larger than the register size for a CPU it will have a huge impact on performance.
>>
>>54760259
>You shouldn't be a pedophile.

Says the guy who named the copy command after his favorite pastime.
>>
>>54756506
>Denise Ritchie
>>
>>54756616
use micro
>>
>>54760287
>I got told, must control damage
I won, shitkid :^)
/thread
>>
>>54760278
Newer implementations have long strings, but I guess it's too late now.

>tfw the two most popular languages of the 80's got a simple thing like a string wrong

Kind of explain why are software is so shit, those guys were the "gurus", I cannot imagine what the average programmer writes.
>>
>>54760348
>He /threads himself

Enjoy you loli collection
>>
>>54760278
>It's not a specific implementation, it's part of the language standard that the size is one byte.

Read more carefully. I said the specific implementation of Pascal strings. That meant on the language level - I did not mean to imply that there were Pascal implementations that were more flexible.

>>54760278
>The idea of having a set size for the length of the string is fundamentally flawed because if it is too small then nobody can make strings larger than the arbitrarily defined limit and if it is too large then it will cause too much overhead for short strings and if it's larger than the register size for a CPU it will have a huge impact on performance.
>too much overhead

I shouldn't need to tell you how bad an idea premature optimization is, not to mention that length-delimited carry several performance benefits as well.

Write code using a safe string library. If you then discover (after profiling, of course) that using said safe strings are a significant performance bottleneck, then you use C strings in that specific section of the code. Not a
moment before, and no-place else, keeping your attack surface as small as possible.
>>
>>54760370
>popular languages of the 80's got a simple thing like a string wrong
>Kind of explain why are software is so shit, those guys were the "gurus"
>I cannot imagine what the average programmer writes.

>
>>
>>54760370
when C was created, the PDP-11 had null terminated strings as part of it's assembly language. It wasn't necessarily "chosen" by the developers of C but was merely built in to the architecture on which C was designed
>>
>>54760483
Actually you could say that PHP is close to C philosophically.
Both kind of evolved to fill a niche, worse is better and all of that.
>>
>>54760532

It is no accident that PHP contains many copies of C standard library functions.
>>
>>54760524
>It wasn't necessarily "chosen" by the developers of C but was merely built in to the architecture on which C was designed

So much for portable assembler
>>
>>54760524
furthermore, as C became more popular, null terminated strings began to be accounted for in the design of future architectures starting in the 90's. Before C came along, Pascal style strings were actually the norm and were used in the design of string handlers of VAX and Z80 CPUs.
>>
>>54760574
C was never designed to be portable assembly, it was just designed as a tool to make the writing of Unix easier.
>>
>>54756616
Atom if you need a pretty gui, gedit otherwise.
>>
>>54760656
Your history is bassackwards kid
>>
Wow, look at you guys, a bunch of no name programers, arguing about something that literally NOBODY ELSE CARES ABOUT.
>>
>>54756296
Differentiating about data types. Learn how to use pointers as soon as possible.
Most people have problems understanding them, but they are essential to understand what the fuck is C.
>>
>>54758730
>see pic
That's why you use typedefs, you dum dum.
>>
>>54756296
K&R C
>>
>>54759994
whoa, really makes you think...
>>
https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list
>>
>>54756296
int main()
>>
>>54756506
>Denise Ritchie
Dennis Ritchie is not a girl!
>>
>>54757803
>Literally a (multi) billion dollar mistake.
Yep, it's amazing how absolutely fucking retarded people can be when programming.
If only people knew how to actually program.
>>
>>54757551
is there anything GNU doesn't fuck up?

>>54757803
>They still do, probably.
Ritchie isn't anymore ;_;
>>
>>54756506

So much this!

I've read a couple of fluffy "intro to c in 20 days" book and they are all shit.

K&R is arround for decades and it is the best book out there for beginners.
>>
>>54756349
Yeah I know right! I can't wait for the next Windows to be written in Javascript. I heard nVidia are working on using PHP for the next lot of graphics drivers!

Everyone should be able to write software, not just those people that study, that's boring!
Thread replies: 140
Thread images: 10

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

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.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.