Jump to content

C newbie question

- - - - -

  • Please log in to reply
3 replies to this topic

#1
whoo0oocaress

whoo0oocaress

    Newbie

  • Members
  • Pip
  • 2 posts
What's up guys, newbie reporting in.



#include <stdio.h>

#include <conio.h>


int main(void)

{

    int num1;

    int num2;

    int sum;

    

    printf("Enter your first number: ");    

    scanf("%d", &num1);

    

    printf("Enter your second number: ");

    scanf("%d", &num2);

    

    sum = num1 + num2;

    printf("The sum is %d", sum);

    

    getch();

}



ok I got 2 questions,

first the program runs fine with/without #include <conio.h>
why and when do I have to include it?

second scanf ("%d", &num1); and printf("The sum is %d", sum);

why do I use & on scanf, but not printf?
when i put & in printf it gives me The sum is 2686780. why?

Thanks ;)

p.s. sorry for my poor english, but I promise you I'll get better :)

Edited by dargueta, 26 November 2010 - 02:10 PM.
Changed quote tags to code


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
1: conio includes functions like getch, or putchar which work directly with input from the console screen. It is not needed for your functions, only stdio.h.
conio's functions and descriptions can be found here: http://poli.cs.vsb.cz/c/help/conio.htm

2: You are referencing to a variable with the & symbol, essentially you tell the program:
"pipe text from stdin as an integer into the memory location of num1 ",

Functions accept input parameters but turn them into local variables, so if you had something like this:
int test = 0;
void change(int x) {
    x = 20;
}
change(test);
It will not be able to change "test", only "x" which exists inside the function. If we apply the reference, we can access "test" directly, this is what scanf needs. Printf only needs to keep the parameter local to display it, not to change it.

If you print the integer without initializing it , you are printing whatever happened to be at that memory location, that is why you get a large number.

Your English is clear by the way. :)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
whoo0oocaress

whoo0oocaress

    Newbie

  • Members
  • Pip
  • 2 posts
Thanks Nullw0rm.

very nice and easy to understand.

Just so I don't spam the forum with noob questions, I'll reply to this thread with new questions instead of creating a new thread

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Don't worry about creating new threads. With a good title, it can be easier to search :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users