0% found this document useful (0 votes)
9 views20 pages

C First

The document explains how to define constants in C using the 'const' keyword and the '#define' directive, emphasizing the need to initialize constants at declaration. It also covers various data types, including integer, float, and char, as well as derived data types and memory allocation techniques. Additionally, it outlines the structure of a C program, detailing sections such as documentation, global declarations, and the main function, along with the compilation and execution process.

Uploaded by

donmathew666666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views20 pages

C First

The document explains how to define constants in C using the 'const' keyword and the '#define' directive, emphasizing the need to initialize constants at declaration. It also covers various data types, including integer, float, and char, as well as derived data types and memory allocation techniques. Additionally, it outlines the structure of a C program, detailing sections such as documentation, global declarations, and the main function, along with the compilation and execution process.

Uploaded by

donmathew666666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 20

Constants

How to define a constant in C?


We define a constant in C language using the const keyword. Also known
as a const type qualifier, the const keyword is placed at the start of the
variable declaration to declare that variable as a constant.

Syntax to Define Constant

const data_type var_name = value;

Eg: const char char_const = 'A';


One thing to note here is that we have to initialize the constant
variables at declaration. Otherwise, the variable will store some garbage
value and we won’t be able to change it.
Syntax of Constant in C using #define

#define const_name value


Literals
Literals are the constant values assigned to the constant variables. We can say that the
literals represent the fixed values that cannot be modified. It also contains memory but
does not have references as variables. For example, const int =10; is a constant integer
expression in which 10 is an integer literal.

Types of literals
There are four types of literals that exist in C programming:

o Integer literal
o Float literal
o Character literal
o String literal
Integer literal

It is a numeric literal that represents only integer type values. It represents the
value neither in fractional nor exponential part.

1. #include <stdio.h>
2. int main()
3. {
4. const int a=23; // constant integer literal
5. printf("Integer literal : %d", a);
6. return 0;
7. }

Integer Data Type

An integer type variable can store zero, positive, and negative values without any
decimal. In C language, the integer data type is represented by the ‘int’ keyword,
and it can be both signed or unsigned. By default, the value assigned to an integer
variable is considered positive if it is unsigned.

The integer data type is further divided into short, int, and long data types. The
short data type takes 2 bytes of storage space; int takes 2 or 4 bytes, and long
takes 8 bytes in 64-bit and 4 bytes in the 32-bit operating system.

If you try to assign a decimal value to the integer variable, the value after the
decimal will be truncated, and only the whole number gets assigned to the
variable.
#include
int main() {
short int a=10000;
int b=11252486;
long num1=499962313469;
long long num2=51454456154585454;
printf("a is %hd \n b is %d \n num1 is %ld \n num2 is %lld \
n",a,b,num1,num2);
return 0;
}

TWhile doing an arithmetic operation, if the result comes out to be a decimal


value, the variable will only accept the whole number and discard the numbers
after the decimal point. For short int, an incorrect value will be displayed if the
number is bigger than 10000.

2. Float-pointing Data Types

The floating-point data type allows a user to store decimal values in a variable. It is
of two types:
 Float
 Double

Float
Float variables store decimal values with up to 6 digits after the decimal place. The
storage size of the float variable is 4 bytes, but the size may vary for different
processors, the same as the ‘int’ data type.

In C language, the float values are represented by the ‘%f’ format specifier. A
variable containing integer value will also be printed in the floating type with
redundant zeroes.

If you assign an integer value to a float variable, the result will always be a float
value with zeroes after the decimal place. As mentioned, float values are
represented by the ‘%f’ format specifier. However, if you try to print float values
with ‘%d’, then the output will not be 67 in the above case. Instead, you will see a
garbage value on the output screen.

Double
The double data type is similar to float, but here, you can have up to 10 digits after
the decimal place. It is represented by the ‘double’ keyword and is mainly used in
scientific programs where extreme precision is required.

3. Char

Char is used to store single-character values, including numerical ones. By creating


an array of this character data type, it becomes a string that can store
name/subject values among

Derived Data Types in C


Derived data types are defined by the user itself, which means that you can
aggregate as many elements of similar data types as required. There are four
types of derived data types:
 Array
 Pointer
 Structure
 Union

Memory Allocation

Static memory allocation is an allocation technique which allocates a


fixed amount of memory during compile time .

Memory is central to any computing system and its architecture


determines the performance of any process. While building system, one
of the fundamental task is to allocate memory.

There are two types of memory allocated to a program:

 Stack memory
 Heap memory
Stack memory is allocated during compilation time execution. This is
known as static memory allocation.
Whereas, heap memory is allocated at run-time compilation. This is
know as dynamic memory allocation.
structure of a C program
A C program is divided into different sections. There are six main sections to a basic c
program.
The six sections are,

 Documentation
 Link
 Definition
 Global Declarations
 Main functions
 Subprograms Consists of the description of the program, programmer's
name, and creation date. These are generally written in the
form of comments.
All header files are included in this section which contains different
functions from the libraries. A copy of these header files is
inserted into your code before compilation.
includes preprocessor directive, which contains symbolic constants .
E.g.: #define allows us to use constants in our code. It replaces all
the constants with its value in the code
Includes declaration of global variables, function
declarations, static global variables, and functions

For every C program, the execution starts from the main() function.
It is mandatory to include a main() function in every C program

Includes all user-defined functions (functions the user


provides). They can contain the inbuilt functions and the
function definitions declared in the Global Declaration

Documentation
It includes the statement specified at the beginning of a program, such as a
program's name, date, description, and title. It is represented as:

Example

/**

File Name: Helloworld.c

Author: Manthan Naik

date: 09/08/2019

description: a program to display hello world

no input needed

*/

It provides an overview of the program. Anything written inside will be considered a part of
the documentation section and will not interfere with the specified code.
Preprocessor section/Link Section
The preprocessor section contains all the header files used in a program. It informs
the system to link the header files to the system libraries. It is given by:

The #include statement includes the specific file as a part of a function at the
time of the compilation. Thus, the contents of the included file are compiled along
with the function being compiled. The #include<stdio.h> consists of the
contents of the standard input output files, which contains the definition of stdin,
stdout, and stderr. Whenever the definitions stdin, stdout, and stderr are used in a
function, the statement #include<stdio.h> need to be used.

There are various header files available for different purposes. For example, #
include <math.h>. It is used for mathematic functions in a program.

Definition Section
In this section, we define different constants. The keyword define is used in this part.

#define PI=3.14

The define section comprises of different constants declared using the define
keyword. It is given by:

#define a = 2

Global Declaration Section


This part of the code is the part where the global variables are
declared. All the global variable used are declared in this part. The user-
defined functions are also declared in this part of the code.

float num = 2.54;


int a = 5;
char ch ='z';

The size of the above global variables


char = 1 byte

float = 4 bytes

int = 4 bytes

We can also declare user defined functions in the global variable section.

Main Function Section


main() is the first function to be executed by the computer. It is necessary for a
code to include the main(). It is like any other function available in the C library.
Parenthesis () are used for passing parameters (if any) to a function.

The main function is declared as:


Local declarations
The variable that is declared inside a given function or block refers to as local
declarations.

1. main()
2. {
3. int i = 2;
4. i++;
5. }

Statements

The statements refers to if, else, while, do, for, etc. used in a program within
the main function.

Expressions

An expression is a type of formula where operands are linked with each other by
the use of operators. It is given by:

1. a - b;
2. a +b;

User defined functions


The user defined functions specified the functions specified as per the
requirements of the user. For example, color(), sum(), division(), etc.

The program (basic or advance) follows the same sections as listed above.

Return function is generally the last section of a code. But, it is not necessary to
include. It is used when we want to return a value. The return function returns a
value when the return type other than the void is specified with the function.

Return type ends the execution of the function. It further returns control to the
specified calling function. It is given by:

1. return;

Or
1. return expression ;

For example,

return 0;

Output

The detailed explanation of each part of a code is as follows:

/* Sum of the two numbers */ It is the comment section.


Any statement described in
it is not considered as a
code. It is a part of the
description section in a
code.
The comment line is
optional. It can be in a
separate line or part of an
executable line.

#include<stdio.h> It is the standard input-


output header file. It is a
command of the
preprocessor section.

int main() main() is the first function


to be executed in every
program. We have used int
with the main() in order to
return an integer value.

{… The curly braces mark the


} beginning and end of a
function. It is mandatory in
all the functions.

printf() The printf() prints text on


the screen. It is a function
for displaying constant or
variables data. Here, 'Enter
two numbers to be added'
is the parameter passed to
it.

scanf() It reads data from the


standard input stream and
writes the result into the
specified arguments.

sum = a + b The addition of the


specified two numbers will
be passed to the sum
parameter in the output.

return 0 A program can also run


without a return 0 function.
It simply states that a
program is free from error
and can be successfully
exited.

Subprograms

This includes the user-defined functions


called in the main() function. User-defined
functions are generally written after the
main() function irrespective of their order.

When the user-defined function is called


from the main() function, the control of the
program shifts to the called function, and
when it encounters a return statement, it
returns to the main() function. In this case,
we've defined the age() function, which
takes one parameter, i.e., the current year.

int age(int current) {


return current - BORN;
}

Compile and execution of a C program


Here, we will discuss the method to compile and run the C program with the help
of the command prompt.

The steps involved in a complete program execution are as follows:

1. Create a program
2. Compile a program
3. Run or execute a program
4. Output of the program

Create a program
It refers to the code created in any text editor.

Compile a program
If refers to the process of checking the errors in the code. The computer displays
all the errors (if any) found in our specified C code. We can make further changes
to correct those errors.
Run or execute a program
The next step is the run or execution part. A program is assembled and linked
without any error. The computer performs different operations, such as decoding,
ALU operations to run a program.

Output of the program


It is the last part of the program where the output of the specified code is produced
as the result.

But, where to write a program and how to open a command prompt to run that
program.

#include is also known as a file inclusion directive. #include directive is used to


add the content/piece of code from a reserved header file into our code file before
the compilation of our C program. These header files include definitions of many
pre-defined functions like printf(), scanf(), getch(), etc.

A source file with the ‘.h’ extension is a header file. While constants, macros, and
system-wide global variables are found in source code, function prototypes, and
declarations are found in header files. We just include the header file where the
function is declared whenever we need to define a function.

There are 2 types available for header files. They are given below:
 System-defined header file: System-defined file is a predetermined header file.
 User-defined header file: User-defined header file is one that has been defined
by the user.
Basic Format Specifiers
There are different format specifiers for each data type.
Comments
It is useful to place a comment in place to indicate what you are doing. It is added
into the program for making the program easier to understand and these are not
compiled by the compiler or interpreter. It is useful if you want someone else to be
able to ever read your code.
There are two ways to use comment in C which are as follows:
 Single Line comment
 Multi Line comment

Single Line comment

It is used to apply the comment on a single line. To apply single line comment // is
used.
e.g.
printf(“hello Intellipaat"); // It will print the string hello Intellipaat

Multi Line comment

It is used to apply the comment on multiple lines. To apply multi line use
/*comment */.
e.g.
printf("hello Intellipaat"); /*It will print the
string hello Intellipaat*/

Constants in C
Its value is fixed throughout the program that means constants are those variables
which value is not changed throughout the program.
C constants can be divided into two major categories:
 Primary Constants
 Secondary Constants

There are two simple ways in C to define constants:

1. Using#define

e.g.
#include <stdio.h>
#define value 10
void main() {
int data;
data = value*value;
printf("value of data : %d",data);
}

Output
value of data : 100

2. Using const keyword

e.g.
#include <stdio.h>
void main() {
const int value = 10;
int data;
data =value*value;
printf("value of data : %d",value);
}

Output
value of data : 100

You might also like