C Programming Constants and Variables

C Programming Constants and Variables refer to constant values that the program cannot change during execution.These constant values are also called literals.

Constants can be any of the basic data types, such as integer constant, floating constant, character constant, or string literal.There are also enumeration constants.

Constants are treated like regular variables, except that their values cannot be changed after their definition.

Tamsayı Değişmezleri / Integer Literals

An integer literal can be a decimal, octal, or hexadecimal constant.Specifies a prefix, base, or number base: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.

An integer literal can also have an snaptuous appendix, which is a combination of U and L for unmarked and long, respectively.The last attachment can be uppercase or lowercase and in any order.

Here are some examples of integer literals:

212         /* Eligible */
215u        /* Eligible */
0xFeeL      /* Not eligible */
078         /* Not eligible: 8 octal-octet numbers */
032UU       /* Not eligible: the last attachment cannot be repeated */

The following are other examples of various integer literals:

85         /* decimal */
0213       /* octal */
0x4b       /* hexadecimal */
30         /* int */
30u        /* unsigned int */
30l        /* long */
30ul       /* unsigned long */

Floating-point Literals

A floating-point literal has an integer part, a de deterring point, a fraction, and a exponent. You can represent floating-point either in de decathlete format or exponential format.

When representing a de deprecity format, you must add the deprecity point, exponent, or both; and the exponential form, you must include the integer part, the fractional part, or both.The signed exponent is introduced with an E.

Here are some examples of floating-point immutables:

3.14159       /* Eligible */
314159E-5L    /* Eligible */
510E          /* Not eligible: incomplete value */
210f          /* Not suitable: no decimal numbers or exponents */
.e55          /* Not Suitable: missing innumber or fraction */

Character Constants

Character literals are enclosed in single quotes, for example, 'x' can be stored in a simple variable of type char.

A character literal can be a solid character (for example, 'x'), an escape sequence (for example, 't'), or a universal character (for example, 'u02C0').

In C, there are certain characters that represent a special meaning when a backslash comes in front of it, such as a new line (n) or tab (t).

Escape sequenceMeaning
\ character
'' character
""character
?? character
aWarning or ring
Bundo
FForm feed
nNew row
rCarriage return
THorizontal tab
vVertical tab
ooooOne to three-digit octet number
xhh . . .Hexadecimal number consisting of one or more digits

Here are examples of several escape sequence characters:

#include <stdio.h></stdio.h>  int main() {
   printf("HellotDunyann");

return 0;
}


When the above code is compiled and executed, it produces the following result:

Hello, World

String/String Literals

String literals or constants are enclosed in double quotes "".A string contains characters similar to character literals: flat characters, escape sequences, and universal characters.

You can split a long line into multiple lines by using string literals and by separating them using white spaces.

Here are some examples of string literals.All three forms are the same strings:

"circuit, burning"

"circuit, 

burning"

"circuit, " "y" "flowing"

Defining Constants

There are two simple ways to define constants in C –

  • #define use the preprocessor.
  • Using the const keyword.

#define Preprocessor

The following is how you use the #define preprocessor to identify a constant:

#define identifier value

The following example describes this in detail:

#include <stdio.h></stdio.h>  #define LENGTH 10  #define WIDTH 5#defineNEWSE 'n'int main() {
   int field;  
  
field = LENGTH * WIDTH;
   printf("Field : %d",field);
   printf("%c", NEWSATIR);

return 0;
}






When the above code is compiled and executed, it produces the following result:

Area : 50

const Keyword

const value = value;

The following example describes this in detail:

#include <stdio.h></stdio.h>  int main() {
   const int LENGTH = 10; 
   const int WIDTH = 5;
   const char NEWSATIR = 'n';

int field;  
  
field = LENGTH * WIDTH;
   printf("Field : %d",field);
   printf("%c", NEWSATIR);

return 0;
}



When the above code is compiled and executed, it produces the following result:

Area : 50

Defining constants in uppercase letters always makes them easier to understand. In our next article, we will examine the topic of Storage Classes.