Find the answer to your Linux question:
Results 1 to 10 of 10
Code: #include <stdio.h> main() { int x,y,b; x = 15; y = 10; b = x*y; printf(b); } how to display ? would i use printf? would i use return ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    May 2009
    Location
    Kitchener, Ontario, Canada
    Posts
    206

    how to print the total of 2 numbers?


    Code:
    #include <stdio.h>
    
    main()
    {
    	int x,y,b;
    	x = 15;
    	y = 10;
    	b = x*y;
    	printf(b);
    }
    how to display ? would i use printf?
    would i use return b;?

    and does any one recommend a good book for linux C++ programming what ive noticed is that C++ windows its a bit different like the #include <iostream> is non existent in linux or am i missing something.
    Last edited by donaldfarkas; 09-03-2010 at 01:08 AM.

  2. #2
    Just Joined!
    Join Date
    Sep 2010
    Posts
    1
    a.) Ask questions like this in the linux programming section. People here will generally get upset with questions that are about programming and not linux but....... since your asking about programming in linux.

    use this:
    #include <stdio.h>

    main(int argc, char** argv) // Don't worry about the meaning of this yet
    {
    int x,y,b;
    x = 15;
    y = 10;
    b = x*y;
    printf("%d\n", b);

    return 0; // Don't worry about the meaning of this yet either
    }

    this is a c program by the way but Im sure you already knew that.

    b.) Get the book "C++ How to Program" by Deitel and Deitel. Its expensive but if you are serious its a minimal investment.

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,598
    This is C code, not C++. Try this:
    Code:
    #include <iostream>
    int main(void)
    {
        int x, y, b;
        x = 15;
        y = 10;
        b = x*y;
        cout << b << endl;
        return 0;
    }
    Note that with C++ and newer versions of C, main() must return an integer. Also, not declaring a return value for a function (including main()) implies an integer return value.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. #4
    Linux Newbie
    Join Date
    May 2009
    Location
    Kitchener, Ontario, Canada
    Posts
    206
    Quote Originally Posted by Rubberman View Post
    This is C code, not C++. Try this:
    Code:
    #include <iostream>
    int main(void)
    {
        int x, y, b;
        x = 15;
        y = 10;
        b = x*y;
        cout << b << endl;
        return 0;
    }
    Note that with C++ and newer versions of C, main() must return an integer. Also, not declaring a return value for a function (including main()) implies an integer return value.

    when i type in the above code it wotn work i tried this already.
    Code:
    [Donald@localhost ~]$ cd Desktop/C++
    [Donald@localhost C++]$ make test
    cc     test.c   -o test
    test.c:1:20: error: iostream: No such file or directory
    test.c: In function ‘main’:
    test.c:9: error: ‘cout’ undeclared (first use in this function)
    test.c:9: error: (Each undeclared identifier is reported only once
    test.c:9: error: for each function it appears in.)
    test.c:9: error: ‘end1’ undeclared (first use in this function)
    make: *** [test] Error 1

  5. #5
    Linux Newbie
    Join Date
    May 2009
    Location
    Kitchener, Ontario, Canada
    Posts
    206
    Quote Originally Posted by codewizzrd View Post
    a.) Ask questions like this in the linux programming section. People here will generally get upset with questions that are about programming and not linux but....... since your asking about programming in linux.

    use this:
    #include <stdio.h>

    main(int argc, char** argv) // Don't worry about the meaning of this yet
    {
    int x,y,b;
    x = 15;
    y = 10;
    b = x*y;
    printf("%d\n", b);

    return 0; // Don't worry about the meaning of this yet either
    }

    this is a c program by the way but Im sure you already knew that.

    b.) Get the book "C++ How to Program" by Deitel and Deitel. Its expensive but if you are serious its a minimal investment.
    Code:
    #include <stdio.h>
    
    main(int argc, char** argv) // On this line what those this mean? int argc, char** argv
    {
    	int x,y,b;
    	x = 15;
    	y = 10;
    	b = x*y;
    	printf("%d\n", b);  // what does %d mean?
    
            return 0;   
    }

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,598
    It seems you don't have the c++ compiler installed, or you need to rename your source file from test.c to test.cpp so make will know to use the c++ compiler. The C compiler doesn't know about C++ stuff like <iostream>, etc.

    As for your question about the arguments passed to main(), "int argc" and "char** argv", these are passed to your program when you have arguments on the command line. If your program doesn't take any arguments, then you can use the void argument instead. Also, it is better to use "const char** argv" since you probably don't want to be able to modify the contents of the arguments directly. So, argc is the number of arguments passed to the program from the command line, including the command itself, and argv is an array of strings that contains the actual arguments themselves. So, if you executed the command "test a b c", argc would equal 4 and argv would be an array that looks like this:

    const char* argv[4] = { "test", "a", "b", "c" };

    As a result, you can process your arguments like this:

    for (int i = 1; i < argc; i++)
    {
    cout << "arg[" << i << "] == " << argv[i] << endl;
    }
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  7. #7
    Linux Newbie
    Join Date
    May 2009
    Location
    Kitchener, Ontario, Canada
    Posts
    206
    changed the test file to test.cpp

    [Donald@localhost ~]$ cd Desktop/C++
    [Donald@localhost C++]$ make test
    g++ test.cpp -o test
    test.cpp: In function ‘int main()’:
    test.cpp:9: error: ‘cout’ was not declared in this scope
    test.cpp:9: error: ‘end1’ was not declared in this scope
    make: *** [test] Error 1

    this is ther error iget when i try to compile

  8. #8
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,598
    1. It is endl (with a letter ell) not end1 (number 1).
    2. It may may be that you need to include the standard namespace, so try this:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main( int argc, const char** argv)
    .
    .
    .
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  9. #9
    Linux Newbie
    Join Date
    May 2009
    Location
    Kitchener, Ontario, Canada
    Posts
    206
    yea i had number 1 there and also i didnt have using namespsace std;

  10. #10
    Linux Newbie
    Join Date
    May 2009
    Location
    Kitchener, Ontario, Canada
    Posts
    206
    Quote Originally Posted by codewizzrd View Post
    b.) Get the book "C++ How to Program" by Deitel and Deitel. Its expensive but if you are serious its a minimal investment.

    i managed to land a digital copy without denting a big whole in my wallet, anyway i got the book and im gonna start working on it the problem is the tutorials are in chm format so i gotta find a way linux can read it the the tutorial program files are cpp so no problem there


    found a chm viewer
    Last edited by donaldfarkas; 09-03-2010 at 07:32 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •