Followers

Monday, May 22, 2023

Variable declaration in C language.


In C, a variable is a named storage location that holds a value. It is used to store and manipulate data within a program. Variables have a specific type that determines the range of values it can hold and the operations that can be performed on it.

Syntax for declaring a variable in C:

C
data_type variable_name;

Here, data_type represents the type of the variable, and variable_name is the name given to the variable. For example, to declare an integer variable named age, you would use the following syntax:

C
int age;

We can also initialize a variable at the time of declaration by assigning a value to it:

C
data_type variable_name = value;

Here's an example of declaring and initializing a few variables in C:

C
#include <stdio.h> 
int main()
{
int age = 25;
float height = 1.75;
char grade = 'A';
printf("Age: %d\n", age);
printf("Height: %.2f\n", height);
printf("Grade: %c\n", grade);
return 0;
}

In this example, three variables are declared and initialized: age of type int, height of type float, and grade of type char. The values are then printed using printf statements.

Output:

C
Age: 25 Height: 1.75 Grade: A

Variables can be modified by assigning new values to them throughout the program, allowing for dynamic storage and manipulation of data. 

Keywords in C language.

 

Keywords: Keywords are reserved words in C that have predefined meanings and cannot be
used as identifiers (user-defined names). They are part of the C language and serve specific

purposes.

Here is a list of keywords in the C programming language:

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

These keywords are reserved and have predefined meanings in the C language. It's

important to avoid using them as identifiers (user-defined names) in your C programs.

Tokens in C language.

 

Tokens: Tokens are the basic building blocks of a program in C. They are the smallest individual units that make up a C program. In the C programming language, tokens can be classified into several categories. Here's a list of common tokens:

  1. Identifiers:

    • User-defined names used to represent variables, functions, and other entities in a program. Examples: sum, i, calculateArea.
  2. Keywords:

    • Reserved words that have predefined meanings in the C language. Examples: if, while, for, int, return.
  3. Constants:

    • Fixed values that do not change during program execution.
    • Integer constants: Examples: 10, -5, 0xFF.
    • Floating-point constants: Examples: 3.14, -0.5, 1.0e-5.
    • Character constants: Examples: 'A', 'x', '\n'.
    • String constants: Examples: "Hello", "World".
  4. Operators:

    • Symbols that perform specific operations on operands.
    • Arithmetic operators: +, -, *, /, %.
    • Assignment operators: =, +=, -=, *=, /=, %=.
    • Increment/Decrement operators: ++, --.
    • Relational operators: ==, !=, <, >, <=, >=.
    • Logical operators: &&, ||, !.
    • Bitwise operators: &, |, ^, ~, <<, >>.
  5. Delimiters:

    • Symbols used to mark the beginning or end of a statement or block of code.
    • Examples: (, ), {, }, [, ], ,, ;, :.
  6. Separators:

    • Symbols used to separate elements in a list or statement.
    • Example: , (comma).

It's important to note that whitespace (spaces, tabs, newlines) is also considered a token but is generally ignored by the compiler unless it is necessary to separate other tokens.