C Programs - printf
• printf(“formatted_string”, arguments_list);
1. formatted_string: Specifies the data to be printed.
2. arguments_list: Variable names corresponding to format
specifier.
Syntax of format specifier:
%[flags][width][.precision][length]specifier
printf() - format specifiers in C
code Format
• %c Display Character
• %d Display signed integer number
• %f Display floating point number
• %ld Display double numbers
• %s Display string
• %p Display pointer
• %x Display hexadecimal numbers
Escape Sequence
Some non-printing characters and some other characters such as double
quote (“), single quote (‘), question mark (?) and backslash (\), require
an escape sequence. A list of commonly used backslash character
constants is given below.
printf – C programs
1. Simple – Hello World.
2. Name and address.
3. Patterns
scanf() command
• Syntax - scanf(“%d”,&x); - scanf(), takes two arguments – the format specifier
and the reference operator, which stores the memory address of the
variable.
• scanf() – Function that reads input from the standard input i.e. keyboard and
stores the input in the variables.
• %d – Format specifier
• & - Address operator – provides the address of the variable in the memory.
• x – Variable name.
Preliminary Requirements:
C Tokens:
• A token is an atomic unit (smallest indivisible units) in a
program.
• The most basic elements in a C program recognised by the
compiler are a single character or a group of characters
called C tokens.
• When you write a C program, the compiler first breaks
your code into tokens before analyzing it. The compiler
cannot breakdown the token any further.
• Example: the words ‘main’, ‘{‘(brace), ‘(’ (parenthesis) are
all tokens of C program.
Types of Tokens in C - 6 main types of tokens
1. Keywords: Reserved words with special meaning. Eg: int,
return, if, while, for.
2. Identifiers: Names given to variables, functions, arrays, etc.
Eg, age, main, sum.
3. Constants: Fixed values that do not change. Eg: 10, 'A',
3.14.
4. String literals: Sequence of characters enclosed in double
quotes. Eg: "Hello World".
5. Operators: Symbols that perform operations. Eg: +, -, *, /,
==, &&.
6. Special symbols / Punctuators: Symbols used for
structure. Eg: ;, {}, (), [], ,.
Example of Token in a C-Program:
int main() { Breaking into Tokens:
int a = 10; •int → keyword •10 → constant
•main → identifier •; → special symbol
•printf → identifier
•(, ) → special
printf("Hello"); symbols
•"Hello" → string
literal
return 0; •{, } → symbols •return →
•int → keyword keyword
} •a → identifier •0 → constant
•= → operator
C Character Set
• The C character set includes the upper case
letters A to Z, the lower case a to z, the decimal
digits 0 to 9 and certain special characters.
Letters a, b, c, …, z; A, B,C,…,Z
Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special characters ~,.;:?‘“!()[]{}/\<
>=+-$#@&*%^
Character/Symbol Meaning Character/Symbol Meaning
~ Tilde , Comma
. Period ; Semicolon
? Ques. mark : Colon
“ Double Quote ‘ Single Quote
( Left parenthesis ) Right Parenthesis
[ Left Bracket ] Right Bracket
{ Left Brace } Right Brace
/ Slash \ Back Slash
< Less than > Greater than
= Equal to ! Exclamatory Mark
- Minus + Plus
# Hash $ Dollar sign
& Ampersand * Asterisk (or star)
% Percent ^ Caret
_ Underscore | Vertical Bar
Keywords in C
• Keywords are predefined tokens in C. These are
also called reserved words.
• Key words have special meaning to the C
compiler. These key words can be used only for
their intended action; they cannot be used for
any other purpose.
• Their meaning cannot be changed at any
instance.
• Serve as basic building blocks for program
statements.
• All keywords are written in only lowercase.
C has 32 keywords.
auto break case char const continue default do
double else enum extern float for goto if
int long register return short signed sizeof static
struct switch typedef union unsigned void volatile while
Identifier
s
• Identifiers are distinct names given by programmer to program
elements such as constants, variables, functions, etc.
• An Identifier is a sequence of letters, digits, and the special
character ‘_’ (underscore).
• It must start with either a letter or underscore. ‘_’
• No commas or blanks are allowed within a variable name.
• The upper case and lower case letters are treated as distinct, i.e.,
identifiers are case-sensitive.
• An identifier can be of any length.
• No special symbol can be used in a variable name.
Valid and Invalid Identifiers
Valid identifiers
• X Invalid identifiers
• Abc • 10abc, 123a
• abc • my-name
• simple_interest • “hello”
• a123 • simple interest
• LIST • (area)
• stud_name • %rate
• Dept. of CSE, IIT KGP • Int - keyword
• Empl_1 • float, void, char
• Empl_2 • int my_var = 5; and char my_var = 's';
• avg_empl_salary
• Int, Float, Void, Char
Data Types
• In C programming, a data type defines the type of data that
a variable can hold.
• It tells the compiler:
• What kind of value will be stored (integer, character, decimal,
etc.)
• How much memory should be allocated
• Which operations can be performed on the variable
• C also has a special data type called void, which represents the
absence of a value or type. It is commonly used for functions that
do not return a value, for functions that take no parameters, and for
generic pointers (void *).
Types of Data Types in C
• Primary (Basic) Data Types: These are the fundamental types in C:
• int → integers (e.g., 5, -20)
• float → single precision decimal numbers (e.g., 3.14)
• double → double precision decimal numbers (e.g., 3.14159265)
• char → single characters (e.g., 'a', 'Z')
• void → means "no value" (used in functions that don’t return anything)
• Derived Data Types: Created from the basic types:
• Pointers (e.g., int *p;)
• Arrays (e.g., int arr[10];)
• Functions (returning or taking data types as parameters)
• User-defined Data Types: Created by programmers:
• struct → structure (group of different data types)
• union → memory-efficient version of struct
• enum → enumeration (collection of named integer constants)
• typedef → alias for existing data types
Primary (Basic) Data Types
Data Type Size (bytes) Range Format Specifier
short int 2 -32,768 to 32,767 %hd
unsigned short int 2 0 to 65,535 %hu
unsigned int 4 0 to 4,294,967,295 %u
int 4 -2,147,483,648 to 2,147,483,647 %d
long int 4 -2,147,483,648 to 2,147,483,647 %ld
unsigned long int 4 0 to 4,294,967,295 %lu
long long int 8 -(2^63) to (2^63)-1 %lld
unsigned long long int 8 0 to 18,446,744,073,709,551,615 %llu
signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
float 4 1.2E-38 to 3.4E+38 %f
double 8 1.7E-308 to 1.7E+308 %lf
long double 16 3.4E-4932 to 1.1E+4932 %Lf
Primary (Basic) Data Types
Some of the basic data types can be augmented by using certain data type
qualifiers:
• short, long, signed, unsigned
Example:
• short int
• long int
• unsigned int
• Note: The long, short, signed and unsigned are datatype modifier that can
be used with some primitive data types to change the size or length of the
datatype.
Constants in
C
A constant is a value that cannot be changed during the execution of
a program.
Syntax: const data_type var_name = value;
CONSTANTS
Numeric constants Character constants
Real Constants Single Character
Integer Constants String Constants
Constants
Constants in
C
Constants are defined using:
#define - preprocessor directive
const - keyword
Examples:
#define PI 3.14159 // macro constant
const int MAX = 100; // typed constant
Variables in C
• A variable in C is a named memory location that stores data which can be changed during program
execution.
Note: It is like a container (with a name + type) for storing information.
• There are two purposes:
1. Tells the compiler what the variable name is.
2. Specifies what type of data the variable will hold - This decides the size in memory and the
operations allowed.
General syntax: data-type variable-list; or data-type variable_name = expression;
Examples:
• int velocity, distance;
• int a =10, b, c;
• float temp;
• char flag, option;
Every variable has an address (in memory), and its contents.
Variables in C
In C terminology, in an expression
• speed refers to the contents of the memory location.
• &speed refers to the address of the memory location.
Examples:
• printf (“%f %f %f”, speed, time, distance);
• scanf (“%f %f”, &speed, &time);
C – Programs – scanf, printf, variables, constants
1. Write a C program to calculate the area of a rectangle, input given by user. L=4,
b=5
2. C program to find area and circumference of a circle of given radius. R=3
3. Write a C program that computes total interest earned on an investment that
pays a fixed rate of simple interest when the principal remains invested for a
specified number of years. Principal=Rs 1000/-, Time Period=3 years, Rate=8.5%.
4. Write a C
Program to Add, subtract, Multiply, Divide and find quotient & remainder of tw
o Numbers
.
5. Write a C Program to find Size of int, float, double and char in your System.
6. Write a C Program to Swap Two Numbers.
7. Write a C Program to find the total cost of items, for an item purchased 10
times.
C – Programs – scanf, printf, variables, constants
8. Write a C Program to find ASCII Value of a Character.
9. Write a C Program to find the sum and average of numbers.
10. Square and cube of a number, entered by the user.