Unit 2 Element of C
Unit 2 Element of C
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.
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.
Int a, b, c, sum=30;
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.
integer constants,
floating point constants,
character constants and
String constants.
Integer Constants:
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,
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,
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
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.
`~@!$#^*%&()[]{}<>+=_–|/\;:'“,.?
() 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.
Data-type variable_list;
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;
int _ab;
int a30;
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.
void function1()
{
int x=10; //local variable
}
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.
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.
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.
%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;
}
Output
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
o %e
int main()
{
float y=3;
printf("Exponential value of y is: %e", y);
return 0;
}
Output
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
C programming
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.
You
are
learning
„c‟ language