Learning C - please help

Pages: 12
hi all, i was learning c++ for few months now but now i want to learn c first, i used eclipse juno for c++ learning and it worked perfectly. Since i'm learning C now i tried to do it in eclipse but i have issues with it. printf and scanf and other stuff. Any thoughts?

can someone recommend me good ide for learning C.

Much abliged
but now i want to learn c first

Why?

can someone recommend me good ide for learning C.

Code::Blocks. Or just any old text editor + compiler. I don't use an IDE for writing C. But I do for C++. Not sure why.

printf and scanf and other stuff.

Issues such as...? Did you include stdio.h?
microcontrolers.

yeah, it seams codeblocks at least works moste of the time for me, for c that is. it could be me beeing used to work a lot with cin and cout in c++ to learn.


say, if i make simple code like this in eclipse juno

int number;
number = 0;

printf("enter whole number!");
scanf("%d", &number);

printf("Number you've entered is %d", number);


it wont work, it wont even show "enter whole number" text.

im kinda demoralized atm, i'm very confident in c++ as beginner, but this c thing issues i'm haveing really isn't fun.

edit:
I know it sounds odd, but i've spent last 14years as designer and i just want to start doing real stuff not pixels ;)
Last edited on
I told you the likely issue; you're not including the appropriate header/s. It would help if you posted the error message and the whole code.
i do use <stdio.h>.

i have to go now, it late but i'll post the code tomorow.

much abliged for your help mate
I compiled this as a C program IronManCro ...

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
int number = 0;

int main(void)
{
 printf("enter whole number! ");
 scanf("%d", &number);
 printf("\nNumber you've entered is %d\n", number);
 getchar();

 return 0;
}


And this is the output I got ...



enter whole number! 25

Number you've entered is 25
Press any key to continue

If you aren't seeing that, then something else is wrong. By the way, had I compiled that as a C++ program using C++ compilation, i.e., used a *.cpp extension on my source code file instead of just C, I would have had exactly the same results. C++ includes C as a subset. Many of us compile as C++ but still use a lot of Cisms (depending on how successfully (or not)we've made the transmorgrification).
Last edited on
By the way, I hope you guys who work on embedded microcontrollers and such stuff with asm and c would improve your coding a bit! Last week I had to have the repair man down to look at my only one year old washing machine. It wouldn't start and appeared dead. Know what the repairman told my wife? Its computer locked up! We're talking here about my washing machine! Had to unplug it for a half hour to reset its computer. Then it worked. I think computers have gone too far. Blast the miserable beasts!!!
I've just realized that theres a huge difference between c++ standard i/o, c++ has cin cout and it handless any type by default, however in c you have to be very picky how do you call the data type.

found this discusions, very similar to my problem,
http://stackoverflow.com/questions/3744776/simple-c-scanf-does-not-work

http://stackoverflow.com/questions/3723768/help-with-c-scanf-syntax/3728189#3728189


quite confusing compared to c++.

btw for anyone learning c, this tutorials i've found are only one that i found to be complete for c, hope it helps.

http://www.youtube.com/user/iTzAdam5X?feature=watch
Ok so i've reinstalled everything on my computer, now it all works fine.

however, i'm wondering why did i had to use %s for char input so my program would work insted what seams more logical to me, the %c for scanf function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>

int main()
{
    float priceInput = 0;
    float total = 0;
    float totalStorage[10];
    int receiptNum = 0;
    char choice = 'y';
    int x;

    printf("Welcome to Ironman receipt calculator\n\n");

    while(choice != 'n')
    {
        if(choice == 'y')
        {
            total = 0;
            printf("Receipt%d:\n", receiptNum+1);
            printf("====================\n(0)to close\n");

            do
            {
                printf("ItemPrice: ");
                scanf("%f", &priceInput);
                total += priceInput;
            }
            while(priceInput != 0);

            printf("--------------------\n");
            printf("Total cost: %.2f$\n\n", total);

            totalStorage[receiptNum] = total;
            receiptNum++;
            printf("Open new receipt? y/n ");
            scanf("%s", &choice);
        }
        else if((choice != 'n') && (choice != 'y'))
        {
            printf("Invalid input, try again!\nOpen new receipt? y/n ");
            scanf("%s,", &choice);
        }
    }

    for(x = 0; x < receiptNum; x++)
    {
        printf("Receipt%d: %.2f$\n", (x+1), totalStorage[x]);
    }

    return 0;
}


Welcome to Ironman receipt calculator

Receipt1:
====================
(0)to close
ItemPrice: 78.59
ItemPrice: 0
--------------------
Total cost: 78.59$

Open new receipt? y/n y
Receipt2:
====================
(0)to close
ItemPrice: 57.6
ItemPrice: 0
--------------------
Total cost: 57.60$

Open new receipt? y/n y
Receipt3:
====================
(0)to close
ItemPrice: 17.25
ItemPrice: 0
--------------------
Total cost: 17.25$

Open new receipt? y/n n
Receipt1: 78.59$
Receipt2: 57.60$
Receipt3: 17.25$

Process returned 0 (0x0)   execution time : 15.553 s
Press any key to continue.
Last edited on
AFAIK, %s is safer.
i'll remember that, thx :)
closed account (z05DSL3A)
IronmanCro,
try: scanf(" %c", &choice); note the space in front of the %c.

that may flush stdin and then lets the user input the character.

you could get all funky and try scanf("%*[ \n\t]%c",&choice);, that should ignore spaces, new lines and tabs, and take the first char it comes across.

or a safer option may be to use getc(stdin); or getchar();
Last edited on
The bottom line is that scanf is rather funky. You wouldn't want to sell a program that collects user input with scanf, and in fact, I'd be hesitant about even giving one away using it. In my opinion, its a bit more useful with files, i.e., fscanf. The issue involves stray characters in the keyboard input buffer, mostly.

The function blocks, and for a more professional console mode input experience I'd recomend the low level Win32 Console Mode Api functions.
@freddie, what about *Nix command line utilities? I'm sure those use scanf().
Use Assembly for microcontrollers. Some don't have alot of memory for holding code.
Thank you guyz so much for pitchin in on this.

All this code i'm writing is just an excercise to confirme what i've learnd in c++ before doing anything concrete. To see do i have solid grounds for c, logic and all other stuff, not so much about makeing some serious software at this point.


Theres no rush, im learning this as hobbiest, i figured since i've rounded the design sphere in past 14years going from graphics design all the way to industrial design, now i want to round the other sphere of interest, programing(c, c++, python, assembly), electronics and MCU) with focus on learning it right not fast;)

Hi ResidentBiscuit,

Ironman has already seen some of the irregularities of scanf. Its just fine for what he is doing in my opinion. But likely the irregularities he is seeing are the result of the things already mentioned. It might help to clean out the input buffer first wirh a getchar(). Then output a "Press Any Key To Continue...". There are various solutions. A lot of us have been dealing with these issues since early DOS days. I use scanf myself; just not much and not for anything where an actual user instead of a coder would be at the other end of it. Its quick and dirty, IMHO.

By the way IronmanCro, I just tried your program with %c instead of %s and far as I could tell it worked normally. That doesn't really mean it wasn't acting up for you though. What was it doing that was abnormal with %c (if you are still following your thread)?
Grey Wolf wrote:
try: scanf(" %c", &choice); note the space in front of the %c.
that may flush stdin and then lets the user input the character.


Huh, why "may"? It must (assuming by "flush" you meant "consume leading whitespace", since there is no such thing as flushing stdin)

If some compiler fails to skip leading whitespace (spaces, newlines, tabs, and anything else for which isspace() returns true) in this case, it has a huge bug.
closed account (z05DSL3A)
Huh, why "may"?
...because I can't remember if it is specified in the standard or not.
Pages: 12