[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 General 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: 64
Thread images: 18

File: 1460592869226.png (1MB, 1000x1400px) Image search: [Google]
1460592869226.png
1MB, 1000x1400px
Thread for C appreciation and programming.
I will start with an exercise from KandRC. Exercise 1-13.
#include<stdio.h>

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-----
>>
yeah baby
>>
('-')
>>
>>58802232
Who's the qt girl with the K&R?
>>
File: image.jpg (5KB, 300x57px) Image search: [Google]
image.jpg
5KB, 300x57px
>>58802938
>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 <stdio.h>
main()
{
double nc;
for(nc = 0; getchar() != EOF; nc++)
;
printf("%.0f\n", nc);
}
>>
>>58803050
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.
>>
>>58802232
This thread is so dead.
/g is for plebs
>>
File: c-105.jpg (117KB, 900x1200px) Image search: [Google]
c-105.jpg
117KB, 900x1200px
>>58802232
gay
>>
I don't understand why this doesn't work
#include <stdio.h>
#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<number_of_chars; i++)
{
putchar('~');
}
putchar('\n');
number_of_chars=0;
}
}
return 0;
}

>>
>>58805075
Maybe you should stop putting pajeet in your namefield and people would take you a bit more seriously
>>
>>58804926
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?
>>
>>58805190
What are you doing?
Replacing all ' ' with '~'?
>>
Bamp
good to see a thread for C even in a place like /g/.
Sorry though, I don't have anything to show right now. jus bampin dawg
>>
>>58802232
Tell me again, why should I use C instead of C++?
>>
>>58802232
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: http://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.
https://www.abebooks.com/servlet/BookDetailsPL?bi=19786293942 ($20 'global' edition)

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

>Competent
- This book, whatever latest edition: https://www.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: https://news.ycombinator.com/item?id=10872747

You can do this in about a year doing it everyday for say, 2 hours. I did.
>>
>>58804926
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
>>
>>58805240
Fuck off with libraries. If you want to use libraries, go use another language.

This is C. Write your own library
>>
>>58809644
K&R is not a good first book. Read pages 20 - 23, it does not actually define what an array is.
>>
>>58809644
>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.
>>
>>58810604
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: https://www.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.
>>
>>58810695
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. https://www.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.
>>
File: miyako_code.png (2MB, 1280x720px) Image search: [Google]
miyako_code.png
2MB, 1280x720px
>>58802232
I-Is C++ allowed?
>>
File: 1486022046731-2.gif (931KB, 500x281px) Image search: [Google]
1486022046731-2.gif
931KB, 500x281px
>>58811525
C++ users are objectively the cutest
>>
>>58809644
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.
>>
>>58811622
Y-you mean that anon?
>>
>>58811718
Yeah, I wish I could be smart and cute enough to be a C++ programmer
>>
File: yearning_lesbian.jpg (115KB, 721x717px) Image search: [Google]
yearning_lesbian.jpg
115KB, 721x717px
>>58811744
Thanks anon. You're cute too.
>>
>>58811825
>>58811744
>>58811718
>>58811622
>>58811525
aaaaand it's a weeb thread
>>
File: B013L6K0HC.01.S001.JUMBOXXX.jpg (959KB, 1489x1920px) Image search: [Google]
B013L6K0HC.01.S001.JUMBOXXX.jpg
959KB, 1489x1920px
>>58811866
I'm sorry
>>
>>58811866
Anon, it's a weeb website.
>>
>>58811866
still better than 99% of the threads in /g/
and nice repeated digits
>>
>>58812037
thanks, I call them displacement dubs
>>
File: akari_chuuni.png (87KB, 283x326px) Image search: [Google]
akari_chuuni.png
87KB, 283x326px
>>58811866
But anon, this is a weeb site.
>>
This is now an embedded C thread.

#define F_CPU 16000000UL
#define BAUD 9600
#include <util/setbaud.h>
#include <stdio.h>
#include <avr/io.h>
#include <util/delay.h>
#include <str.h>

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*/
}
}
>>
>>58812235
Bitwise operators are the comfiest.

What does this program do? Read speed values and convert them?
>>
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).
>>
>>58805190
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.
>>
>>58813500
>C# for Windows programming, Unity, Xamarin, i.e botnet
>C for Linux programming and embedded system
>C++ for everything else
>>
File: paul deninose.jpg (133KB, 640x325px) Image search: [Google]
paul deninose.jpg
133KB, 640x325px
>>58813605
>C++ for everything else
>>
>>58813605
Just accept the fact that the only reason you're not using C everywhere is money.
>>
File: pic_c-girl_anime.png (769KB, 1052x1342px) Image search: [Google]
pic_c-girl_anime.png
769KB, 1052x1342px
>>58809298
You use C to avoid the C++ programmers desu.
>>
>>58813802
But C++ programmers are the cutest?
>>
File: really_implores_you_to_ponder.png (147KB, 231x361px) Image search: [Google]
really_implores_you_to_ponder.png
147KB, 231x361px
>>58813802
>namefag
>hates C++ users
>>
>>58813962
If you're talking about >>58811622 then it's a malicious attempt to lure cuter anon into hell.
>>
>>58813605
You can still write windows programs in C using the Win32 Api.
>>
File: 1475188746718-g.jpg (475KB, 852x973px) Image search: [Google]
1475188746718-g.jpg
475KB, 852x973px
>>58813990
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?
>>
File: interesting.jpg (13KB, 112x212px) Image search: [Google]
interesting.jpg
13KB, 112x212px
>>58815332
>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.
>>
File: pic_fite-me.gif (605KB, 500x280px) Image search: [Google]
pic_fite-me.gif
605KB, 500x280px
>>58815508
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 "}".
>>
>>58803050
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.
>>
>>58815802

> 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
>>
File: natsumi_tears_of_joy.png (797KB, 812x806px) Image search: [Google]
natsumi_tears_of_joy.png
797KB, 812x806px
>>58815802
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.
>>
>>58802232
why use c when I can use thousands of c lines in one python line
>>
>>58812235
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
>>
>>58802232
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
>>
>>58816389
Wew don't bully C pls
>>
>>58809644
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.
>>
>>58810885
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.
>>
File: 1476419131941.webm (505KB, 720x404px) Image search: [Google]
1476419131941.webm
505KB, 720x404px
>>58817060
pls rspnd
>>
>>58817338
>C is okay, made horrible by its literal definition
What?
Thread posts: 64
Thread images: 18


[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.