A Beginning of Foundation of "C"
A Beginning of Foundation of "C"
Contents:
Introduction to C Programming
What is C programming and why is it important?
Control Structures
if-else statements
switch statements
for loops
Functions
Defining and calling functions
Recursion
Function pointers
Multidimensional arrays
Pointers
What are pointers and how to use them
Pointer arithmetic
Pointers to functions
Nesting structures
Unions
File Input/Output
Opening and closing files
Random access
realloc function
free function
Advanced Topics
Preprocessor directives
Bitwise operators
Welcome to the world of C programming! This book is designed to be a comprehensive guide to the C
programming language, covering all of the essential concepts and skills that you will need to become
a proficient C programmer.
Whether you are a beginner with no programming experience or an experienced developer looking to
add C to your skill set, this book is for you. We will start from the very basics and work our way up to
more advanced topics, providing plenty of examples and exercises along the way.
By the end of this book, you will have a solid understanding of the C language and be able to write
efficient and effective C programs. So, let's get started on your C programming journey!
Thanks to my elder brother Er. Govind Jha and Er. Amit Jha for inspiring me…
And special thanks to Ph.D. Ekta Vyas Ma’am to make me efficient creative and always supported in
my journey.
No part and style of the book may be copied in any form without the written
permission of the author.
© Aadarsha Jha
Note: For the students of classes 11 & 12 topics such as Dynamic Memory
Allocation and Advanced topics are not mandatory to go through it. Interested
Candidate Can proceed forward and build their strong foundation in C
programming.
1. Choose a C compiler: There are several C compilers that you can use,
such as GCC (GNU Compiler Collection) and Clang. These compilers
are free and open source, and they are available for various operating
systems, including Windows, macOS, and Linux.
3. Choose a text editor: A text editor is a software program that you will
use to write and edit your C code. Some popular text editors for C
programming include Atom, Sublime Text, and Visual Studio Code.
4. Install the text editor: Follow the instructions for your chosen text
editor to download and install it on your computer.
5. Create a new project: Open your text editor and create a new file for
your C program. Save the file with a .c extension
(e.g.,"myprogram.c").
6. Write and compile your C code: Use your text editor to write your C
code, and use the C compiler to compile it. To compile your code,
open a command prompt or terminal and navigate to the directory
where your C file is saved. Then, use the compiler's command-line
interface to compile your code. For example, with GCC you would
use the following command:
[Note: If you are still confused by the provided instruction above, then you can
click here for the clear and detailed instruction.]
And if you don’t want to install any of these you can proceed with IDE named
“Quincy”. It has its inbuilt compiler so it will be easier to write your program on
this platform.
3. Each function has a "name" and a "return type". The return type tells
you what kind of value the function will give you when it's done.
5. C programs use curly braces "{ }" to group things together. Each line
of code (also called a "statement") must end with a semicolon ";".
8. C programs can also have "comments", which are lines of text that the
computer ignores. Comments are used to explain what the code does.
Comments in C are indicated by a forward slash followed by an
asterisk (/) at the beginning of the comment, and an asterisk followed
by a forward slash (/) at the end.
Types of C constants:
- C constants can be divided into two major categories:
a) Primary Constants
b) Secondary Constants
Figure 1.0
5. Symbol constants: These are named constants that are defined using the
#define directive. For example:
6. Enumeration constants: These are constants that are defined using the
Enum keyword. For example:
Figure 1.1
There are only 32 keywords in ‘C’ as shown in the fig above. A detailed discussion of each of
these keywords would be taken up in later chapters wherever their use is relevant.
You can also initialize an “int” variable with a value when you declare it:
There are also several other data types that can be used in C, including double,
short, and long.
It's important to choose the appropriate data type for your variables to ensure
that your code is efficient and accurate. For example, using a float to store a
large integer might lead to rounding errors, while using an int to store a large
decimal number might result in data loss.
For example, the following code declares a variable of the int data type
called "age":
You can also declare multiple variables of the same data type in a single
statement by separating them with commas:
For example, the following code declares and initializes a variable of the int
data type called "age" with the value 25:
There are several built-in data types that you can use to declare variables,
such as int (integer), float (floating-point number), and char (character).
There are also derived data types, such as pointers, arrays, structures, and
unions. You should choose the data type that is most appropriate for the type
of data that you will be storing in the variable.
Scope and lifetime of variables:
- In C, the scope of a variable refers to the part of the program where the
variable is visible and can be used. There are two types of variable scope in
C: global and local.
Global variables are declared outside of any function and are visible and
accessible throughout the entire program. They are typically defined at the
beginning of the program, before any functions are defined. Here is an
example of a global variable in C:
The lifetime of a variable refers to the period of time during which the
variable exists in memory. In C, the lifetime of a global variable begins
when the program starts and ends when the program terminates. The lifetime
of a local variable begins when the function is called and ends when the
function returns.