Thread: General C questions

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    37

    General C questions

    Hi, i have a few C questions that are nagging me:

    1) Why don't I need to place a '&' before strings when using scanf()?

    2) How can access individual characters in an integer? (e.g. - find the occurrences of 4 in the integer 142342)

    3) How can I assign string to a variable after its initial declaration?
    example -
    when i try to compile:

    char string[10] = "First";
    string = "second";

    i get:

    test.c: In function ‘main’:
    test.c:10: error: incompatible types in assignment

    4) How can I blank out an element of an array? (e.g. - remove the 'b' in "qubestion")

    5) Are there any GUI's for C?

    I know i have a few more but I can't remember them at the moment. Thanks in advance.

  2. #2
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by bluej322 View Post
    Hi, i have a few C questions that are nagging me:

    1) Why don't I need to place a '&' before strings when using scanf()?

    2) How can access individual characters in an integer? (e.g. - find the occurrences of 4 in the integer 142342)

    3) How can I assign string to a variable after its initial declaration?
    example -
    when i try to compile:

    char string[10] = "First";
    string = "second";

    i get:

    test.c: In function ‘main’:
    test.c:10: error: incompatible types in assignment

    4) How can I blank out an element of an array? (e.g. - remove the 'b' in "qubestion")

    5) Are there any GUI's for C?

    I know i have a few more but I can't remember them at the moment. Thanks in advance.
    1. Someone told me the array name is a pointer to the first element.

    2. The itoa function takes an integer as input and returns a string.
    itoa(3) - Linux man page

    3. I've had trouble with that, too. When in doubt, use bzero and sprintf. Those usually work for me.
    man page bzero section 3
    sprintf(3): formatted output conversion - Linux man page

    4. If that string were an array, you could assign the third element of that array to an invisible character, so users see "question", without the "b". Don't make it a null byte, though, that will cut the string short at "qu".

    5. The X Window System and Apple's Carbon framework are to be used with C.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    37
    Oh yeah, and how can i run executable files with C?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by bluej322 View Post
    Hi, i have a few C questions that are nagging me:

    1) Why don't I need to place a '&' before strings when using scanf()?

    2) How can access individual characters in an integer? (e.g. - find the occurrences of 4 in the integer 142342)

    3) How can I assign string to a variable after its initial declaration?
    example -
    when i try to compile:

    char string[10] = "First";
    string = "second";

    i get:

    test.c: In function ‘main’:
    test.c:10: error: incompatible types in assignment

    4) How can I blank out an element of an array? (e.g. - remove the 'b' in "qubestion")

    5) Are there any GUI's for C?

    I know i have a few more but I can't remember them at the moment. Thanks in advance.
    1. When needed, the name of an array will degrade to a pointer to the first element.
    2. Use sscanf to convert the integer to a string. Then you can access the indexes of the string with [].
    3. You cannot reassign an array like that. You can reassign a pointer though:
    Code:
    const char* s = "first";
    s = "second";
    4. You can't. What you can do is use memmove() to move all the characters up one spot in the array (which will overwrite your unwanted character).
    5. Yes, but this depends on the operating system you are targeting.

    Oh yeah, and how can i run executable files with C?
    The result of compiling the source file should be an executable file. Just run it as you would any other executable file.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by bluej322 View Post
    Hi, i have a few C questions that are nagging me:
    You're getting some pretty good answers so far, so consider this to be in addition to what you already have...
    1) Why don't I need to place a '&' before strings when using scanf()?
    Because a string is just an array of characters with a 0 after the last used slot.
    The name of an array is a pointer to it's first element.

    However the name of an integer returns it's value, it is not a pointer.

    So when you are using scanf() for integers you need to make it into a pointer with the "Address of" operator ... &... but since a string's name is already a pointer you don't need to do that.

    2) How can access individual characters in an integer? (e.g. - find the occurrences of 4 in the integer 142342)
    You can't do this directly because integers are storead as binary values. They only display as character strings for our convenience.

    The easiest way to access individual digits is to convert it to a string with functions such as sprintf(), etc. then use cell addressing -- string[x]-- to pick characters from the resulting character array.

    3) How can I assign string to a variable after its initial declaration?
    example -
    when i try to compile:

    char string[10] = "First";
    string = "second";

    i get:

    test.c: In function ‘main’:
    test.c:10: error: incompatible types in assignment
    C is a language without real strings. As I mentioned above, what we call a "string" in C is actually just an array of characters with a 0 after the last used space. Since a "string" is not a native type in C you cannot manipulated it with the operators used for native types. Thus, we have a number of library functions that do string-like operations on char arrays... string.h

    4) How can I blank out an element of an array? (e.g. - remove the 'b' in "qubestion")
    Move each character after the offending character forward one space. Generally this is done with a for() loop but you can also concoct while() loops to do the job as well.

    5) Are there any GUI's for C?
    You mean like Windows and Linux? Both were written in C and are available to C.

    I know i have a few more but I can't remember them at the moment. Thanks in advance.
    Well... please understand that a couple of questions here and there is a fun thing. But this isn't an online programming course, there are limits... If you don't already have books and tutorials on C, you should start reading... it's a fascinating language that has stood the test of time quite well.

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by bluej322 View Post
    Oh yeah, and how can i run executable files with C?
    An executable is what your compiler outputs by default. If you compile this C program...

    Code:
    #include <stdio.h>
    
    int main() {
        printf("hello world\n");
        return 0;
    }
    ...using this command (assuming the source file's name is prog.c)...

    Code:
    cc prog.c
    ...your compiler will produce an executable file named a.out. On some operating systems, you can double-click on this executable, and the OS will open a terminal window. On Windows, the Command Prompt window will be open for a second; on Mac OS X, the Terminal.app window will stay open until you close it; on Ubuntu, it appears nothing happened.

    To run your executable, type the following command.

    Code:
    ./a.out
    You should see "hello world" appear on the output. Of course, the compilation and execution method varies from operating system to operating system, from IDE to IDE.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    For everyone here who thinks arrays are just pointers, go and read this -> Question 6.2

    Then read the rest of section 6 of the CLC FAQ.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C interview questions
    By natrajdreams in forum C Programming
    Replies: 7
    Last Post: 12-12-2010, 12:40 PM
  2. [Newbie] Quick general questions about C#
    By PaulBlay in forum C# Programming
    Replies: 1
    Last Post: 06-17-2009, 08:14 AM
  3. The Tone of General Discussions
    By sean in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-03-2009, 09:06 PM
  4. a few opengl(or general programming) questions
    By linuxdude in forum Game Programming
    Replies: 20
    Last Post: 06-14-2004, 07:47 AM
  5. several questions
    By PHP in forum C++ Programming
    Replies: 3
    Last Post: 12-19-2002, 10:25 PM