0% found this document useful (0 votes)
38 views

C Fundamentals

The document provides an overview of the C programming language, including its history, features, program structure, data types, variables, constants, and keywords. It was developed in the 1970s and is a procedural language well-suited for systems programming. A C program structure typically includes comments, preprocessor directives, global declarations, and the main function with declarations and executable code. The document outlines various data types in C like integers, characters, and floats, as well as modifiers. It also discusses identifiers, keywords, variables, and constants.

Uploaded by

Claidie Fatallo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

C Fundamentals

The document provides an overview of the C programming language, including its history, features, program structure, data types, variables, constants, and keywords. It was developed in the 1970s and is a procedural language well-suited for systems programming. A C program structure typically includes comments, preprocessor directives, global declarations, and the main function with declarations and executable code. The document outlines various data types in C like integers, characters, and floats, as well as modifiers. It also discusses identifiers, keywords, variables, and constants.

Uploaded by

Claidie Fatallo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

C Fundamentals

At the end of the session, students should be able to:


1. Identify the program structure of C programming language
2. Enumerate keywords in C
3. Enumerate data types in C

Lecture:

What is C ?

C is a programming language developed at AT & T laboratories of USA in 1972 by


Dennis Ritchie in 1970. It was initially implemented on the system that used UNIX
operating system. C is a midlevel computer language. C is a nice blend of high level
languages and low level assembly language. C code is portable. Portability means that it
is easy to adapt software written for one type of OS to run on another type.

Important Features of C Language


1. C is a system programming language which provides flexibility for writing
compilers, operating systems, etc.
2. It can also be used for writing the application programs for scientific, engineering
and business applications.
3. C is famous for its portability, meaning that program written in C for one computer
can be easily loaded to another computer with little or no changes.
4. C supports variety of data types like integers, float point numbers, characters, etc.
5. C is a procedure oriented language which is most suited for structured
programming practice. vi. It provides a rich set of built in functions

Program Structure in C
To accomplish the given task programs are written and it is executed in Turbo C/C++
compiler. The structure of the program is given below.
Comments / /* Sample C Program */

Preprocessor Directive #include <stdio.h>

Global Declaration Section main() {

main()

{ int a,b;

clrscr();

Executable Part printf(“Enter two numbers”);

scanf(“%d%d”, &a,&b);

} sum = a + b;

printf(“Sum = %d”, sum);

i. Comments : It refers to the added explanation located anywhere in the


program which serves only as the remainder to the programmer or to make the
code readable to the user
// single line comment
- It terminates at the end of the current line
/*…*/ multiple line comment
- containing many lines
ii. Preprocessor directive ; It provides instruction to the compiler to link some
functions or do some processing prior to the execution of the program. It is also
used to define symbolic constants of the program.
 Header file used by the example is a standard input/output file (stdio.h),
programs must contain an #include line for each header file
 There are two punctuation forms for head file.
1. #include <stdio.h>
2. #include “stdio.h”
iii. Global Declaration Section : There are some variables that are used in more
than one function. Such variables are called global variables and are declared
in this section that is outside of all other functions.
iv. The main() function section : Every C program must have one main() function.
This section contains two parts, declaration part and executable part. The
declaration part declares all the variables used in the executable part. There is
at least one statement in the executable part. These two parts must appear
between the opening and closing braces. The program execution begins at the
opening brace and ends at the closing brace. The closing brace of the main
function is the logical end of the program. All statements in the declaration part
and executable parts must end with a semicolon.

Executing a C program
Executing a C program involves a series of following steps:
1. Creating a program
2. Compiling the program
3. Linking the program with functions that are needed from the C library.
4. Executing the program

Important points to remember:


1. Every C program requires a main() function. Use of more than one main() is illegal. The
place of main() is where the program execution begins.
2. The Execution of the function begins at the opening brace and ends at the closing
brace.
3. C programs are written in lowercase letters. However uppercase letters may be used
for symbolic names and constants.
4. All the words in a program line must be separated from each other by at least one
space or a tab, or a punctuation mark.
5. Every statement must end with a semicolon.
6. All variables must be declared for their type before they are used in the program.
7. Compiler directives such as define and include are special instructions to the compiler,
so they do not end with a semicolon.
8. When braces are used in the program make sure that the opening brace has
corresponding ending brace.
9. C is a free form language and therefore proper form of indentation of various sections
would improve the legibility of the program.

Identifiers and Keywords


Identifiers are the names given by user to various program elements such as variables,
functions and arrays. The rules for identifiers are given below.
1. Letters, digits and underscore can be used.
2. Must not start with a digit.
3. Avoid using underscore at the start of identifier
4. Identifier cannot be same as reserved word (Key Word) or C library function names
5. Identifiers are Case sensitive. For example india is different from India.
Examples of Valid and Invalid Identifier
Valid Identifiers:
1) X1x
2) Count_2
3) Num1
Invalid Identifiers:
1) 5thstandard - first character must be a letter
2) “Sam” - illegal character (“ ”)
3) Emp-no - illegal character( - )
4) Reg no - illegal character (blank space)

Keywords
Key words or Reserve words of the C language are the words whose meaning is already
defined and explained to the C language compiler. Therefore, reserve words cannot be
used as identifiers or variable names. They should only be used to carry the pre-defined
meaning. For example, int is a reserve word. It indicates the data type of the variable as
integer. Therefore, it is reserved to carry the specific meaning. Any attempt to use it other
than the intended purpose will generate a compile time error. The following list shows the
reserved words in C.

auto else long switch

break enum register typedef

case extern return union

char for signed void

continue goto sizeof volatile

default if static while

do int struct _Packed

double
Basic Data Types
C supports five fundamental data types: Character, Integer, Floating-Point, Double
floating-Point, and valueless. These are denoted as char, int, float double, void,
respectively, ’void’ is typically used to declare as function as retuning null value.

Table 2.1 Fundamental data types


Data Type Description Typical memory
requirements

int integer quantity 2 bytes or one word


(varies from one compiler
to another)

char single character 1 byte

float floating-point number (i.e., a number 1 word (4 bytes)


containing a decimal point and or an
exponent)

double double-precision floatingpoint 2 words (8 bytes)


number (i.e., more significant figures,
and an exponent which may be
larger in magnitude)

Modifiers to Basic Data Types


Modifiers are used to alter the meaning of basic data types to fit various needs. Except
the type void, all others data type can have various modifiers preceding them

List of modifiers used in C are:


 Signed
 Unsigned
 Long
 Short

Constants

It refers to fixed values that the program may not alter during its execution. These
fixed values are also called literals. Constants can be of any of the basic data types like
an integer constant, a floating constant, a character constant, or a string literal. There are
enumeration constants as well. Constants are treated just like regular variables except
that their values cannot be modified after their definition.

Variables

A variable is an identifier that may be used to store data value. A value or a quantity
which may vary during the program execution can be called as a variable. Each variable
has a specific memory location in memory unit, where numerical values or characters can
be stored. A variable is represented by a symbolic name. Thus variable name refers to
the location of the memory in which a particular data can be stored. Variables names are
also called as identifiers since they identify the varying quantities.
For Ex : sum = a+b. In this equation sum, a and b are the identifiers or variable names
representing the numbers stored in the memory locations.
Rules to be followed for constructing the Variable names(identifiers):
1. They must begin with a letter and underscore is considered as a letter.
2. It must consist of single letter or sequence of letters, digits or underscore character.
3. Uppercase and lowercase are significant. For ex: Sum, SUM and sum are three
distinct variables.
4. Keywords are not allowed in variable names.
5. Special characters except the underscore are not allowed.
6. White space is also not allowed.

Variable Declarations

In the program data types are written as given below.


 Integer - int
 Floating point - float
 Double floating point - double
 Character - char

Assigning an identifier to data type is called type declaration. In other words a declaration
associates a group of variables with a specific data types. All variables must be declared
before they appear in executable statements. The syntax of variable declaration and
some examples are given below. The syntax of variable declaration and some examples
are given below.
Syntax :
Data type ‘variable’
For example:
int a;
float c;
char name;
double count;

If two are more variables of the same data type has to be declared they can be clubbed
as shown in example given below.
 int a; intb; intc;
this is same as
int a, b, c;

 float d; float e; float f;


this can be written as
float d, e, f;

 short int a, b, c ;
long int r, s, t ;
The above declarations cab also be written as :
short a, b, c;
long r, s, r;

Assigning Values to Variables

Values can be assigned to variables using assignment operator “=”.

Syntax:
Variable name = value;
For Example:
a = 10; int lvalue = 0;

It is also possible to assign a value to a variable at the time the variable is declared. The
process of giving initial value to the variable is called initialization of the variable.

For Example:
int a=10, b=5; float x=10.5, y=1.2e-9;
The data item can be accessed in the program simply by referring to the variable name.
Data type associated with the variable cannot be changed. However, variables hold the
most recently assigned data.
Declaring a variable as constant
The value of the certain variable to remain constant during the execution of the program.
It can be achieved by declaring the variable with const qualifier at the time of initialization.

For example:
const int tax_rate = 0.30;

The above statement tells the compiler that value of variable must not be modified during
the execution of the program. Any attempt change the value will generate a compile time
error.

You might also like