Another mock question that doesnt compile!

This is a discussion on Another mock question that doesnt compile! within the C Programming forums, part of the General Programming Boards category; Hi, another mock exam question right here! Ive tried to run this code to work out what it is doing ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    93

    Another mock question that doesnt compile!

    Hi, another mock exam question right here! Ive tried to run this code to work out what it is doing but it says line1 hasnt been declared. The exam question asks what the output will be so i will have to try and work out what its result is meant to be.

    Code:
    char c *line, line2[20]
    int index = 0;
    strcpy(line2, "2 and 2 are 4");
    c = line2[0];
    line1 = (char *)malloc(15*sizeof(char));
    while (c != '\0'
    {
          line1[index] = c;
          if (c = '2') printf("ok\n");
          index = index+1;
          c = line2[index];
    };
    line1[index-1]='\0';
    printf("%s\n",line1);
    There are no scanf so that i know there is no input. The two outputs I can see if "ok \n" and printing the line1 string. There is a while conditional saying that while the value of c is not equal to "\0" then print ok. I have two questions about the code thus far.

    1. What does '\0' mean. It says if the value of c is not equal to that. However it looks like c is being defined as a char so how can a char be 0?

    2. What else is going on wiht this code, i dont really undestand the malloc and strcpy lines.

    3. Is the [20] after line2 defining how many is that maximum chars that this can hold in the string?

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    it says line1 hasnt been declared
    It hasn't. On the first line you declare line and line2, and you're also missing a semi-colon.

    What does '\0' mean.
    It's called a null-character. It signifies the end of a string

    However it looks like c is being defined as a char so how can a char be 0?
    A char is just a number used to to represent characters. At some point it has to be stored in binary, which in this case at least, is just 8 bits. Make all the bits 0, and you have the number zero. Represent that number as a character, and it means the end of a string.

    i dont really undestand the malloc and strcpy lines.
    malloc allocates a certain amount of memory for you dynamically. It's used when you're using large amounts of memory, and/or you don't know beforehand how much. However it needs to be used in conjunction with free() so that you're program doesn't leak memory.

    strcpy is used for string assignment. Do a google search on both of these and you'll find plenty of documentation and examples.

    Is the [20] after line2 defining how many is that maximum chars that this can hold in the string?
    Right - but that includes the null character. So the biggest string this array could hold would have the first character at line2[0] and the last at line2[18] and a null character at line2[19]. All the array elements are next to eachother in memory, so with no null character, string functions would just keep reading the memory that's AFTER the array is supposed to end.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by spadez View Post
    Hi, another mock exam question right here! Ive tried to run this code to work out what it is doing but it says line1 hasnt been declared.
    I imagine "line" should be "line1". There is another trick or typo in here:
    Code:
    if (c = '2')
    If this is suppose to be ==, then the output would be "ok\nok\n2 and 2 are 4".
    If left the way it is, the output will probably be the same thing.

    1. What does '\0' mean. It says if the value of c is not equal to that. However it looks like c is being defined as a char so how can a char be 0?
    Oh dear. '\0' is the null terminator which signifies the end of a string, it is automatically added by the strcpy(). That's pretty basic, you will probably have an easier time with this stuff is you actually do some programming, which you could not get past chapter 2 of the most basic C book without learning this. I am absolutely positive you will be better prepared for the exam if spend the time doing that INSTEAD of studying old exam questions (if you don't have time to do both). Really. Truly. Honestly.

    A single char can be defined as a number from -128 to 127 (in fact, all single chars are always such a number, under the surface) -- that number is not what the char will "print". Google ascii table.

    2. What else is going on wiht this code, i dont really undestand the malloc and strcpy lines.
    malloc assigns memory to the pointer.
    strcpy copies "2 and 2 are 4" into line2.

    3. Is the [20] after line2 defining how many is that maximum chars that this can hold in the string?
    Yes. But remember that there needs to be space for the '\0' at the end, so actually 19.
    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
    Feb 2009
    Posts
    93
    Hi, thank you both for the help. To explain why i am in this situation, these are not old exam questions, these are what i have been sent by my exam teacher. I have an exam in three weeks on these questions. I have been using the tutorial section of this site to learn, so far im on tutorial 5 where i think i should be able to answer these questions.

    First, c should be == because it is comparing the value and not setting it, is that correct?

    Secondly, i understand that letters can now be represented as numbers. To do this a char is given a numerical value, but when it comes to print the numerical value will be converted into its equivalent letter. Is this correct? In that case would the char 10 for example still count as a single character, becuase two numbers are used to make it but it would print only a single char? In the same way I take it /0 counts as a single char despite being technically two? A space also contributes to a char i assume.

    Since the numbers 2 and 4 are defined as part of a char string and not an int string, should they not be converted to ascii letters when they are printed? What is going on with the [index] parts of the code, is it copying the individual chars into line1?

    Finally may i please just state my understanding of the script now.

    First C is defined as a char, line is defined as a pointer and line2 is set a char limit of 20. The text "2 and 2 are 4" is then copied into line2. Then a while loop is started, and while it hasnt reached the end of the string, it will execute the middle code. This states that is the char 2 is present then print "ok\n". When the end of the string is reached, the loop ends, and prints out the complete line1. Hence why this is printed:

    ok\n
    ok\n
    2 + 2 = 4

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    First, c should be == because it is comparing the value and not setting it, is that correct?
    Correct. If you're saying "if(c = 6)" that's like saying "if(6)" which is true. If it doesn't make sense why:

    Code:
    a = 6; // a is 6
    b = a = 6; // b is now 6
    therefore
    ((a = 6) == 6) is true

    Secondly, i understand that letters can now be represented as numbers. To do this a char is given a numerical value, but when it comes to print the numerical value will be converted into its equilalent letter. Is this correct? In that case would the char 10 for example still count as a single character, becuase two numbers are used to make it but it would print only a single char? In the same way I take it /0 counts as a single char despite being technically two? A space also contributes to a char i assume.
    You need to look at the ascii table. A char IS a number. It isn't represented as a number, it basically IS a number. When you print a number (char), it prints the character that is represented by that number. So to print the number 7 on the screen, you don't send the number 7, you send the number that represent the character for 7. The ascii value for '7' is not 7. Make sense?

    So '/0' is 0, but 0 is something else (it's 100 and something, I think)

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Hi,

    I found this online:
    http://www.cs.utk.edu/%7Epham/ascii_table.jpg

    Am i correct in thinking if i wanted to print the number 7 i would use the number 55 (the decimal equivilient). So a char doesnt have to be a letter, it is just a charactor, which can include letters, numbers and symbols.

    In which case why is it that when we want to print the char letter "a" we simply put "a" in the printf? Also why were the numbers 2 and 4 in this equation not converted?

    Im sorry if im getting confused, im trying to get my head around all this.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    printf( "%d is %c", 'a', 'a' );
    It's all in the way you display your output.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    The %c tells the computer to interpret the number and display the appropriate character. %d means to print the number in decimal format.

    edit: and yes - you would be correct in that assumption - my guess was wayy off!

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Ah that makes slightly more sence. Would i be right in saying that if set to %c or %s letters and numbers could be displayed as they are seen:

    abc123
    would be displayed as:

    abc123
    However if %d then the decimal values of the numbers and letters would be shown instead.

    EDIT: Ah you cleared it up for me sean, thank you.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    If anyone happens to look at this again would you mind explaining how the "2+2=4" is copied to line1. I can see it has something to do with [index] but im not sure what exactly is going on.

    If it is easier to point me in the direction of information that would help me understand that would also be helpful.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    char c *line, line2[20]
    int index = 0;
    strcpy(line2, "2 and 2 are 4");
    c = line2[0];
    line1 = (char *)malloc(15*sizeof(char));
    while (c != '\0'
    {
          line1[index] = c;  //c drops it's char onto the end of line1, first '2', then others
          if (c = '2') printf("ok\n");
          index = index+1;  //index increments, so 
          c = line2[index];  //c can pick up another char from line2, here
    };
    line1[index-1]='\0';
    printf("%s\n",line1);

    In the red line of code, c is set equal to 2.

    After that, in the loop in blue, c is used to "carry" the 2 (first), and then the rest of the line2 string, char by char, into line1.

    It picks up it's char at the bottom of the loop, and drops it off at the top, when it's looping around.

    C is the bucket, in the bucket brigade throwing water on the fire.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Ive been looking at that for a while but i think I understand what you mean now.
    C is set as line2[0];, which is the first letter on line2, which is 2. Index is added one and that new index then stored the second char of line two in c, which is then copied to line1 at the start of the loop.

    [Index], is that a universal name, or is this just what he has decided to call it? What actually is index, or any value placed in brackets like that [].

  13. #13
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    When he uses the word index in a program, he has to define it, because it's not a word recognized by the compiler. But in general, the number inside the square brackets is referred to as the 'index' of that element.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 01:14 PM
  2. Compile question?
    By tomy in forum C++ Programming
    Replies: 6
    Last Post: 04-11-2008, 06:33 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 05:22 AM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21