C Programming Lesson 2
C Programming Lesson 2
KIIT 2014
Objectives
After completing this lesson, you should be able to do the following:
• Identify various tokens in C language
• List and describe various data types
• List the uses of variables
• Declare and initialize variables
• Define constants and literals
• Use of storage classes
• Identify and use of operators
KIIT 2014
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::
• Keyword
• Identifier
• Constant
• String Literals
• Symbols
KIIT 2014
Token - Example
KIIT 2014
Semicolons
In C program, the semicolon is a statement terminator.
Example:
printf("Hello, World! \n");
return 0;
KIIT 2014
Comments
Comments are like helping text in your C program and they are
ignored by the compiler.
Example:
/* my first program in C */
KIIT 2014
Identifiers
Identifiers are used for:
• Naming a variable
• Providing a convention for variable names:
– Must start with a letter
– Can include letters or numbers
– Can’t include special characters such as dollar sign,
percentage and pound sign
– Must not be keywords
KIIT 2014
Keywords
The following list shows the reserved words 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 _Packed
double
KIIT 2014
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.
int age;
KIIT 2014
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:
• Basic Types
• Enumerated Types
• The Type Void
• Derived Types
KIIT 2014
Integer Types
Standard integer types with its storage sizes and value
ranges:
KIIT 2014
Floating - Point Types
Standard floating - point types with its storage sizes and
value ranges:
KIIT 2014
C - Variables
A variable is nothing but a name given to a storage area
that programs can manipulate.
Syntax:
type variable_list;
Example:
int i, j, k;
char c, ch;
float f, salary;
double d;
KIIT 2014
Variable Initialization
Variables can be initialized (assigned an initial value) in their
declaration. The initializer consists of an equal sign followed
by a constant expression as follows:
Syntax:
KIIT 2014
Variable Declaration
KIIT 2014
Variable Declaration - Example
#include <stdio.h>
// Variable declaration:
extern int a, b;
extern int c;
extern float f;
int main ()
{
/* variable definition: */
int a, b;
int c;
float f;
/* actual initialization */
a = 10;
b = 20;
c = a + b;
printf("value of c : %d \n", c);
f = 70.0/3.0;
printf("value of f : %f \n", f);
return 0;
}
KIIT 2014
// function declaration
int func();
int main()
{
// function call
int i = func();
}
// function definition
int func()
{
return 0;
}
KIIT 2014
Variable Types
KIIT 2014
Local Variable
KIIT 2014
Local Variable
• Variables declared inside outer block are visible or meaningful only
inside outer block
• Similarly variables declared inside inner block are visible or meaningful
only inside inner block
• Variables declared inside inner block are not accessed by outer block
#include<stdio.h>
void main()
{
int var1=10;
{
int var2 = 20;
printf("%d %d",var1,var2);
}
printf("%d %d",var1,var2);
}
KIIT 2014
Global Variable
KIIT 2014
Global Variable
• Inner block variables declared in outer block acts as “Global Variable”
#include<stdio.h>
int var=10;
void message();
void main()
{
int var=20;
{
int var = 30;
printf("%d ",var);
}
printf("%d ",var);
message();
}
void message()
{
printf("%d ",var);
}
KIIT 2014
Attributes of Variable
KIIT 2014
Constants and Literals
KIIT 2014
Character Literals
KIIT 2014
Escape Sequence
KIIT 2014
String Literals
String literals or constants are enclosed in double quotes
""
Example:
"hello, dear"
KIIT 2014
Operators
KIIT 2014
Summary
In this lesson, you should have learned how to:
• Identify various tokens in C language
• List and describe various data types
• List the uses of variables
• Declare and initialize variables
• Define constants and literals
• Identify and use of operators
KIIT 2014