Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Chapter 2: (what is C, features of C)

 Character Set
 Identifiers
 Keywords
 Data Types
 Constants and Variables
 Statements
 Expressions
 Operators
 Precedence of Operators
 Input-Output Assignments

1. Character Set (Characters that are valid) in C?


The character set refers to a set of all the valid characters that we can use in the source program for
forming words, expressions, and numbers.

Types of Characters in C

Characters in C are classified into 4 groups:

a. Alphabets
C programming language supports a total of 52 different characters- 26 uppercase and 26 lowercase.
Type of Character Description Characters
Lowercase Alphabets a to z a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
Uppercase Alphabets A to Z A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z

b. Digits
C programming language support all the digits ranging from 0-9 that help in constructing numeric
values or expressions in a program.
Type of Character Description Characters
Digits 0 to 9 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
c. Special Characters
C Programming language allows programmers to use the following special characters:
Type of Character Examples
Special Characters `~@!$#^*%&()[]{}<>+=_–|/\;:‘“,.?

comma , slash /
period . backslash
semicolon ; percentage %
colon : left and right brackets [ ]
question mark ? left and right parenthesis ( )

d. White Spaces
In C Programming, white spaces contain:
 Blank Spaces
 Tab
 New Line
 Carriage Return

2. Keywords (reserved words)


Keywords are predefined, reserved words used in programming that have special meanings to the
compiler. The functions and meanings of these words cannot be altered. There are only 32 reserved
words (keywords) in the C language.
Keywords in C Programming are:
auto break case char
continue const do default

double else enum extern

for float go if

int long register return

sign static sizeof short

struct switch typedef union

void volatile while unsigned

Consider an example below:


 int age;
 float height;
Here, int is a keyword that declares age as a variable of integer data type.
Similarly, float is also a keyword that declares height is a variable of floating integer data type.

3. Identifiers (user-defined names)


Identifiers are user-defined names of variables, functions, and arrays. It comprises of a combination of
letters and digits.
Rules for naming identifiers
 It must begin with an alphabet or an underscore and not digits.
 It must contain only alphabets, digits or underscore.
 There is no rule on how long an identifier can be. However, you may run into problems in
some compilers if the identifier is longer than 31 characters.

Examples of valid identifiers


 int money;
 double accountBalance;
Here, money and accountBalance are identifiers.

Examples of invalid identifiers


 2sum (starts with a numerical digit)
 int (reserved word)
 char (reserved word)
 m+n (special character, i.e., '+')

4. Variables
A variable is the name of the memory location. It is used to store data. Its value can be changed, and
it can be reused many times.

The syntax to declare a variable:


type variable_name;

Examples of declaring the variable is given below:


 int a=10, b=20;//declaring 2 variable of integer type
 float f=20.8;
 char c='A';
Here, a, b, c are variables. The int, float, char are the data types.

Rules for defining variables


o A variable can have alphabets, digits, and underscore.
o A variable name can start with the alphabet, and underscore only. It can't start with a digit.
o No whitespace is allowed within the variable name.
o A variable name must not be any reserved word or keyword, e.g., int, float, etc.

Types of Variables in C

There are many types of variables in c:


o local variable
o global variable
o static variable
o automatic variable
a) Local variable- A variable that is declared inside the function or block is called a local variable.
1. void function1()
2. {
3. int x=10;//local variable
4. }

b) Global Variable-A variable that is declared outside the function or block is called a global
variable.

1. int value=20;//global variable


2. void function1(){
3. int x=10;//local variable
4. }
c) Static Variable- A variable that is declared with the static keyword is called a static variable.
It retains its value between multiple function calls.

1. void function1(){
2. int x=10;//local variable
3. static int y=10;//static variable
4. x=x+1;
5. y=y+1;
6. printf("%d,%d",x,y);
7. }
d) Automatic Variable- All variables in C that are declared inside the block, are automatic
variables by default. We can explicitly declare an automatic variable using auto keyword.

1. void main(){
2. int x=10;//local variable (also automatic)
3. auto int y=20;//automatic variable
4. }
e) External Variable- We can share a variable in multiple C source files by using an external
variable. To declare an external variable, you need to use the extern keyword.
myfile.h
extern int x=10;//external variable (also global)
program1.c
1. #include "myfile.h"
2. #include <stdio.h>
3. void printValue(){
4. printf("Global variable: %d", global_variable);
5. }

5. 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;

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

#define PI 3.1415

6. Literals
Literals are the Constant values that are assigned to the constant variables. Literals represent fixed
values that cannot be modified.
For example,
const int =10; is a constant integer expression in which 10 is an integer literal.

Types of literals
There are four types of literals that exist in C programming:

o Integer literal It is a numeric literal that represents only integer type values.
Example: const int a=23; // constant integer literal
o Float literal It is a literal that contains only floating-point values or real numbers.
Example: const float a=4.5; // constant float literal

o Character literal A character literal contains a single character enclosed within single quotes.
Example: const char c='ak'; // constant character literal

o String literal A string literal represents multiple characters enclosed within double quotes.
Example: const string s= "javatpoint"; // constant string literal

7. Data Types
A data type specifies the type of data that a variable can store such as integer, floating, character, etc.

There are the following data types in C language.

Types Data Types

Basic Data Type int, char, float, double

Derived Data Type array, pointer, structure, union

Enumeration Data Type enum

Void Data Type void

1. Basic Data Types


In C language, basic data types are used to store values in integer and decimal forms. It
supports both signed and unsigned literals. There are four basic data types, in both signed and
unsigned forms:

o Int
o Float
o Double
o Char

1. Integer (int): Refers to positive and negative whole numbers (without decimal),
such as 10, 12, 65, 3400, etc.
Example:

#include <stdio.h>

void main()

int i = 5;

printf("The integer value is: %d \n", i);

2. Character (char): Refers to all the ASCII character sets within single quotes such
as ‘a’, ‘A’, etc.
Example:

#include <stdio.h>

void main()

char c = 'b';

printf("The character value is: %c \n", c);

3. Floating-point (float): Refers to all the real number values or decimal points,
such as 3.14, 10.09, 5.34, etc.
Example:

#include <stdio.h>

void main()

float f = 7.2357;

printf("The float value is: %f \n", f);


}

4. Double (double): Used when the range exceeds the numeric values that do not
come under either floating-point or integer data type.
Example:

#include <stdio.h>

void main()

double d = 71.2357455;

printf("The double value is: %lf \n", d);

These data types require specific keywords for defining them:

Keyword Used Data Type

int Integer

float Floating-point

double Double

char Character

void Void

Size Of Data Types In C


The size of each data type is defined in bits or bytes (8bits). Each data type in C is associated with
a specific range of values defined as below:

Data Type Format Specifier Minimal Range Size in bit

unsigned char %c 0 to 255 8

char %c -127 to 127 8

signed char %c -127 to 127 8


int %d, %i -32,767 to 32,767 16 or 32

unsigned int %u 0 to 65,535 16 or 32

signed int %d, %i -32,767 to 32,767 (same as int) 16 or 32

short int %hd -32,767 to 32,767 16

unsigned short int %hu 0 to 65,535 16

signed short int %hd Same as short int

2. Derived Data Types


Derived data types are primary data types that are grouped together. You can group many
elements of similar data types. These data types are defined by the user. The following are the
derived data types in C:

 Array
 Pointers
 Structure
 Union

o Array
An array in C is a collection of multiple values of a similar data type and is stored in a contiguous
memory location. An array can consist of chars, integers, doubles, etc.

Declaration of Array in C

data_type array_name[array_size];

o Pointer
The pointer data type is used to store the address of another variable. A pointer can store the
address of variables of any data type. Pointers allow users to perform dynamic memory
allocation. They also help to pass variables by reference.

A pointer with no address is called a null pointer. A pointer with no data type is a void Pointer. It
is defined by using a ‘*’ operator.
o Structure
It is a data type that can store variables of similar or different data types. For example, we can use
structures to store information about an employee, such as the employee name, employee ID,
salary, and more. Each employee’s record will be represented by an object of the structure. The
size of the structure is the sum of the storage size required by each variable. The ‘struct’ keyword
defines a structure.

o Union
A union is a group of elements with similar or different data types. In a union, the memory
location is the same for all the elements. Its size will be equal to the memory required for the
largest data type defined. We use the keyword ‘union’ to define a union. You can declare many
variables. However, just one variable can store the value at a time.

3. Enumerated Data Types


Enumerated data types are user-defined data types that consist of integer values. They are used to
define variables that can only assign certain discrete integer values in the program. They are used
to make a program more readable, flexible, and maintainable. We use the keyword ‘enum’ to
declare new enumeration types in the C programming language.

Enum syntax:

enum flag {const1, const2, const3………};

A popular example of enumerated data types is the days of the week.

4. Void
The void is just an empty data type that depicts that no value is available. Typically, the void is
used for functions. When we declare a function as void, it doesn’t have to return anything.

Void is used in three situations:

 Function returns as void – A function with no return value will have the return type as
void.
 Function arguments as void – A function with no parameter can accept the void.
 Pointers to void – It represents the address of an object, but not its type.

Example – The below function will not return any value to the calling function.

void sum (int a, int b);

You might also like