Hey guys. Thanks for taking the time to read this post.

I'm relatively comfortable with C++ and am taking a class in C for the first time. I'm finding it difficult and frustrating to adapt to the new language. It's kind of like trying to box with a hand tied behind my back.

At any rate, I have a few general questions and a few specific questions that might help me to better understand general and specific aspects of the language . Feel free to tackle the questions that you'd like.

Is C a subset of C++? Do they share libraries? Are there libraries that are specific to C or C++?

Part of the reason I'm having trouble with C is that I'm finding it difficult to search for answers online. With C++ I can just include the phrase "C++" on any question and I'll arrive at a destination.

With C, however, I'm really not sure what combination of words to add to my search. "C programming," "C language," "C," Any advice?

Additionally, are there any websites that you can point me to for general information on C?

http://www.cprogramming.com/ seems to have some of the information that I'm looking for.

Also, I'm having trouble with a specific piece of code.
This is a homework assignment to explain the relationships between two numbers through addition, subtraction, multiplication, division, and % (modulus?). I'm not sure if it matters, but I work in Cygwin.

The problems I'm encountering are with division and modulus. The numbers that I am using are 5 and 10, respectively.

The output that the program gives me is as follows:

> 5 / 10 = 0.000000.
> 5 %d = 10.

I'm looking for an answer of .5 and 5(?).

I'm pretty sure that this has something to do with variable types; but changing the variable types in the declarations does does not produce the results that I'd like.

#include <stdio.h>

int main(void)
{

  int num1;
  int num2;
  float div;
  int mod;
  
  printf("> Please enter an integer and hit the [Enter] button.\n> ");
  scanf("%d", &num1);
  printf("\n> Please enter another integer and hit the [Enter] button.\n> ");
  scanf("%d", &num2);

// division
  div = num1 / num2;
  printf("> %d / %d = %f.\n", num1, num2, div);
// remainder  
  mod = num1 % num2;
  printf("> %d % %d = %d.\n", num1, num2, mod);
  
  return 0;
}

Additionally, how can I display the "%" character without sabotaging my printf(); command?

That would be in this section of code:

printf("> %d % %d = %d.\n", num1, num2, mod);

I really want to learn this language; I just need a push in the right direction. Thank you for reading.
- LordoftheFly

Recommended Answers

All 3 Replies

You are losing the precision in your division. You need to cast:

div = (float)num1 / (float)num2;

Your printf requires "%%" to represent a literal % symbol:

printf("> %d %% %d = %d.\n", num1, num2, mod);

Can't help you with the search parameter for "C", but I'll be watching to see what others say.

Cheers!

>I'm relatively comfortable with C++ and am taking a class in C for the
>first time. I'm finding it difficult and frustrating to adapt to the new
>language. It's kind of like trying to box with a hand tied behind my back.

If you're used to C++ and the benefits of such things as the more powerful standard library, templates, and RAII (those are the things I miss most when I use C), then C can certainly feel restrictive.

>Is C a subset of C++? Do they share libraries? Are
>there libraries that are specific to C or C++?

C is not a subset of C++, but C++ does implement the C libraries as they were before C99 (C++0x should include more of C99). C does not share any of C++'s libraries.

>Additionally, are there any websites that you can point me to for general information on C?
Honestly, the best place to get your questions answered would be forums like this (or cprogramming.com) where there are people knowledgeable about C. Otherwise, just google different combinations of keywords until you find matches. Searching for C is more difficult than C++ as you've already discovered. Not for lack of resources, of course. Also, keep in mind that C and C++ of often similar enough that the low level C++ answer is also the C answer with a few minor changes.

>http://www.cprogramming.com/ seems to have some of the information that I'm looking for.
The tutorials there are dubious at best. I tried to update them a few years ago to make them technically correct, but they're still pretty weak in terms of usefulness. The FAQ is far better, and the forum is very strong. I'd recommend starting with the FAQ and if that doesn't answer your questions, post to the forum.

You are losing the precision in your division. You need to cast:

div = (float)num1 / (float)num2;

Your printf requires "%%" to represent a literal % symbol:

printf("> %d %% %d = %d.\n", num1, num2, mod);

Awesome, it worked! Thanks for the coding help. These kinds of details can be tough to iron out sometimes, so I appreciate your assistance.

Honestly, the best place to get your questions answered would be forums like this (or cprogramming.com) where there are people knowledgeable about C.

Much appreciated. This website's forums seem to have the most polite and knowledgable users. I appreciate that response times are generally fast and that answers are decipherable.

Thanks for the info and insights.
- LordoftheFly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.