Which program I should use for "C" programming?

ErAnushka

ErAnushka

@eranushka-4BgWe8 Oct 26, 2024
Hello again 😀 I'm using "codeblocks" for "c" programming. But I don't think it's a great program for learning the basics of "C".
Do you have anyother program for "C" ?
If allowed, anyone can upload TCC? I think we're using that in our college computers.

I'm asking this because, I've seen one tutorial with getch() function. we write this at the end so the program will wait for our input and then only it can close itself.
getch = get character 😀 (I guess)
There one thing more used, and that is return(0), so many things are there😲
I need to practice these stuff, But I want to use the program that is used in colleges; or similar to that. Anyone can help me please ?

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Aug 22, 2012

    getch = get character 😀 (I guess)
    Thats right.
    Which operating system you are using...
    If windows 7 then it is little bit tricky to install TCC. You need to use DOSBOX.
    #-Link-Snipped-#
  • ErAnushka

    ErAnushka

    @eranushka-4BgWe8 Aug 22, 2012

    ianoop
    Thats right.
    Which operating system you are using...
    If windows 7 then it is little bit tricky to install TCC. You need to use DOSBOX.
    #-Link-Snipped-#
    works perfectly.... thanks a lot... and sorry for replying late, I'm in the college just for attendance 😀

    I need to know, is that necessary to write return(0) at the end? If I don't write it, I get an error saying that it must return a value or something like that. Why it need to return a value? If I write return(0) at the end, it compile and run without errors.
    So, return(0) is to return a 0 value to the program? I really don't understand.
    And I've to write it in every program?
  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Aug 22, 2012

    I think your program is like this
    #include<stdio.h>
    int main()
    {
    printf(“Hello World\n”);
    return 0;
    }
    here int before main() is expecting to return something from the function main() , That is why a return statement is necessary. It is intrinsic that main() should return a int value.
  • [Prototype]

    [Prototype]

    @prototype-G9Gn5k Aug 22, 2012

    Its not exactly necessary to return 0, but depending on compiler, it may or may not return the compilation error. GCC doesn't requires that, while it sounds like TCC does requires it.

    Whenever an error occurs, an error code, which is an integer, is returned. Just to ensure that the program has finished successfully & executed till end, we write return 0. That means, if 0 is returned, the program executed till the last line. The TCC as well requires just because a function is expected to return a value. You can write return 123 & it'll work too.

    You can just specify the type of main as void if you don't want to write return 0, but its not recommended since you'll not be able to see if any error occurred since main will not return any error code to the OS.
  • ErAnushka

    ErAnushka

    @eranushka-4BgWe8 Aug 22, 2012

    #-Link-Snipped-# #-Link-Snipped-# Thanks for the info but still I've some questions 😀

    here is my simple program:

    #include <stdio.h>
    int main (void)
    {
    printf ("Hello World");
    getch();
    return(0);
    }

    here, I wrote return(0) which will return a 0 value. We can also write it as "return 0", right?
    But as Prototype said, I can write "return 1" or "return 2" etc.
    So, I think the program should return any value, no matter what it is. Please correct me if I'm wrong 😀


    I made another program for addition of two numbers:
    #include <stdio.h>
    int main (void)
    {
    int no1;
    int no2;
    int ans;

    printf ("enter the value:");

    scanf("%d", &no1);

    printf("enter another value:");

    scanf("%d", &no2);

    ans = no1 + no2;

    printf ("The answer is %d", &ans);

    getch();

    return(0);
    }


    but I got the wrong answer:
    [​IMG]

    after searching on internet, I realized that I need to add #include <conio.h> but that won't work... not sure what I'm doing wrong...😕
  • [Prototype]

    [Prototype]

    @prototype-G9Gn5k Aug 22, 2012

    #-Link-Snipped-#, Yes, you can return anything that you can uniquely identify as successful program execution. Returning 0 has become a tradition because no other error code is 0. It may be possible that you return something like 87234 but its already an error code for some problem. In such case, you won't be able to identify if that code is returned by you or was returned due to error.


    You don't write "&" in printf. It should be like below.

    printf ("The answer is %d", ans);
  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Aug 22, 2012

    In line: printf ("The answer is %d", &ans);
    are you scanning again?? it should be like this
    printf ("The answer is %d", ans);
  • ErAnushka

    ErAnushka

    @eranushka-4BgWe8 Aug 22, 2012

    [Prototype]
    #-Link-Snipped-#, Yes, you can return anything that you can uniquely identify as successful program execution. Returning 0 has become a tradition because no other error code is 0. It may be possible that you return something like 87234 but its already an error code for some problem. In such case, you won't be able to identify if that code is returned by you or was returned due to error.


    You don't write "&" in printf. It should be like below.

    printf ("The answer is %d", ans);
    Thanks a bunch, I got this now 😀
  • ErAnushka

    ErAnushka

    @eranushka-4BgWe8 Aug 22, 2012

    ianoop
    In line: printf ("The answer is %d", &ans);
    are you scanning again?? it should be like this
    printf ("The answer is %d", ans);
    I thought writing "&" will calculate the answer 😒
    Just removed it, it's working fine now 😀 Thanks 😀
  • [Prototype]

    [Prototype]

    @prototype-G9Gn5k Aug 22, 2012

    ErAnushka
    I thought writing "&" will calculate the answer 😒
    Just removed it, it's working fine now 😀 Thanks 😀
    Writing "&" passes the address of the variable..😀

    You can as well send private message to me regarding the small errors..😀
  • ErAnushka

    ErAnushka

    @eranushka-4BgWe8 Aug 22, 2012

    [Prototype]
    Writing "&" passes the address of the variable..😀

    You can as well send private message to me regarding the small errors..😀
    Okay 😀
  • rahul69

    rahul69

    @rahul69-97fAOs Aug 23, 2012

    In TCC "void main()" works fine😀
  • ErAnushka

    ErAnushka

    @eranushka-4BgWe8 Aug 23, 2012

    rahul69
    In TCC "void main()" works fine😀
    I'm using int main (void) 😀
  • Neeraj Sharma

    Neeraj Sharma

    @neeraj-iAaNcG Aug 25, 2012

    rahul69
    In TCC "void main()" works fine😀
    It works fine but its always advisable to go with int main()
  • KenJackson

    KenJackson

    @kenjackson-mBf7HF Aug 25, 2012

    ErAnushka
    I made another program for addition of two numbers:
    #include <stdio.h>
    int main (void)
    {
    ...
    ans = no1 + no2;

    printf ("The answer is %d", &ans);

    getch();

    return(0);
    }
    Here's my version. There's a big difference. I put all the inputs on the command line.

    I'm guessing from your use of "getch()" that you are clicking on the executable or the icon of a Windows shortcut. If you do that, it would exit before you could see the answer. So getch() holds the window open until you press a key.

    With my solution, I start with an open command shell (preferably a bash shell--if using Windows I use <a href="https://cygwin.com/" target="_blank" rel="nofollow noopener noreferrer">Cygwin</a>), but a Windows CMD shell would do. So I don't need the final getch().

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
     
    int main(int argc, char *argv[])
    {
        int error = 0, i, sum;
     
        for (i=1, sum=0;  i<argc && !error;  i++)
        {
            if (isdigit(argv[i][0]))
                sum += strtol(argv[i], NULL, 0);
            else
            {
                fprintf(stderr, "ERROR: \"%s\" is not numeric\n", argv[i]);
                error = 1;
            }
        }
     
        if (!error)
            printf("The sum is %d\n", sum);
        return error;
    }
  • ErAnushka

    ErAnushka

    @eranushka-4BgWe8 Aug 25, 2012

    Nick_Sharma
    It works fine but its always advisable to go with int main()
    Okay 😀
  • ErAnushka

    ErAnushka

    @eranushka-4BgWe8 Aug 25, 2012

    KenJackson
    Here's my version. There's a big difference. I put all the inputs on the command line.

    I'm guessing from your use of "getch()" that you are clicking on the executable or the icon of a Windows shortcut. If you do that, it would exit before you could see the answer. So getch() holds the window open until you press a key.

    With my solution, I start with an open command shell (preferably a bash shell--if using Windows I use <a href="https://cygwin.com/" target="_blank" rel="nofollow noopener noreferrer">Cygwin</a>), but a Windows CMD shell would do. So I don't need the final getch().

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
     
    int main(int argc, char *argv[])
    {
        int error = 0, i, sum;
     
        for (i=1, sum=0;  i<argc && !error;  i++)
        {
            if (isdigit(argv[i][0]))
                sum += strtol(argv[i], NULL, 0);
            else
            {
                fprintf(stderr, "ERROR: \"%s\" is not numeric\n", argv[i]);
                error = 1;
            }
        }
     
        if (!error)
            printf("The sum is %d\n", sum);
        return error;
    }

    I'm just started using C 😒 I'm not the master but your program looks cool. I don't know why you've used int main(int argc, char *argv[]) it's totally new to me 😀

    and your program is so different.... [] is used for arrays, I guess.
    I hope I'll make programs like you 😀
  • KenJackson

    KenJackson

    @kenjackson-mBf7HF Aug 25, 2012

    ErAnushka
    I don't know why you've used int main(int argc, char *argv[]) it's totally new to me
    That's the standard prototype for main(). argc is the number of arguments, plus the command itself, on the command line. argv[] is an array of pointers to the arguments.

    So if you name the resulting executable something imaginative like prog, You could use it like this:
    prog 123 456 789
    Then argc would be 4 and pointers to these 4 strings would be in argv[]: "prog", "123", "456", and "768".

    Pointers and arrays take a little getting used to.
  • ErAnushka

    ErAnushka

    @eranushka-4BgWe8 Aug 25, 2012

    KenJackson
    That's the standard prototype for main(). argc is the number of arguments, plus the command itself, on the command line. argv[] is an array of pointers to the arguments.

    So if you name the resulting executable something imaginative like prog, You could use it like this:
    prog 123 456 789
    Then argc would be 4 and pointers to these 4 strings would be in argv[]: "prog", "123", "456", and "768".

    Pointers and arrays take a little getting used to.

    Yes, I'm learning pointers and arrays 😀 Once I master it, then only, I can understand what you did in the program 😀

    One thing I want to ask, computer science and IT both are different branch?
    I can score very well in these branch except programming and electronics stuff 😐
  • Neeraj Sharma

    Neeraj Sharma

    @neeraj-iAaNcG Aug 25, 2012

    ErAnushka
    Yes, I'm learning pointers and arrays 😀 Once I master it, then only, I can understand what you did in the program 😀

    One thing I want to ask, computer science and IT both are different branch?
    I can score very well in these branch except programming and electronics stuff 😐
    Programming is something that matters a lot specially when you are in Computer Science or IT branch. Your interviews for good companies will always be programming-centric So its better to master programing. Your score or percentage only will be important to pass the cut-off filter for appearing in the company but after that its all your programming knowledge and its theoretical base. Its good that you are learning. It will be better that you dedicate this thread completely for your programming preparation. Learn a new concept and write programs here whenever you have doubts or you need explanation. Based on that, I will post one more program and make you understand it. Let's get going...
  • ErAnushka

    ErAnushka

    @eranushka-4BgWe8 Aug 26, 2012

    Nick_Sharma
    Programming is something that matters a lot specially when you are in Computer Science or IT branch. Your interviews for good companies will always be programming-centric So its better to master programing. Your score or percentage only will be important to pass the cut-off filter for appearing in the company but after that its all your programming knowledge and its theoretical base. Its good that you are learning. It will be better that you dedicate this thread completely for your programming preparation. Learn a new concept and write programs here whenever you have doubts or you need explanation. Based on that, I will post one more program and make you understand it. Let's get going...
    I'm in civil branch 😀 I'm preparing CPU subject. but it would be awesome if you share some useful info which is rarely available on any books 😀
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Aug 26, 2012

    I thought writing "&" will calculate the answer
    actually "&" means the "address" or more technically, you can call it as "memory address"
    printf() just needs values to output them
    whereas, scanf() needs to store the values hence we provide "memory address" that is "&" to store the value at a memory location.
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Aug 26, 2012

    #-Link-Snipped-#
    In TCC "void main()" works fine
    Using void main() is a bad practice of programming. Majority of lecturers use void main() while teaching the programs, but it is a bad practice as you will never know what value your program returns (it takes a garbage return which varies each time you execute the program) Hence it is advisable to use int main()
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Aug 26, 2012

    #-Link-Snipped-#

    but it would be awesome if you share some useful info which is rarely available on any books
    As you are just a starter in C, I'd suggest you to go with your course book. In that way you'll easily make your basics strong. And after that you can go for other books or take useful info from anyone of us!!
  • ErAnushka

    ErAnushka

    @eranushka-4BgWe8 Aug 26, 2012

    #-Link-Snipped-# thank you for the info 😀 especially about the "&" 😀