PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label reserved-words. Show all posts
Showing posts with label reserved-words. Show all posts

Sunday, December 11, 2022

[FIXED] What are the rules about using an underscore in a C identifier?

 December 11, 2022     c, language-lawyer, reserved-words, syntax     No comments   

Issue

It's common in C (and other languages) to use prefixes and suffixes for names of variables and functions. Particularly, one occasionally sees the use of underscores, before or after a "proper" identifier, e.g. _x and _y variables, or _print etc. But then, there's also the common wisdom of avoiding names starting with underscore, so as to not clash with the C standard library implementation.

So, where and where is it ok to use underscores?


Solution

Good-enough rule of thumb

Don't start your identifier with an underscore.

That's it. You might still have a conflict with some file-specific definitions (see below), but those will just get you an error message which you can take care of.

Safe, slightly restrictive, rule of thumb

Don't start your identifier with:

  • An underscore.
  • Any 1-3 letter prefix, followed by an underscore, which isn't a proper word (e.g. a_, st_)
  • memory_ or atomic_.

and don't end your identifier with either _MIN or _MAX.

These rules forbid a bit more than what is actually reserved, but are relatively easy to remember.

More detailed rules

This is based on the C2x standard draft (and thus covers previous standards' reservations) and the glibc documentation.

Don't use:

  • The prefix __ (two underscores).
  • A prefix of one underscore followed by a capital letter (e.g. _D).
  • For identifiers visible at file scope - the prefix _.
  • The following prefixes with underscores, when followed by a lowercase letter: atomic_, memory_, memory_order_, cnd_, mtx_, thrd_, tss_
  • The following prefixes with underscores, when followed by an uppercase ltter : LC_, SIG_, ATOMIC, TIME_
  • The suffix _t (that's a POSIX restriction; for C proper, you can use this suffix unless your identifier begins with int or uint)

Additional restrictions are per-library-header-file rather than universal (some of these are POSIX restrictions):

If you use header file... You can't use identifiers with ...
dirent.h Prefix d_
fcntl.h Prefixes l_, F_, O_, and S_
grp.h Prefix gr_
limits.h Suffix _MAX (also probably _MIN)
pwd.h Prefix pw_
signal.h Prefixes sa_ and SA_
sys/stat.h Prefixes st_ and S_
sys/times.h Prefix tms_
termios.h Prefix c_

And there are additional restrictions not involving underscores of course.



Answered By - einpoklum
Answer Checked By - Marilyn (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Saturday, July 9, 2022

[FIXED] What is the "type" reserved word in TypeScript?

 July 09, 2022     keyword, reserved-words, typescript     No comments   

Issue

I just noticed when trying to create an interface in TypeScript that "type" is either a keyword or a reserved word. When creating the following interface, for example, "type" is shown in blue in Visual Studio 2013 with TypeScript 1.4:

interface IExampleInterface {
    type: string;
}

Let's say that you then try to implement the interface in a class, like this:

class ExampleClass implements IExampleInterface {
    public type: string;

    constructor() {
        this.type = "Example";
    }
}

In the first line of the class, as you type (sorry) the word "type" in order to implement the property required by the interface, IntelliSense appears with "type" having the same icon as other keywords like "typeof" or "new".

I've had a look around, and could find this GitHub issue which lists "type" as a "strict mode reserved word" in TypeScript, but I have not found any further information about what its purpose actually is.

I suspect I'm having a brain fart and this is something obvious I should already know, but what is the "type" reserved word in TypeScript for?


Solution

It's used for "type aliases". For example:

type StringOrNumber = string | number;
type DictionaryOfStringAndPerson = Dictionary<string, Person>;

Reference: (edit: removed outdated link) TypeScript Specification v1.5 (section 3.9, "Type Aliases", pages 46 & 47)

Update: (edit: removed outdated link) Now on section 3.10 of the 1.8 spec. Thanks @RandallFlagg for the updated spec and link

Update: (edit: deprecated link) TypeScript Handbook, search "Type Aliases" can get you to the corresponding section.

Update: Now it's here in the TypeScript Handbook.



Answered By - Jcl
Answer Checked By - Robin (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing