0% found this document useful (0 votes)
50 views48 pages

Unit 1-Identifiers and Keywords, Data Types, Variables, and Constants

The document provides an introduction to programming in C. It discusses what C is, why it is useful to learn C, and its uses. C was created in 1972 and can be used to develop operating systems, databases, and other applications. It is portable, procedural, and a general purpose language. Learning C helps understand computer architecture and provides opportunities for open source work. The document outlines setting up a C development environment and covers basic C syntax, keywords, variables, data types, and outputting data.

Uploaded by

Leah Rachael
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)
50 views48 pages

Unit 1-Identifiers and Keywords, Data Types, Variables, and Constants

The document provides an introduction to programming in C. It discusses what C is, why it is useful to learn C, and its uses. C was created in 1972 and can be used to develop operating systems, databases, and other applications. It is portable, procedural, and a general purpose language. Learning C helps understand computer architecture and provides opportunities for open source work. The document outlines setting up a C development environment and covers basic C syntax, keywords, variables, data types, and outputting data.

Uploaded by

Leah Rachael
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/ 48

PROGRAMMING IN C

Introduction

 What is C?
 Why Learn C?
What is C?

 C is a general-purpose programming language created by Dennis Ritchie at the


Bell Laboratories in 1972.
 C can be used to develop software like operating systems (Windows and iOS) ,
databases, compilers, 3D movies and so on.
 C is strongly associated with UNIX, as it was developed to write the UNIX
operating system.
• C is a procedural Language, meaning instructions in a C program are executed step by
step.
• C is portable, you can move C programs from one platform to another, and run it
without any or minimal changes.
• C is a general purpose language, can be used to develop operating systems, embedded
systems, databases, and so on.
Why Learn C?

 It is one of the most popular programming language in the world


 If you know C, you will have no problem learning other popular programming
languages such as Java, Python, C++, C#, etc, as the syntax is similar
 C is very fast, compared to other programming languages, like Java and
Python
 C helps you to understand the internal architecture of a computer, how
computer stores and retrieves information.
 Opportunity to work on open source projects. Some of the largest open-
source projects such as Linux kernel, Python interpreter, SQLite database,
etc. are written in C programming.
C as a General-purpose Language

 Despite being old, C is used in a variety of applications. For example,


 Embedded Systems
 Operating System - Windows, Linux, OSX, Android, iOS
 Databases - PostgreSQL, Oracle, MySQL, MS SQL Server
 Other Uses - Network drivers, Compilers, Print spoolers
Setting up the production Environment

 You need a text editor and a compiler to write programs in C


 A compiler is used to translate the C code into a language that the computer will
understand
OR
 An IDE (Integrated Development Environment) used to edit and compile the
code.
 C is a case
sensitive language, all keywords
must be written in lowercase.
Syntax

 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 refers to name given to entities such as variables, functions,


structures etc.
 Identifiers must be unique. They are created to give a unique name to an
entity to identify it during the execution of the program. For example:
int money;
double accountBalance;
 money and accountBalance are 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

 Single new line  Multiple new lines

#include <stdio.h> #include <stdio.h>

int main() { int main() {


printf("Hello World!\n"); printf("Hello World!\nI am
printf("I am learning C."); learning C.\nAnd it is
return 0; awesome!");
} return 0;
}
C New Lines \n…

 Double line creates a white space

#include <stdio.h>

int main() {
printf("Hello World!\n\n");
printf("I am learning C.");
return 0;
}
Comments in C

 Single comment  Multiple comment

//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

 All C variables must be identified with unique names.


 These unique names are called identifiers.
 Identifiers can be short names (like x and y) or more descriptive names (age,
sum, totalVolume).
 It is recommended to use descriptive names in order to create understandable
and maintainable code:
// Good
int minutesPerHour = 60;

// OK, but not so easy to understand what m actually is


int m = 60;
Declaring (Creating) Variables

 To create or declare a variable, specify the type and assign it


a value:

 Syntax
 type variableName = value;

 E.g int num = 10;


Two ways of declaring variables

 Assigning a value  Not assigning a value

int myNum = 15; // Declare a variable


int myNum;

// Assign a value to the


variable
myNum = 15;
Outputting variables in C

 You cannot use the C output to display a variable.


 Use format specifiers together with the printf() function to tell the compiler
what type of data the variable is storing.
 Format specifier syntax
 “%character”.

 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:

Format Specifier Data Type

%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:

 int myNum = 15;


char myLetter = 'D';
printf("My number is %d and my letter is %c", myNum, myLetter);
Change Variable Values

 Note: If you assign a new value to an existing variable, it will


overwrite the previous value:
 int myNum = 15; // myNum is 15
myNum = 10; // Now myNum is 10
You can also assign the value of one
variable to another:
 int myNum = 15;

int myOtherNum = 23;

// Assign the value of myOtherNum (23) to myNum


myNum = myOtherNum;

// myNum is now 23, instead of 15


printf("%d", myNum);
Or copy values to empty variables:

 // Create a variable and assign the value 15 to it


int myNum = 15;

// Declare a variable without assigning it a value


int myOtherNum;

// Assign the value of myNum to myOtherNum


myOtherNum = myNum;

// myOtherNum now has 15 as a value


printf("%d", myOtherNum);
Add Variables Together

 To add a variable to another variable, you can use the + operator:


 int x = 5;
int y = 6;
int sum = x + y;
printf("%d", sum);
Declare Multiple Variables

 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("%d", sum); // Outputs


2
C Type Conversion

 convert the value of one data // Automatic


type to another type
conversion: float to
int
// Automatic int myInt = 9.99;
conversion: int to
float printf("%d", myInt); //
float myFloat = 9; 9

printf("%f", myFloat);
// 9.000000
Try

// Manual conversion: int to  int num1 = 5;


float int num2 = 2;
float sum = (float) num1 /
float sum = 5 / 2;
num2;
printf("%f", sum); // 2.000000
printf("%.1f", sum); // 2.5
C Constants

 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.

You might also like