I don't want to write a code I don't understand ..!!

This is a discussion on I don't want to write a code I don't understand ..!! within the C Programming forums, part of the General Programming Boards category; Hi, I'm new in the world of programming, I just begun to program in C langauage.. I started to write ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    25

    I don't want to write a code I don't understand ..!!

    Hi,
    I'm new in the world of programming, I just begun to program in C langauage.. I started to write my first program using Borland C++ and I can use different styles :

    1.
    Code:
    #include <stdio.h>
       main() {
         printf("Help meee");
       }
    2.
    Code:
    #include <stdio.h>
       int main() {
         printf("Help meee");
       return 0;
       }
    3.
    Code:
    #include <stdio.h>
       int main(void) {
         printf("Help meee");
       return 0;
       }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    I suggest choosing one of the latter two since the first lacks a return type for main(). The last one comes directly from the C Standard. You should also keep the indentation consistent:
    Code:
    #include <stdio.h>
    int main(void) {
        printf("Help meee");
        return 0;
    }
    Now, what do you not understand?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    25
    I don't understand the differences, I installed Div C++ and I got another code for hello world app:

    Code:
    int main(int argc, char *argv[])
    {
      printf("Hello World!\n");
    
      printf("Press ENTER to continue...\n");
      getchar();
      return 0;
    }

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    In windows the console window will disapear as soon as the program finishes. Printing a message than waiting for input keeps the window open until the user responds.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    I don't understand the differences, I installed Div C++ and I got another code for hello world app:
    You probably mean Dev-C++. Anyway, what you are looking for is explained in an FAQ on main and another FAQ on command line arguments.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,454
    There are basically two prototypes for main - both as you see. In C, you must specify void in the parameter list if it does not take any arguments. int main() is technically wrong as it should be int main(void), so there's two to choose from!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    25
    Quote Originally Posted by laserlight View Post
    You probably mean Dev-C++. Anyway, what you are looking for is explained in an FAQ on main and another FAQ on command line arguments.
    Yes Dev-C++ .. I'm gonne read it but now I have another Q:

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
      int a[10];
      int i, j, temp;
      printf("Give me 10 integers:\n");
      
      for (i = 0; i<10; i++){
          printf("Give number &#37;d: ",i+1);
          scanf("%d",&a[i]);
      }
      
      printf("\nAfter sorting ..\n");
      getchar(); // why do I have to add this ??
      
      for (i = 0; i<10; i++){
          for (j = i; j<10; j++) {
              if (a[j] > a[i]) {
                       temp = a[i];
                       a[i] = a[j];
                       a[j] = temp;
              }
          }      
      }  
      
      for (i = 0; i<10 ; i++) {      
          printf("Number %d : %d\n",i+1,a[i]);
      }
      
      printf("Press any key to exit ...\n");
      getchar();
      return 0;
    }
    Firstable, I couldn't run it after compiling but I added getchar(); I just did that ..

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,454
    Getchar() basically waits for a keypress. It halts the program execution until you press a key - I'm assuming you need to pause to see that message before proceeding?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    Firstable, I couldn't run it after compiling but I added getchar(); I just did that ..
    Perspective and Elysia both answered that, but we actually have an FAQ on it. You might want to read the FAQs.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    25
    Quote Originally Posted by Elysia View Post
    Getchar() basically waits for a keypress. It halts the program execution until you press a key - I'm assuming you need to pause to see that message before proceeding?

    Mmm so Getchar() waits for keypress, O.K .. I get it but why do I have to add it ?? why do my program stop if I don't add Getchar() ???


    Normally it has to process untill if arrives at }

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,454
    What? No? Your program does not stop if you don't add it - that's the reason you do add it.
    The second statement is also false; execution does not stop when it gets to a }.
    In machine language, there are no }. There are just instructions. Loads of them and nothing more.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, I suppose what you mean is that the "window just disappears" after you enter the 10 numbers. This is because the scanf() function leaves a newline dangling in the input buffer, so your first getchar() actually removes the newline, then the second getchar() waits for you pressing enter.

    In standard C library, all input is "cooked", meaning that the input is not given directly to the application, but rather stored temporarily until a newline is hit, so any "getchar()" will wait for newline to enter. Try this:
    Code:
    int main()
    {
       char ch;
       do {
           ch = getchar();
           printf("char = %c\n", ch);
       } while(ch != '\n');
       printf("Hit enter to continue\n");
       getchar();
       return 0;
    }
    If you use that, you will see that all the "char = ..." output comes out in one hit just after you hit enter, rather than one char at a time like you may expect. This helps the application in that it doesn't have to deal with detecting backspace, arrows, delete keys and other complications and deal with these - you get what the user has confirmed, not all the typos and changes of mind.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    25

    I don't understandddd ...

    You are talking about the function scanf () which leaves according to you a newline dangling in the input buffer !!!

    BUT

    Code:
    #include <stdio.h>
    int main(void) {
    	 printf("Help meee");
    	 return 0;
    }
    If I write this program in Dev-C++ and compile+run it, I don't see anything because the black window disapears after runnig but I tried the same program in Borland C++ and I could see my output !!!!!!


  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,454
    That's IDE-specific. Windows automatically closes a dos prompt after the program finishes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Yes, well this is covered in the FAQ. http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    matsp also just explained it quite well in post #12.

    Nevertheless, I'll say something too . . . the console window -- that black window that your program uses to get input and print output -- this window doesn't always act the same way. Some editors will automatically keep it open for you (like MSVC and, apparently, Borland C++), but you can't be certain of this. Some editors don't keep it open for you (like Dev-C++), and when you don't run it from an editor it's not kept open either. (Unless you run it from the command prompt.)

    So, keeping that in mind, you can force the window to be kept open in case it isn't done automatically. Something like
    Code:
    getchar()
    works most of the time, although if you're using scanf() for input you might have to use something more like
    Code:
    while(getchar() != '\n') {}
    getchar();
    to eat up any characters already existing in the standard input.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need software to help to understand C source code
    By Kincider in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2006, 09:44 PM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  3. Some humour...
    By Stan100 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-06-2003, 09:25 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  5. How to write code to change the printer resolution
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 07-26-2002, 06:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21