Thread: A Few Basic Questions

  1. #1
    Registered User Euclid365BCE's Avatar
    Join Date
    Jul 2015
    Location
    USA
    Posts
    33

    A Few Basic Questions

    Code:
    //Printf and Scanf syntax
    
    //Include c libraries
    #include <stdio.h>
    
    //Define main function
    int main (void)
    {
        //Declare local variables
        int a, b, c;
    
        //Request integer and character values, and print to screen
        printf ("Enter first integer: ");
        scanf ("%d", &a);//& is a "Pointer" and is being used to assign this integer to memory
        printf ("\n%d", a);//After pointer above has assigned integer to memory, the variable has been initialized so it is used here to identify the scanf input that is called
        printf ("\n\nEnter second integer: ");
        scanf ("%d", &b);
        printf ("\n%d", b);
        printf("\n\n%d %d", a, b); //Note that \n can be added for each additional line needed
        printf ("\n\nEnter a character: ");
        scanf ("\n%c", &c);
        printf ("\n%c", c);
        printf ("\n\n%c %d %d", c, a, b);
        printf ("\n\n%d %c %d", b, c, a);
        return (0);
    }
    1) My understanding of "&" (e.g., line 14) is that it is a pointer and in this context is used to initialize variable a; correct?

    2) I have not studied the use of "%" so am contextually figuring out how this works. So far, I understand it to be used in conjunction with scanf to capture user input, and with this in mind have defined "%c" as used for characters, and "%d" as used for digits. Is this a fair beginner assumption?

    3) Why do some lines require a double "\n" to effect single line spacing? For example, I use one "\n" in line 15 to get single spacing, but in the next line 16 I have to use double "\n" to get the same spacing in command line output.

    4) C libraries such as #include <stdio.h> tell the compiler to pull code for functions such as printf and scanf. Without these library declarations the compiler wouldn't know what to do with such functions; correct? And is it correct to call printf and scanf functions?

    Thank you everyone.
    Last edited by Euclid365BCE; 10-20-2015 at 08:00 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    1) My understanding of "&" (e.g., line 14) is that it is a pointer and in this context is used to initialize variable a; correct?
    The '&' has several different meanings depending upon content. In this content the '&' is considered a referencing operator.

    2) I have not studied the use of "%" so am contextually figuring out how this works. So far, I understand it to be used in conjunction with scanf to capture user input, and with this in mind have defined "%c" as used for characters, and "%d" as used for digits. Is this a fair beginner assumption?
    Much like the operator'&' the operator'%' can have different meaning depending on content. In this content it is considered a "special" character by the scanf() function that indicates a conversion specification follows. See the documentation for scanf() for more information. There are quite a few other format specifiers that you should know about, again see the documentation.

    3) Why do some lines require a double "\n" to effect single line spacing? For example, I use one "\n" in line 15 to get single spacing, but in in the next line 16 I have to use double "\n" to get the same spacing in command line output.
    This is because when you use scanf() you pressed the "enter" key to terminate the input which adds a newline to the screen. The printf() function doesn't automatically add a newline to the output, if you want it you need to add it to your format string. By the way I would consider the output to be "double" spaced, not "single" spaced since there is an extra blank line between each value.

    4) C libraries such as #include <stdio.h> tell the compiler to pull code for functions such as printf and scanf. Without these library declarations the compiler wouldn't know what to do with such functions; correct? And is it correct to call printf and scanf functions?
    No, stdio.h is not a library. It is an include file. An include file is used to inform the compiler about function implementations that will be encountered later, either in another file or somewhere after the first use in a program. In this case the implementations will be found by the linker in the standard C library.

    Jim
    Last edited by jimblumberg; 10-20-2015 at 08:29 AM.

  3. #3
    Registered User Euclid365BCE's Avatar
    Join Date
    Jul 2015
    Location
    USA
    Posts
    33
    Quote Originally Posted by jimblumberg View Post
    The '&' has several different meanings depending upon content. In this content the '&' is considered a referencing operator.


    Much like the operator'&' the operator'%' can have different meaning depending on content. In this content it is considered a "special" character by the scanf() function that indicates a conversion specification follows. See the documentation for scanf() for more information. There are quite a few other format specifiers that you should know about, again see the documentation.



    This is because when you use scanf() you pressed the "enter" key to terminate the input which adds a newline to the screen. The printf() function doesn't automatically add a newline to the output, if you want it you need to add it to your format string. By the way I would consider the output to be "double" spaced, not "single" spaced since there is an extra blank line between each value.


    No, stdio.h is not a library. It is an include file. An include file is used to inform the compiler about function implementations that will be encountered later, either in another file or somewhere after the first use in a program. In this case the implementations will be found by the linker in the standard C library.

    Jim
    Excellent comments. Thank you for taking time to make them. I learn a lot from this and follow through reviewing linked documentation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic C questions
    By voklta7 in forum C Programming
    Replies: 5
    Last Post: 05-07-2015, 12:43 PM
  2. Some Basic questions
    By BlaX in forum C Programming
    Replies: 7
    Last Post: 06-30-2009, 09:51 AM
  3. Some basic C questions
    By WarDoGG in forum C Programming
    Replies: 5
    Last Post: 07-12-2008, 10:40 AM
  4. 2 Basic Questions
    By Dan99 in forum C++ Programming
    Replies: 2
    Last Post: 04-05-2007, 06:11 PM
  5. Basic questions about C
    By FlatLost in forum C Programming
    Replies: 6
    Last Post: 11-01-2005, 01:08 PM