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

Programming with C

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: 35
Thread images: 4

Can I mix an 'if' statement with a case? It would make this a lot easier if I could.
>>
>>>/g/
>>
What do you mean? Give an exemple.

Also if I write a letter in one of your questions your program will crash.
>>
>>8805821
[code] int input;
float grams_per_mile;
int odometer;
const char* pollutants[] = {
"Carbon monoxide", "Hydrocarbons",
"Nitrogen oxides", "Nomethane hydrocarbons"
};

for(int i = 0; i < 4; ++i){
printf("(%d) %s\n", (i+1), pollutants[i]);
}
printf("Enter pollutant number: ");
scanf("%d", &input);
printf("Enter number of grams emitted per mile: ");
scanf("%f", &grams_per_mile);
printf("Enter odometer reading: ");
scanf("%d", &odometer);

printf("The pollutant is: %s\n", pollutants[input+1]);
[/code]

no idea if code tags work outside of /g/ but whatever. havent used c in awhile and im tired, but i think thatll work.

as a general rule, if you have to repeatedly do something, like print a set, you want probably want to store the objects as an array and iterate over them, and then if you want to select one, you simply index the array.

no idea what you mean by mix if statements with switches. switches are just generally just implemented as jump tables if the indexes are contiguous, or implemented the same as if statements, if they are fragmented
>>
Go to /g/ or irc or a programming related sub..

But yeah, you can do switch (var) { case 1: if else blah blah break; case 2: blah blah.. } etc
Sorry for shit formatting I'm on mobile.
>>
>>8805890
>But yeah, you can do switch (var) { case 1: if else blah blah break; case 2: blah blah.. } etc
you can do this, but its still bad form
>>
File: 1488759634319.png (769KB, 1052x1342px) Image search: [Google]
1488759634319.png
769KB, 1052x1342px
>>8805821
learn C
>>
switch/case only works for numerical values, that is: no strings, and no runtime evaluations, and no composite datastructures.
Note that chars are numerical values.
You can only use numbers (or specific chars) in the case tests, you can not do something like case sqrt(n) or case "falafel".
>>
>>8806025
>all these pictures of 2dwaifus with programming tomes
>none of them are modern texts

Why is this? These images always feature SCIP and other, older texts. Even that C book in the pic is very dated, with up to date C texts existing. Where's my 2hus fawning over Drozdek's Data Structures and Algorithms? My 2dqts working in R or Python?
>>
>>8805821
no you can't
>>
File: ShinobuWateryEyed.gif (128KB, 500x500px) Image search: [Google]
ShinobuWateryEyed.gif
128KB, 500x500px
>>8806306
>Even that C book in the pic is very dated, with up to date C texts existing

>tfw I just bought that book
>>
>>8806349
>dated
C really hasnt changed that much.
My university exam had to be written in either ansi or 99.
I used the exact same book my dad did and it was as "up to date" as the lecture.
>>
>>8806306
Nostalgia + contrarianism + it takes time for classics to be recognized as such.
Obviously, are you autistic or just stupid?
Sorry.
>>
>>8806460
C has changed a ton you stupid fuck, it has changed a ton in that you need to know to not use half the functions from the stdib and to never use strcpy or motherfucking scanf.
>>
>>8806472
>C has changed
No it didnt.

The way you properly write C has changed and that is something you can easily learn in a lecture.
For me a book about a programming language is more like a dictionary, something where you look up things you forgot.

>you need to know to not use half the functions from the stdib and to never use strcpy or motherfucking scanf
try stopping me.
>>
>>8805821
>putting all your declarations at top
>using floats over doubles
>indenting scanf
>not using C++

What are you doing nigger?

>Can I mix an 'if' statement with a case

No but you can or cases together by not breaking them

case 1:
case 2:
//do shit if case 1 or 2
break;
>>
>>8806488
>try stopping me.

compiler error: using strcpy and not strncpy
>>
>>8806506
>not compiling with "-ansi"

Also C mostly isnt taught so people can use it, it is a stepping stone to get comfortable with important programming concepts.
You usually move on to something like C++ afterwards.
>>
>>8806472
why not scanf? i thought that was okay to use
>>
File: hurr.jpg (7KB, 213x250px) Image search: [Google]
hurr.jpg
7KB, 213x250px
>>8806519
>Using C++ over the superior C
>>
>>8806501
why is it bad to put declarations at top?
>>
>>8806306
New software is still being written in ANSI C for compatibility. K&R is still a solid way to learn C
>>
>>8806655
It's stupid. It's better to place them right before you use them so the reader knows what you're using them for. Also it makes it far less likely that you'll use a variable before initializing them.
>>
>>8806306
C is dated
>>
I am this anon: >>8806306
I am *not* >>8806472
(I would never call a fellow /sci/ poster a "stupid fuck".)

>>8806460
>>8806349
C as a whole hasn't changed but there is value in learning a language with a text that contains the most up-to-date information.

>>8806519
>>8806640
C and C++ each have their place and use. Only bakas hold one above the other.
>>
>>8806673
Some projects/standards require all declarations at the top of the program. For example, the Linux kernel
>>
>>8806519
>[C] is a stepping stone to get comfortable with important programming concepts.
I know this isn't quite what you meant, but I need to rant.

I hate this concept that programmers should always start by learning C, and I have no idea why people believe it. People seem to think that C code is somehow more "real" than languages like Python, and that learning to write code in high-level languages will "ruin" programmers. It's stupid; C is exactly as real and as fake as Javascript, and the most important ideas in programming are ideas about abstraction, process and specification - not how to use "maloc" and "free".
>>
>>8806501
>>8806655

Variables in C go at the top so you can see how much memory a program uses.

Often times C programs have a setup stage where they call malloc. This is done so that they don't get a null pointer in the middle of execution by calling malloc over and over again. OP is not calling malloc here, but it's usually done just because that's what people are used to. You can see that the program only uses a couple bytes.

>>8806964

Go back to your postironic scalable javascript framework, Ashish
>>
>>8805821

Your program has many problems and that is a nonsense question

First of all, the "char * " argument to the first scanf: A char is only one byte but a string is arbitrarily long. You need to make pnum an array of chars.

It's also stupid how you try to get a single _ASCII_ char from scanf and then use it like an int in the switch. If you had more than 9 options it wouldn't work because '10' means the number you would get if you interpreted the ascii representation of "10" as a binary number instead of ascii. Just get an int from scanf, that's the point of (scan format).

http://www.cplusplus.com/reference/cstdio/scanf/ <----- you dumbfuck
>>
>>8806640
I never said that I do.
I absolutely hated C++ in my university class and by far prefer C.
>>8806964
I believe that starting by learning C is the best thing you can do.
C offers 2 very important things, firstly it is a very compact language, which is reasonably easy to teach.
And it allows someone to get a basic understanding how a computer works.

Of course later on abstractions will become more and more important, but you really need to understand the basics before you can go there.
>>
>>8806693
>(I would never call a fellow /sci/ poster a "stupid fuck".)
You should give it a go, you get too many flat-earthers for witticisms every time
>>
>>8807576
>I believe that starting by learning C is the best thing you can do.
>C offers 2 very important things, firstly it is a very compact language, which is reasonably easy to teach.
>And it allows someone to get a basic understanding how a computer works.
>Of course later on abstractions will become more and more important, but you really need to understand the basics before you can go there.

this, basically. I know "programmers" nowadays that don't know what is the heap or the stack.
>>
People say Go is ugly, do you agree?

Is Rust just better?
>>
>>8807687
Go is meant to compete with Java in the web back end stuff and such. It is not a language with /sci/ applications
>>
>>8807687
>Is Rust better than Go?
The two languages are intended for completely different tasks. I'm not sure why everyone wants to compare them.
Thread posts: 35
Thread images: 4


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