Unit No.1
Unit No.1
1
#include <stdio.h>
2
3
int main()
4
{
5
printf("Hello World!");
6
return 0;
7
}
Output
Hello World!
In this C tutorial, we’ll cover everything from basic syntax, data
types, and control structures to advanced
topics like pointers, memory management, and file handling.
By the end, you’ll gain hands-on experience and a solid
understanding of C, which is essential for mastering other
programming languages like C++ and Java. Let’s dive into the
world of C programming and build a strong coding foundation!
Tokens in C
Last Updated : 28 Aug, 2024
A token in C can be defined as the smallest individual element of the
C programming language that is meaningful to the compiler. It is the
basic component of a C program.
Types of Tokens in C
The tokens of C language can be classified into six types based on
the functions they are used to perform. The types of C tokens are as
follows:
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special Symbols
6. Operators
1. C Token – Keywords
The keywords are pre-defined or reserved words in a programming
language. Each keyword is meant to perform a specific function in a
program. Since keywords are referred names for a compiler, they
can’t be used as variable names because by doing so, we are trying
to assign a new meaning to the keyword which is not allowed. You
cannot redefine keywords. However, you can specify the text to be
substituted for keywords before compilation by using C preprocessor
directives. C language supports 32 keywords which are given
below:
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Note: The number of keywords may change depending on the
version of C you are using. For example, keywords present in ANSI C
are 32 while in C11, it was increased to 44. Moreover, in the latest
c23, it is increased to around 54.
2. C Token – Identifiers
Identifiers are used as the general terminology for the naming of
variables, functions, and arrays. These are user-defined names
consisting of an arbitrarily long sequence of letters and digits with
either a letter or the underscore(_) as a first character. Identifier
names must differ in spelling and case from any keywords. You
cannot use keywords as identifiers; they are reserved for special
use. Once declared, you can use the identifier in later program
statements to refer to the associated value. A special identifier
called a statement label can be used in goto statements.
Rules for Naming Identifiers
Certain rules should be followed while naming c identifiers which are
as follows:
They must begin with a letter or underscore(_).
They must consist of only letters, digits, or underscore. No other
special character is allowed.
It should not be a keyword.
It must not contain white space.
It should be up to 31 characters long as only the first 31
characters are significant.
Note: Identifiers are case-sensitive so names like variable and
Variable will be treated as different.
For example,
main: method name.
a: variable name.
3. C Token – Constants
The constants refer to the variables with fixed values. They are like
normal variables but with the difference that their values can not be
modified in the program once they are defined.
Constants may belong to any of the data types.
Examples of Constants in C
const int c_var = 20;
const int* const ptr = &c_var;
4. C Token – Strings
Strings are nothing but an array of characters ended with a null
character (‘\0’). This null character indicates the end of the string.
Strings are always enclosed in double quotes. Whereas, a character
is enclosed in single quotes in C and C++.
Examples of String
char string[20] = {‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’,
’e’, ‘e’, ‘k’, ‘s’, ‘\0’};
char string[20] = “geeksforgeeks”;
char string [] = “geeksforgeeks”;
5. C Token – Special Symbols
The following special symbols are used in C having some special
meaning and thus, cannot be used for some other purpose. Some of
these are listed below:
Brackets[]: Opening and closing brackets are used as array
element references. These indicate single and multidimensional
subscripts.
Parentheses(): These special symbols are used to indicate
function calls and function parameters.
Braces{}: These opening and ending curly braces mark the start
and end of a block of code containing more than one executable
statement.
Comma (, ): It is used to separate more than one statement like
for separating parameters in function calls.
Colon(:): It is an operator that essentially invokes something
called an initialization list.
Semicolon(;): It is known as a statement terminator. It indicates
the end of one logical entity. That’s why each individual
statement must be ended with a semicolon.
Asterisk (*): It is used to create a pointer variable and for the
multiplication of variables.
Assignment operator(=): It is used to assign values and for
logical operation validation.
Pre-processor (#): The preprocessor is a macro processor that
is used automatically by the compiler to transform your program
before actual compilation.
Period (.): Used to access members of a structure or union.
Tilde(~): Bitwise One’s Complement Operator.
6. C Token – Operators
Operators are symbols that trigger an action when applied to C
variables and other objects. The data items on which operators act
are called operands.
Depending on the number of operands that an operator can act
upon, operators can be classified as follows:
Unary Operators: Those operators that require only a single
operand to act upon are known as unary operators.For Example
increment and decrement operators
Binary Operators: Those operators that require two operands to
act upon are called binary operators. Binary operators can further
are classified into:
1. Arithmetic operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Bitwise Operator
Ternary Operator: The operator that requires three operands to
act upon is called the ternary operator. Conditional Operator(?) is
also called the ternary operator.
Keywords in C
Last Updated : 28 Nov, 2024
doubl
else enum extern float for goto if
e
registe
int long return short signed sizeof static
r
typede volatil
struct switch union unsigned void while
f e
// C program to demonstrate
2
// auto keyword
3
#include <stdio.h>
4
int printvalue()
6
{
7
printf("%d", a);
9
}
10
11
// Driver code
12
int main()
13
{
14
printvalue();
15
return 0;
16
Output
10
#include <stdio.h>
4
// Driver code
6
int main()
7
{
8
{
10
if (i == 2)
11
{
12
continue;
13
}
14
if (i == 6)
15
{
16
break;
17
}
18
printf("%d ", i);
19
}
20
return 0;
21
Output
1 3 4 5
// C program to demonstrate
2
#include <stdio.h>
4
// Driver code
6
int main() {
7
int i = 4;
8
switch (i) {
9
case 1:
10
printf("Case 1\n");break;
11
case 2:
12
printf("Case 2\n");break;
13
case 3:
14
printf("Case 3\n");break;
15
case 4:
16
printf("Case 4\n");break;
17
default:
18
printf("Default\n");break;
19
}
20
Output
Case 4
Note: it is best to add a break statement after every case so that
switch statement doesn’t continue checking the remaining cases.
Output
Case 4
Default
char
char keyword in C is used to declare a character variable in the C
programming language.
char x = 'D';
Below is the C program to demonstrate the char keyword:
C
// C program to demonstrate
2
// char keyword
3
#include <stdio.h>
4
// Driver code
6
int main() {
7
char c = 'a';
8
printf("%c", c);
9
return 0;
10
Output
a
const
The const keyword defines a variable who’s value cannot be
changed.
const int num = 10;
Below is the C program to demonstrate the const keyword:
C
// C program to demonstrate
2
// const keyword
3
#include <stdio.h>
4
// Driver code
6
int main() {
7
a = a + 2;
9
printf("%d", a);
10
return 0;
11
}
This code will produce an error because the integer a was defined as
a constant and it’s value was later on changed.
Output:
error: assignment of read-only variable 'a'
a = a + 2;
do
The do statement is used to declare a do-while loop. A do-while loop
is a loop that executes once, and then checks it’s condition to see if
it should continue through the loop. After the first iteration, it will
continue to execute the code while the condition is true.
Below is the C program to demonstrate a do-while loop.
C
// C program to demonstrate
2
// do-while keyword
3
#include <stdio.h>
4
// Driver code
6
int main()
7
{
8
int i = 1;
9
do {
10
i++;
12
14
return 0;
15
}
Output
1 2 3 4 5
// C program to demonstrate
2
#include <stdio.h>
4
// Driver code
6
int main() {
7
float f = 0.3;
8
double d = 10.67;
9
return 0;
12
Output
Float value: 0.300000
Double value: 10.670000
if-else
The if-else statement is used to make decisions, where if a condition
is true, then it will execute a block of code; if it isn’t true (else), then
it will execute a different block of code.
if(marks == 97) {
// if marks are 97 then will execute this block of code
}
else {
// else it will execute this block of code
}
Below is the C program to demonstrate an if-else statement:
C
// C program to demonstrate
2
// if-else keyword
3
#include <stdio.h>
4
// Driver code
6
int main()
7
{
8
int a = 10;
9
{
11
}
13
else
14
{
15
}
18
return 0;
19
Output
A is less than 11
enum
The enum keyword is used to declare an enum (short for
enumeration). An enum is a user-defined datatype, which holds a
list of user-defined integer constants. By default, the value of each
constant is it’s index (starting at zero), though this can be changed.
You can declare an object of an enum and can set it’s value to one
of the constants you declared before. Here is an example of how an
enum might be used:
C
1
// An example program to
2
// demonstrate working of
3
// enum in C
4
#include<stdio.h>
5
// enum declaration:
7
// Driver code
10
int main()
11
{
12
day = Wed;
15
printf("%d", day);
16
return 0;
17
Output
2
extern
The extern keyword is used to declare a variable or a function that
has an external linkage outside of the file declaration.
C
#include <stdio.h>
2
extern int a;
4
int main(){
6
printf("%d", a);
8
return 0;
10
}
for
The “for” keyword is used to declare a for-loop. A for-loop is a loop
that is specified to run a certain amount of times.
Below is the C program to demonstrate a for-loop:
C
// C program to demonstrate
2
// for keyword
3
#include <stdio.h>
4
// Driver code
6
int main()
7
{
8
{
10
}
12
return 0;
13
Output
0 1 2 3 4
goto
The goto statement is used to transfer the control of the program to
the given label. It is used to jump from anywhere to anywhere within
a function.
Example:
goto label;
// code
label:
Below is the C program to demonstrate the goto keyword:
C
1
// C program demonstrate
2
// goto keyword
3
#include <stdio.h>
4
// from 1 to 10
7
void printNumbers() {
8
int n = 1;
9
10
label:
11
n++;
13
}
15
16
// Driver code
17
int main(){
18
printNumbers();
19
return 0;
20
Output
1 2 3 4 5 6 7 8 9 10
int
int keyword is used in a type declaration to give a variable an
integer type. In C, the integer variable must have a range of at least
-32768 to +32767.
Example:
int x = 10;
Below is the C program to show the int keyword:
C
// C program to demonstrate
2
// int keyword
3
#include <stdio.h>
4
void sum() {
6
int sum;
8
sum = a + b;
9
printf("%d", sum);
10
}
11
12
// Driver code
13
int main() {
14
sum();
15
return 0;
16
Output
30
unsigned short
2 0 to 65,535 %hu
int
-2,147,483,648 to
4 %ld
long int 2,147,483,647
int
unsigned long 0 to
8 %llu
long int 18,446,744,073,709,551,615
// C program to demonstrate
2
#include <stdio.h>
5
// Driver code
7
int main() {
8
// short integer
9
11
// signed integer
12
14
// unsigned integer
15
17
// L or l is used for
18
// long int in C.
19
21
return 0;
26
Output
Integer value with a short int data: 12345
Integer value with a signed int data: -34
Integer value with an unsigned int data: 12
Integer value with a long int data: 99998
return
The return statement returns a value to where the function was
called.
Example:
return x;
Below is the C program to demonstrate the return keyword:
C
// C program to demonstrate
2
// return keyword
3
#include <stdio.h>
4
int sum;
6
sum = x + y;
7
return sum;
8
}
9
10
// Driver code
11
int main() {
12
printf("Sum: %d",
15
sum(num1, num2));
16
return 0;
17
Output
Sum: 30
sizeof
sizeof is a keyword that gets the size of an expression, (variables,
arrays, pointers, etc.) in bytes.
Example:
sizeof(char);
sizeof(int);
sizeof(float); in bytes.
Below is the C program to demonstrate sizeof keyword:
C
// C program to demonsstrate
2
// sizeof keyword
3
#include <stdio.h>
4
// Driver code
6
int main() {
7
int x = 10;
8
printf("%d", sizeof(x));
9
return 0;
10
Output
4
register
Register variables tell the compiler to store variables in the CPU
register instead of memory. Frequently used variables are kept in
the CPU registers for faster access.
Example:
register char c = 's';
static
The static keyword is used to create static variables. A static
variable is not limited by a scope and can be used throughout the
program. It’s value is preserved even after it’s scope.
For Example:
static int num;
struct
The struct keyword in C programming language is used to declare a
structure. A structure is a list of variables, (they can be of different
data types), which are grouped together under one data type.
For Example:
struct Geek {
char name[50];
int num;
double var;
};
Below is the C program for the struct keyword:
C
1
// C program to demonstrate
2
// struct keyword
3
#include <stdio.h>
4
#include <string.h>
5
struct Books {
7
char title[50];
8
char author[50];
9
};
10
11
// Driver code
12
int main( ) {
13
16
// book 1 specification
17
20
return 0;
24
Output
Book 1 title : C++ Programming
Book 1 author : Bjarne Stroustrup
typedef
The typedef keyword in C programming language is used to define a
data type with a new name in the program. typedef keyword is used
to make our code more readable.
For Example:
typedef long num
In this example we have changed the datatype name of “long” to
“num”.
union
The union is a user-defined data type. All data members which are
declared under the union keyword share the same memory location.
Example:
union GeekforGeeks {
int x;
char s;
} obj;
Below is the C program for the union keyword:
C
1
#include <stdio.h>
2
union student {
3
int age;
4
char marks;
5
} s;
6
// Driver code
8
int main() {
9
s.age = 15;
10
s.marks = 56;
11
Output
age = 56
marks = 56
void
The void keyword means nothing i.e, NULL value. When the function
return type is used as the void, the keyword void specifies that it
has no return value.
Example:
void fun() {
// program
}
volatile
The volatile keyword is used to create volatile objects. Objects which
are declared volatile are omitted from optimization as their values
can be changed by code outside the scope of the current code at
any point in time.
For Example:
const volatile marks = 98;
marks are declared constant so they can’t be changed by the
program. But hardware can change it as they are volatile objects.
while
The while keyword is used to declare a while loop that runs till the
given condition is true.
Example:
C++
#include <stdio.h>
2
int main() {
4
int i = 0;
5
while (i < 3) {
8
printf("Hi\n");
9
i++;
10
}
11
12
return 0;
13
Output
Hi
Hi
Hi
Conclusion
In this article, the points we learned about the keywords are
mentioned below:
Keywords are Reserved words in C with certain meanings.
We can’t use keywords as any element’s name.
There are 32 keywords in C all having unique meanings.
Keywords in C – FAQs
What are keywords in C?
Keywords in C are reserved words that have certain meanings and
cannot be declare as any element’s name. For example: for is used
for declaring loop and it can’t be declared as an element’s name.
How many keywords are there in the C language?
There are 32 keywords in the C language.
What is the sizeof keyword in C?
Sizeof is a keyword that gets the size of an expression, (variables,
arrays, pointers, etc.) in bytes.
What is the default keyword in C?
The default keyword in C is used to specify the default case for the
switch statement.
What is volatile in c used for?
Volatile keywords in C are used for volatile objects.
Difference between keywords and identifiers.
Keywords are reserved words that have some meaning whereas
identifiers are the name-generated names for any variable, struct,
class, object, or function in C.