0% found this document useful (0 votes)
9 views22 pages

Introduction to C Programming Basics

C is a general-purpose procedural programming language developed by Dennis Ritchie in 1972, primarily for system programming and UNIX. Learning C provides a strong foundation for understanding modern programming languages and is widely used in various applications, including operating systems and embedded systems. The document covers the basic structure of a C program, including header files, functions, operators, and examples of arithmetic, relational, logical, and bitwise operations.

Uploaded by

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

Introduction to C Programming Basics

C is a general-purpose procedural programming language developed by Dennis Ritchie in 1972, primarily for system programming and UNIX. Learning C provides a strong foundation for understanding modern programming languages and is widely used in various applications, including operating systems and embedded systems. The document covers the basic structure of a C program, including header files, functions, operators, and examples of arithmetic, relational, logical, and bitwise operations.

Uploaded by

kanu07227
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Language Introduction

Last Updated : 11 Sep, 2025

●​
●​
●​

C is a general-purpose procedural programming language initially developed by Dennis


Ritchie in 1972 at Bell Laboratories of AT&T Labs. It was mainly created as a system
programming language to write the UNIX operating system.

Why Learn C?
●​ C is considered mother of all programming languages as many later languages like Java,
PHP and JavaScript have borrowed syntax/features directly or indirectly from the C.
●​ If a person learns C programming first, it helps to learn any modern programming language
as it provide a deeper understanding of the fundamentals of programming and underlying
architecture of the operating system like pointers, working with memory locations etc.
●​ C is widely used in operating systems, embedded systems, compilers, databases,
networking, game engines, and real-time systems for its efficiency to work in low resource
environment and hardware-level support.
Writing First Program in C Language
This simple program demonstrates the basic structure of a C program. It will also help us
understand the basic syntax of a C program.

#include <stdio.h>
int main(void)
{
// This prints "Hello World"
printf("Hello World");
return 0;
}

Output
Hello World
Let us analyze the structure of our program line by line.
Structure of the C program
After the above discussion, we can formally assess the basic structure of a C program. By
structure, it is meant that any program can be written in this structure only. Writing a C program
in any other structure will lead to a Compilation Error. The structure of a C program is as
follows:

Header Files Inclusion - Line 1 [#include <stdio.h>]


The first component is the Header files in a C program. A header file is a file with extension .h
which contains C function declarations and macro definitions to be shared between several
source files. All lines that start with # are processed by a preprocessor which is a program
invoked by the compiler. In the above example, the preprocessor copies the preprocesses code of
stdio.h to our file. The .h files are called header files in C.​
Some of the C Header files:
Header Files
The C standard library is declared in a number of header files, each containing function
declarations, data type definitions, and macros. Some of the key header files include:
●​ <stdio.h>: Defines core input and output functions.
●​ <stdlib.h>: Defines numeric conversion functions, pseudo-random numbers generation
functions, memory allocation, and process control functions.
●​ <string.h>: Defines string-handling functions.
●​ <math.h>: Defines common mathematical functions.
Functions
The library provides a wide range of functions for various tasks:
●​ String Handling: Functions like strcpy, strcat, and strlen are used for string manipulation.
●​ Mathematical Computations: Functions like sin, cos, and sqrt are used for mathematical
calculations.
●​ Memory Management: Functions like malloc, calloc, and free are used for dynamic memory
allocation.
Main Method Declaration - Line 2 [int main()]
The next part of a C program is the main() function. It is the entry point of a C program and the
execution typically begins with the first line of the main(). The empty brackets indicate that the
main doesn't take any parameter (See this for more details). The int that was written before the
main indicates the return type of main(). The value returned by the main indicates the status of
program termination.
Body of Main Method - Line 3 to Line 6 [enclosed in {}]

\
Operators are the basic components of C programming. They are symbols that represent some
kind of operation, such as mathematical, relational, bitwise, conditional, or logical computations,
which are to be performed on values or variables. The values and variables used with operators
are called operands.
Example:

#include <stdio.h>
int main() {

// Expression for getting sum


int sum = 10 + 20;

printf("%d", sum);
return 0;
}

Output
30
In the above expression, '+' is the addition operator that tells the compiler to add both of the
operands 10 and 20. To dive deeper into how operators are used with data structures, the C
Programming Course Online with Data Structures covers this topic thoroughly.
Unary, Binary and Ternary Operators
On the basis of the number of operands they work on, operators can be classified into three types
:
1.​ Unary Operators: Operators that work on single operand.​
Example: Increment( ++) , Decrement(--)
2.​ Binary Operators: Operators that work on two operands.​
Example: Addition (+), Subtraction( -) , Multiplication (*)
3.​ Ternary Operators: Operators that work on three operands.​
Example: Conditional Operator( ? : )
Types of Operators in C
C language provides a wide range of built in operators that can be classified into 6 types based
on their functionality:
Table of Content
●​ Arithmetic Operators
●​ Relational Operators
●​ Logical Operator
●​ Bitwise Operators
●​ Assignment Operators
●​ Other Operators
Arithmetic Operators
The arithmetic operators are used to perform arithmetic/mathematical operations on operands.
There are 9 arithmetic operators in C language:
Symbol Operator Description Syntax

Adds two numeric


+ Plus a+b
values.

Subtracts right
- Minus operand from left a-b
operand.

Multiply two
* Multiply a*b
numeric values.

Divide two
/ Divide a/b
numeric values.

Returns the
remainder after
% Modulus diving the left a%b
operand with the
right operand.

Used to specify
+ Unary Plus the positive +a
values.

Flips the sign of


- Unary Minus -a
the value.

Increases the
++ Increment value of the a++
operand by 1.

Decreases the
-- Decrement value of the a--
operand by 1.

Example of C Arithmetic Operators


#include <stdio.h>

int main() {

int a = 25, b = 5;

// using operators and printing results
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a % b = %d\n", a % b);
printf("+a = %d\n", +a);
printf("-a = %d\n", -a);
printf("a++ = %d\n", a++);
printf("a-- = %d\n", a--);

return 0;
}

Output
a + b = 30
a - b = 20
a * b = 125
a/b=5
a%b=0
+a = 25
-a = -25
a++ = 25
a-- = 26
Relational Operators
The relational operators in C are used for the comparison of the two operands. All these
operators are binary operators that return true or false values as the result of comparison.
These are a total of 6 relational operators in C:
Symbol Operator Description Syntax

Returns true if the


left operand is less
< Less than than the right a<b
operand. Else
false

Returns true if the


left operand is
> Greater than greater than the a>b
right operand.
Else false

Returns true if the


left operand is less
Less than or
<= than or equal to a <= b
equal to
the right operand.
Else false

Returns true if the


left operand is
Greater than or greater than or
>= a >= b
equal to equal to right
operand. Else
false

Returns true if
== Equal to both the operands a == b
are equal.

Returns true if
!= Not equal to both the operands a != b
are NOT equal.

Example of C Relational Operators

#include <stdio.h>

int main() {
int a = 25, b = 5;

// using operators and printing results
printf("a < b : %d\n", a < b);
printf("a > b : %d\n", a > b);
printf("a <= b: %d\n", a <= b);
printf("a >= b: %d\n", a >= b);
printf("a == b: %d\n", a == b);
printf("a != b : %d\n", a != b);

return 0;
}

Output
a<b :0
a>b :1
a <= b: 0
a >= b: 1
a == b: 0
a != b : 1
Here, 0 means false and 1 means true.
Logical Operator
Logical Operators are used to combine two or more conditions/constraints or to complement
the evaluation of the original condition in consideration. The result of the operation of a logical
operator is a Boolean value either true or false.
There are 3 logical operators in C:
Symbol Operator Description Syntax

Returns true if
&& Logical AND both the operands a && b
are true.

Returns true if
|| Logical OR both or any of the a || b
operand is true.

Returns true if the


! Logical NOT !a
operand is false.
Example of Logical Operators in C

#include <stdio.h>

int main() {
int a = 25, b = 5;

// using operators and printing results
printf("a && b : %d\n", a && b);
printf("a || b : %d\n", a || b);
printf("!a: %d\n", !a);

return 0;
}

Output
a && b : 1
a || b : 1
!a: 0
Bitwise Operators
The Bitwise operators are used to perform bit-level operations on the operands. The operators
are first converted to bit-level and then the calculation is performed on the operands.
Note: Mathematical operations such as addition, subtraction, multiplication, etc. can be
performed at the bit level for faster processing.
There are 6 bitwise operators in C:
Symbol Operator Description Syntax

Performs
bit-by-bit AND
& Bitwise AND a&b
operation and
returns the result.

Performs
bit-by-bit OR
| Bitwise OR a|b
operation and
returns the result.
Symbol Operator Description Syntax

Performs
bit-by-bit XOR
^ Bitwise XOR a^b
operation and
returns the result.

Flips all the set


Bitwise First
~ and unset bits on ~a
Complement
the number.

Shifts bits to the


left by a given
number of
<< Bitwise Leftshift positions; a << b
multiplies the
number by 2 for
each shift.

Shifts bits to the


right by a given
Bitwise number of
>> a >> b
Rightshilft positions; divides
the number by 2
for each shift.

Example of Bitwise Operators

#include <stdio.h>

int main() {
int a = 25, b = 5;

// using operators and printing results
printf("a & b: %d\n", a & b);
printf("a | b: %d\n", a | b);
printf("a ^ b: %d\n", a ^ b);
printf("~a: %d\n", ~a);
printf("a >> b: %d\n", a >> b);
printf("a << b: %d\n", a << b);

return 0;
}

Output
a & b: 1
a | b: 29
a ^ b: 28
~a: -26
a >> b: 0
a << b: 800
Assignment Operators
Assignment operators are used to assign value to a variable. The left side operand of the
assignment operator is a variable and the right side operand of the assignment operator is a value.
The value on the right side must be of the same data type as the variable on the left side
otherwise the compiler will raise an error.
The assignment operators can be combined with some other operators in C to provide multiple
operations using single operator. These operators are called compound operators.
In C, there are 11 assignment operators:
Symbol Operator Description Syntax

Assign the value


Simple of the right
= a=b
Assignment operand to the left
operand.

Add the right


operand and left
operand and
+= Plus and assign a += b
assign this value
to the left
operand.

Subtract the right


operand and left
operand and
-= Minus and assign a -= b
assign this value
to the left
operand.
Symbol Operator Description Syntax

Multiply the right


operand and left
Multiply and operand and
*= a *= b
assign assign this value
to the left
operand.

Divide the left


operand with the
Divide and right operand and
/= a /= b
assign assign this value
to the left
operand.

Assign the
remainder in the
Modulus and division of left
%= a %= b
assign operand with the
right operand to
the left operand.

Performs bitwise
AND and assigns
&= AND and assign a &= b
this value to the
left operand.

Performs bitwise
OR and assigns
|= OR and assign a |= b
this value to the
left operand.

Performs bitwise
XOR and assigns
^= XOR and assign a ^= b
this value to the
left operand.

Performs bitwise
Rightshift and
>>= Rightshift and a >>= b
assign
assign this value
Symbol Operator Description Syntax

to the left
operand.

Performs bitwise
Leftshift and
Leftshift and
<<= assign this value a <<= b
assign
to the left
operand.

Example of C Assignment Operators

#include <stdio.h>

int main() {
int a = 25, b = 5;

// using operators and printing results
printf("a = b: %d\n", a = b);
printf("a += b: %d\n", a += b);
printf("a -= b: %d\n", a -= b);
printf("a *= b: %d\n", a *= b);
printf("a /= b: %d\n", a /= b);
printf("a %%= b: %d\n", a %= b);
printf("a &= b: %d\n", a &= b);
printf("a |= b: %d\n", a |= b);
printf("a ^= b: %d\n", a ^= b);
printf("a >>= b: %d\n", a >>= b);
printf("a <<= b: %d\n", a <<= b);

return 0;
}

Output
a = b: 5
a += b: 10
a -= b: 5
a *= b: 25
a /= b: 5
a %= b: 0
a &= b: 0
a |= b: 5
a ^= b: 0
a >>= b: 0
a <<= b: 0
Other Operators
Apart from the above operators, there are some other operators available in C used to perform
some specific tasks. Some of them are discussed here:
sizeof Operator
●​ sizeof is much used in the C programming language.
●​ It is a compile-time unary operator which can be used to compute the size of its operand.
●​ The result of sizeof is of the unsigned integral type which is usually denoted by size_t.
●​ Basically, the sizeof the operator is used to compute the size of the variable or datatype.
Syntax
sizeof (operand)
Comma Operator ( , )
The comma operator (represented by the token) is a binary operator that evaluates its first
operand and discards the result, it then evaluates the second operand and returns this value (and
type).
The comma operator has the lowest precedence of any C operator. It can act as both operator and
separator.
Syntax
operand1 , operand2
Conditional Operator ( ? : )
The conditional operator is the only ternary operator in C++. It is a conditional operator that we
can use in place of if..else statements.
Syntax
expression1 ? Expression2 : Expression3;
Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then
we will execute and return the result of Expression2 otherwise if the condition(Expression1)
is false then we will execute and return the result of Expression3.
dot (.) and arrow (->) Operators
Member operators are used to reference individual members of classes, structures, and unions.
●​ The dot operator is applied to the actual object.
●​ The arrow operator is used with a pointer to an object.
Syntax
structure_variable . member;
structure_pointer -> member;
Cast Operators
Casting operators convert one data type to another. For example, int(2.2000) would return 2.
●​ A cast is a special operator that forces one data type to be converted into another.
Syntax
(new_type) operand;
addressof (&) and Dereference (*) Operators
Addressof operator & returns the address of a variable and the dereference operator * is a
pointer to a variable. For example *var; will pointer to a variable var.
Example of Other C Operators

// C Program to demonstrate the use of Misc operators


#include <stdio.h>

int main()
{
// integer variable
int num = 10;
int* add_of_num = &num;

printf("sizeof(num) = %d bytes\n", sizeof(num));
printf("&num = %p\n", &num);
printf("*add_of_num = %d\n", *add_of_num);
printf("(10 < 5) ? 10 : 20 = %d\n", (10 < 5) ? 10 : 20);
printf("(float)num = %f\n", (float)num);

return 0;
}

Output
sizeof(num) = 4 bytes
&num = 0x7ffdb58c037c
*add_of_num = 10
(10 < 5) ? 10 : 20 = 20
(float)num = 10.000000
Operator Precedence and Associativity
Operator Precedence and Associativity is the concept that decides which operator will be
evaluated first in the case when there are multiple operators present in an expression to avoid
ambiguity. As, it is very common for a C expression or statement to have multiple operators and
in this expression.
The below table describes the precedence order and associativity of operators in C. The
precedence of the operator decreases from top to bottom.
Precedence Operator Description Associativity

1 () Parentheses (function call) left-to-right


Precedence Operator Description Associativity

[] Brackets (array subscript) left-to-right

Member selection via object


. left-to-right
name

-> Member selection via a pointer left-to-right

Postfix increment/decrement (a
a++ , a-- left-to-right
is a variable)

Prefix increment/decrement (a is
++a , --a right-to-left
a variable)

+,- Unary plus/minus right-to-left

Logical negation/bitwise
!,~ right-to-left
complement

Cast (convert value to temporary


(type) right-to-left
value of type)

* Dereference right-to-left

& Address (of operand) right-to-left

Determine size in bytes on this


sizeof right-to-left
2 implementation

3 *,/,% Multiplication/division/modulus left-to-right

4 +,- Addition/subtraction left-to-right

Bitwise shift left, Bitwise shift


<< , >> left-to-right
5 right
Precedence Operator Description Associativity

Relational less than/less than or


< , <= left-to-right
equal to

Relational greater than/greater


> , >= left-to-right
6 than or equal to

Relational is equal to/is not


== , != left-to-right
7 equal to

8 & Bitwise AND left-to-right

9 ^ Bitwise XOR left-to-right

10 | Bitwise OR left-to-right

11 && Logical AND left-to-right

12 || Logical OR left-to-right

13 ?: Ternary conditional right-to-left

= Assignment right-to-left

+= , -= Addition/subtraction assignment right-to-left

Multiplication/division
*= , /= right-to-left
assignment

Modulus/bitwise AND
%= , &= right-to-left
assignment

Bitwise exclusive/inclusive OR
^= , |= right-to-left
14 assignment
Precedence Operator Description Associativity

Bitwise shift left/right


<<=, >>= right-to-left
assignment

15 , expression separator left-to-right

The body of the main method in the C program refers to statements that are a

part of the main function. It can be anything like manipulations, searching, sorting, printing, etc.
A pair of curly brackets define the body of a function. All functions must start and end with curly
brackets. A variable in C is a named piece of memory which is used to store data and access it
whenever required. It allows us to use the memory without having to memorize the exact
memory address.
To create a variable in C, we have to specify a name and the type of data it is going to store in
the syntax.
data_type name;
C provides different data types that can store almost all kinds of data. For example, int, char,
float, double, etc.
int num;
char letter;
float decimal;
In C, every variable must be declared before it is used. We can also declare multiple variables of
same data type in a single statement by separating them using comma as shown:
data_type name1, name2, name3, ...;
Rules for Naming Variables in C
We can assign any name to a C variable as long as it follows the following rules:
●​ A variable name must only contain letters, digits, and underscores.
●​ It must start with an alphabet or an underscore only. It cannot start with a digit.
●​ No white space is allowed within the variable name.
●​ A variable name must not be any reserved word or keyword.
●​ The name must be unique in the program.
C Variable Initialization
Once the variable is declared, we can store useful values in it. The first value we store is called
initial value and the process is called Initialization. It is done using assignment operator (=).

int num;
num = 3;
It is important to initialize a variable because a C variable only contains garbage value when it is
declared. We can also initialize a variable along with declaration.
int num = 3;
Note: It is compulsory that the values assigned to the variables should be of the same data type
as specified in the declaration.
Accessing Variables
The data stored inside a C variable can be easily accessed by using the variable's name.
Example:

#include <stdio.h>

int main() {

// Create integer variable


int num = 3;

// Access the value stored in


// variable
printf("%d", num);
return 0;
}

Output
3
Changing Stored Values
We can also update the value of a variable with a new value whenever needed by using the
assignment operator =.
Example:

#include <stdio.h>

int main() {

// Create integer variable


int n = 3;

// Change the stored data


n = 22;

// Access the value stored in


// variable
printf("%d", n);
return 0;
}

Output
22
How to use variables in C?
Variables act as name for memory locations that stores some value. It is valid to use the variable
wherever it is valid to use its value. It means that a variable name can be used anywhere as a
substitute in place of the value it stores.
Example: An integer variable can be used in a mathematical expression in place of numeric
values.

#include <stdio.h>
int main() {

// Expression that uses values


int sum1 = 20 + 40;

// Defining variables
int a = 20, b = 40;

// Expression that uses variables


int sum2 = a + b;

printf("%d\n%d", sum1, sum2);


return 0;
}

Output
60
60
Memory Allocation of C Variables
When a variable is declared, the compiler is told that the variable with the given name and type
exists in the program. But no memory is allocated to it yet. Memory is allocated when the
variable is defined.
Most programming languages like C generally declare and define a variable in the single step.
For example, in the above part where we create a variable, variable is declared and defined in a
single statement.
The size of memory assigned for variables depends on the type of variable. We can check the
size of the variables using sizeof operator.
Example:

#include <stdio.h>

int main() {
int num = 22;

// Finding size of num


printf("%d bytes", sizeof(num));
return 0;
}

Output
4 bytes
Variables are also stored in different parts of the memory based on their storage classes.
Scope of Variables in C
We have told that a variable can be accessed anywhere once it is declared, but it is partially true.
A variable can be accessed using its name anywhere in a specific region of the program called
its scope. It is the region of the program where the name assigned to the variable is valid.
A scope is generally the area inside the {} curly braces.

Comment - Line 7[// This prints "Hello World"]


The comments are used for the documentation of the code or to add notes in your program that
are ignored by the compiler and are not the part of executable program .
Statement - Line 4 [printf("Hello World");]
Statements are the instructions given to the compiler. In C, a statement is always terminated by
a semicolon (;). In this particular case, we use printf() function to instruct the compiler to display
"Hello World" text on the screen.
Return Statement - Line 5 [return 0;]
The last part of any C function is the return statement. The return statement refers to the return
values from a function. This return statement and return value depend upon the return type of the
function. The return statement in our program returns the value from main(). The returned value
may be used by an operating system to know the termination status of your program. The value 0
typically means successful termination.
Execute C Programs
To run this program, you need to create a development environment for C. A development
environment mainly consists of a text editor, which is used to write the code, and a compiler that
converts the written code into the executable file. Refer to this article - Setting Up C
Development Environment
Application of C Language
C language is being used since the early days of computer programming and still is used in wide
variety of applications such as:
●​ C is used to develop core components of operating systems such as Windows, Linux, and
macOS.
●​ C is applied to program embedded systems in small devices such as washing machines,
microwave ovens, and printers.
●​ C is utilized to create efficient and quick game engines. For example, the Doom video game
engine was implemented using C.
●​ C is employed to construct compilers, assemblers, and interpreters. For example, the
CPython interpreter is written partially using C.
●​ C is applied to develop efficient database engines. The MySQL database software is
implemented using C and C++.
●​ C is employed to create programs for devices and sensors of Internet of Things (IoT). A
common example is a house automation system comprising temperature sensors and
controllers that is often prepared with C.
●​ C is employed for creating lightweight and speedy desktop applications. The widely used
text editor Notepad++ employs C for performance-sensitive sections.
History Of C
C is a general-purpose procedural programming language developed by Dennis Ritchie in 1972
at Bell Labs to build the UNIX operating system. It provides low-level memory access, high
performance, and portability, making it ideal for system programming. Over the years, C has
evolved through standards like ANSI C, C99, C11, and C23, adding modern features while
retaining its simplicity.

You might also like