Thread: C Control Strings?

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    1

    C Control Strings?

    I know that in other languages such as Java when I use System.out.println(); and I put a variable inside of it like an int that holds the number 22 it will print 22 to the console.

    In C if I do the same thing with printf(); I need to specify the type in the string such as printf("%d", n); I also know that Java has its own printf function.

    What I am trying to get at here is how the C control String works compared to other languages such as Java where you don't have to provide the type identifier in the System.out.println(); and it automatically recognizes the variable is an int.

    Is this part of C's way of efficiency and does it not actually check the type and rely's on the programmer to understand the type they are providing?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is no means in standard C for a function to recognise the type of arbitrary arguments without assistance. Similarly, C does not support function overloading (two functions of the same name but different argument types, where the arguments passed by the caller determine which version is called).

    It is not related to "C's way of efficiency", except possibly in the sense of keeping compilers as simple as possible, and able to be ported to more operating systems than most other languages.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by tj9413 View Post
    Is this part of C's way of efficiency and does it not actually check the type and rely's on the programmer to understand the type they are providing?
    With some compilers like GCC, the compiler can do some checking on the printf format strings and arguments at compile time if you turn on warnings. E.g. writing printf("%d", x) would issue a warning if x is actually a pointer to an int.

    With C this type of checking is normally done at compile time.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Not using a format string even when you can get away with it (like when you want to print a string by itself) is dangerous because you could fall victim to a format string attack.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by c99tutorial View Post
    With some compilers like GCC, the compiler can do some checking on the printf format strings and arguments at compile time if you turn on warnings. E.g. writing printf("%d", x) would issue a warning if x is actually a pointer to an int.

    With C this type of checking is normally done at compile time.
    That's not what the OP is asking about. The question is why it is not possible for the compiler to deduce the required format from the type of the actual value supplied. For example, printf(2) would produce formatted int, printf(2.5) would produce a formatted float, printf('a') would output a single char -- all without the need for a format string to tell printf() what the arguments are.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hosting a C# user control Hwnd within an ATL control
    By subdene in forum C++ Programming
    Replies: 0
    Last Post: 11-02-2013, 06:09 PM
  2. Replies: 2
    Last Post: 05-16-2012, 06:08 AM
  3. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  4. Creating Functions that use Format control Strings
    By tzuchan in forum C Programming
    Replies: 6
    Last Post: 03-29-2004, 12:50 PM
  5. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM

Tags for this Thread