Unit 1-Identifiers and Keywords, Data Types, Variables, and Constants
Unit 1-Identifiers and Keywords, Data Types, Variables, and Constants
Introduction
What is C?
Why Learn C?
What is C?
Syntax is a set of rules that defines how a valid program statement should be
arranged or written.
Syntax help to control the structure of the symbols, punctuation, and words of a
programming language.
Syntax…
1. #include <stdio.h> is a header file library
that lets us work with input and output
#include <stdio.h> functions.
int main() { 2. A blank line. C ignores white space.
printf("Hello World!"); 3. Int main() is a function and any code inside
return 0; its curly brackets {} will be executed.
}
4. int main is a function that returns some
integer even ‘0’ at the end of the program
execution. ‘0’ represents the successful
execution of a program.
5. printf() is a function used to output/print
text to the screen.
Note: every c statement ends with a 6. return 0 ends the main() function.
semicolon ;
C code block example
#include <stdio.h>
int main() {
printf("Hello World!");
printf("I am learning C.");
return 0;
}
UNIT 1: UNDERSTANDING THE BASIC
CONCEPT OF C
Keywords and Identifiers
Data Types
Constants
Variables
Arithmetic
Unary
Relational and Logical, Assignment and Conditional
Bitwise Operator
C Keywords
Keywords are predefined, reserved words used in programming that have special
meanings to the compiler.
Keywords are part of the syntax and they cannot be used as an identifier. For example:
int money;
int is a keyword that indicates money is a variable of type int (integer).
List of all keywords allowed in ANSI C.
C Identifiers
Identifier names must be different from keywords. You cannot use int as an
identifier because int is a reserved C keyword.
Rules for naming identifiers
A valid identifier can have letters (both uppercase and lowercase letters),
digits and underscores.
Names can contain letters, digits and underscores
Names must begin with a letter or an underscore (_)
Names are case sensitive (myVar and myvar are different variables)
Names cannot contain whitespaces or special characters like !, #, %,
Reserved words (such as int) cannot be used as names
Give meaningful names to identifiers that make sense.
Character set
A character set is a set of alphabets, letters and some special characters that are valid in C
language.
Alphabets
Uppercase: A B C ................................... X Y Z
Lowercase: a b c ...................................... x y z
C accepts both lowercase and uppercase alphabets as variables and functions.
Digits
0123456789
Special Characters
White space Characters
Blank space
Newline
Horizontal tab
Carriage return
Form feed.
C New Lines \n
#include <stdio.h>
int main() {
printf("Hello World!\n\n");
printf("I am learning C.");
return 0;
}
Comments in C
//this is my single comment /*The code below will print the words
Hello World! to the screen, and it is
amazing*/
C Data Type
Data types are declarations for variables, that determines the type and size of data
associated with variables.
Format specifier is used inside the printf() function to display a variable with
a specified data type and size.
Basic Data Types
Basic Data Types
C Variables
Variables are containers for storing data values, like numbers and characters.
Examples of variable types defined with different keywords:
int - stores integers (whole numbers), without decimals, such as 123 or -123
float - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by
single quotes
C Variable Names
Syntax
type variableName = value;
Int: %d or %i
Char: %c
Float: %f
Basic Format Specifiers
There are different format specifiers for each data type. Here
are some of them:
%d or %i int
%f float
%lf double
%c char
%s strings (text)
Outputting variables in C example
#include <stdio.h>
int main() {
// Create variables
int myNum = 15; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
char myLetter = 'D'; // Character
// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
return 0;
To combine both text and a variable,
separate them with a comma inside the
printf() function:
int myNum = 15;
printf("My favorite number is: %d", myNum);
To print different types in a single printf()
function, you can use the following:
To declare more than one variable of the same type, use a comma-
separated list:
int x = 5, y = 6, z = 50;
printf("%d", x + y + z);
You can also assign the same value to
multiple variables of the same type:
int x, y, z;
x = y = z = 50;
printf("%d", x + y + z);
simplify variable names to match their
data type
// Student data
int studentID = 15;
int studentAge = 23;
float studentFee = 75.25;
char studentGrade = 'B';
// Print variables
printf("Student id: %d\n", studentID);
printf("Student age: %d\n", studentAge);
printf("Student fee: %f\n", studentFee);
printf("Student grade: %c", studentGrade);
Try
int x = 5;
int y = 2;
int sum = 5 / 2;
printf("%f", myFloat);
// 9.000000
Try
Const keyword is used when you don’t want to change existing variable values.
#include <stdio.h>
int main() {
const int myNum = 15; // myNum will always be 15
myNum = 10; // error: assignment of read-only variable
'myNum’
printf("%d\n", myNum);
printf("%f\n", myNum);
return 0;
}
#include <stdio.h>
int main() {
const int minutesPerHour = 60;
const float PI = 3.14;
printf("%d\n", minutesPerHour);
printf("%f\n", PI);
return 0;
}
Class activity
Create a program that assign a constant age 30 to a variable myage and print
it out as an integer.