Variables Declaration in C

Author
By Darío Rivera
Posted On in Lenguaje C

Remember that C is a statically typed programming language, which means that it checks variable typing at compilation time rather than at execution time. This feature makes there be particular ways to declare and initialize variables in C. Below we will see each of them and some rules that you should keep in mind for variable naming.

Rules for Variable Naming

Case-Sensitivity: All variable names are case-sensitive, which means that the variable num is different from the variable Num.

Label-Rule: Variables must start with a letter or underscore, followed by a combination of letters, numbers, and underscores.

Not-Keyword: Variable names cannot be the same as a reserved word in the C language.

These variable naming rules apply to all identifiers in C. These identifiers can be used in variable names, function names, field names, labels, typedef names, enumerations, and constants.

Declaration

The standard way to declare a variable is to place its type before the variable name in the following way:

int entero;
char letra;

It is also possible to declare two or more variables of the same type in the same declaration.

int numero_1, numero_2;

Initialization

A value can be assigned to a previously initialized variable using the assignment operator (=).

entero = 3435;
letra = 'S';

It is also possible to declare and initialize a variable in the same statement like this:

int entero = 3435;
char letra = "S";

Since the result of an assignment is the assigned value in C, it is possible to make assignments inside control statements like the conditional. Notice where the assignment of the size_of_data variable occurs.

int data[3] = {10, 11, 21};
int size_of_data;

if (size_of_data = sizeof(data) / sizeof(int)) {
    printf("Size of data is %d\n", size_of_data);
}

Acerca de Darío Rivera

Author

Application Architect at Elentra Corp . Quality developer and passionate learner with 10+ years of experience in web technologies. Creator of EasyHttp , an standard way to consume HTTP Clients.

LinkedIn Twitter Instagram

Sólo aquellos que han alcanzado el éxito saben que siempre estuvo a un paso del momento en que pensaron renunciar.