You are on page 1of 22

C Fundamental

The basic elements used to construct a simple C program are called C fundamental and
these are: the C character set, identifiers and keywords, data types, constants, arrays,
declarations, expressions and statements.

C Character Set:
In the C programming language, 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 character set in c language

Purpose of character set


The character sets help in defining the valid characters that we can use in the source
program or can interpret during the running of the program.
C Tokens
The smallest individual element in C is known as tokens. Token in C is the most
important element to be used in creating a program in C.

We cannot create a sentence without using words; similarly, we cannot create a program
in C without using tokens in C. Therefore, we can say that tokens in C are the building
block or the basic component for creating a program in C language.

Keywords
Keywords are reserved words that have standard predefined meanings. These keywords
can only be used for their special purpose; they cannot be used as programmer defined
identifiers.

Examples of some keywords are: int, main, void, if.

There are total 32 keywords available in C programming language.


Identifiers and Keywords:
Identifiers are names given to various program elements such as variables, functions,
and arrays.

Int a, b, c, sum=30;

Here a, b, c and sum are the identifier.

Rules for giving identifier name

An identifier can only have alphanumeric characters (a-z, A-Z, 0-9) (i.e. letters &
digits) and underscore (_) symbol.
Identifier names must be unique
The first character must be an alphabet or underscore.
You cannot use a keyword as identifiers.
Only the first thirty-one (31) characters are significant.
It must not contain white spaces.
Identifiers are case-sensitive.
Constants:
A constant is an identifier whose value remains unchanged throughout the program.

To declare any constant the syntax is:


const datatype varname = value; where const is a keyword that declares the variable
to be a fixed value entity.

There are four basic types of constants in C.

integer constants,
floating point constants,
character constants and
String constants.

Integer Constants:

An integer constant is an integer valued number. It consists of a sequence of digits.


Integer constants can be written in the following three number systems:

Decimal (base 10): A decimal constant can consist of any combination of digits from 0
to 9. If it contains two or more digits, the first digit must be something other than 0,

for example: const int size =50;

Octal (base 8): An octal constant can consist of any combination of digits from 0 to 7.
The first digit must be a 0 to identify the constant as an octal number,

for example: const int a=074; const int b= 0;

Hexadecimal constant (base 16): A hexadecimal constant can consist of any


combination of digits from 0 to 9 and a to f (either uppercase or lowercase). It must
begin with 0x or 0X to identify the constant as a hexadecimal number,

for example: const int c= 0x7FF;

The integer constant used in a program can also be of an unsigned type or a long type.
We suffix the unsigned constant value with „u‟ and we suffix the long integer constant
value with „l‟. Also, we suffix the unsigned long integer constant value using „ul‟.
Examples,
55 —> Decimal Integer Constant
0x5B —> Hexa Decimal Integer Constant
023 —> Octal Integer Constant
68ul —> Unsigned Long Integer Constant
50l —> Long Integer Constant
30u —> Unsigned Integer Constant

Floating Point Constants / Real Constants


This type of constant must contain both the parts- decimal as well as integers.
Sometimes, the floating-point constant may also contain the exponential part. In such a
case when the floating-point constant gets represented in an exponential form, its value
must be suffixed using „E‟ or „e‟.
Example,
We represent the floating-point value 3.14 as 3E-14 in its exponent form.

Character Constants
The character constants are symbols that are enclosed in one single quotation. The
maximum length of a character quotation is of one character only.
Example,
„B‟
„5‟
„+‟
Some predefined character constants exist in the C programming language, known as
escape sequences. Each escape sequence consists of a special functionality of its own,
and each of these sequences gets prefixed with a „/‟ symbol. We use these escape
sequences in output functions known as „printf()‟.

String Constants
The string constants are a collection of various special symbols, digits, characters, and
escape sequences that get enclosed in double quotations.
The definition of a string constant occurs in a single line:
“This is Cookie”
We can define this with the use of constant multiple lines as well:
” This\
is\
Cookie”
The definition of the same string constant can also occur using white spaces:
“This” “is” “Cookie”
All the three mentioned above define the very same string constant.

Strings in C
The strings in C always get represented in the form of an array of characters. String is
also called a sequence of character. We have a ‘\0′ null character at the end of any
string- thus, this null character represents the end of that string.
Now, there are different ways in which we can describe a string:
char x[9] = “chocolate”;
char x[] = “chocolate”;
char x[9] = {„c‟,‟h‟,‟o‟,‟c‟,‟o‟,‟l‟,‟a‟,‟t‟,‟e‟,‟\0′};
Note: we will learn in details in string chapter.

Special Symbol/ Character


We also use some of the special characters in the C language, and all of them hold a
special meaning that we cannot use for any other purpose.
List of special characters are:

`~@!$#^*%&()[]{}<>+=_–|/\;:'“,.?
 () Simple brackets – We use these during function calling as well as during
function declaration. For instance, the function printf() is pre-defined.
 [ ] Square brackets – The closing and opening brackets represent the
multidimensional and single dimensional.
 (,) Comma – We use the comma for separating more than one statement,
separating the function parameters used in a function call, and for separating
various variables when we print the value of multiple variables using only one
printf statement.
 { } Curly braces – We use it during the closing as well as opening of any code.
We also use the curly braces during the closing and opening of the loops.
 (*) Asterisk – We use this symbol for representing the pointers and we also use
this symbol as a type of operator for multiplication.
 (#) Hash/preprocessor – We use it for the preprocessor directive. This processor
basically denotes that the user is utilizing the header file.
 (.) Period – We use the period symbol for accessing a member of a union or a
structure.
 (~) Tilde – We use this special character in the form of a destructor for free
memory.

Operators in C
The operators in C are the special symbols that we use for performing various functions.
Operands are those data items on which we apply the operators. We apply the
operators in between various operands.
On the basis of the total number of operands, here is how we classify the
operators:

Unary Operator
Binary Operator
Ternary Operator

Unary Operator
The unary operator in c is a type of operator that gets applied to one single operand, for
example: (--) decrement operator, (++) increment operator, (type)*, sizeof, etc.

Binary Operator
Binary operators are the types of operators that we apply between two of the operands.
Here is a list of all the binary operators that we have in the C language:

 Relational Operators
 Arithmetic Operators
 Logical Operators
 Shift Operators
 Conditional Operators
 Bitwise Operators
 Misc Operator
 Assignment Operator

Ternary Operator
Using this operator would require a total of three operands. For instance, we can use the
?: in place of the if-else conditions.

Variable in C

A variable is a name of the memory location. It is used to store data. Its value can be
changed, and it can be reused many times. It is a way to represent memory location
through symbol so that it can be easily identified.

Syntax to declare a variable.

Data-type variable_list;

The example of declaring the variable is given below

int a;
float b;
char c;

Here, a, b, c are variables. The int, float, char are the data types.

We can also provide values while declaring the variables as given below

int a=10,b=20; //declaring 2 variable of integer type


float f=20.8;
char c='A';
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.

Valid variable names:

int a;
int _ab;
int a30;

Invalid variable names:

int 2;
int a b;
int long;
ef int age+;

Types of Variables in C
There are many types of variables in c:

1. local variable
2. global variable
3. static variable
4. automatic variable
5. external variable
Local Variable
A variable that is declared inside the function or block is called a local variable.

It must be declared at the start of the block.

void function1()
{
int x=10; //local variable
}

You must have to initialize the local variable before it is used.

Global Variable
A variable that is declared outside the function or block is called a global variable. Any
function can change the value of the global variable. It is available to all the functions.

It must be declared at the start of the block:

int value=20; //global variable


void function1()
{
int x=10; //local variable
}

Static Variable
A variable that is declared with the static keyword is called static variable. It retains its
value between multiple function call.

void function1()
{
int x=10; //local variable
static int y=10; //static variable
x=x+1;
y=y+1;
printf("%d,%d" , x, y);
}

If you call this function many times, the local variable will print the same value for
each function call, e.g, 11,11,11 and so on. But the static variable will print the
incremented value in each function call, e.g. 11, 12, 13 and so on.

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.

void main()
{
int x=10; //local variable (also automatic)
auto int y=20; //automatic variable
}

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 extern keyword.

extern int x=10; //external variable (also global)

Data Types in C
A data type specifies the type of data that a variable can store such as integer, floating,
character, etc.
Basic Data Types
Basic datatypes are also non as primitive datatype. The basic data types are integer-
based and floating-point based. C language supports both signed and unsigned literals.

The memory size of the basic data types may change according to 32 or 64-bit
operating system.

Let's see the basic data types. Its size is given according to 32-bit architecture.
C Format Specifier
The Format specifier is a string used in the formatted input and output functions. The
format string determines the format of the input and output. The format string always
starts with a '%' character.

Mostly the format specifier used are:

 %d //integer
 %f //floating point number
 %c //character
 %If //double
 %s //String

Apart from that there are various specifier used in C language which is listed below

The commonly used format specifiers in printf() & scanf() functions are:
Some of the example listed below
o %d

int main()
{
int b=6;
int c=8;
printf("Value of b is:%d", b);
printf("\nValue of c is:%d",c);
return 0;
}

Output

Value of b is : 6

Value of c is : 8

o %u

int main()
{
int b=10;
int c= -10;
printf("Value of b is:%u", b);
printf("\nValue of c is:%u",c);
return 0;
}

In the above program, we are displaying the value of b and c by using an unsigned format
specifier, i.e., %u. The value of b is positive, so %u specifier prints the exact value of b, but it
does not print the value of c as c contains the negative value.

Output

Value of b is : 10

Value of c is : 4294967286

o %o

int main()
{
int a=0100;
printf("Octal value of a is: %o", a);
printf("\n Integer value of a is: %d",a);
return 0;
}

We are displaying octal value and integer value of a.

Output

Octal value of a is : 100

Integer value of a is : 64

o %f

int main()
{
float y=3.4;
printf("Floating point value of y is: %f", y);
return 0;
}

Output

Floating point value of y is : 3.400000

o %e

int main()
{
float y=3;
printf("Exponential value of y is: %e", y);
return 0;
}
Output

Exponential values of y is : 3.000000e+00

o %c

int main()
{
char a='c';
printf("Value of a is: %c", a);
return 0;
}

Output

Value of a is : c

o %s

int main()
{
char a[10]=”Bishal Patel”
printf(“Value of a is :%s”,a);
printf("%s", "C programming");
return 0;
}

Output

Value of a is : Bishal Patel

C programming

Minimum Field Width Specifier


Suppose we want to display an output that occupies a minimum number of spaces on
the screen. You can achieve this by displaying an integer number after the percent sign
of the format specifier.

int main()
{
int x=900;
printf("%8d", x);
printf("\n %-8d",x);
printf("%08d", x);
return 0;
}

In the above program, %8d specifier displays the value after 8 spaces while %-8d
specifier will make a value left-aligned.

Output

900

900

00000900
Specifying Precision
We can specify the precision by using '.' (Dot) operator which is followed by integer and
format specifier.

int main()
{
float x=12.2;
printf("%.2f", x);
return 0;
}

Output

12.20

Escape Sequence in C
An escape sequence in C language is a sequence of characters that doesn't represent
itself when used inside string literal or character.

It is composed of two or more characters starting with backslash \. For example: \n


represents new line.

List of Escape Sequences in C


#include<stdio.h>
int main()
{
int number=50;
printf("You\nare\nlearning\n\'c\' language\n\"Do you know C language\"");
return 0;
}
Output

You

are

learning

„c‟ language

“Do you know C language”

You might also like