0% found this document useful (0 votes)
1 views48 pages

C Programming Introduction

Beginners guide to C programming

Uploaded by

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

C Programming Introduction

Beginners guide to C programming

Uploaded by

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

Introduction to the C Language

Objectives
❏ To understand the structure of a C-language program.
❏ To write your first C program.
❏ To introduce the include preprocessor command.
❏ To be able to create good identifiers for objects in a program.
❏ To be able to list, describe, and use the C basic data types.
❏ To be able to create and use variables and constants.
❏ To understand input and output concepts.
❏ To be able to use simple input and output statements.

1
2-1 Background

C is a structured programming language. It is


considered a high-level language because it allows the
programmer to concentrate on the problem at hand
and not worry about the machine that the program
will be using. That is another reason why it is used by
software developers whose applications have to run on
many different hardware platforms.

2
2-2 C Programs

It's time to write your first C program.

Topics discussed in this section:


Structure of a C Program
Your First C Program
Comments
The Greeting Program

Computer Science: A Structured Programming Approach Using C 3


FIGURE 2-2 Structure of a C Program

4
FIGURE 2-3 The Greeting Program

5
PROGRAM 2-1 The Greeting Program

6
FIGURE 2-4 Examples of Block Comments

7
FIGURE 2-5 Examples of Line Comments

8
2-3 Identifiers

One feature present in all computer languages is the


identifier. Identifiers allow us to name data and other
objects in the program. Each identified object in the
computer is stored at a unique address.

9
Table 2-1 Rules for Identifiers

10
Note
An identifier must start with a letter or underscore:
it may not have a space or a hyphen.

11
Note
C is a case-sensitive language.

12
Table 2-2 Examples of Valid and Invalid Names

13
2-4 Types

A type defines a set of values and a set of operations


that can be applied on those values.

Topics discussed in this section:


Void Type
Integral Type
Floating-Point Types
14
FIGURE 2-7 Data Types

15
FIGURE 2-8 Character Types

16
FIGURE 2-9 Integer Types

17
Note
sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long)

18
Table 2-3 Typical Integer Sizes and Values for Signed Integers

19
FIGURE 2-10 Floating-point Types

20
Note
sizeof (float) ≤ sizeof (double) ≤ sizeof (long double)

21
Table 2-4 Type Summary

22
2-5 Variables

Variables are named memory locations that have a type,


such as integer or character, which is inherited from
their type. The type determines the values that a variable
may contain and the operations that may be used with
its values.

Topics discussed in this section:


Variable Declaration
Variable Initialization

23
FIGURE 2-11 Variables

24
Table 2-5 Examples of Variable Declarations and Definitions

25
‘B’

FIGURE 2-12 Variable Initialization

26
Note
When a variable is defined, it is not initialized.
We must initialize any variable requiring
prescribed data when the function starts.

27
PROGRAM 2-2 Print Sum of Three Numbers

28
PROGRAM 2-2 Print Sum of Three Numbers (continued)

29
PROGRAM 2-2 Print Sum of Three Numbers (continued)

30
2-6 Constants

Constants are data values that cannot be changed


during the execution of a program. Like variables,
constants have a type. In this section, we discuss
Boolean, character, integer, real, complex, and string
constants.

Topics discussed in this section:


Constant Representation
Coding Constants

31
Note
A character constant is enclosed in single quotes.

32
Table 2-6 Symbolic Names for Control Characters
33
Table 2-7 Examples of Integer Constants

34
Table 2-8 Examples of Real Constants

35
FIGURE 2-13 Some Strings

36
FIGURE 2-14 Null Characters and Null Strings

37
Note
Use single quotes for character constants.
Use double quotes for string constants.

38
PROGRAM 2-3 Memory Constants

39
PROGRAM 2-3 Memory Constants (continued)

40
PROGRAM 2-4 Enumeration Data Type

enum week{sunday, monday, tuesday, wednesday,


thursday, friday, saturday};
enum week day;

LCD Example

41
// An example program to demonstrate working
// of enum in C
#include<stdio.h>

enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun}

int main()
{
enum week day;
day = Wed;
printf("%d",day);
return 0;
} 42
#include <stdio.h>
#include <float.h>

int main(){

printf("Storage size for float : %d \n", sizeof(float));


printf("Minimum float positive value: %E\n", FLT_MIN );
printf("Maximum float positive value: %E\n", FLT_MAX );
printf("Precision value: %d\n", FLT_DIG );

return 0;
}

43
USER DEFINED (DATA)
Keyword Size
≥ sum of size
TYPES Note

An aggregate type which can contain more


struct of each
than one different types.
member
typedef struct
tag or label is optional {
struct theEmployee { int x;
int age; int SomeArray[100];
} MyFoo;
double salary;
char department; int main()
char name[15]; {
char address[5][25]; MyFoo strctVar;
};
struct theEmployee workerRec; return 0;
}
struct newPoint {
short xPoint;
short yPoint;
} justPoint;

justPoint thePoint;
44
USER DEFINED (DATA)
TYPES
An aggregate type which can contain more than
≥ size of the
one other types. union uses shared memory
union largest
space compared to struct, so only one member
member
can be accessed at one time.
union someData
{
int pNum;
float qNum;
double rNum;
};
union someData simpleData;
union OtherData{
char aNum;
int xNum;
float fNum;
} simpleData;
simpleData saveData;
45
USER DEFINED (DATA)
typedef TYPES
same as the type;
being given a new
typedef used to give new identifier names or alias (to
simplify the long identifier names), normally used for
name aggregate defined types.
typedef unsigned char BYTE; /* Declares BYTE to be a synonym for unsigned char */
typedef float FLOAT; /* Declares FLOAT (uppercase letter) to be a synonym for unsigned float
(lowercase) */

tag or label is optional

typedef struct TOKEN_SOURCE {


typedef struct simpleData
{ int nData; CHAR SourceName[8];
char cData; LUID SourceIdentifier;
} newNameType; } TOKEN_SOURCE, *PTOKEN_SOURCE;
Or
TOKEN_SOURCE newToken;
typedef struct { int nData; char
cData;} newNameType;
newNameType strctType;
typedef enum DayNames { Monday,
typedef union unData{
Tuesday,
double lngSalary;
Wednesday,
int nDay;
Thursday,
}newUntype;
Friday, Saturday, Sunday
} Weekdays;
newUnType lntotalSalary;
Weekdays dayOfWeek; 46
DERIVED (DATA)
Type
TYPES
Size

Note
Hold the memory address which point to the actual
data/value.
 0 address always represents the null pointer (an
address where no data can be placed),
irrespective of what bit sequence represents the
value of a null pointer.
type*  Pointers to different types will have different sizes.
≥ size of char So they are not convertible to one another.
(a pointer)  Even in an implementation which guarantees all
data pointers to be of the same size, function
pointers and data pointers are in general
incompatible with each other.
 For functions taking a variable number of
arguments, the arguments passed must be of
appropriate type.
char *ptoChar;
char csimpleChr = 'T';
int iNumber = 20;
char *chptr;
int *imyPtr = &iNumber;
// assignment
chptr = &csimpleChr;
47
DERIVED (DATA)
TYPESUse to declare a variable with

collection of identical properties or


types.
 Simplify variable declaration.
 In a declaration which also initializes
type [integer]
≥ integer × size of the array (including a function
type parameter declaration), the size of
(an array)
the array (the integer) can be
omitted, which is called unsized.
 type [ ] is not the same as type*.
Only under some circumstances one
can be converted to the other.

char cName1[ ] =
{'a','r','r','a','y'};
int fstudentNumber[3] = {4,7,1};
char cName2[ ] = {"array"};
int nrowandColumn[1][2] = {34, 21};
char cName3[6] = "array";
int nlongHeightWidth[3][4][5] = 0; int nrowCol[2][3] = {4,2,3,7,2,8};

48

You might also like