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

Lesson 01 - C Introduction - Data Types & Variables

Uploaded by

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

Lesson 01 - C Introduction - Data Types & Variables

Uploaded by

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

C Fundamentals

First C Program

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 1


Lesson Objectives
 History of C, About C

 First C Program

 Variables, Constants

 Identifier Names

 Data Types
7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 2
Section 1
HISTORY OF C, ABOUT C

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 3


History of C

 History of C
 Evolved from two other programming languages
• BCPL and B
– “Typeless” languages
 Dennis Ritchie (Bell Laboratories)
• Added data typing, other features
 Development language of UNIX
 Hardware independent
• Portable programs
 1989: ANSI standard
 1990: ANSI and ISO standard published
• ANSI/ISO 9899: 1990

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 4


Application Areas Of C

 C was initially used for systems programming


 A system program forms a portion of the operating system
of the computer or its support utilities
 Operating Systems, Interpreters, Editors, Assembly
programs are usually called system programs
 The UNIX operating system was developed using C
 There are C compilers available for almost all types
of PC’s

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 5


About C & C Program Structure

 C has 32 keywords
 These keywords combined with a formal syntax form a C programming
language
 Rules to be followed for all programs written in C:

- All keywords are lowercased main()


- C is case sensitive, do while is different from {
/* This is a sample Program*/
DO WHILE
int i,j;
- Keywords cannot be used as a variable or i=100;
function name j=200;
}

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 6


Section 2
FIRST C PROGRAM

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 7


First C Program: Source codes
Left brace { begins function body. Single-line comments.
1 // hello.c Function main appears
exactly once in every C/C++
2 // A first program in C program..
3 #include <stdio.h>
4
Name printf belongs to namespace stdio.
5 // function main begins program execution
6 int main()
7 { Statements end with a semicolon “;”
8 printf("Hello, world!\n");
9 // indicate that program ended successfully
10 return 0; Keyword return is one of several means to exit function;
11 value 0 indicates program terminated successfully.
12 } // end function main
Corresponding right
brace } ends function
body.

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 8


First C Program: Anatomy of a C Program

program header comment

preprocessor directives (if any)

int main ( void )


{
statement(s)
return 0 ;
}
7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 9
First C Program: Program Header Comment

 A comment is descriptive text used to help a reader of the


program understand its content.
 All comments must begin with the characters /* and end
with the characters */
 These are called comment delimiters
 The program header comment always comes first.
 Look at the class web page for the required contents of our
header comment.

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 10


First C Program: Preprocessor Directives

 Lines that begin with a # in column 1 are called


preprocessor directives (commands).
 Example: the #include <stdio.h> directive causes the
preprocessor to include a copy of the standard input/output
header file stdio.h at this point in the code.
 This header file was included because it contains
information about the printf ( ) function that is used in this
program.

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 11


First C Program: Tool Preparation: Dev-C++

 Download & Install Dev-C++ from


http://www.bloodshed.net/dev/devcpp.html
 Run Dev-C++ and discover its features
 Creating a Program: Ctrl+N
 Compiling a Program: Ctrl+F9
 Running a Program: F9

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 12


First C Program: Tool Preparation Visual Studio

 Download & Install Visual Studio from


https://visualstudio.microsoft.com/
 Run Visual Studio and
discover its features
 Creating a new Project: Ctrl+N
 Compiling a Program: Ctrl+B
 Running a Program: F5

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 13


First C Program: Compiling & Running

Compiler converts a
C program into an
executable.
There are four phases
for a C program to
become an
executable:
1- Pre-processing
2- Compilation
3- Assembly
4- Linking

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 14


Section 3
VARIABLES, CONSTANTS

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 15


Variables

Memory

Data 15

15

Variables allow to provide a Data in memory


meaningful name for
the location in memory
Each location in the memory is unique

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 16


Constants

 A constant is a value whose worth never changes

 Examples
5 numeric / integer constant
 5.3 numeric / float constant
 ‘Black’ string constant
 ‘C’ Character constant

 Variables hold constant values

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 17


Numeric Constants

 Numeric constants are an uninterrupted


sequence of digits (and may contain a
period). They never contain a comma.
 Examples:
 123
 98.6
 1000000

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 18


Character Constants

 Singular!
 One character defined character set.
 Surrounded on the single quotation mark.
 Examples:
 ‘A’
 ‘a’
 ‘$’
 ‘4’
7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 19
String Constants

 A sequence characters surrounded by double


quotation marks.
 Considered a single item.
 Examples:
 “UMBC”
 “I like ice cream.”
 “123”
 “CAR”
 “car”

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 20


Section 4
IDENTIFIER NAMES

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 21


Identifier Names

 Some correct identifier names


 arena
 s_count
 marks40
 class_one

 Examples of erroneous identifiers


 1sttest
 oh!god
 start... End

 Identifiers in C are case sensitive

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 22


Identifier Names: Guidelines

Variable names should begin with an alphabet

The first character can be followed by alphanumeric characters

Proper names should be avoided while naming variables

A variable name should be meaningful and descriptive

Confusing letters should be avoided

Some standard variable naming convention should be followed


while programming

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 23


Section 5
DATA TYPES

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 24


Data Types

 Different types of data are stored in variables. Some


examples are:

 Numbers
 Whole numbers. For example, 10 or 178993455
 Real numbers. For example, 15.22 or 15463452.25
 Positive numbers
 Negative numbers
 Names. For example, John
 Logical values. For example, Y or N

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 25


Data Types (1)

• A data type describes the kind of data that will fit into a variable.

• The name of the variable is preceded with the data type.

• For example, the data type int would precede the name varName

Datatype variableName

int varName

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 26


Data Types: Basic Types

The basic data types are

int float double char void

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 27


Data Types: integer

 Stores numeric data


int num;
 Cannot then store any other type of
data like “Alan” or “abc”
 16 bits (2 bytes)
 Integers in the range -32768 to 32767
 Examples: 12322, 0, -1991

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 28


Data Types: float

 Stores values containing decimal places

float num;

 Precision of upto 6 digits

 32 bits (4 bytes) of memory

 Examples: 69.96, 28.07, 2020


7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 29
Data Types: char

 Stores a single character of information

char gender;
gender='M';

 8 bits (1 byte) of memory

 Examples: ‘a’, ‘m’, ‘$’, ‘%’, ‘1’, ‘5’


7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 30
Data Types: void

 Stores nothing void func()


{
 Indicates the compiler that // do something
there is nothing to expect return;
}

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 31


Derived Data Types

Data type Basic Data


Modifiers
Derived data type
types

unsigned int (Permits only


unsigned
int positive numbers)

short short int (Occupies less


int
memory space than int)

long int/double Long int /longdouble (Occupies


more space than int/double)

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 32


signed and unsigned Types

 unsigned type specifies that a variable can take only


positive values

unsigned int varNum;


varNum = 23123;

 varNum is allocated 2 bytes


 modifier may be used with the int and float data
types
 unsigned int supports range from 0 to 65535

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 33


long and short Types
 short int occupies 8 bits (1 byte)

 allows numbers in the range -128 to 127

 long int occupies 32 bits (4 bytes)

 -2,147,483,647 and 2,147,483,647

 long double occupies 128 bits (16


bytes)

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 34


Data Types: Range (1)

Type Approximate Size in Bits Minimal Range

char 8 -128 to 127 The size of data


unsigned char 8 0 to 255 type depends
signed char 8 -128 to 127 Operating System
int 16 -32,768 to 32,767 Sizeof (data type)
unsigned int 16 0 to 65,535 function return
signed int 16 Same as int the size of data
type
short int 8 -128 to 127
unsigned short int 8 0 to 255

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 35


Data Types: Range (2Data Types: Range (1))

Type Approximate Size Minimal Range


in Bits
signed short int 8 Same as short int
long int 32 -2,147,483,647 to 2,147,483,647

signed long int 32 0 to 4,294,967,295


unsigned long int 32 0 to 4,294,967,295
float 32 Six digits of precision
double 64 Ten digits of precision
long double 128 Ten digits of precision
7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 36
Data Types: Sample Declaration

main ()
{
char abc; /*abc of type character */
int xyz; /*xyz of type integer */
float length; /*length of type float */
double area; /*area of type double */
long liteyrs; /*liteyrs of type long int */
short arm; /*arm of type short integer*/
}

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 37


Thank you
Q&A

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 38

You might also like