Part 2C Lang - Programiz.lwk
Part 2C Lang - Programiz.lwk
Variables
In programming, a variable is a container (storage area) to hold data.
To indicate the storage area, each variable should be given a unique name (identifier). Variable names are
just the symbolic representation of a memory location. For example:
int playerScore = 95;
Here, playerScore is a variable of int type. Here, the variable is assigned an integer value 95.
The value of a variable can be changed, hence the name variable.
char ch = 'a';
// some code
ch = 'l';
Literals
Literals are data used for representing fixed values. They can be used directly in the code. For
example: 1, 2.5, 'c' etc.
Here, 1, 2.5 and 'c' are literals. Why? You cannot assign different values to these terms.
1
1. Integers
An integer is a numeric literal(associated with numbers) without any fractional or exponential part. There
are three types of integer literals in C programming:
decimal (base 10)
octal (base 8)
hexadecimal (base 16)
For example:
Decimal: 0, -9, 22 etc
Octal: 021, 077, 033 etc
Hexadecimal: 0x7f, 0x2a, 0x521 etc
In C programming, octal starts with a 0, and hexadecimal starts with a 0x.
2. Floating-point Literals
A floating-point literal is a numeric literal that has either a fractional form or an exponent form. For
example:
-2.0
0.0000234
-0.22E-5
Note: E-5 = 10-5
3. Characters
A character literal is created by enclosing a single character inside single quotation marks. For
example: 'a', 'm', 'F', '2', '}' etc.
4. Escape Sequences
Sometimes, it is necessary to use characters that cannot be typed or has special meaning in C programming.
For example: newline(enter), tab, question mark etc.
In order to use these characters, escape sequences are used.
2
Escape Sequences
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\? Question mark
\0 Null character
For example: \n is used for a newline. The backslash \ causes escape from the normal way the characters are
handled by the compiler.
5. String Literals
A string literal is a sequence of characters enclosed in double-quote marks. For example:
"good" //string constant
"" //null string constant
" " //string constant of six white space
"x" //string constant having a single character.
3
"Earth is round\n" //prints string with a newline
Constants
If you want to define a variable whose value cannot be changed, you can use the const keyword. This will
create a constant. For example,
const double PI = 3.14;
Notice, we have added keyword const.
Here, PI is a symbolic constant; its value cannot be changed.
const double PI = 3.14;
PI = 2.9; //Error
You can also define a constant using the #define preprocessor directive. We will learn about it in C
Macros tutorial.
4
C Data Types
In C programming, data types are declarations for variables. This determines the type and size of data
associated with variables. For example,
int myVar;
Here, myVar is a variable of int (integer) type. The size of int is 4 bytes.
Basic types
Here's a table containing commonly used types in C programming for quick access.
char 1 %c
float 4 %f
double 8 %lf
signed char 1 %c
unsigned char 1 %c
5
int
Integers are whole numbers that can have both zero, positive and negative values but no decimal values. For
example, 0, -5, 10
We can use int for declaring an integer variable.
int id;
Here, id is a variable of type integer.
You can declare multiple variables at once in C programming. For example,
int id, age;
The size of int is usually 4 bytes (32 bits). And, it can take 232 distinct states from -
2147483648 to 2147483647.
char
Keyword char is used for declaring character type variables. For example,
char test = 'h';
The size of the character variable is 1 byte.
void
void is an incomplete type. It means "nothing" or "no type". You can think of void as absent.
For example, if a function is not returning anything, its return type should be void.
Note that, you cannot create variables of void type.