Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Dive Into Sea of C
Dive Into Sea of C
Dive Into Sea of C
Ebook208 pages1 hour

Dive Into Sea of C

Rating: 0 out of 5 stars

()

Read preview

About this ebook

Dive into the world of C programming with our accessible guide, where every concept is demystified in simple English terms. This book goes beyond theory, offering hands-on learning with clear and concise code examples for each topic. From foundational principles to advanced techniques, readers progress seamlessly, gaining confidence through practical exercises. Real-world applications are woven into the fabric of the book, connecting theory to practice. Whether you're a beginner or seeking to enhance your skills, this resource equips you with the tools to write efficient and robust code. Unlock the power of C programming with clarity, simplicity, and a focus on real-world applicability in every chapter.

LanguageEnglish
PublisherM Ashok
Release dateJan 14, 2024
ISBN9798223226086
Dive Into Sea of C

Related to Dive Into Sea of C

Related ebooks

Programming For You

View More

Related articles

Reviews for Dive Into Sea of C

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Dive Into Sea of C - M Ashok

    Preface

    Four decades after its introduction, C remains the most popular and widely used programming language. Its versatility can be attributed to the fact that operating systems such as Windows and Unix, as well as many high-level languages, inherit C's control structures and other basic features. Python programming language is written in C, and most used libraries in Machine learning and Deep learning, like Tensorflow, are written in C. Programmers around the world use the C language for its incredible efficiency. In some cases, C code can run as fast as code written in assembly language. C code is versatile, extensible, and portable. Therefore, for most students, C remains the first step towards learning programming skills.

    Table of Contents

    First C Program—1

    Tokens—5

    Operators,Control and Looping statements—30

    Functions—82

    Arrays—92

    Strings—118

    Pointers—143

    Structures, Unions and Enums—174

    1. Understanding the First C Program:

    Output:

    After executing the above 5-line C program in any IDE or code editor, we get Hello World!!! as output. Let’s understand our first program by breaking it into three simpler parts.

    The above line simply says to include the file named stdio.h into the program. Files ending with. h extensions are called header files. Why should a C program include header files? In the C language, there is flexibility for programmers to include already written or predefined functionality into the program. In the above program, the printf() function is used. The functionality or working of the printf() function is already written in the stdio.h file, which is to print sentences enclosed in double quotes as output. The sections that follow provide answers to the following queries: Why is printf() referred to as a function? What is a function? How does a function operate?

    Every header file has some predefined functions in it. To use those predefined functions, the corresponding header file must be included. Below are the most used header files in the C language:

    ➢  stdio.h  –  It has all the predefined input and output functions, such as printf(), scanf(), gets(), puts(), etc.

    ➢  math.h  –  It has all the predefined mathematical functions such as ceil(), sqrt(), pow(), exp(), etc.

    ➢  string.h  –  It has all the predefined string manipulation functions such as strncpy(), strncat(), strncmp(), strchr(), etc.  (string is nothing but any English statement enclosed in double quotes like I am learning, My mobile is not working, I have a laptop etc)

    In the C language, there are two types of functions, one is predefined in header files, and the other is user-defined functions. Let’s understand the predefined functions here.

    Predefined functions make programmer's lives easy. Every predefined function takes zero or more inputs with brackets enclosed,  just like the printf function took Hello World! as input in our program. Here, printf works by taking the statement enclosed in the brackets as input and printing that statement onto the output screen. Below are a few examples of predefined functions:

    ➢  printf(Any English statement)  –  printf() function takes an English statement as input and prints that English statement as output.

    Examples:

    printf(C language) prints C language as output,

    printf(welcome to programming) prints welcome to programming as output.

    ➢  sqrt(x)  –  sqrt() function takes a number as input and gives the square root of that number as output.

    Examples:

    sqrt(25)=5.0

    sqrt(4)=2.0

    ➢  pow(a,b)  –  pow() function takes two numbers (like a and b) as input and gives a to the power of b as the output.

    Examples:

    pow(4,2)=16.0

    pow(10,2)=100.0

    main is the most important function in the C language. Above is one of the simplest ways of writing the main function and In the coming chapters, we will learn about other variations of the main function.The execution of any C program starts with main function only, and any number of statements or lines can be included or written in between opening and closing flower brackets or curly braces. (Every line in the C program can be called a statement.) At the end of every statement, a semicolon (;) is used to indicate the end of the line or statement.

    In our above program, only 1 statement (i.e. printf(Hello World!!!); ) is included or written in between the opening and closing flower brackets or braces of the main function, so execution starts and ends with the first line. If a main function has 5 lines, then execution starts with the 1st line and continues sequentially with the 2nd, 3rd, 4th, and finally the 5th line. Execution stops after the 5th line is executed. The main function gives the direction for execution of a C program, that's why it is called the most important function in C.

    Output:

    Compiler:

    It is system software that executes the C program. User-written programs or codes are translated by the compiler into a different kind of program or code that the operating system can understand. The compiler first checks for the main function in the program and starts execution from the first line of the main function.

    2. Tokens :

    Tokens in the C language are the minor elements or the building blocks used to construct, write, or develop a C program. It is the basic component of a C program. These tokens in C are meaningful to the compiler. The compiler breaks a program into tokens and proceeds with further execution.

    There are 5 tokens in the C language which are keywords, special symbols, constants, identifiers, and operators.

    Keywords:

    In our first C program, there are some words like include, main, printf, and void. These words have specific functionality associated with them, and based on that functionality, the compiler is programmed to execute the C program. By convention, all keywords must be written in lowercase. Below is the functionality of the keywords used in our first program.

    ➢  main  –  compiler understands that execution of the program should start from here.

    ➢  include  –  when any header file is included in a program by using the include word (like #include , #include ) then the compiler understands to include the functionality (like functions present) of the files into the program.

    ➢  printf  –  it is a predefined function whose functionality is to print strings to the output screen or console.

    Following is a list of all 32 keywords in the C language:

    Special symbols:

    Special symbols define the syntax and organization of code, and their proper use is crucial for writing syntactically correct programs. Following are the special symbols used in the C language.

    ➢  Semicolon (;)

    ➢  Parentheses (())

    ➢  Braces ({ })

    ➢  Brackets ([ ])

    ➢  Comma (,)

    ➢  Ampersand (&)

    ➢  Asterisk Symbol(*)

    Variables and Data Types:

    The aim of any program is to convert given inputs (or input data) into required or desired outputs( or output data). In this process, the program might use some temporary data or values as well.

    To use data or value in a program, two things must be mentioned: first, the type of data (referred to as DataType) and, second, the variable. Data is typically stored in memory locations, to which a variable is attached. A variable is simply a name for a memory location in which data is stored.

    Let’s store temperature data in a variable,

    Here, int is the data type, which indicates that the data that is stored is an integer value; temperature is the variable; and 35 is the value or data.

    Let’s store height,

    Here, float is the data type, which indicates that the data that is stored is a decimal value, h is the variable, and 5.9 is the value or data.

    Any datatype in C has three important properties:

    ➢  Range: It indicates the interval of values a data type can hold.

    ➢  Size: It depends on the compiler, which

    Enjoying the preview?
    Page 1 of 1