0% found this document useful (0 votes)
5 views28 pages

C Programming Basics: Keywords & Data Types

This document is a lecture on Keywords, Identifiers, and Data Types in C programming, presented by S.I.M. Adnan. It covers C basics, character sets, basic syntax including tokens, comments, identifiers, keywords, and whitespace, as well as fundamental data types and their storage sizes. Additionally, it explains input and output functions like scanf and printf.
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)
5 views28 pages

C Programming Basics: Keywords & Data Types

This document is a lecture on Keywords, Identifiers, and Data Types in C programming, presented by S.I.M. Adnan. It covers C basics, character sets, basic syntax including tokens, comments, identifiers, keywords, and whitespace, as well as fundamental data types and their storage sizes. Additionally, it explains input and output functions like scanf and printf.
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

CSE-161

Programming Language I
Lecture 3
Keywords/Identifiers
and
Data Types in C

S.I.M. ADNAN
Lecturer
Department Of Computer Science and Engineering
Southeast University
Reference Books

1. Programming in ANSI C
— By E. Balagurusamy

2. The C Programming Language


— By Brian Kernighan and Dennis Ritchie

3. Teach Yourself C
— By Herbert Schildt

2
Outline

• C Basics
• Character Set in C
• C Basic Syntax
• Variables
• Data Types
• Variable Declaration
• Lvalue and Rvalue

3
C Basics Cont…
/*
* Author Name:CSE-49
* Source file name: hello.c Comment
* Comments: My first program in C.
*/
#include <stdio.h> Preprocesso
r
/*the main function*/ Comment

int main ()
Main
{
Function
/*print the phrase */ Comment
printf("Hello, CSE 49!!");
Function
return 0;
}

4
C Basics Cont…
• Let us take a look at the various parts of the above program −
– The first line of the program #include <stdio.h> is a preprocessor
command, which tells a C compiler to include stdio.h file before going to
actual compilation.
– The next line int main() is the main function where the program execution
begins.
– The next line /*this is a comment*/ will be ignored by the compiler and
it has been put to add additional comments in the program. So such lines
are called comments in the program.
– The next line printf("Hello, CSE 49!!" ); is another function available in C
which causes the message "Hello, CSE 49!!" to be displayed on the screen.
– The next line return 0; terminates the main() function and returns the
value 0.
5
Character Set in C

• Character set:-
– The character set is the fundamental raw material of any language and they are
used to represent information.
– Like natural languages, computer language will also have well defined character set,
which is useful to build the programs.

• The characters in C are grouped into the following two categories:


– Source character set
▪ Alphabets
▪ Digits
▪ Special Characters
– Execution character set
▪ Escape Sequence

6
Character Set in C cont.
Types Character Set
Lowercase Letters a-z
Uppercase Letters A to Z
Digits 0-9
Special Characters !@#$%^&*
White Spaces Tab Or New line Or Space
• C is case sensitive which means the small letters and capital letters
are distinct.
7
Character Set in C cont…
• Special Characters
Symbol Meaning Symbol Meaning Symbol Meaning Symbol Meaning
Tilde ` Apostrophe ( Left parenthesis " Quotation mark
~
Exclamation - Minus sign ) Right ; Semicolon
! mark parenthesis
Number sign = Equal to sign _ Underscore < Opening angle
# bracket
Dollar sign { Left brace + Plus sign > Closing angle
$ bracket
Percent sign } Right brace | Vertical bar ? Question mark
%
Caret [ Left bracket \ Backslash , Comma
^
Ampersand ] Right bracket / Slash . Period
&
Asterisk : Colon
* 8
Printing a Line of Text

9
Basic Syntax

• You have seen the basic structure of a C program


• The basic building blocks of C are:
– Tokens
– Semicolons
– Comments
– Identifiers
– Keywords
– Whitespace

10
Basic Syntax: Tokens

• In a passage of text, individual words and punctuation marks are called


tokens.
• A C program consists of various tokens and a token is either a keyword, an
identifier, an operator, a constant, a string literal, or a symbol.
• For example, the following C statement consists of five tokens −
printf(“Hello World!!”);
1. printf
2. (
3. “Hello World!!”
4. )
5. ;
11
Basic Syntax: Tokens Cont.

12
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 −

printf("Hello World!!");
return 0;

13
Basic Syntax: Comments

• Comments are like helping text in your C program and they are
ignored by the compiler.
• Two Types
– Block Comment
– Single Line Comment (Only Supported in C99 or later)
• Block Comment 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.
14
Basic Syntax: Comments

• Single Line Comment (C99 or later )start with ‘//’ and


continue until the end of the line.

• // my first program in C

• Multiple // operator can be used in a single comment.

15
Basic Syntax: Identifier

• In C, an 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 does not allow punctuation characters such as @, $, and % within identifiers.
• C is a case-sensitive programming language.

16
Basic Syntax: Keyword

• Keywords are set of reserved words in C

• Have special meaning and that meaning unchangeable.

• Keywords serve as basic building blocks for program statements.


• All keywords must be written in lowercase.
• Keywords cannot be used as constants or variables or any other identifier
names.

17
Basic Syntax: Keyword Cont…

• The following list shows the keywords in C.


auto else long switch
break enum register typedef

case extern return union


char float short unsigned

const for signed void


continue goto sizeof volatile
default if static while

do int struct double


18
Basic Syntax: Whitespaces

• 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
– enables the compiler to identify where one element in a statement (such as int,)ends and the
next element begins

19
Basic Syntax: Whitespaces Cont…
• Therefore, in the following statement −
– int age;
• 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 −
– fruit=apples+oranges; // get the total fruit
• 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.
20
Data Types

• The type of a variable determines how much space it


occupies in storage and how the bit pattern stored is
interpreted.

21
Data Types Cont…

• All C compiler support following fundamental data types


– Void (void )
– Integer (int)
– Character (char)
– Floating point (float)
– Double precision floating point (double)
– Long int (long long int)
– Long double (long double)

22
Data Types: Primary

23
Data Types: Integers

Type Storage size Value range

char 1 byte -128 to 127 or 0 to 255

unsigned char 1 byte 0 to 255

signed char 1 byte -128 to 127

-2,148,483,648 to 2,148,483,648
int 4 bytes

unsigned int 4 bytes 0 to 4,294,967,295

short 2 bytes -32,768 to 32,767

unsigned short 2 bytes 0 to 65,535

long long 8 bytes -9,223,372,036,854,775,808 to


9,223,372,036,854,775,807
unsigned long long 8 bytes 0 to 18,446,744,073,709,551,615
24
Data Types: Decimal

• The following table provide the details of standard floating-point types with
storage sizes and value ranges and their precision −

Type Storage Size Value Range

float 4 bytes

±3.40282347E+38F

double 8 bytes ±1.79769313486231570E+308

long double 16 bytes

25
Data Types: Void

• The void type specifies that no value is available. It is used in three kinds of
situations −
[Link]. Types & Description
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);
3 Pointers to void
A pointer of type void * represents the address of an object, but not its
type. For example, a memory allocation function void *malloc( size_t 26
Keyboard Input using scanf

• General format
scanf(format-string, address-list)
• Example
scanf (“%d”, &age);
• The format string contains format specifier(one per address) to be
used in converting the input.
– %d – Tells scanf that the program is expecting an ASCII
encoded integer number to be typed in, and that scanf
should convert the string of ASCII characters to internal
binary integer representation.
• Address-list: List of memory addresses to hold the input values
27
Syntax: printf function

printf(format_string, expression_list);

• Format_string specifies how expressions are to be


printed
– Contains format specifiers for each expression
• format specifier begins with % and end with type
• Expression list is a list of zero or more expressions separated by
commas
• Returns number of characters printed

28

You might also like