Thread: Value of the variable

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    26

    Value of the variable

    Hey Guys,

    Just wondering....what is the value of the variables a and b after executing each line of the following code in C?


    Code:
    int a, b;
    int *pa, *pb;
    
    a= 2;
    b= 3;
    
    pa =&a;
    pb= pa:
    
    b++;
    *pb= *pa+1;
    a +=b;
    Cheers

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Lego_TeCh
    Just wondering....what is the value of the variables a and b after executing each line of the following code in C?
    Write a program to find out.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Are you in an exam and using your iPhone or something? They should put a compiler in those

    What do think the answer should be? Were you right, or wrong?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    26
    Quote Originally Posted by laserlight View Post
    Write a program to find out.
    Quote Originally Posted by MK27 View Post
    Are you in an exam and using your iPhone or something? They should put a compiler in those

    What do think the answer should be? Were you right, or wrong?
    yeah you guys are right i`m currently downloading c - compiler.....so keep your hair on! lol

  5. #5
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Are you in an exam and using your iPhone or something? They should put a compiler in those
    Actually, the jailbroken ipod touches/iphones let you install gcc on them

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    26
    Hey Guys,

    Just compiled the code below, I`v debugged some of the errors....but i`m still receiving these errors;


    value.c
    value.c(5) : error C2449: found '{' at file scope (missing function header?)
    value.c(16) : error C2059: syntax error : '}'



    Code:
    #include <stdio.h>
    
    int a, b;
    int *pa, *pb;
    {
    a= 2;
    b= 3;
    
    pa =&a;
    pb= pa;
    
    b++;
    *pb= *pa+1;
    a +=b;
    return 0;
    }
    Which function header do I need to implement?

  7. #7
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    main() maybe ?
    Code:
    #include <stdio.h>
    
    int a, b;
    int *pa, *pb;
    
    int main()
    {
    a= 2;
    b= 3;
    
    pa =&a;
    pb= pa;
    
    b++;
    *pb= *pa+1;
    a +=b;
    return 0;
    }
    Spidey out!

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    You need a main() function...

    Have you not written any program in this course until now? And you are already working with pointers?

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    26
    Quote Originally Posted by cyberfish View Post
    You need a main() function...

    Have you not written any program in this course until now? And you are already working with pointers?
    Preparing for college this Fall

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Ah well, I suggest you start reading the book from chapter 1, then.

  11. #11
    Registered User
    Join Date
    Jul 2009
    Posts
    26
    Quote Originally Posted by cyberfish View Post
    Ah well, I suggest you start reading the book from chapter 1, then.
    I just have gaps of knowledge of the language hence, reviewing the material

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It sounds to me like you do not merely have "gaps," but more like a poor understanding of the language in general.
    Regardless, whether you're reviewing or not, re-reading any books you might have is a very good idea.
    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.

  13. #13
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Yeah, that's how I figured s/he must have skipped (or skimmed through) quite a few chapters. Unfortunately for learning the basics of C, that won't work. For advanced topics maybe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM