Beginners Guide to C by Soni - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Introduction to C Language

C is a general purpose, high level programming language and is a widely used language. This language has facilities for structured programming and allows to efficiently provide machine instructions. Hence, the language is used for coding assembly language too. The system software like Unix operating system has been coded using C. C is a procedural language. It was designed to be compiled using a relatively straightforward compiler, to provide low-level access to memory, to provide language constructs that map efficiently to machine instructions, and to have a have need of minimal run-time support. C is useful for many applications that had formerly been coded in assembly language, such as in system programming.

Software programs required for a C program

As no computer can understand C, you need several programs that help the computer understand the instructions. The programs are:

  • Editor
  • Compiler
  • Linker
  • Loader

Here Turbo C editor is used for C programming.

The table below explains the purpose of the various programs.

img4.png

The basics of a C programming language needs an understanding of the basic

img5.png

building blocks of the programming language, the bare minimum C program contains the following parts:

  • Preprocessor Commands
  • Functions
  • Variables
  • Statements & Expressions
  • Comments

Every C program consists of one or more functions, one of which must be main().

Let us look at a simple code that would print the words "My First Program in C" followed by an explanation of the various parts of the C program:

img6.png

Structure of a C program

The various parts of the program given above are explained below:

Preprocessor Commands

1. #include<stdio.h>

Start writing a C program with a preprocessor command #include<stdio.h>. 

This informs a C compiler to include a header file stdio.h  before compiling the program.

  • # is a preprocessor command.
  • Include instructs the C compiler to include the header file library function. The header file contains the instruction for standard input and output. 
  • <stdio.h> - All the header file library functions are written within open and close angular brackets, <>. 
  • The expansion of stdio.h is “standard input and output header file.
  • h is the extension of the library function file identified by the compiler with .h extension.
  • All the library functions included in a C program are written as #include<header file name.h>

Refer Library Functions to know about the list of other library functions that can be included in a C program.   

2. main() function

The next line int main() is the main function where program execution begins.

img7.png

All programs in C should have a main() function. There are two variations of main(). They are void main() and int main(). void main() denotes a function does not return a value, int main() denotes a function that is of a numeric return type.

3. Starting Brace

All C program start with a left brace {.and the program code always ends with a closing brace - }.

4. Statements

Comment Statement

The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the program. So such lines are called comments in the program. You can give a brief description about the program within the /*…..*/ comment line statements.

Input and Output statements

Output statement

printf(...)

The next line printf(...) is another function available in C which causes the message "My First Program in C” to be displayed on the screen. All the statements that you want to display on the screen should start with the printf( ) function.

Input Statement

scanf(“%d”, &a);

The data type of the input is specified within double quotes, if it is of integer type then it is % d and &a represents a variable data item.

scanf(…..) function can be used to enter any combination of numerical values, single characters and strings.

Compile & Execute C Program

Let’s look at how to save the source code in a file, and how to compile and run it. Following are the simple steps:

1. Open a text editor and add the above-mentioned code.

2. Save the file as First.c in the TC\Bin directory. The directory where you have the C compiler.

3. Open a command prompt and go to the directory where you saved the file.

4. Type TC First.c and press enter.

5. This opens up the First.c program in the Turbo C window.

6. To compile your code press Alt + F9 key simultaneously.

7. If there are no errors there will be no error message.

8. You have to execute the file to view the output. To execute the file press Ctrl + F9 keys simultaneously.

9. You will be able to see "My First Program in C" printed on the screen.

Output screen of First.c

img8.png

C uses structured programming with variables and recursive statements. In C, all executable code is contained within "functions" All C program use the semicolon as a statement terminator and curly braces({ }) for grouping blocks of statements.

The following are the key points of C language:

  • Uses preset keywords
  • Includes flow of control structures for, if/else, while, switch, and do/while.
  • A large number of arithmetical and logical operators are used.
  • Statements are used to specify actions. Common statements are input, output and an expression statement. The expression statement consists of an expression to be evaluated, followed by a semicolon;.
  • Functions and procedures may be called and
  • Variables may be assigned new values.

The basic characters that are included in a program are:

  • Lowercase and uppercase letters: a–z A–Z
  • Decimal digits: 0–9
  • Graphic characters: ! " # % & ' ( ) * + , - . / : ; < = > ? [ \ ] ^ _ { | } ~
  • Newline indicates the end of a text line; it need not correspond to an actual single character, although for convenience C treats it as one.

Data types

C supports different types of data, the basic C data types are as listed below:

img9.png

The array types and structure types are referred to collectively as the aggregate The type of a function specifies the type of the function's return value. We will see basic types in the following section.

The void Type

 The void type specifies that no value is available. It is used in the following kinds of situations:

  1. When a function does not return a value.
  2. For empty set of values.

Variables

Variable store values and they are used to perform calculations and evaluate A variable refers to values that are not fixed, they keep changing. C programming language also allows defining various other types of variables, other those defined in the following type:

img10.png

Variable Definition in C

A variable definition means to tell the compiler where and how much to create the storage for the variable. A variable definition specifies a data type and contains a list of one or more variables of a specific data type.

Variable Initialization

Assigning value to the variable is called Variable Initialization. A variable can be initialized at the place where the variable is declared.

Syntax

data_type

You may also like...