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

Lecture 02 Basics of C Programming - Data Types

Uploaded by

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

Lecture 02 Basics of C Programming - Data Types

Uploaded by

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

C Programming Basics

ICT 012

• Instructor: Mr. Katwale, S


• Moshi Co-operative University (MoCU)
C - Basic Syntax
Tokens in C

• A C program consists of various tokens and a token is either


a keyword, an identifier, a constant, a string literal, or a
symbol.

• For example, the following C statement consists of five


tokens −

• The individual tokens are −


C - Basic Syntax
Semicolons (;)

• In a C program, the semicolon is a statement


terminator. That is, each individual statement must
be ended with a semicolon.

• It indicates the end of one logical entity.

• Given below are two different statements −


C - Basic Syntax
Comments

• Comments are like helping text in your C program and they are
ignored by the compiler.

• They start with /* and terminate with the characters */ as shown


below −

/* my first program in C */

• You cannot have comments within comments and they do not


occur within a string or character literals.
C - Basic Syntax
Identifiers

• A C identifier is a name used to identify a variable, function,


or any other user-defined item.

• An identifier starts with a letter A to Z, a to z, or an


underscore '_' followed by zero or more letters, underscores,
and digits (0 to 9).
C - Basic Syntax
Identifiers

• C does not allow punctuation characters such as @, $, and %


within identifiers.

• C is a case-sensitive programming language. Thus, Manpower


and manpower are two different identifiers in C.

• Here are some examples of acceptable identifiers −


C - Basic Syntax
Keywords
• Keywords in C programming are reserved words that have a specific
meaning and cannot be used as identifiers (names) for variables,
functions, or any other user-defined entities.
C - Basic Syntax
Whitespace in C

• A line containing only whitespace, possibly with a comment, is known


as a blank line, and a C compiler totally ignores it.

• Whitespace is the term used in C to describe blanks, tabs, newline


characters and comments.

• Whitespace separates one part of a statement from another and


enables the compiler to identify where one element in a statement,
such as int, ends and the next element begins.
C - Basic Syntax
Whitespace in C
• Therefore, in the following statement −

• There must be at least one whitespace character (usually a space) between int
and age for the compiler to be able to distinguish them. On the other hand, in
the following statement −

• No whitespace characters are necessary between fruit and =, or between = and


apples, although you are free to include some if you wish to increase readability.
C - Data Types
• Data types in c refer to an extensive system used for declaring
variables or functions of different types.

• The type of a variable determines how much space it occupies in


storage and how the bit pattern stored is interpreted.

• The types in C can be classified as follows −


C - Data
Types
C - Data Types
Integer Types:
• int: Standard integer type, usually 32 bits on most systems.
• short: Short integer type, usually 16 bits.
• long: Long integer type, usually 32 bits.
• long long: Extended long integer type, introduced in C99, often 64
bits.
C - Data Types
Floating-Point Types:

• float: Single-precision floating-point type.

• double: Double-precision floating-point type.

• long double: Extended double-precision floating-point type.


C - Data Types

The void Type

• The void type specifies that no value is available.

• void: Represents the absence of a type.

• It is used in following situations


C - Data Types
1. Function returns as void

• There are various functions in C which do not return any value or you can say
they return void. A function with no return value has the return type as void.

• For example, void exit (int status);

2. Function arguments as void

• There are various functions in C which do not accept any parameter.

• A function with no parameter can accept a void. For example, int rand(void);
C - Data Types
Character Types:

• char: Represents a single character.

Derived Types:

• Array: A collection of elements of the same data type.


C - Data Types
Character Types:

• char: Represents a single character.

Derived Types:

• Array: A collection of elements of the same data type.


C - Data Types
String Types:

• In C, there is no built-in data type called "String" like in some other


programming languages (e.g., Java or Python).

• However, strings in C are represented as arrays of characters.

Character Arrays:

• Strings are represented as arrays of characters.

• For Example
C - Data Types
String Types:

• In C, there is no built-in data type called "String" like in some other


programming languages (e.g., Java or Python).

• However, strings in C are represented as arrays of characters.

Character Arrays:
In this example, greeting is an
• Strings are represented as arrays of characters. array of characters containing the
characters of the string "Hello,
• For Example World!".
C - Data Types
String Input and Output:

The standard input/output functions (printf() and scanf()) can be used


for input and output of strings respectively.
C - Data Types
• Example

Output
C - Data Types
Basic Format Specifiers

• There are different format specifiers for each data type. Here are some
of them:
C - Data Types
• Constants

• When you don't want others (or yourself) to override existing variable
values, use the const keyword (this will declare the variable as
"constant", which means unchangeable and read-only):
C - Data Types
Constants

Example 1

Output
C - Data Types
Constants
Example 2

Output

Note:

• When you declare a constant variable, it must be assigned with a value.

You might also like