Talk:C Programming/Variables

Semicolon
Think phrase: "semicolon important part of every statement", more appropriate, because every statement has its semicolon, even last.194.85.82.1 00:30, 20 February 2007 (UTC)

Presentation of Nameing identifiers
Instead of "Naming of Variables" the section should be called "Naming of Identifiers" because the same conventions hold for variables and function names. Kinglag 22:50, 10 March 2006 (UTC)

Forget that 86.129.225.142 18:24, 1 Jul 2005 (UTC)

C strings
[quote]There is one more kind of literal that needs to be explained in connection with chars: the string literal. A string is a series of characters, usually intended to be output to the string. They are surrounded by double quotes (" ", not ' '). An example of a string literal is the "Hello, world!\n" in the "Hello, World" example.[/quote]

How can one assign string to another string? Will this work and why?

char str[20]; str = "mystring";

Answer
No this won't work. C is a low level language and you need to copy the data from one string to the next:

char str[20]; strncpy (str, "mystring", 19); str [19] = '\0';

Supprised about the 3rd line? The bitter truth is that none of the predefined string operations in "string.h" could be considerd safe.


 * strcpy, strcat : will copy a larger string into a smaller overwriting memory that follows.
 * strncpy : won't add a '\0' to the end of the string when the source string is longer then the max. lenght specfied.
 * strncat : For once strncat counts the characters read from the source string - which is not as usefull then counting the characters used up in the destination string. And then - just to supprise you and unlike strncpy - will copy n + 1 characters allways adding a '\0'.
 * strlen : will run forever if the '\0' is missing for whatever reason - with strncpy beeing one possible reason.

An realy experienced C programmer - like myself - writes his/her own string operations with propper support for a maximum length on the destination string.

--Krischik T 13:13, 5 October 2005 (UTC)


 * However, you can initialize an array of characters with a literal string, like char my_string[20] = "za warudo!";. This won't work with anything except things that are literals after preprocessing, as you all should know, and hardly merits a mention in the module proper. (Perhaps in a section on advanced syntax, or something? If this is actually advanced in any way.) (and please don't advocate reinventing the C library. that's something that students do. experienced programmers roll with whatever is available instead of fighting the tide.) 85.76.93.185 11:59, 22 April 2006 (UTC)

Mention 64 bit pc's
It is mentioned that most modern pc's are 32 bit. But shouldn't there be written that the most modern PC's are now 64 bit?--Sanderd17 (talk) 20:24, 28 January 2010 (UTC)

C level one doubt
main { int a=5,b=2; printf("%d", a+++b); }

what will be the output??


 * You could just try it. It outputs 7.  I guess the question is whether   is interpreted as   (it is), or as  .  This sort of question never occurs to me because I make effective use of whitespace when I write code.  Ambiguous code is hard to maintain, and is therefore bad code. --Jomegat (discuss • contribs) 15:36, 22 September 2011 (UTC)

Contents/In this section
At the end of the page there is a section about Contents and In this section These do not appear correct as they mention arrays, which are not discussed there. MadCowpoke (discuss • contribs) 05:52, 21 May 2012 (UTC)

missing int8_t, uint16_t, etc.
Hi! I find it more and more important to think about different processor architectures (16 bit, 32 bit, 64 bit...). In general it's a good idea to determine how "big" your integer variables need to be. Therefore I think it might be good to mention these specific integer data types. Further more, it should be shown that integer variables can be split bitwise. Example:


 * uint16_t nibble1:4, nibble2:4, nibble3:4, nibble4:4;
 * uint8_t bit0:1, bit1:1, threebits:3;

What do you think about this? Maybe it's not that important for a beginner's guide... I don't know... --Lexikorn (discuss • contribs) 07:22, 8 January 2023 (UTC)


 * The C ISO standards declare the  types optional. I think this chapter is really too early to address such details. ‑‑ Kai Burghardt (discuss • contribs) 17:44, 10 January 2023 (UTC)