Data types, variables, and constants C Language
C KEYWORDS AND IDENTIFIERS
C Programming that are part of the syntax. Also, you will learn about identifiers and how to name them.
Character Set:
A character set is a set of alphabets, letters and some special characters that are valid in C language.
Alphabets : A to Z, or, a to z
Digits : 0 to 9
Special characters : Symbols (!,@,#,$,%,",:,) etc.
White space Char. : Blank space, new line, horizontal tab, carriage return and form feed.
C Keywords:
Keywords are predefined, reserved words used in programming that have special meanings to the compiler.
Keywords are part of the syntax and they cannot be used as an identifier.
Ex. int money;
Keywords must be written in lowercase.
Keyword List:
auto double int struct
break else long switch
case enum register typedef
char extern return union
continue for signed void
do if static while
double goto sizeof volatile
const float short unsigned
C Identifiers:
Identifier refers to name given to entities such as variables, functions, structures etc.
Identifiers must be unique. They are created to give a unique name to an entity to identify it during the execution of the program.
Ex: int money; , double accountBalance;
Here, money and accountBalance are identifiers.
C VARIABLE, CONSTANTS AND LITERALS
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
Ex. 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. Ex. char ch='a'; , c='1';
Rules for naming a variable:
A variable name can only have letters (A to Z or a to z, digits and underscore).
The first letter of a variable should be either a letter or an underscore.
There is no rule on how long a variable name (identifier) can be.
If the variable name is longer than 31 characters, you may run into problems in some compilers.
Always give meaningful names to variables.
Literals:
Literals are data used for representing fixed values. They can be used directly in the code.
For example 1,2.5,'c' etc.
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, Octal, Hexadecimal
Floating - point literals:
A floating - point literal is a numeric literal that has either a fractional form or an exponent form.
Ex. -2.0, 0.0000234
Characters:
A character literal is created by enclosing a single character inside single quotation marks. For example: 'a','m','F','2' etc.
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.
Escape Sequences Character
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
\0 Null character
String Literals:
A string literal is a sequence of characters enclosed in double-quote marks.
Ex. "good", " ", " ", "x". etc.
Constants:
If you want to define a variable whose value cannot be changed, you can use the const keyword. This will create a constant.
Ex. const double PI=3.14;
We have added keyword const.
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;
Basic Data Types and Size:
Here's a table containing commonly used types in C programming for quick access.
Type Size (bytes) Format Specifier
int at least 2, usually 4 %d, %i
char 1 %c
float 4 %f
double 8 %lf
short int 2 usually %hd
unsigned int at least 2, usually 4 %u
long int at least 4, usually 8 %ld, %li
long long int at least 8 %lld, %lli
unsigned long long int at least 8 %llu
signed char 1 %c
unsigned char 1 %c
long double at least 10, usually 12 or 16 %Lf
int, char, float and double:
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. Ex. int id;
You can declare multiple variable at once in C programming. For example, int id, age;
float and double are used to hold real numbers.
Ex. float salary=10,000;, double price=30;
Keyword char is used for declaring character type variables. For example, char test='h';
short and long:
If you need to use a large number, you can use a type specifier long.
long a;, long long b;, long double c;
If you are sure, only a small integer ([-32,767,+32,767] range) will be used, you can use short. Ex. short d;
Programs 1:
#include<stdio.h>
main()
{
int a; //int or short int are same
long int b;
long long int c;
printf("size of int is:%lu \n",sizeof(a));
printf("size of long int is:%li \n",sizeof(b));
printf("size of long long int is:%lli \n",sizeof(c));
return 0;
}
Program 2:
#include<stdio.h>
main()
{
float a; //int or short int are same
long double b;
printf("size of int is:%lu \n",sizeof(a));
printf("size of long int is:%lu \n",sizeof(b));
return 0;
}
signed and unsigned:
In C, signed and unsigned are type modifiers. You can alter the data storage of a data type by using them.
signed - allows for storage of both positive and negative numbers.
unsigned - allows for storage of only positive numbers
Program:
#include<stdio.h>
main()
{
float a; //int or short int are same
long double b;
printf("size of int is:%lu \n",sizeof(a));
printf("size of long int is:%lu \n",sizeof(b));
return 0;
}
Derived Data Types:
Data types that are derived from fundamental data types are derived types. For example: arrays, pointers, function types, structures, etc.
We will learn about these derived data types in later tutorials: bool type, Enumerated type, Complex types
Comments
Post a Comment