Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Silver Oak Group of Institutes

Silver Oak College of Computer Application

Created by : Kinjal Mehta


Silver Oak Group of Institutes
Silver Oak College of Computer Application

Chapter 2 : Getting Started With ‘C’ Language


● Basic Structure of C
The basic structure of a C program is divided into 6 parts which makes it
easy to read, modify, document, and understand in a particular format. There are
6 basic sections responsible for the proper execution of a program. Sections are
mentioned below:
○ Documentation
○ Preprocessor Section
○ Definition
○ Global Declaration
○ Main() Function
○ Sub Programs

1. Documentation
This section consists of the description of the program, the name of the
program, and the creation date and time of the program. It is specified at the start
of the program in the form of comments. Documentation can be represented as:
// description, name of the program, programmer name,
date, time etc.
/*
description, name of the program, programmer name,
date, time etc.
*/

Anything written as comments will be treated as documentation of the


program and this will not interfere with the given code. Basically, it gives an
overview to the reader of the program.

2. Preprocessor Section

All the header files of the program will be declared in the Processor
section of the program. Header files help us to access other’s improved code into
our code. A copy of these multiple files is inserted into our program before the
process of compilation.

Created by : Kinjal Mehta


Silver Oak Group of Institutes
Silver Oak College of Computer Application
Example:

#include<stdio.h>
#include<math.h>

3. Definition

Preprocessors are the programs that process our source code before the
process of compilation. There are multiple steps which are involved in the writing
and execution of the program. Preprocessor directives start with the ‘#’ symbol.
The #define preprocessor is used to create a constant throughout the program.
Whenever this name is encountered by the compiler, it is replaced by the actual
piece of defined code.
Example:
#define long long ll

4. Global Declaration

The global declaration section contains global variables, function


declaration, and static variables. Variables and functions which are declared in
this scope can be used anywhere in the program.
Example:
int num = 18;

5. Main() Function

Every C program must have a main function. The main() function of the
program is written in this section. Operations like declaration and execution are
performed inside the curly braces of the main program. The return type of the
main() function can be int as well as void too. void() main tells the compiler that
the program will not return any value. The int main() tells the compiler that the
program will return an integer value.
Example:
void main()
Created by : Kinjal Mehta
Silver Oak Group of Institutes
Silver Oak College of Computer Application
or
int main()

6. Sub Programs

User-defined functions are called in this section of the program. The


control of the program is shifted to the called function whenever they are called
from the main or outside the main() function. These are specified as per the
requirements of the programmer.
Example:
int sum(int x, int y)
{
return x+y;
}

● Executing C program
C program file is compiled and executed, the compiler generates some files with
the same name as that of the C program file but with different extensions.Below image
shows the compilation process with the files created at each step of the compilation
process:

The file extension ".c" must be used when saving any file that includes a C
programme. This is required in order for the compiler to understand that the file is a C
programme.
Suppose a program file is named, first.c. The file first.c is called the source file
which keeps the code of the program. Now, when we compile the file, the C compiler

Created by : Kinjal Mehta


Silver Oak Group of Institutes
Silver Oak College of Computer Application
looks for errors. If the C compiler reports no error, then it stores the file as a .obj file of
the same name, called the object file. So, here it will create the first.obj. This .obj file is
not executable.
The process is continued by the Linker which finally gives a .exe file which is
executable.Whenever we give the command to execute a particular program, the loader
comes into work. The loader will load the .exe file in RAM and inform the CPU with the
starting point of the address where this program is loaded.

● Character set & C Tokens


Character set
Like every other language, ‘C’ also has its own character set. A program is
a set of instructions that, when executed, generate an output. The data that is
processed by a program consists of various characters and symbols. The output
generated is also a combination of characters and symbols.
A character set in ‘C’ is divided into,
■ Letters
■ Numbers
■ Special characters
■ White spaces (blank spaces)
A compiler always ignores the use of characters, but it is widely used for
formatting the data. Following is the character set in ‘C’ programming:
1)Letters
● Uppercase characters (A-Z)
● Lowercase characters (a-z)
2) Numbers
● All the digits from 0 to 9
3) White spaces
● Blank space
● New line
● Carriage return
● Horizontal tab

Token in C
TOKEN is the smallest unit in a ‘C’ program. It is each and every word and
punctuation that you come across in your C program. The compiler breaks a
program into the smallest possible units (Tokens) and proceeds to the various
Created by : Kinjal Mehta
Silver Oak Group of Institutes
Silver Oak College of Computer Application
stages of the compilation. C Token is divided into six different types, viz,
Keywords, Operators, Strings, Constants, Special Characters, and Identifiers.

● Identifiers & Keywords


○ Keywords are specific reserved words in C each of which has a
specific feature associated with it. Almost all of the words which help
us use the functionality of the C language are included in the list of
keywords. So you can imagine that the list of keywords is not going
to be a small one! There are a total of 32 keywords in C:
auto break case char const
continue default do double else enum
extern float for goto if int
long register return short signed sizeof
static struct switch typedef union unsigned
void volatile while

○ Identifiers are used as the general terminology for naming of


variables, functions and arrays. These are user-defined names
consisting of arbitrarily long sequences of letters and digits with
either a letter or the underscore(_) as a first character. Identifier
names must differ in spelling and case from any keywords. You
cannot use keywords as identifiers; they are reserved for special
use. Once declared, you can use the identifier in later program
statements to refer to the associated value. A special kind of
identifier, called a statement label, can be used in goto statements.

Created by : Kinjal Mehta


Silver Oak Group of Institutes
Silver Oak College of Computer Application

● Data Types
Each variable in C has an associated data type. Each data type
requires different amounts of memory and has some specific operations
which can be performed over it. It specifies the type of data that the
variable can store like integer, character, floating, double, etc. The data
type is a collection of data with values having fixed values, meaning as
well as its characteristics.

Integer Types

The integer data type in C is used to store the whole numbers


without decimal values. Octal values, hexadecimal values, and decimal
values can be stored in int data type in C. We can determine the size of
the int data type by using the sizeof Operator in C. Unsigned int data type
in C is used to store the data values from zero to positive numbers but it
can’t store negative values like signed int. Unsigned int is larger in size
than signed int and it uses “%u” as a format specifier in C programming
language.
● Range: -2,147,483,648 to 2,147,483,647
● Size: 2 bytes or 4 bytes
● Format Specifier: %d
Created by : Kinjal Mehta
Silver Oak Group of Institutes
Silver Oak College of Computer Application
// C program to print Integer data types.
#include <stdio.h>
int main()
{
// Integer value with positive data.
int a = 9;

// integer value with negative data.


int b = -9;
// U or u is Used for Unsigned int in C.
int c = 89U;
// L or l is used for long int in C.
long int d = 99998L;
printf("Integer value with positive data: %d\n", a);
printf("Integer value with negative data: %d\n", b);
printf("Integer value with an unsigned int data: %u\n", c);
printf("Integer value with an long int data: %ld", d);
return 0;
}

Character Types

Character data type allows its variable to store only a single


character. The storage size of the character is 1. It is the most basic data
type in C. It stores a single character and requires a single byte of memory
in almost all compilers.
● Range: (-128 to 127) or (0 to 255)
● Size: 1 byte
● Format Specifier: %c

// C program to print Integer data types.

#include <stdio.h>
Created by : Kinjal Mehta
Silver Oak Group of Institutes
Silver Oak College of Computer Application
int main()
{
char a = 'a';
char c;
printf("Value of a: %c\n", a);
a++;
printf("Value of a after increment is: %c\n", a);
// c is assigned ASCII values
// which corresponds to the
// character 'c'
// a-->97 b-->98 c-->99
// here c will be printed
c = 99;
printf("Value of c: %c", c);
return 0;
}

Floating-Point Types

In C programming float data type is used to store floating-point


values. Float in C is used to store decimal and exponential values. It is
used to store decimal numbers (numbers with floating point values) with
single precision.

● Range: 1.2E-38 to 3.4E+38


● Size: 4 bytes

Created by : Kinjal Mehta


Silver Oak Group of Institutes
Silver Oak College of Computer Application
● Format Specifier: %f

// C Program to demonstrate use


// of Floating types
#include <stdio.h>
int main()
{
float a = 9.0f;
float b = 2.5f;
// 2x10^-4
float c = 2E-4f;
printf("%f\n",a);
printf("%f\n"b);
printf("%f",c);
return 0;
}

● Constants and Variables


Variable

A variable is like a container containing the values or data of a


particular data type that helps to execute the program. The value of a
variable can change depending on the conditions or information given at
the program's run time. A variable can be defined in both ways, like the
uppercase and lowercase letters, digits and the underscores. A variable
can store the value of the different data types like integer, float, character,
enum.

Rules for defining the variable name in C programming language:


Created by : Kinjal Mehta
Silver Oak Group of Institutes
Silver Oak College of Computer Application

○ The variables are case sensitive.

○ The variable name must start with an alphabet letter or underscore.

○ It includes the letter, digits and underscore.

○ There should not be a white space in a variable name.

○ The name of a variable should not be any reserved keywords like int,
float, str, char, etc.

// declare the variable in C.

Datatype variable_name;

Datatype variable_name1, variable_name2, vriable_name3;

int a, b, d;

int _c;

char letter;

float z ;

har str[] = "welcome";

Constants

A constant is a fixed value whose value cannot be changed during


program's execution or once the value is defined. It is also known as literal.
For example, 5, 20, 'a', 'Radius', 5.2, "Welcome back", etc. A constant can
be defined in two ways, like #define pre-processor and by const keyword.
The constants can be different data types, such as integer constants,

Created by : Kinjal Mehta


Silver Oak Group of Institutes
Silver Oak College of Computer Application
floating constants, character constants, string constants and enumeration
constants. Let's understand briefly about them:

1. Integer constant
An integer constant is a whole number and can be large without including
any decimal points. For example, 0, 1, 2, 123, 5767, 05, 0X23, 0xFFF, etc.

2. Float constant
The float constants are the part of an integer constant that containing a
decimal point, fractional form and exponential form.
Here are some example of floating point constants:
0.5, 35.05, 2.3e6, 3.52f or 3.52F, PI = 3.14, etc.

3. Character Constants
It is a single character constant enclosed within a single quotation mark
(like 'a', 'A'), called a character constants. There are some valid constants
as: 'g', 'D', ' ', '#'.

4. String Constant
It is the character set of string constants that are enclosed in a double
quote. The character may be letters, numbers, special symbols and some
blank space. Furthermore, a string constant contains zero, one or more
continuous character in double quotation marks. For example, "Hello
Friends", "Computer", "5987", " " , "A".

#include<stdio.h>

#include<conio.h>

#define PI 3.14

Created by : Kinjal Mehta


Silver Oak Group of Institutes
Silver Oak College of Computer Application
void main()

int radius, area;

printf(" Please enter the radius of circle\n");

scanf("%d", &radius);

area = PI * radius * radius;

printf(" Area of circle is : %d", area);

getch();

● Type Casting
Typecasting in C is the process of converting one data type to
another data type by the programmer using the casting operator during
program design.
In typecasting, the destination data type may be smaller than the
source data type when converting the data type to another data type, that’s
why it is also called narrowing conversion.
int x;

float y;

y = (float) x;
Types of Type Casting in C
C there are two major types to perform type casting

○ Implicit type casting


■ Implicit type casting in C is used to convert the data type of
any variable without using the actual value that the variable

Created by : Kinjal Mehta


Silver Oak Group of Institutes
Silver Oak College of Computer Application
holds. It performs the conversions without altering any of the
values which are stored in the data variable. Conversion of
lower data type to higher data type will occur automatically.

// C program to illustrate the use of


// typecasting
#include <stdio.h>
// Driver Code
int main()
{
// Given a & b
int a = 15, b = 2;
float div;
// Division of a and b
div = a / b;
printf("The result is %f\n", div);
return 0;
}
○ Explicit type casting
■ There are some cases where if the datatype remains
unchanged, it can give incorrect output. In such cases,
typecasting can help to get the correct output and reduce the
time of compilation. In explicit type casting, we have to force
the conversion between data types. This type of casting is
explicitly defined within the program.

Created by : Kinjal Mehta


Silver Oak Group of Institutes
Silver Oak College of Computer Application
// C program to showcase the use of
// typecasting
#include <stdio.h>
// Driver Code
int main()
{
// Given a & b
int a = 15, b = 2;
char x = 'a';
double div;
// Explicit Typecasting in double
div = (double)a / b;
// converting x implicitly to a+3 i.e, a+3 = d
x = x + 3;
printf("The result of Implicit typecasting is %c\n", x);
printf("The result of Explicit typecasting is %f", div);
return 0;
}

Advantages of Type Casting


■ Type casting in C programming makes the program very
lightweight.
■ Type representation and hierarchies are some features we
can take advantage of with the help of typecasting.
■ Type casting helps programmers to convert one data type to
another data type.
Created by : Kinjal Mehta
Silver Oak Group of Institutes
Silver Oak College of Computer Application

● Comments

The comments in C are human-readable explanations or notes in


the source code of a C program. A comment makes the program easier to
read and understand. These are the statements that are not executed by
the compiler or an interpreter.
Types of comments in C
In C there are two types of comments in C language:
● Single-line comment : A single-line comment in C starts with ( // )
double forward slash. It extends till the end of the line and we
don’t need to specify its end.
// C program to illustrate
// use of single-line comment
#include <stdio.h>

int main(void)
{
// This is a single-line comment
printf("Welcome to GeeksforGeeks");
return 0;
}

● Multi-line comment : The Multi-line comment in C starts with a


forward slash and asterisk ( /* ) and ends with an asterisk and
forward slash ( */ ). Any text between /* and */ is treated as a
comment and is ignored by the compiler.
/* C program to illustrate
use of
multi-line comment */
#include <stdio.h>
int main(void)
{
/*
This is a
multi-line comment
*/

/*
Created by : Kinjal Mehta
Silver Oak Group of Institutes
Silver Oak College of Computer Application
This comment contains some code which
will not be executed.
printf("Code enclosed in Comment");
*/
printf("Welcome to GeeksforGeeks");
return 0;
}

Created by : Kinjal Mehta

You might also like