Programming with C

Can I mix an 'if' statement with a case? It would make this a lot easier if I could.

Other urls found in this thread:

cplusplus.com/reference/cstdio/scanf/
twitter.com/NSFWRedditGif

...

What do you mean? Give an exemple.

Also if I write a letter in one of your questions your program will crash.

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

>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

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

>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?

no you can't

>Even that C book in the pic is very dated, with up to date C texts existing

>tfw I just bought that book

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

Nostalgia + contrarianism + it takes time for classics to be recognized as such.
Obviously, are you autistic or just stupid?
Sorry.

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.

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

>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;

>try stopping me.

compiler error: using strcpy and not strncpy

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

why not scanf? i thought that was okay to use

>Using C++ over the superior C

why is it bad to put declarations at top?

New software is still being written in ANSI C for compatibility. K&R is still a solid way to learn C

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.

C is dated

I am this user: I am *not* (I would never call a fellow Veeky Forums poster a "stupid fuck".)

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.

C and C++ each have their place and use. Only bakas hold one above the other.

Some projects/standards require all declarations at the top of the program. For example, the Linux kernel

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

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.

Go back to your postironic scalable javascript framework, Ashish

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

cplusplus.com/reference/cstdio/scanf/

I never said that I do.
I absolutely hated C++ in my university class and by far prefer C.
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.

>(I would never call a fellow Veeky Forums poster a "stupid fuck".)
You should give it a go, you get too many flat-earthers for witticisms every time

>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?

Go is meant to compete with Java in the web back end stuff and such. It is not a language with Veeky Forums applications

>Is Rust better than Go?
The two languages are intended for completely different tasks. I'm not sure why everyone wants to compare them.