Understanding ctype.h

This is a discussion on Understanding ctype.h within the C Programming forums, part of the General Programming Boards category; Originally Posted by yougene Are you saying that if I do printf("%c", someChar), printf is going to convert someChar into ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Quote Originally Posted by yougene View Post
    Are you saying that if I do printf("%c", someChar), printf is going to convert someChar into an integer before it works it's magic?
    Yes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    May 2006
    Posts
    182
    Quote Originally Posted by Elysia View Post
    And don't use gets.
    Don't worry, I might be new to C but I'm well aware of the dangers of bad habits and buffer overflows. Thank you for the heads up.


    It will actually work just fine to print with printf("%s", s), because gets() and printf() will both interpret the string as a char array. The fact that you declared it as an int array will only come into effect if you try to step throgh it, then you will get 'abcd' (0x64636261) as the integer value [1] of the first integer - because gets() have stuffed four bytes into the first four bytes of your array.
    Basically data types are just a way to impose boundaries on continous, but these boundaries aren't absolute.


    I probably sound silly restating what you guys are saying, but it's helping me absorb the information.

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Quote Originally Posted by yougene View Post
    Basically data types are just a way to impose boundaries on continous, but these boundaries aren't absolute.
    Types are a way for the language to interpret the raw data into something more suitable for humans (huwoman?).
    And indexes in arrays are just for the compiler to calculate an offset in the big memory block to read/write to/from.
    Arrays are just a way to create big blocks of memory to use.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    May 2006
    Posts
    182
    Thank you for all the help guys and girls. I don't think It'd even be possible for me to learn C without these forums.

  5. #20
    Registered User
    Join Date
    May 2006
    Posts
    182
    Quote Originally Posted by Elysia View Post
    Types are a way for the language to interpret the raw data into something more suitable for humans (huwoman?).
    I prefer homo sapien sapiens

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by yougene View Post
    Are you saying that if I do printf("%c", someChar), printf is going to convert someChar into an integer before it works it's magic?
    Splitting hairs, but actually printf RECEIVES that char as a integer, because the compiler has magically converted it - this is because we don't want the compiler to have to understand "%c" inside the argument for printf means a char, and "%d" means an integer - what happens if you write this:
    Code:
    char *strs[3] = { "%d", "%c", "%x" };
    char ch;
    
    for(i = 0; i < 3; i++) printf(strs[i], ch)
    And we could easily make an ever more complex situation where the actual string CAN NOT POSSIBLY be known by the compiler. So there has to be rules on how the arguments are passed to printf, so that printf itself can figure out what's what of its arguments.

    There are some compilers (such as gcc) that understand how to figure out if you pass the right things to a printf function, but that's only as a way to help you get the code right.

    [And as a side not, any other "variable argument function" (printf can take any number of arguments from 1 up to hundreds) will follow the same rule].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #22
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    Basically data types are just a way to impose boundaries on continous, but these boundaries aren't absolute.
    Yes, built-in types are about supporting ranges of values. It's one of the more important C "secrets." Along the lines of what mats was saying, I suspect that the practical reason printf promotes the way it does is due to specific issues when the function either does arithmetic, or calls something that does arithmetic on your variables. (Functions promote lesser types of arguments according to their signatures.) The printf your standard library supplies is often built out of system specific API calls that would have their own signatures.

    Of course, that's just based on information I gleaned from here. I expect if you read that you might gain a clearer understanding.
    Last edited by whiteflags; 03-05-2008 at 02:28 PM.

  8. #23
    Registered User
    Join Date
    May 2006
    Posts
    182
    >>matsp
    This conversion is only done in the context of printf's presence right?

    >>citizen
    Thanks, I'll have a look when I return. Hopefully it's not too much over my head.

  9. #24
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Quote Originally Posted by yougene View Post
    >>matsp
    This conversion is only done in the context of printf's presence right?
    It is done with the presence of any function that takes a variable number of arguments, such as printf, fprintf, sprintf, etc, etc.
    And arguments are promoted as they are passed into the function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by citizen View Post
    I suspect that the practical reason printf promotes the way it does is due to specific issues when the function either does arithmetic, or calls something that does arithmetic on your variables.
    I do not get it. printf as any other function that does not has specified types of argumants in the signature promotes integer types to int, float types to double as Standard specifies. Standard says nothing about "arithmetic" done by function - only about missing type specification in the signature
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #26
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    Not going to argue that point. There are practical reasons why the standard is written the way it is for implementors, though.

    Honestly though, you should have read a little further. I'm discussing implementation detail. I suspect that somewhere down the line arithmetic (in C) is a factor in all of these promotions as dictated by the standard. A function would need to be called to do the work of converting doubles to strings and printing them to precision -- you can't convert from a numerical type to a string type without some arithmetic operations. And as my source explains, the compiler promotes accordingly to make sure that arithmetic is only performed on certain types.

  12. #27
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Quote Originally Posted by Prelude View Post
    >Sometimes characters are 1 bytes. Sometimes they are
    >2 bytes for extended characters like chinese characters.
    Not really, in more ways than one.

    First, Chinese is a bad example for the two byte explanation because the current Chinese logogram collection is probably pushing 100,000 unique characters. Assuming a byte is the smallest addressable unit, you need at least three bytes to cover the most recent dictionary, which still isn't complete.

    Second, "char" in C is synonymous with "byte". That's not going to change, and that's why the framework around wchar_t exists to support larger character sets than a byte can handle.
    Yea, I'm struggling with words today. I was thinking of unicode when I wrote that. Thanks for clarifying.
    Don't quote me on that... ...seriously

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    It is done with the presence of any function that takes a variable number of arguments, such as printf, fprintf, sprintf, etc, etc.
    And arguments are promoted as they are passed into the function.
    And if you declare your own variable argument functions:
    Code:
    #include <stdarg.h>
    
    int func(int firstarg, ...);
    
    ...
    
       char c1, c2;
       float f1;
       double d1;
    
       func(42, c1, c2, d1, f1);
    ...
    In this case, func will be called with two integers (c1 and c2 extended to integer) and two double values (d1 as it is and f1 extended). Of course, it would be up to the func() implementation to make meaningfull use of the data passed in, and that may not be easy with the above function...

    printf() and it's close siblings are not "magical" in this respect. It's a simple fact that "if you don't give the arguments a name, it can will be converted".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Understanding Headers
    By AeonMoth in forum C++ Programming
    Replies: 2
    Last Post: 06-27-2007, 05:53 AM
  3. trouble understanding the source file structure
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 05-26-2006, 06:46 PM
  4. understanding recursive functions
    By houler in forum C Programming
    Replies: 7
    Last Post: 12-09-2004, 11:56 AM
  5. Help understanding conditional operator
    By Sereby in forum C Programming
    Replies: 7
    Last Post: 08-09-2004, 12:24 PM

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