C General Thread

Thread for C appreciation and programming.
I will start with an exercise from KandRC. Exercise 1-13.
#include

main(){
int c, length;
length = 0;

while( (c = getchar()) != EOF){
if( c == ' ' || c == '\t' || c == '\n'){
/* found one word */
while( --length >= 0)
putchar('-');
putchar('\n');
length = 0;
}
else{
/* inside word */
putchar(c);
++length;
}
}
}


cc 1-13.c

./a.out
I'm not a robot
I'm---
not---
a-
robot-----

Other urls found in this thread:

man.openbsd.org/style
abebooks.com/servlet/BookDetailsPL?bi=19786293942
matt.sh/howto-c
news.ycombinator.com/item?id=10908217
algorist.com/
cert.org/secure-coding/publications/books/cert-c-secure-coding-standard.cfm?
news.ycombinator.com/item?id=10872747
edx.org/course/how-code-systematic-program-design-part-ubcx-spd1x
youtube.com/playlist?list=PLSE8ODhjZXjbisIGOepfnlbfxeH7TW-8O
twitter.com/SFWRedditGifs

yeah baby

('-')

Who's the qt girl with the K&R?

>girl

As someone going through K&R,
I'm a little confused about Exercises 1.6 - 1.8.

I can get both the sample programs in the book, as well as what I can come up with for the examples, to compile. However, once compiled, they run but don't actually appear to do anything.

Am I supposed to be making them read a file?

sample program:
#include
main()
{
double nc;
for(nc = 0; getchar() != EOF; nc++)
;
printf("%.0f\n", nc);
}

This confused me too when I first started. So this book was written for C back in the Unix days it expects a unix like enviornment and for you to know the basics. This progam would then take input from a file which is the screen and end when you send the EOF character to the screen. I think my workaround since I knew c++ was to use some other character to signal end of input I used backquotes ( ` ). But the right way would be to just input the EOF might be ctr-D or something else I forget and it works as expected.

This thread is so dead.
/g is for plebs

gay

I don't understand why this doesn't work
#include
#define DEFAULT 1
int main()
{
puts("I'll mimic everything you type~~");
char in=DEFAULT;
while((in=getchar())!='\n')
{
int number_of_chars = 0;
if (in!=' ')
{
number_of_chars++;
putchar(in);
}
else
{
for (int i = 0; i

Maybe you should stop putting pajeet in your namefield and people would take you a bit more seriously

Control + c

Which utility library to use for files+directory handling, allocators, data structures and threads when there's so many of them like qlibc, tbox, pjlib, glib?

What are you doing?
Replacing all ' ' with '~'?

Bamp
good to see a thread for C even in a place like Sup Forums.
Sorry though, I don't have anything to show right now. jus bampin dawg

bump

Tell me again, why should I use C instead of C++?

Anybody interested this is how you learn C in 2017:

>Intro
- K&R as everybody starts with this.
- A current operating system kernel style guide, to show you what declarations today should look like: man.openbsd.org/style (linux has a guide too)
- CS:APP as a second intro, learning bits/binary/floating point/compilers/assembly level pointers ect ect.
abebooks.com/servlet/BookDetailsPL?bi=19786293942 ($20 'global' edition)

>Intermediate
- The Book 'Hackers Delight 2' to continue with CS:APP bitshifting tricks
- This guide: matt.sh/howto-c
- Critique of said guide: news.ycombinator.com/item?id=10908217
- This book to learn algorithm design in C: algorist.com/ (includes free lectures)

>Competent
- This book, whatever latest edition: cert.org/secure-coding/publications/books/cert-c-secure-coding-standard.cfm?
- Finally the Art of Software Security Assessment where you go back over every program you've ever written with both the CERT guide and this book in order to audit your old programs for vulnerabilities.

Posts like this help too, from people in the industry who have exp pushing out 'secure' C programs: news.ycombinator.com/item?id=10872747

You can do this in about a year doing it everyday for say, 2 hours. I did.

This, indeed Ctrl-D sends end of input signal.

There's a small book called "Unix Programming Environment" by kernighan everybody should read since it will teach you basics of unix which still works today such as Ctrl-D on any current Linux/BSD system (plus you learn filters like sed so can get better at vim if you choose that editor). There's also an interesting last chapter where they show you exactly how they did software development back then from scratch

Fuck off with libraries. If you want to use libraries, go use another language.

This is C. Write your own library

K&R is not a good first book. Read pages 20 - 23, it does not actually define what an array is.

>Thank you good sire
How much do you earn now given that those stuffs are pretty damn good for one to become above average programmer and you said you did it.

It's assumed you already know basic programming and want to learn C. If anybody doesn't know the basics that's what this course is for: edx.org/course/how-code-systematic-program-design-part-ubcx-spd1x (based on the book HtDP) or SICP.

However most people I know learned to program from K&R and just searched for information like 'what is an array' into youtube and got a video explaining it.

The only thing I used C for was mainly in other courses I took customizing dbms like Postegres and for a security internship I did reverse engineering malware. youtube.com/playlist?list=PLSE8ODhjZXjbisIGOepfnlbfxeH7TW-8O

I work remote as a DbaaS "distributed systems programmer" which is almost entirely in Go and Ruby, but learning how C works inside out definitely helps me every single day to do things like optimize the Go GC, understand that division takes a lot of clock cycles and bitshifting is preferred, or understand Ruby better. After the security internship they offered me a F/T job as a Jr Consultant but by that time I was more interested in distributed systems, and it paid a lot more than what the local sec corp offered me. I live in a shitty country and my job pays almost $100k USD per year which is a fortune here, the local appsec job offered $55k/year in my currency which is monopoly money tier and was like 12hrs a day of work.

I-Is C++ allowed?

C++ users are objectively the cutest

checked, and that's a pretty gud guide. I'm a somewhat seasoned C programmer and I pretty much agree. That and some resources I didn't know.
How about modern C or whatever the name? I've heard good stuff about it though I've never read it.
Also, Expert C should definetely be in that list.

Y-you mean that user?

Yeah, I wish I could be smart and cute enough to be a C++ programmer

Thanks user. You're cute too.

aaaaand it's a weeb thread

I'm sorry

user, it's a weeb website.

still better than 99% of the threads in Sup Forums
and nice repeated digits

thanks, I call them displacement dubs

But user, this is a weeb site.

This is now an embedded C thread.

#define F_CPU 16000000UL
#define BAUD 9600
#include
#include
#include
#include
#include

int direction, mph, lasthallread, rps = 0, i = 1;
char directionstr[10];

void inituart(){
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
UCSR0A &= ~(_BV(U2X0));
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
UCSR0B = _BV(RXEN0) | _BV(TXEN0);
}

void putcharuart(char c, FILE *stream){
if (c == '\n')
{
putcharuart('\r', stream);
}
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
}

void setupdigital(){
DDRB &= ~_BV(PB0);
PORTB |= _BV(PB0);
}

void setupadc(){
ADCSRA |= _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0);
ADMUX |= _BV(REFS0);
ADCSRB &= ~(_BV(ADTS2) | _BV(ADTS1) | _BV(ADTS0));
ADMUX |= _BV(ADLAR);
ADCSRA |= _BV(ADEN);
ADCSRA |= _BV(ADATE);
ADCSRA |= _BV(ADSC);
}
FILE outputuart = FDEV_SETUP_STREAM(putcharuart, NULL, _FDEV_SETUP_WRITE);

main (){
inituart();
stdout = &uart_output;
setupadc();
setupdigital();
while (1){
while (i < 1000){
if (PINB & _BV(PB0)){
lasthallread = 1;
}
if (!(PINB & _BV(PB0)) && lasthallread == 1){
rps++;
lasthallread = 0;
}
_delay_ms(1);
i++;
}
mph = round(rps*1.1244);
rps = 0;
i = 1;
/*field too long*/
}
}

Bitwise operators are the comfiest.

What does this program do? Read speed values and convert them?

Hash tables or balanced trees?

Should I start with C#, then move to C/C++?

I have good PHP, decent Python and limited Java knowledge. (With some BCPL but we won't go into that right now).

In your code, number_of_chars gets initialized to zero each time after the while loop since you declare it there. Look at OP code.

>C# for Windows programming, Unity, Xamarin, i.e botnet
>C for Linux programming and embedded system
>C++ for everything else

>C++ for everything else

Just accept the fact that the only reason you're not using C everywhere is money.

You use C to avoid the C++ programmers desu.

But C++ programmers are the cutest?

>namefag
>hates C++ users

If you're talking about then it's a malicious attempt to lure cuter user into hell.

You can still write windows programs in C using the Win32 Api.

Listen senpai, C++ isn't always shit. But it allows people to write shit code. And once people get into the habit of making shit code they become bad programmers.

My point is, you could use C++ just like C but with some extra features here and there. But that opens up for a lot of bad programmers to submit code to your projects... And suddenly your project isn't C with some convenient features from C++, it's a bloody full on C++ project using all of C++'s bad stuff.

I don't hate C++. I actually use it along side OpenGL and it works fairly well because I know what I'm doing. But doing big projects in C++ will in most case scenarios turn into a scary mess. Trust me senpai.

I suggest we change the name of this thread to /gcc/ - General C Circlejerk
pretty pls?

>says C++ has bad stuff
>doesn't actually give examples of said bad stuff
I am compelled by your words to stimulate the fatty neurons in my cranial organ, anonymous.

If you want an example just look at the boost library.

C++ gives developers a false sense of security. Like using new and delete is safer than malloc and free, I swear some people actually believe that for some reason. C++ didn't even have a good string implementation until it was too late and a lot of software was written using it's own string type implementation making it hard to use two libs with each other for instance.

Also I get you're concern with me not giving any examples, I apologize for that. I'll do better in the future.

Another bad thing is the whole header and source file mess. I get why people had that in the beginning of C, obviously to save CPU cycles during compilation. Lib interfaces were not the only reason for header files back in the days. And yes you could technically use #pragma once or a simple if def in the source file and include them in other source files... But that's a "hack" and C++ was not meant for that.

You can have your opinion about #define in C but I think macros should always be avoided where possible. So while macros in C are sometimes unavoidable that's not the case in C++ and therefore the fact that they even support #define in C++ (Don't say muh backwards capability) is a bad practice and motivates very very strange code. Trust me, I've seen people macro "THEN" and "END" to "{" and "}".

It looks to me like it's supposed to be incrementing nc until getchar returns EOF? Which I think only happens if you press a special key combination, like CTRL+Z I think it is on Windows? And then it prints out nc which is the number of characters read from stdin before EOF.

Not sure why it's using a double though, since it's only counting whole characters.

> languages should force you to do things in one specific way

no they should not. C/C++ are more powerful languages than really any other and should give you the tools to deal with any situation. Removing defines would be like a paint company removing shit brown for being an ugly color.

Also nothing is stopping you from writing implementations in headers

Ok thanks for the info but those seem like edge-cases or outliers desu. I've personally never seen any of the sort of stuff you mentioned but then again the only other person that uses C++ amongst my colleagues is a coauthor of mine who taught me C++ and everybody else either uses Fortran or Python.

why use c when I can use thousands of c lines in one python line

it really is a whole different game. I haven't fucked around with embedded projects at all, but I feel like I will definitely need to know this shit when I start uni for CompE in 7 months. Bitwise operators are cute tho

Is it a sin to generate C code from header files using Haskell to avoid having to write the library bindings for pforth manually? Currently working on the tool so I can get glew, glfw, openal, ect... bindings in pforth and can't be bothered to do it manually

C is only good for embedded programming, C++ has a wealth of libraries unlike C. Unless you want to re-invent the wheel, C is pointless for anything but small projects

Wew don't bully C pls

C programming a modern approach it's still probably the best beginner's book, even though it's kind of outdated but there are other reference books for that.

What was the total time span from learning-to-getting-a-job it took you?

C is an okay language, made horrible by the C standard, POSIX and portability fags. But god there's nothing I hate more than the C standard library and its ilk.

pls rspnd

>C is okay, made horrible by its literal definition
What?