C Programming Keywords and Identifiers Character Set
C Programming Keywords and Identifiers Character Set
Character set
Character set are the set of alphabets, letters and some special characters that are valid in C
language.
Alphabets:
Uppercase: A B C .................................... X Y Z
Lowercase: a b c ...................................... x y z
Digits:
0123456 89
Special Characters:
Special Characters in C language
,< > . _ ( ); $ :% [] # ?
' & { } " ^ !* / | -
\ ~ +
double int
struct
break
else
switch
case
long
char
extern return
union
continue for
signed void
do
static
while
default goto
sizeof
volatile
const
short
unsigned
if
float
Besides these keywords, there are some additional keywords supported by Turbo C.
Additional Keywords for Borland C
asm far interrupt pascal near huge cdecl
All these keywords, their syntax and application will be discussed in their respective topics.
However, if you want brief information about these keywords without going further visit page:
list of all C keywords.
Identifiers
In C programming, identifiers are names given to C entities, such as variables, functions,
structures etc. Identifier are created to give unique name to C entities to identify it during the
execution of program. For example:
int money;
int mango_tree;
Here, money is a identifier which denotes a variable of type integer. Similarly, mango_tree is
another identifier, which denotes another variable of type integer.
Rules for writing identifier
1. An identifier can be composed of letters (both uppercase and lowercase letters), digits
and underscore '_' only.
2. The first letter of identifier should be either a letter or an underscore. But, it is
discouraged to start an identifier name with an underscore though it is legal. It is because,
identifier that starts with underscore can conflict with system names. In such cases,
compiler will complain about it. Some system names that start with underscore are
_fileno, _iob, _wfopen etc.
3. There is no rule for the length of an identifier. However, the first 31 characters of an
identifier are discriminated by the compiler. So, the first 31 letters of two identifiers in a
program should be different.
Tips for Good Programming Practice :
Programmer can choose the name of identifier whatever they want. However, if the programmer
choose meaningful name for an identifier, it will be easy to understand and work on,
particularly in case of large program.
C Programming Variables and Constants
Variables
Variables are memory location in computer's memory to store data. To indicate the memory
location, each variable should be given a unique name called identifier. Variable names are just
the symbolic representation of a memory location. Examples of variable name: sum, car_no,
count etc.
int num;
Here, num is a variable of integer type.
Rules for writing variable name in C
1. Variable name can be composed of letters (both uppercase and lowercase letters), digits
and underscore '_' only.
2. The first letter of a variable should be either a letter or an underscore. But, it is
discouraged to start variable name with an underscore though it is legal. It is because,
variable name that starts with underscore can conflict with system names and compiler
may complain.
3. There is no rule for the length of length of a variable. However, the first 31 characters
of a variable are discriminated by the compiler. So, the first 31 letters of two variables in
a program should be different.
In C programming, you have to declare variable before using it in the program.
Constants
Constants are the terms that can't be changed during the execution of a program. For example:
1, 2.5, "Programming is easy." etc. In C, constants can be classified as:
Integer constants
Integer constants are the numeric constants(constant associated with number) without any
fractional part or exponential part. There are three types of integer constants in C language:
decimal constant(base 10), octal constant(base 8) and hexadecimal constant(base 16) .
Decimal digits: 0 1 2 3 4 5 6 7 8 9
Octal digits: 0 1 2 3 4 5 6 7
Hexadecimal digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F.
For example:
Decimal constants: 0, -9, 22 etc
Octal constants: 021, 077, 033 etc
Hexadecimal constants: 0x7f, 0x2a, 0x521 etc
Notes:
1. You can use small caps a, b, c, d, e, f instead of uppercase letters while writing a
hexadecimal constant.
2. Every octal constant starts with 0 and hexadecimal constant starts with 0x in C
programming.
Floating-point constants
Floating point constants are the numeric constants that has either fractional form or exponent
form. For example:
-2.0
0.0000234
-0.22E-5
Note:Here, E-5 represents 10-5. Thus, -0.22E-5 = -0.0000022.
Character constants
Character constants are the constant which use single quotation around characters. For example:
'a', 'l', 'm', 'F' etc.
Escape Sequences
Sometimes, it is necessary to use newline(enter), tab, quotation mark etc. in the program which
either cannot be typed or has special meaning in C programming. In such cases, escape
sequence are used. For example: \n is used for newline. The backslash( \ ) causes "escape" from
the normal way the characters are interpreted by the compiler.
Escape Sequences
Escape Sequences
Character
\b
Backspace
\f
Form feed
\n
Newline
\r
Return
\t
Horizontal tab
Escape Sequences
Escape Sequences
Character
\v
Vertical tab
\\
Backslash
\'
\"
\?
Question mark
\0
Null character
String constants
String constants are the constants which are enclosed in a pair of double-quote marks. For
example:
"good"
//string constant
""
//null string constant
" "
//string constant of six white space
"x"
//string constant having single character.
"Earth is round\n"
//prints string with newline
Enumeration constants
Keyword enum is used to declare enumeration types. For example:
enum color {yellow, green, black, white};
Here, the variable name is color and yellow, green, black and white are the enumeration
constants having value 0, 1, 2 and 3 respectively by default. For more information about
enumeration, visit page: Enumeration Types.
C Programming Data Types
In C, variable(data) should be declared before it can be used in program. Data types are the
keywords, which are used for assigning a type to a variable.
Data types in C
1. Fundamental Data Types
o Integer types
o Floating Type
o Character types
2. Derived Data Types
o Arrays
o
o
o
Pointers
Structures
Enumeration
Volatile qualifiers:
A variable should be declared volatile whenever its value can be changed by some external
sources outside program. Keyword volatile is used to indicate volatile variable.
C Programming Input Output (I/O)
ANSI standard has defined many library functions for input and output in C language.
Functions printf() and scanf() are the most commonly used to display out and take input
respectively. Let us consider an example:
#include <stdio.h> //This is needed to run printf() function.
int main()
{
printf("C Programming"); //displays the content inside quotation
return 0;
}
Output
C Programming
Explanation of How this program works
1. Every program starts from main() function.
2. printf() is a library function to display output which only works if #include<stdio.h>is
included at the beginning.
3. Here, stdio.h is a header file (standard input output header file) and #include is command
to paste the code from the header file when necessary. When compiler encounters printf()
function and doesn't find stdio.h header file, compiler shows error.
4. Code return 0; indicates the end of program. You can ignore this statement but, it is good
programming practice to use return 0;.
I/O of integers in C
#include<stdio.h>
int main()
{
int c=5;
printf("Number=%d",c);
return 0;
}
Output
Number=5
Inside quotation of printf() there, is a conversion format string "%d" (for integer). If this
conversion format string matches with remaining argument,i.e, c in this case, value of c is
displayed.
#include<stdio.h>
int main()
{
int c;
printf("Enter a number\n");
scanf("%d",&c);
printf("Number=%d",c);
return 0;
}
Output
Enter a number
4
Number=4
The scanf() function is used to take input from user. In this program, the user is asked a input
and value is stored in variable c. Note the '&' sign before c. &c denotes the address of c and
value is stored in that address.
I/O of floats in C
#include <stdio.h>
int main(){
float a;
printf("Enter value: ");
scanf("%f",&a);
printf("Value=%f",a); //%f is used for floats instead of %d
return 0;
}
Output
Enter value: 23.45
Value=23.450000
Conversion format string "%f" is used for floats to take input and to display floating value of a
variable.
I/O of characters and ASCII code
#include <stdio.h>
int main(){
char var1;
}
Similarly, any number of input can be taken at once from user.
C Programming Operators
Operators are the symbol which operates on value or a variable. For example: + is a operator to
perform addition.
C programming language has wide range of operators to perform various operations. For better
understanding of operators, these operators can be classified as:
Operators in C programming
Arithmetic Operators
Increment and Decrement Operators
Assignment Operators
Relational Operators
Logical Operators
Conditional Operators
Bitwise Operators
Special Operators
Arithmetic Operators
Operator
Meaning of Operator
multiplication
division
Same as
a=b
a=b
+=
a+=b
a=a+b
-=
a-=b
a=a-b
*=
a*=b
a=a*b
/=
a/=b
a=a/b
%=
a%=b
a=a%b
Relational Operator
Relational operators checks relationship between two operands. If the relation is true, it returns
value 1 and if the relation is false, it returns value 0. For example:
a>b
Here, > is a relational operator. If a is greater than b, a>b returns 1 if not then, it returns 0.
Relational operators are used in decision making and loops in C programming.
Operator
Meaning of Operator
Example
==
Equal to
>
Greater than
<
Less than
!=
Not equal to
>=
<=
Logical Operators
Logical operators are used to combine expressions containing relation operators. In C, there are
3 logical operators:
Meaning of
Operator
Operator
Example
&&
Logial AND
||
Logical OR
Logical NOT
Explanation
For expression, ((c==5) && (d>5)) to be true, both c==5 and d>5 should be true but, (d>5) is
false in the given example. So, the expression is false. For expression ((c==5) || (d>5)) to be
true, either the expression should be true. Since, (c==5) is true. So, the expression is true. Since,
expression (c==5) is true, !(c==5) is false.
Conditional Operator
Conditional operator takes three operands and consists of two symbols ? and : . Conditional
operators are used for decision making in C. For example:
c=(c>0)?10:-10;
If c is greater than 0, value of c will be 10 but, if c is less than 0, value of c will be -10.
Bitwise Operators
A bitwise operator works on each bit of data. Bitwise operators are used in bit level
programming.
Operators
Meaning of operators
&
Bitwise AND
Bitwise OR
Bitwise exclusive OR
Bitwise complement
<<
Shift left
>>
Shift right
Bitwise operator is advance topic in programming . Learn more about bitwise operator in C
programming.
Other Operators
Comma Operator
Comma operators are used to link related expressions together. For example:
int a,c=5,d;
The sizeof operator
It is a unary operator which is used in finding the size of data type, constant, arrays, structure
etc. For example:
#include <stdio.h>
int main(){
int a;
float b;
double c;
char d;
1.
2.
3.
4.
C Introduction Examples
C Programming Introduction Examples
C Program to Print a Sentence
C Program to Print a Integer Entered by a User
C Program to Add Two Integers Entered by User
C Program to Multiply two Floating Point Numbers
C Program to Find ASCII Value of Character Entered by User
C Program to Find Quotient and Remainder of Two Integers Entered by User
C Program to Find Size of int, float, double and char of Your System
C Program to Demonstrate the Working of Keyword long
C Program to Swap Two numbers Entered by User
C Program to Print a Sentence
Source Code
/* C Program to print a sentence. */
#include <stdio.h>
int main()
{
printf("C Programming"); /* printf() prints the content inside quotation */
return 0;
}
Output
C Programming
Explanation
Every C program starts executing code from main( ) function. Inside main( ), there is a printf( )
function which prints the content inside the quotation mark which is "C Programming" in this
case. Learn more about output in C programming language.
scanf("%d %d",&num1,&num2); /* Stores the two integer entered by user in variable num1
and num2 */
sum=num1+num2; /* Performs addition and stores it in variable sum */
printf("Sum: %d",sum); /* Displays sum */
return 0;
}
Output
Enter two integers: 12
11
Sum: 23
Explanation
In this program, user is asked to enter two integers. The two integers entered by user will be
stored in variables num1 and num2 respectively. This is done using scanf( ) function. Then, +
operator is used for adding variables num1 and num2 and this value is assigned to variable sum.
/* C programming source code to add and display the sum of two integers entered by user using
two variables only. */
#include <stdio.h>
int main( )
{
int num1, num2, sum;
printf("Enter a two integers: ");
scanf("%d %d",&num1,&num2);
num1=num1+num2; /* Adds variables num1 and num2 and stores it in num1 */
printf("Sum: %d",num1); /* Displays value of num1 */
return 0;
}
This source code calculates the sum of two integers and displays it but, this program uses only
two variables.
C Program to Multiply two Floating Point Numbers
In this program, user is asked to enter two floating point numbers and this program will
mulitply these two numbers and display it.
Source Code
/*C program to multiply and display the product of two floating point numbers entered by user.
*/
#include <stdio.h>
int main( )
{
float num1, num2, product;
printf("Enter two numbers: ");
scanf("%f %f",&num1,&num2);
/* Stores the two floating point numbers entered by user
in variable num1 and num2 respectively */
product = num1*num2; /* Performs multiplication and stores it */
printf("Product: %f",product);
return 0;
}
Output
Enter two numbers: 2.4
1.1
Product: 2.640000
Explanation
In this program, user is asked to enter two floating point numbers. These two numbers entered
by user will be stored in variables num1 and num2 respectively. This is done using scanf( )
function. Then, * operator is used for multiplying variables and this value is stored in variable
product.
In this program, user is asked to enter two integers(dividend and divisor) and this program will
compute the quotient and remainder and display it.
Source Code
/* C Program to compute remainder and quotient */
#include <stdio.h>
int main(){
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d",÷nd);
printf("Enter divisor: ");
scanf("%d",&divisor);
quotient=dividend/divisor;
/* Computes quotient */
remainder=dividend%divisor;
/* Computes remainder */
printf("Quotient = %d\n",quotient);
printf("Remainder = %d",remainder);
return 0;
}
Output
Enter dividend: 25
Enter divisor: 4
Quotient = 6
Remainder = 1
Explanation
This program takes two integers(dividend and divisor) from user and stores it in variable
dividend and divisor. Then, quotient and remainder is calculated and stored in variable quotient
and remainder. Operator / is used for calculation of quotient and % is used for calculating
remainder. Learn more about divison(/) and modulo division(%) operator in C programming
You can also program can be performed using only two variables as:
/* C Program to compute and display remainder and quotient using only two variables */
#include <stdio.h>
int main(){
int dividend, divisor;
printf("Enter dividend: ");
scanf("%d",÷nd);
printf("Enter divisor: ");
scanf("%d",&divisor);
scanf("%f",&a);
printf("Enter value of b: ");
scanf("%f",&b);
temp = a; /* Value of a is stored in variable temp */
a = b;
/* Value of b is stored in variable a */
b = temp; /* Value of temp(which contains initial value of a) is stored in variable b*/
printf("\nAfter swapping, value of a = %.2f\n", a);
printf("After swapping, value of b = %.2f", b);
return 0;
}
Output
Enter value of a: 1.20
Enter value of b: 2.45
After swapping, value of a = 2.45
After swapping, value of b = 1.2