0% found this document useful (0 votes)
25 views35 pages

Unit No.1

c language

Uploaded by

rajashriwadkar68
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views35 pages

Unit No.1

c language

Uploaded by

rajashriwadkar68
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

C Programming Language Tutorial

Last Updated : 17 Dec, 2024



C is a general-purpose, procedural, and middle-


level programming language used for developing computer
software, system programming, applications, games, and
more. Known for its simplicity and efficiency, C is an excellent
choice for beginners as it provides a strong foundation in
programming concepts. C was developed by Dennis M. Ritchie at
Bell Laboratories in 1972. Initially, it was created for programming
the UNIX operating system.
 Bridges the gap between low-level programming (closer to
hardware and machine code) and high-level
programming (closer to human-readable code).
 Referred as the "mother of all programming
languages" because it influenced many modern programming
languages like C++, Java, Python and Go.
 C programs are platform-independent i.e. code can be
compiled and run on different systems with minimal
modifications.
 Does not require heavy runtime environments or libraries, making
it ideal for low-resource systems.
Hello World Program
C

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!

Features of C Programming Language


Last Updated : 11 Oct, 2024



C is a procedural programming language. It was initially developed by


Dennis Ritchie in the year 1972. It was mainly developed as a system
programming language to write an operating system.
The main features of C language include low-level access to memory, a
simple set of keywords, and a clean style, these features make C language
suitable for system programming like an operating system or compiler
development.
What are the Most Important Features of C
Language?
Here are some of the most important features of the C language:
1. Procedural Language
2. Fast and Efficient
3. Modularity
4. Statically Type
5. General-Purpose Language
6. Rich set of built-in Operators
7. Libraries with Rich Functions
8. Middle-Level Language
9. Portability
10. Easy to Extend

If you’re looking to explore the full potential of C, especially in working with


data structures, the C Programming Course Online with Data
Structures provides a complete guide to mastering C’s features for real-
world applications.
1. Procedural Language
In a procedural language like C step by step, predefined instructions are
carried out. C program may contain more than one function to perform a
particular task. New people to programming will think that this is the only way
a particular programming language works. There are other programming
paradigms as well in the programming world. Most of the commonly used
paradigm is an object-oriented programming language.
2. Fast and Efficient
Newer languages like Java, python offer more features than c programming
language but due to additional processing in these languages, their
performance rate gets down effectively. C programming language as the
middle-level language provides programmers access to direct manipulation
with the computer hardware but higher-level languages do not allow this.
That’s one of the reasons C language is considered the first choice to start
learning programming languages. It’s fast because statically typed
languages are faster than dynamically typed languages.
3. Modularity
The concept of storing C programming language code in the form of libraries
for further future uses is known as modularity. This programming language
can do very little on its own most of its power is held by its libraries. C
language has its own library to solve common problems.
4. Statically Type
C programming language is a statically typed language. Meaning the type of
variable is checked at the time of compilation but not at run time. This means
each time a programmer types a program they have to mention the type of
variables used.
5. General-Purpose Language
From system programming to photo editing software, the C programming
language is used in various applications. Some of the common applications
where it’s used are as follows:
 Operating systems : Windows, Linux, iOS, Android, OXS
 Databases: PostgreSQL, Oracle, MySQL, MS SQL Server, etc.
6. Rich set of built-in Operators
It is a diversified language with a rich set of built-in operators which are used
in writing complex or simplified C programs.
7. Libraries with Rich Functions
Robust libraries and functions in C help even a beginner coder to code with
ease.
8. Middle-Level Language
As it is a middle-level language so it has the combined form of both
capabilities of assembly language and features of the high-level language.
9. Portability
C language is lavishly portable as programs that are written in C language
can run and compile on any system with either no or small changes.
10. Easy to Extend
Programs written in C language can be extended means when a program is
already written in it then some more features and operations can be added to
it.

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



In C Programming language, there are many rules so to avoid


different types of errors. One of such rule is not able to declare
variable names with auto, long, etc. This is all because these are
keywords. Let us check all keywords in C language.
What are Keywords?
Keywords are predefined or reserved words that have special
meanings to the compiler. These are part of the syntax and cannot
be used as identifiers in the program. A list of keywords in C or
reserved words in the C programming language are mentioned
below:
auto break case char const continue default do

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

For a better understanding of C syntax and keyword usage, our C


programming course covers all the essential C keywords and how
they fit into the structure of a C program
auto
auto is the default storage class variable that is declared inside a
function or a block. auto variables can only be accessed within the
function/block they are declared. By default, auto variables have
garbage values assigned to them. Automatic variables are also
called local variables as they are local to a function.
auto int num;
Here num is the variable of the storage class auto and its type is int.
Below is the C program to demonstrate the auto keyword:
C

// C program to demonstrate
2
// auto keyword
3

#include <stdio.h>
4

int printvalue()
6

{
7

auto int a = 10;


8

printf("%d", a);
9

}
10

11

// Driver code
12

int main()
13

{
14

printvalue();
15

return 0;
16

Output
10

break and continue


The break statement is used to terminate the innermost loop. It
generally terminates a loop or a break statement. The continue
statement skips to the next iteration of the loop. Below is the C
program to demonstrate break and continue in C:
C
1

// C program to show use


2

// of break and continue


3

#include <stdio.h>
4

// Driver code
6

int main()
7

{
8

for (int i = 1; i <= 10; i++)


9

{
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

switch, case, and default


The switch statement in C is used as an alternate to the if-else
ladder statement. For a single variable i.e, switch variable it allows
us to execute multiple operations for different possible values of a
single variable.
switch(Expression)
{
case '1': // operation 1
break;
case:'2': // operation 2
break;
default: // default statement to be executed
}
Below is the C program to demonstrate the switch case statement:
C

// C program to demonstrate
2

// switch case statement


3

#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

const int a = 11;


8

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

printf("%d ", i);


11

i++;
12

} while(i <= 5);


13

14

return 0;
15

}
Output
1 2 3 4 5

double and float


The doubles and floats are datatypes used to declare decimal type
variables. They are similar, but doubles have 15 decimal digits, and
floats only have 7.
Example:
float marks = 97.5;
double num;
Below is the C program to demonstrate double float keyword:
C

// C program to demonstrate
2

// double float keyword


3

#include <stdio.h>
4

// Driver code
6

int main() {
7

float f = 0.3;
8

double d = 10.67;
9

printf("Float value: %f\n", f);


10

printf("Double value: %f\n", d);


11

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

if(a < 11)


10

{
11

printf("A is less than 11");


12

}
13

else
14

{
15

printf("A is equal to or "


16

"greater than 11");


17

}
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

enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};


8

// Driver code
10

int main()
11

{
12

//object of the enum (week), called day


13

enum week day;


14

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

for (int i = 0; i < 5; i++)


9

{
10

printf("%d ", i);


11

}
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

// Function to print numbers


6

// from 1 to 10
7

void printNumbers() {
8

int n = 1;
9

10

label:
11

printf("%d ", n);


12

n++;
13

if (n <= 10) goto label;


14

}
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 a = 10, b = 20;


7

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

short, long, signed, and unsigned


Different data types also have different ranges up to which they can
store numbers. These ranges may vary from compiler to compiler.
Below is a list of ranges along with the memory requirement and
format specifiers on the 32-bit GCC compiler.
Memory Format
Data Type (bytes) Range Specifier

short int 2 -32,768 to 32,767 %hd

unsigned short
2 0 to 65,535 %hu
int

unsigned int 4 0 to 4,294,967,295 %u

-2,147,483,648 to
4 %ld
long int 2,147,483,647

unsigned long 4 0 to 4,294,967,295 %lu


Memory Format
Data Type (bytes) Range Specifier

int

long long int 8 -(2^63) to (2^63)-1 %lld

unsigned long 0 to
8 %llu
long int 18,446,744,073,709,551,615

signed char 1 -128 to 127 %c

unsigned char 1 0 to 255 %c

long double 16 3.4E-4932 to 1.1E+4932 %Lf

Below is the C program to demonstrate the short, long, signed, and


unsigned keywords:
C

// C program to demonstrate
2

// short, long, signed,


3

// and unsigned keyword


4

#include <stdio.h>
5

// Driver code
7

int main() {
8
// short integer
9

short int a = 12345;


10

11

// signed integer
12

signed int b = -34;


13

14

// unsigned integer
15

unsigned int c = 12;


16

17

// L or l is used for
18

// long int in C.
19

long int d = 99998L;


20

21

printf("Integer value with a short int data: %hd", a);


22

printf("\nInteger value with a signed int data: %d", b);


23

printf("\nInteger value with an unsigned int data: %u", c);


24

printf("\nInteger value with a long int data: %ld", d);


25

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(int x, int y) {


5

int sum;
6

sum = x + y;
7

return sum;
8

}
9

10

// Driver code
11

int main() {
12

int num1 = 10;


13

int num2 = 20;


14

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

// Declare Book1 of type Book


14

struct Books book1;


15

16

// book 1 specification
17

strcpy(book1.title, "C++ Programming");


18
strcpy(book1.author, "Bjarne Stroustrup");
19

20

// Print book details


21

printf("Book 1 title : %s\n", book1.title);


22

printf("Book 1 author : %s\n", book1.author);


23

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

printf("age = %d", s.age);


12

printf("\nmarks = %d", s.marks);


13

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 loop that execute till i is less than 3


7

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.

You might also like