Thread: 30 years after my first exposure to C, I'm finally doing it.

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    44

    30 years after my first exposure to C, I'm finally doing it.

    A bit of a monologue. I've always been fascinated by my experience that those that love assembler programming, dislike C, and vice versa.

    I'm programmed since 1977 in HS and have worked in 4 different hardware (assembler) platforms (and a dozen languges). Every instruction set I've run across has been pretty intuitive (6502 the best of micros and the Four-Phase 32-bit mini the best instruction set ever). That is, until I looked at my first assembly listed for my first C program. What an uuuuuuuuuuuuuuuuugly instruction set. It's almost like a C-language designer designed the instruction set. However, I will say I'm vastly impressed by the quality of code generated (quite impressed actually).

    Anyhoo, when I saw C in college, I couldn't understand how anyone would design a language like that when they can design one from scratch.

    But, here I am. I wrote my first .bat script ever a month ago (2700 lines) and thought I'd be able to make it shorter by using C. Wrongo.


    Questions:


    It seems char* whatever and char *whatever are identical?

    When you have a pointer, are you allowed to increment it by one and keep using it? It seems the compiler will "know" it's a pointer and increment it by 4 (i.e. it's a pointer to a 4-byte INT) for you.

    I see a lot of "while (joevariable)" of "if (joevariable)' but it doesn't have an == or != condition of any kind. It seems that that's the same as joevariable != 0)?

    Unicode. Looks like it's simply 2-bytes per "ascii" character. I played around with printf to see if I could get a handle on what occurs and interestingly, it's like incrementing the subscript for the string increments by 2 if wchar vs. 1 for char. Is it a good habit to do things a certain way to allow unicode stuff????

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Yes, the placement of the pointer symbol doesn't matter. The only issue is when you get a situation such as this:
    Code:
    char *e, f;
    , which could be confusing (as F isn't a pointer).

    The pointer increment will take into account the size of the type, and automatically increment by that much.

    You're pretty much right, but just to clarify:
    Code:
    while (i)            /* basic */
    while (i != 0)     /* literal translation, but not necessary */
    
    printf("%d", (i == 0));  /* will be either 1 or 0, depending if "i" is zero or not */
    printf("%d", (i != 3));   /* will be either 1 (i is not 3) or 0 (i is 3) */
    
    /* ^ shows that "while" and "if" operate with boolean logic, which is why it works */
    
    while ( (i != 0) != 1) /* now thats just silly, but it works */
    As for Unicode, I'm not really sure, but these might help:
    > wchar.h man page
    > wprintf(3) - Linux man page
    Last edited by memcpy; 02-01-2012 at 10:37 PM.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by jlewand View Post
    A bit of a monologue. I've always been fascinated by my experience that those that love assembler programming, dislike C, and vice versa.
    Why would it have to be one or the other? There's absolutely room for both. It's not really about what one likes, it's about using the right tool for the job. These days assembly is far more often than not, the wrong tool for the job, but I'll use it if I need to.

    When you have a pointer, are you allowed to increment it by one and keep using it? It seems the compiler will "know" it's a pointer and increment it by 4 (i.e. it's a pointer to a 4-byte INT) for you.
    Yes it will. It's an abstraction. You don't have to know how big it is, it you want to move 3 items over then you just add 3.
    You're protected from the lower level details that the memory address has say 3*4=12 added to it in such a case. It's simply not as productive or flexible to have to deal with manually incrementing by a multiple of specific size. Heaven forbid you get it wrong and access the 2.5th item in an array and cause a bus error or slower misaligned read.

    I see a lot of "while (joevariable)" of "if (joevariable)' but it doesn't have an == or != condition of any kind. It seems that that's the same as joevariable != 0)?
    Yes and it's largely just personal style. It's just like how in assembly you can use a branch-if-zero instruction or you can fill a register with zero and use a compare instruction then branch based on the flags that the comparison instruction set. The difference being though that a typical C compiler is smart enough to turn an if (joevariable != 0) into a simple branch-if-zero instruction as well.

    Unicode. Looks like it's simply 2-bytes per "ascii" character. I played around with printf to see if I could get a handle on what occurs and interestingly, it's like incrementing the subscript for the string increments by 2 if wchar vs. 1 for char. Is it a good habit to do things a certain way to allow unicode stuff????
    In many circumstances yes, but not necessarily. It probably makes no sense to try and cater for either in an assembly language program.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    1. Yes it's identical, the positon of the indirection symbol doesn't matter, as long as it isn't at the back of the variable.
    2. This depends, there are 2 kinds of pointers: constant pointers and variable pointers, the name of an array for example is a constan pointer. You can't increment these, variable pointers you can increment.
    3. This are so called booleans, since there isn't a real boolean-type in C, we use integers that can have the value 1 or 0. 1 stands for TRUE, 0 for FALSE, how that if (1) works, you can figure out yourself.
    4. I'm not sure of this, all I can say is that a char (ASCII) variable is 1 byte.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kevinstrijbos
    the name of an array for example is a constan pointer.
    Bad example because it is not true
    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

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Uh? It's true... Well yeah, if you don't think to deep... The name of an array is the name of an adress, much like a pointer...

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Complete your understanding here -> Arrays and Pointers
    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. Exposure time formula
    By ICool in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-16-2007, 01:53 AM
  2. New Years
    By linuxdude in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-02-2004, 02:32 PM
  3. The 50 years old mom is back for help:
    By laraine andaya in forum C Programming
    Replies: 2
    Last Post: 08-02-2002, 11:04 PM
  4. in 4 years...
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 01-23-2002, 07:16 AM
  5. 10 years?
    By iain in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 09-13-2001, 09:46 AM