Variables, Datatypes and Elements in C

Variables

A variable is nothing but a name given to a storage area that our programs can manipulate. It is defined by the following syntax.

Storage-class-specifier <space> type-specifier <space> variable-name,…

The Storage-class-specifier can be one of the following:

typedef The symbol name “variable-name” becomes a type-specifier of type “type-specifier”.
No variable is actually created, this is merely for convenience.
Extern Inticates that the variable is defined outside of the current file.
This brings the variables scope into the current scope. No variable is actually created by this.
Static Causes a variable that is defined within a function to be preserved in subsequent calls to the function.
Auto Causes a local variable to have a local lifetime (default).
register Request that the variable be accessed as quickly as possible. This request is not guaranteed.
Normally, the variable’s value is kept within a CPU register for maximum speed.

e.g. int I,j,k;

float x,y,z;

char ch;

 

Constants

ANSI C allows you to declare constants. When you declare a constants it is a bit like a variable declaration except the value cannot be changed.

 

The const keyword is to declare a constant, as shows below:

int const a = 1;

const int a = 2;

Note:

  • You can declare the const before or after the type.
  • It is usual to initialize a const with a value as it cannot get a value any other way.

The preprocessor #define is another more flexible method to define constants in a program.

 

Datatypes

Datatypes Details Format Specifier & Size Range
char, signed char Variable is large enough to store a basic character in character set.
May be either signed or nonnegative
%c 8 bits -128 to 127
unsigned char Same as char, but unsigned only %c 8 bits  0 to 255
short, signed short, short int, signed short int Defines a short signed int. %d 8 bits or 16 bits  -128 to 127

Or

-32,768 to 32,767

unsigned short, unsigned short int Defines an unsigned short integer. %d 8 bits or 16 bits  0 to 255 or

0 to 65,535

int, signed int Defines a signed integer %d 16 bits  -32,768 to 32,767
unsigned int Same as int, but unsigned values only. %u 16 bits  0 to 65,535
long, signed long, long int, signed long int Defines a long signed integer. %ld 32 bits  -2.147,483,648 to 2,147,483,647
unsigned long, unsigned long int Same as long, but unsigned values only. %lu 32 bits  0 to 4,294,967,295
Float A floating-point number. %f 32 bits  1.17549435 * (10^-38) to 3.40282347*(10^+38)
Double A more accurate floating-point number than float. %lf 64 bits  2.2250738585072014 *(10^-308 to 1.7976931348623157 * (10^+308)
long double Increase the size of double. %Lf 80 bits  3.4 * (10^-4932) to 1.1 * (10^4932)

 

Note: Size of datatype depends on different implementation of c by different compilers.

 

C Elements

Tokens

                In a C source program, the basic element recognized by the compiler is the “token”.

A token is source-program text that the compiler does not break down into component elements.

Tokens can be keyword, identifier, constant, string-literal, operator, punctuator.

Identifiers

“Identifiers” or  “symbols” are the names you supply for variables, types, functions and labels in your program.
Identifier names must differ in spelling and case from any keywords.

  • Must start with alphabet (a..z A…Z) or underscore(_).
  • Next letter can be alphabet (a…z A…Z), underscore(_) or digit.
  • You cannot use keywords as identifiers.

Literals

Invariant program elements are called “literals” or “constants”. The terms “literal” and “constant” are used interchangeably here. Literals fall into four major categories:

  • integer
  • character
  • floating-point
  • string literals

A “string literal” is a sequence of characters from the source character set enclosed in double quotation marks (“ “). String literals are also used to represent a sequence of characters which, taken together, from a null-terminated string. The characters of a literal string are stored in order at continuous memory locations. A null character (represented by the \0 escape sequence) is automatically appended to and marks the end of each string literal.

e.g.

  • 157 // integer constant
  • 0xFE // integer constant
  • ‘c’ // character constant
  • 2 // floating constant
  • 2E-01 // floating constant
  • “dog” string literal

Constants

                 A “constant” is a number, character or character string that can be used as a value in a program. Use constants to represent floating-point, integer, enumeration or character values that cannot be modified.

Keywords

                “Keywords” are words that have special meaning to the C compiler.

An identifier cannot have the same spelling and case as a C keyword.

The C language uses following keywords:

 

auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while

 

Comments

Comments in the source code are ignored by the compiler. They are encapsulated starting with /* and ending with */. According to ANSI standard, nested comments are not allowed, although some implementations allow it.

Single line comments are becoming more common, although not defined in the ANSI standard. Single line comments begin with // and are automatically terminated at the end of the current line.

Punctuation and Special Characters

                The punctuation and special characters in the C character set have various uses, from organizing program text to defining the tasks that the compiler or the compiled program carries out. They do not specify an operation to be performed. Some punctuation symbols are also operators. The compiler determines their use from context.

Punctuator: one of [ ] ( ) { } * , : = ; … #

2,046 Replies to “Variables, Datatypes and Elements in C”

  1. Нужен проектор? https://projector24.ru/ большой выбор моделей для дома, офиса и бизнеса. Проекторы для кино, презентаций и обучения, официальная гарантия, консультации специалистов, гарантия качества и удобные условия покупки.

  2. Лучшее казино up x официальный играйте в слоты и live-казино без лишних сложностей. Простой вход, удобный интерфейс, стабильная платформа и широкий выбор игр для отдыха и развлечения.

  3. Лучшее казино up-x играйте в слоты и live-казино без лишних сложностей. Простой вход, удобный интерфейс, стабильная платформа и широкий выбор игр для отдыха и развлечения.

  4. I’ve learn a few good stuff here. Certainly price
    bookmarking for revisiting. I surprise how much effort you set to make one of these fantastic
    informative web site.

  5. Good day! I know this is somewhat off topic but I was wondering which blog platform are you using for this site? I’m getting tired of WordPress because I’ve had problems with hackers and I’m looking at alternatives for another platform. I would be awesome if you could point me in the direction of a good platform.

  6. I do not even understand how I finished up right here, but I believed this publish was good. I do not understand who you are but certainly you are going to a well-known blogger if you happen to are not already. Cheers!

  7. Have you ever considered publishing an e-book or guest authoring on other websites? I have a blog based upon on the same ideas you discuss and would love to have you share some stories/information. I know my audience would value your work. If you are even remotely interested, feel free to shoot me an e-mail.

  8. For those seeking an exceptional online gaming experience, us.com](https://maxispin.us.com/) stands out as a premier destination. At Maxispin Casino, players can enjoy a vast array of pokies, table games, and other thrilling options, all accessible in both demo and real-money modes. The casino offers attractive bonuses, including free spins and a generous welcome offer, along with cashback promotions and engaging tournaments. To ensure a seamless experience, Maxispin provides various payment methods, efficient withdrawal processes, and reliable customer support through live chat. Security is a top priority, with robust safety measures and a strong focus on responsible gambling tools. Players can easily navigate the site, with detailed guides on account creation, verification, and payment methods. Whether you’re interested in high RTP slots, hold and win pokies, or the latest slot releases, Maxispin Casino delivers a user-friendly and secure platform. Explore their terms and conditions, read reviews, and discover why many consider Maxispin a legitimate and trustworthy choice in Australia.
    The website offers advanced tools for generating unique and engaging text content.

    **Features of MaxiSpin.us.com**
    One of the standout features of MaxiSpin.us.com is its ability to generate content in multiple languages.

    **Benefits of Using MaxiSpin.us.com**
    The platform’s scalability ensures that it meets the needs of users regardless of their size or industry.

Leave a Reply to Davidslume Cancel reply

Your email address will not be published.