CodeNewbie Community 🌱

Discussion on: String in C Programming

Collapse
 
ac000 profile image
Andrew Clayton

Hi,

Please allow me to correct a few things here.

1) The NUL (not to be confused with NULL) character is usually written as \0

2) Your single quotes are strange \342 characters and not the usual single quote (ASCII 39).

3) Your first array example

char myArray[4] = { 'A', 'L', 'I', 'M', 'A', 'M' };
Enter fullscreen mode Exit fullscreen mode

hasn't allocated enough space. For the above it would need a size of 6, however it wouldn't be NUL terminated. You probably meant something like

char myArray[7] = { 'A', 'L', 'I', 'M', 'A', 'M', '\0' };
Enter fullscreen mode Exit fullscreen mode

4) Please don't use gets(3). To quote the man page

  Never use gets().  Because it is impossible to tell without knowing the
  data  in  advance  how  many  characters  gets() will read, and because
  gets() will continue to store characters past the end of the buffer, it
  is  extremely dangerous to use.  It has been used to break computer se‐
  curity.  Use fgets() instead.

  For more information, see CWE-242 (aka  "Use  of  Inherently  Dangerous
  Function") at http://cwe.mitre.org/data/definitions/242.html

In fact gets(3) has been marked as obsolescent in POSIX.1-2008 and has been removed from C11.

puts(3) is of course fine to use.

5) Oh, and in C if your not passing any parameters into a function you should declare it as void otherwise it will be (...) which is something completely different. So your main should look like

int main(void)
Enter fullscreen mode Exit fullscreen mode

Cheers,
Andrew

Collapse
 
alimammiya profile image
Alimam Miya

Thanks for suggest