C Programming Keywords and Identifiers
C Programming Keywords and Identifiers
Identifiers
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:
0 1 2 3 4 5 6 8 9
Special Characters:
Special Characters in C language
,
<
>
'
&
"
Keywords:
Keywords are the reserved words used in programming. Each
keywords has fixed meaning and that cannot be changed by user.
For example:
int money ;
Here, int is a keyword that indicates, 'money' is of type integer.
As, C programming is case sensitive, all keywords must be written
in lowercase. Here is the list of all keywords predefined by ANSI
C.
Keywords in C Language
auto
double
int
struct
break
else
long
switch
case
enum
register
typedef
char
extern
return
union
continue
for
signed
void
do
if
static
while
default
goto
sizeof
volatile
const
float
short
unsigned
far
interrupt
pascal
near
huge
cdecl
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 :
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.
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:
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
\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
"
"
"x"
"Earth is round\n"
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
Data types in C
1. Fundamental Data Types
o Integer types
o Floating Type
o Character types
2. Derived Data Types
o Arrays
o Pointers
o Structures
o Enumeration
Floating types
Variables of floating types can hold real values(numbers) such as:
2.34, -9.382 etc. Keywords either float or double is used for
declaring floating type variable. For example:
float var2;
double var3;
Here, both var2 and var3 are floating type variables.
In C, floating values can be represented in exponential form as
well. For example:
float var3=22.442e2
Character types
Qualifiers
Qualifiers alters the meaning of base data types to yield a new
data type.
Size qualifiers:
Size qualifiers alters the size of basic data type. The keywords
long and short are two size qualifiers. For example:
long int i;
The size of int is either 2 bytes or 4 bytes but, when long keyword
is used, that variable will be either 4 bytes of 8 bytes. Learn more
about long keyword in C programming . If the larger size of
variable is not needed then, short keyword can be used in similar
manner as long keyword.
Sign qualifiers:
Whether a variable can hold only positive value or both values is
specified by sign qualifiers. Keywords signed and unsigned are
used for sign qualifiers.
unsigned int a;
// unsigned variable can hold zero and positive values only
It is not necessary to define variable using keyword signed
because, a variable is signed by default. Sign qualifiers can be
applied to only int and char data types. For a int variable of size 4
bytes it can hold data from -2 31 to 2 31 -1 but, if that variable is
defined unsigned, it can hold data from 0 to 2 32 -1.
Constant qualifiers
Constant qualifiers can be declared with keyword const. An object
declared by const cannot be modified.
const int p=20;
The value of p cannot be changed in the program.
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>
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
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);
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.
Output
Enter character: g
You entered g.
"%c"
ASCII code
When character is typed in the above program, the character itself
is not recorded a numeric value(ASCII value) is stored. And when
we displayed that value by using "%c" , that character is displayed.
#include <stdio.h>
int main(){
char var1;
printf("Enter character: ");
scanf("%c",&var1);
printf("You entered %c.\n",var1);
/* \n prints the next line(performs work of enter). */
printf("ASCII value of %d",var1);
return 0;
}
Output
Enter character:
g
103
When, 'g' is entered, ASCII value 103 is stored instead of g .
You can display character if you know ASCII code only. This is
shown by following example.
#include <stdio.h>
int main(){
int var1=69;
printf("Character of ASCII value 69: %c",var1);
return 0;
}
Output
Character of ASCII value 69: E
The ASCII value of 'A' is 65, 'B' is 66 and so on to 'Z' is 90.
Similarly ASCII value of 'a' is 97, 'b' is 98 and so on to 'z' is 122.
Click here to learn about complete reference of ASCII code .
Output
Case 1: 9876
Case 2:9876
Case 3:987.65
Case 4:988
Case 5:9.876543e+002
Conditional Operators
Bitwise Operators
Special Operators
Arithmetic Operators
Operator
Meaning of Operator
multiplication
division
printf("a-b=%d\n",c);
c=a*b;
printf("a*b=%d\n",c);
c=a/b;
printf("a/b=%d\n",c);
c=a%b;
printf("Remainder when a divided by b=%d\n",c);
return 0;
}
a+b=13
a-b=5
a*b=36
a/b=2
Remainder when a divided by b=1
Explanation
Here, the operators +, - and * performed normally as you
expected. In normal calculation, 9/4 equals to 2.25. But, the output
is 2 in this program. It is because, a and b are both integers. So,
the output is also integer and the compiler neglects the term after
decimal point and shows answer 2 instead of 2.25. And, finally a
%b is 1,i.e. ,when a=9 is divided by b=4 , remainder is 1.
Suppose a=5.0, b=2.0, c=5 and d=2
In C programming,
a/b=2.5
a/d=2.5
c/b=2.5
c/d=2
Note: % operator can only be used with integers.
var++), operator will return the value of operand first and then
only increment it. This can be demonstrated by an example:
#include <stdio.h>
int main(){
int c=2,d=2;
printf("%d\n",c++); //this statement displays 2 then, only c
incremented by 1 to 3.
printf("%d",++c);
displayed.
return 0;
}
Output
2
4
Assignment Operators
The most common assignment operator is = . This operator assigns
the value in right side to the left side. For example:
var=5 //5 is assigned to var
a=c;
//value of c is assigned to a
5=c;
// Error! 5 is a constant.
Operator
Example
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
5==3 returns
Operator
Meaning of Operator
Example
false (0)
>
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:
Operat
or
Meaning of
Operator
Example
&&
Logial AND
||
Logical OR
Logical NOT
Explanation
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
Operators
Meaning of operators
>>
Shift right
Other Operators
Comma Operator
Comma operators are used to link related expressions together.
For example:
int a,c=5,d;
#include <stdio.h>
int main(){
int a;
float b;
double c;
char d;
printf("Size of int=%d bytes\n",sizeof(a));
printf("Size of float=%d bytes\n",sizeof(b));
printf("Size of double=%d bytes\n",sizeof(c));
Output
Size of int=4 bytes
Size of float=4 bytes
Size of double=8 bytes
Size of char=1 byte
expression1
is returned and if
int days;
printf("Enter l if the year is leap year otherwise enter 0: ");
scanf("%c",&feb);
days=(feb=='l')?29:28;
/*If test condition (feb=='l') is true, days will be equal to 29. */
/*If test condition (feb=='l') is false, days will be equal to 28. */
printf("Number of days in February = %d",days);
return 0;
}
Output
Enter l if the year is leap year otherwise enter n: l
Number of days in February = 29
Other operators such as &(reference operator), *(dereference
operator) and ->(member selection) operator will be discussed
in pointer chapter.
C Programming Introduction
Examples
This page contains example and source code on very basic
features of C programming language. To understand the examples
on this page, you should have knowledge of following topics:
1. Variables and Constants
2. Data Types
3. Input and Output in C programming
4. Operators
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
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
Source Code
#include <stdio.h>
int main()
{
int num;
printf("Enter a integer: ");
scanf("%d",&num); /* Storing a integer entered by user in variable
num */
printf("You entered: %d",num);
return 0;
}
Output
Enter a integer: 25
You entered: 25
In this program, a variable num is declared of type integer using
keyword int . The printf() function prints the content inside quotation
mark which is "Enter a integer: ". Then, the scanf() takes integer
value from user and stores it in variable num . Finally, the value
entered by user is displayed in the screen using printf() .
Programming
Programming
Programming
Programming
Data Types
Variables and Constants
Input Output (I/O)
Operators
Source Code
/*C programming source code to add and display the sum of two
integers entered by user */
#include <stdio.h>
int main( )
{
int num1, num2, sum;
printf("Enter two integers: ");
scanf("%d %d",&num1,&num2); /* Stores the two integer entered by
user in variable num1 and num2 */
sum=num1+num2;
sum */
Output
Enter two integers: 12
11
Sum: 23
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.
#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 above calculates the sum of two integers and
displays using only two variables.
Programming
Programming
Programming
Programming
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
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 .
Source Code
/* Source code to find ASCII value of a character entered by user */
#include <stdio.h>
int main(){
char c;
printf("Enter a character: ");
scanf("%c",&c);
Output
Enter a character: G
ASCII value of G = 71
In this program, user is asked to enter a character and this
character will be stored in variable c , i.e., the ASCII value of that
character is stored in variable c . When, this value is displayed
usingconversion format string %c , the actual variable is displayed
but, when this variable is displayed using format string %d , the
ASCII value of that character is displayed.
C Programming Operators
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:
#include <stdio.h>
int main(){
int dividend, divisor;
printf("Enter dividend: ");
scanf("%d",÷nd);
printf("Enter divisor: ");
scanf("%d",&divisor);
temp = sizeof(operand);
/* Here, temp is a variable of type integer,i.e, sizeof() operator
returns integer value. */
Source Code
/* This program computes the size of variable using sizeof operator.*/
#include <stdio.h>
int main(){
int a;
float b;
double c;
char d;
printf("Size of int: %d bytes\n",sizeof(a));
printf("Size of float: %d bytes\n",sizeof(b));
printf("Size of double: %d bytes\n",sizeof(c));
printf("Size of char: %d byte\n",sizeof(d));
return 0;
}
Output
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
Note: You may get different output depending upon your system.
Source Code
#include <stdio.h>
int main(){
int a;
long int b;
long long int c;
/* int is optional. */
/* int is optional. */
Output
Size of int = 4 bytes
Size of long int = 4 bytes
Size of long long int = 8 bytes
In this program, the sizeof operator is used for finding the size
of int , long int and long long int .
Thus, int and long int for my system can hold values from -2 31 to 2 31 1. If I have to work on data outside this range, I have to use long
long int , which can hold values from -2 63 to 2 63 -1 .
Similarly, the long keyword can be used double and floats types.
int main(){
float a, b, temp;
printf("Enter value of a: ");
scanf("%f",&a);
printf("Enter value of b: ");
scanf("%f",&b);
temp = a;
a = b;
Output