0% found this document useful (0 votes)
51 views15 pages

A Beginning of Foundation of "C"

The Beginning of Foundation of C" is an essential guide for anyone new to the world of C programming. Written in a clear and concise manner, this book provides an in-depth introduction to the basics of C programming and is designed to take the reader from beginner to proficient in an easy-to-follow manner. This book covers topics such as data types, variables, control structures, functions, and pointers, providing the reader with a strong foundation in C programming.

Uploaded by

Reason Bhattarai
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)
51 views15 pages

A Beginning of Foundation of "C"

The Beginning of Foundation of C" is an essential guide for anyone new to the world of C programming. Written in a clear and concise manner, this book provides an in-depth introduction to the basics of C programming and is designed to take the reader from beginner to proficient in an easy-to-follow manner. This book covers topics such as data types, variables, control structures, functions, and pointers, providing the reader with a strong foundation in C programming.

Uploaded by

Reason Bhattarai
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
You are on page 1/ 15

Page |1 Author: Aadarsha Jha

Contents:
 Introduction to C Programming
 What is C programming and why is it important?

 Setting up a development environment

 Basic syntax and structure of a C program

 Data Types and Variables


 Basic data types (int, float, char, etc.)

 Declaring and initializing variables

 Operators and Expressions


 Arithmetic operators

 Relational and logical operators

 Assignment and increment/decrement operators

 Type conversion and casting

 Control Structures
 if-else statements

 switch statements

 while and do-while loops

 for loops

 break and continue statements

 Functions
 Defining and calling functions

 Function prototypes and header files

 Recursion

 Function pointers

Page |2 Author: Aadarsha Jha


 Arrays and Strings
 Declaring and initializing arrays

 Multidimensional arrays

 Character arrays and string manipulation functions

 Pointers
 What are pointers and how to use them

 Pointer arithmetic

 Pointers and arrays

 Pointers to functions

 Structures and Unions


 Defining and using structures

 Nesting structures

 Unions

 File Input/Output
 Opening and closing files

 Reading and writing files

 Random access

 Dynamic Memory Allocation


 malloc and calloc functions

 realloc function

 free function

 Advanced Topics
 Preprocessor directives

 Command line arguments

 Bitwise operators

 Enumerated data types

Page |3 Author: Aadarsha Jha


 Conclusion
 Recap of key C programming concepts

 Further resources for learning C programming

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.

All the best and Happy programming!

Page |4 Author: Aadarsha Jha


 Introduction to C Programming:

C is a high-level programming language that was developed in the 1970s. It


is a general-purpose language that is often used to develop operating
systems, as well as various applications that run on those systems.

C is important because it is a widely used and versatile programming


language. It is considered a "low-level" language because it is closer to
machine code (the code that a computer can understand and execute) than
many other high-level languages. This means that it can be used to write
code that is very efficient and has a small memory footprint. C is also a
relatively simple language, making it easier for beginners to learn and
understand.

C is often used as a "base" language, meaning that other languages (such as


C++ and Objective-C) are built on top of it and use many of the same
concepts and syntax. Many modern programming languages and tools, such
as Python and Git, were also implemented in C or used C code as a
foundation.

In summary, C is an important programming language because of its


versatility, efficiency, and wide usage in operating systems and other
applications.

Page |5 Author: Aadarsha Jha


 Setting up a development environment:

To set up a development environment for C programming, you will need to


install a C compiler and a text editor on your computer. Here is a step-by-
step guide to setting up a development environment for 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.

2. Install the C compiler: Follow the instructions for your chosen C


compiler to download and install it on your computer. You may need
to add the compiler's installation directory to your system's PATH
environment variable so that you can run it from the command line.

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:

Page |6 Author: Aadarsha Jha


This will create an executable file called "myprogram" that you can run to
execute your C program.

[Note: If you are still confused by the provided instruction above, then you can
click here for the clear and detailed instruction.]

Link of Compiler Website: http://www.mingw.org/

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.

To download Quincy, go through this link: Click Here

Page |7 Author: Aadarsha Jha


 Basic Syntax and Structure of C Program:

 Here is a brief overview of the basic syntax and structure of a C program:

1. C programs are made up of one or more functions, which are self-


contained blocks of code that perform a specific task. The main
function is the entry point of a C program and is where execution
begins.

2. Functions are defined using the following syntax:

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.

4. A function can also have something called "parameters". These are


like little pockets that you can put values into when you call the
function.

5. C programs use curly braces "{ }" to group things together. Each line
of code (also called a "statement") must end with a semicolon ";".

6. C programs use special words called "keywords" to do different


things, like "if" to make a choice or "for" to repeat something.

7. C programs also use "variables" to store and manipulate data. You


have to tell C what kind of data a variable will hold (like a number or
a word) before you can use it.

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.

Page |8 Author: Aadarsha Jha


 Introduction to Constants and its types
- In C, a constant is a value that cannot be modified during the execution of a
program. Constants are used to represent fixed values that do not change,
such as the value of pi (3.14159).

 Types of C constants:
- C constants can be divided into two major categories:
a) Primary Constants
b) Secondary Constants

Here’s the flowsheet diagram of division of constants of C in major categories:

Figure 1.0

At this stage, we would restrict our discussion to only primary constants. As


well as you will be familiar with two more constants called “string constants”
and “symbol constants”.

1. Integer constants: These are whole numbers, such as 0, 1, 2, etc. It must


not have a decimal point & can be either +ve or -ve. No commas or
blanks are allowed within an integer constant.

2. Floating-point constants: These are numbers with decimal points, such as


3.14159 or 0.00001. It could either be positive or negative but the default
sign is positive. No commas and blanks are allowed within a floating-
point constant (Real Constant).

Page |9 Author: Aadarsha Jha


3. Character constants: These are single characters enclosed in single
quotes, such as 'A' or '9'. Character constants can be used to store and
manipulate individual characters, such as letters, numbers, and special
symbols. Character constants are stored in the computer's memory as
integer values, with each character represented by a unique number called
an ASCII (American Standard Code for Information Interchange) value.
For example, the ASCII value for the character 'A' is 65, and the ASCII
value for the character 'B' is 66.

4. String constants: These are sequences of characters enclosed in double


quotes, such as "hello" or "goodbye".

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:

P a g e | 10 Author: Aadarsha Jha


 Keywords in C
- In C, keywords are predefined words that have special meanings in the
language. They cannot be used as identifiers (names of variables, functions,
etc.) because they are reserved for specific purposes.
There are several keywords in C, including:
a) auto: This keyword is used to declare automatic variables, which are local
variables that are created when a function is called and destroyed when
the function returns.
b) break: This keyword is used to exit a loop or switch statement.
c) case: This keyword is used in a switch statement to specify a particular
case to be executed.
d) char: This keyword is used to declare variables of the char data type,
which can store a single character.
e) const: This keyword is used to declare constant variables, which cannot
be modified during the execution of a program.
f) continue: This keyword is used to skip the rest of the current iteration of a
loop and move on to the next iteration.
g) default: This keyword is used in a switch statement to specify the default
case to be executed if none of the other cases match.
h) do: This keyword is used to begin a do-while loop, which executes a
block of code at least once before checking the condition.
i) double: This keyword is used to declare variables of the double data type,
which can store decimal values with double precision.
j) else: This keyword is used in an if-else statement to specify the code to be
executed if the condition is false.

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.

P a g e | 11 Author: Aadarsha Jha


 Data Types and Variables:
- In C, there are several basic data types that can be used to store different
types of information. Here is a brief overview of some of the most
commonly used data types:
-
 int: This data type is used to store integers (whole numbers). It can store
values ranging from -2147483648 to 2147483647. Here is an example of
how to declare an int variable in C:

You can also initialize an “int” variable with a value when you declare it:

 float: This data type is used to store floating-point numbers (numbers


with decimal points). For example: float y = 3.14;
 char: This data type is used to store single characters. Characters are
enclosed in single quotes. For example: char c = 'a';

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.

P a g e | 12 Author: Aadarsha Jha


 Declaring and initializing variables:
- In C, a variable is a named location in memory where a value can be stored
and manipulated. Before a variable can be used in a C program, it must be
declared, which involves specifying the data type and the name of the
variable.
Here is the general syntax for declaring a variable in C:

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:

In C, it is also possible to initialize a variable at the same time that it is


declared. Initialization is the process of giving a variable its first value.

Here is the general syntax for initializing a variable in C:

For example, the following code declares and initializes a variable of the int
data type called "age" with the value 25:

P a g e | 13 Author: Aadarsha Jha


You can also initialize multiple variables of the same data type in a single
statement by separating them with commas:

It is generally a good practice to initialize variables when you declare them,


as this ensures that they have a known and consistent value throughout the
program. In C, variable names can be composed of letters, digits, and the
underscore character (_). They must begin with a letter or an underscore, and
they are case-sensitive. For example, "age" and "Age" are considered to be
different variables. It is a good idea to choose descriptive and meaningful
names for your variables to make your code easier to read and understand.

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:

P a g e | 14 Author: Aadarsha Jha


Local variables are declared within a function and are only visible and
accessible within that function. They are created when the function is called
and destroyed when the function returns. Here is an example of a local
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.

It is important to understand the scope and lifetime of variables in C, as it


can affect the way you write and organize your code. For example, you may
need to be careful about using the same variable name in different parts of
your program, as it can cause confusion or unexpected results if the variables
have different scopes.

P a g e | 15 Author: Aadarsha Jha

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy