0% found this document useful (0 votes)
4 views

C Keywords and Identifiers

Uploaded by

raisingnik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C Keywords and Identifiers

Uploaded by

raisingnik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

C Keywords and Identifiers

• 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
• White space Characters
• Blank space, newline, horizontal tab, carriage return and form feed.
Special Characters
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;

Here, is a keyword that indicates


int money is a variable of type (integer).
int

As C is a case sensitive language, all keywords must be written in lowercase. Here is a


list of all keywords allowed in ANSI C.
C Keywords

C Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union

continue for signed void

do if static while
default goto sizeof volatile

const float short unsigned


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;
• Here, money and accountBalance are identifiers.

• Also remember, identifier names must be different from keywords. You


cannot use int as an identifier because int is a keyword.
Rules for naming identifiers

• A valid identifier can have letters (both uppercase and lowercase letters), digits
and underscores.

• The first letter of an identifier should be either a letter or an underscore.

• You cannot use keywords as identifiers.

• There is no rule on how long an identifier can be.

• However, you may run into problems in some compilers if the identifier is longer
than 31 characters
C Variables, Constants and Literals

• Variables : is a container (storage area) to hold data.

• To indicate the storage area, each variable should be given a unique name (identifier).

• Variable names are just the symbolic representation of a memory location.


• For example:

• int playerScore = 95;

• Here, playerScore is a variable of int type. Here, the variable is assigned an integer value 95.

• The value of a variable can be changed, hence the name variable.


• char ch = 'a';

• // some code

• ch = 'l';
Rules for naming a variable

• A variable name can only have letters (both uppercase and lowercase

letters), digits and underscore.

• The first letter of a variable should be either a letter or an underscore.


Why C is strongly typed language?
• C is a strongly typed language. This means that the variable type cannot be changed once it is
declared. For example:
• int number = 5; // integer variable
• number = 5.5; // error
• double number; // error

• Here, the type of number variable is int.

• You cannot assign a floating-point (decimal) value 5.5 to this variable.

• Also, you cannot redefine the data type of the variable to double. By the way, to store the
decimal values in C, you need to declare its type to either double or float.
Literals

• Literals are data used for representing fixed values.


• The values assigned to each constant variables are referred to as the literals.
• Generally, both terms, constants and literals are used interchangeably.
For eg, “const int = 5;“, is a constant expression and the value 5 is referred to
as constant integer literal.

• They can be used directly in the code. For example: 1, 2.5, 'c' etc.

• Here, 1, 2.5 and 'c' are literals. Why? You cannot assign different values to
these terms.
Integers

• An integer is a numeric literal(associated with numbers) without any fractional or


exponential part. There are three types of integer literals in C programming:
• decimal (base 10)

• octal (base 8)

• hexadecimal (base 16)


• For example:
• Decimal: 0, -9, 22 etc
• Octal: 021, 077, 033 etc
• Hexadecimal: 0x7f, 0x2a, 0x521 etc
• In C programming, octal starts with a 0, and hexadecimal starts with a 0x.
Floating-point Literals

• A floating-point literal is a numeric literal that has either a fractional


form or an exponent form. For example:
• -2.0
• 0.0000234
• -0.22E-5
• Note: E-5 = 10-5
Characters

• A character literal is created by enclosing a single character inside


single quotation marks. For example: 'a', 'm', 'F', '2', '}' etc.
Escape Sequences

• Sometimes, it is necessary to use characters that cannot be typed or


has special meaning in C programming.
• For example: newline(enter), tab, question mark etc.
• In order to use these characters, escape sequences are used.
Escape Sequences

• Escape Sequences Character


• \b Backspace
• \f Form feed
• \n Newline
• \r Return
• \t Horizontal tab
• \v Vertical tab
• \\ Backslash
• \' Single quotation mark
• \" Double quotation mark
• \? Question mark
• \0 Null character
String Literals
• A string literal is a sequence of characters enclosed in double-quote
marks. For example:

• "good" //string constant


• "" //null string constant
•" " //string constant of six white space
• "x" //string constant having a single character.
• "Earth is round\n" //prints string with a newline
Constants

• If you want to define a variable whose value cannot be changed, you can use
the const keyword. This will create a constant. For example,

• const double PI = 3.14;


• Notice, we have added keyword const.

• Here, PI is a symbolic constant; its value cannot be changed.

• const double PI = 3.14;


• PI = 2.9; //Error
C Data Types
• In C programming, data types are declarations for variables.
• This determines the type and size of data associated with variables.
For example,

• int myVar;
• Here, myVar is a variable of int (integer) type. The size of int is 4 bytes.
Basic types
int

• Integers are whole numbers that can have both zero, positive and negative values but no decimal
values. For example, 0, -5, 10

• We can use int for declaring an integer variable.

• int id;
• Here, id is a variable of type integer.

• You can declare multiple variables at once in C programming. For example,

• int id, age;


• The size of int is usually 4 bytes (32 bits). And, it can take 232 distinct states from -2147483648 to
2147483647.
float and double

• float and double are used to hold real numbers.

• float salary;
• double price;
• In C, floating-point numbers can also be represented in exponential. For example,

• float normalizationFactor = 22.442e2;


• What's the difference between float and double?

• The size of float (single precision float data type) is 4 bytes. And the size of double (double precision
float data type) is 8 bytes.
• 1.123456789101112131415
• 1.12345678910111213141123456789101112131415
char

• Keyword char is used for declaring character type variables. For


example,

• char test = 'h';


• The size of the character variable is 1 byte.
void

• void is an incomplete type. It means "nothing" or "no type". You can


think of void as absent.

• For example, if a function is not returning anything, its return type


should be void.

• Note that, you cannot create variables of void type.


short and long

• If you need to use a large number, you can use a type specifier long. Here's how:

• long a;
• long long b;
• long double c;
• Here variables a and b can store integer values. And, c can store a floating-point number.

• If you are sure, only a small integer ([−32,767, +32,767] range) will be used, you can use
short.

• short d;
T he sizeof() operator.

• #include <stdio.h>
• int main()
• {
• short a;
• long b;
• long long c;
• long double d;

• printf("size of short = %d bytes\n", sizeof(a));


• printf("size of long = %d bytes\n", sizeof(b));
• printf("size of long long = %d bytes\n", sizeof(c));
• printf("size of long double= %d bytes\n", sizeof(d));
• return 0;
• }
signed and unsigned

• In C, signed and unsigned are type modifiers. You can alter the data
storage of a data type by using them. For example,

• unsigned int x;
• int y;
• Here, the variable x can hold only zero and positive values because we
have used the unsigned modifier.

• Considering the size of int is 4 bytes, variable y can hold values from -231
to 231-1, whereas variable x can hold values from 0 to 232-1.
Derived Data Types

• Data types that are derived from fundamental data types are derived
types.

• For example: arrays, pointers, function types, structures, etc.


C Output
• In C programming, printf() is one of the main output function. The function
sends formatted output to the screen. For example,
• Example 1: C Output
• #include <stdio.h>
• int main()
•{
• // Displays the string inside quotations
• printf("C Programming");
• return 0;
•}
How does this program work?
• All valid C programs must contain the main() function. The code execution begins from
the start of the main() function.

• The printf() is a library function to send formatted output to the screen. The function
prints the string inside quotations.

• To use printf() in our program, we need to include stdio.h header file using the #include
<stdio.h> statement.

• The return 0; statement inside the main() function is the "Exit status" of the program.
It's optional.
Example 2: Integer Output

• #include <stdio.h>
• int main()
• {
• int testInteger = 5;
• printf("Number = %d", testInteger);
• return 0;
• }
• Output

• Number = 5
• We use %d format specifier to print int types. Here, the %d inside the quotations will be
replaced by the value of testInteger.
Example 3: float and double Output
• #include <stdio.h>
• int main()
• {
• float number1 = 13.5;
• double number2 = 12.4;

• printf("number1 = %f\n", number1);


• printf("number2 = %lf", number2);
• return 0;
• }
• Output

• number1 = 13.500000
• number2 = 12.400000
• To print float, we use %f format specifier. Similarly, we use %lf to print double values.
Example 4: Print Characters

• #include <stdio.h>
• int main()
• {
• char chr = 'a';
• printf("character = %c", chr);
• return 0;
• }
• Output

• character = a
C Input
• In C programming, scanf() is one of the commonly used function to take input from the user.
• The scanf() function reads formatted input from the standard input such as keyboards.
• Example 5: Integer Input/Output
• #include <stdio.h>
• int main()
• {
• int testInteger;
• printf("Enter an integer: ");
• scanf("%d", &testInteger);
• printf("Number = %d",testInteger);
• return 0;
• }
• Output

• Enter an integer: 4
• Number = 4
Example 6: Float and Double Input/Output
• #include <stdio.h>
• int main()
• {
• float num1;
• double num2;

• printf("Enter a number: ");


• scanf("%f", &num1);
• printf("Enter another number: ");
• scanf("%lf", &num2);

• printf("num1 = %f\n", num1);


• printf("num2 = %lf", num2);

• return 0;
• }
Example 7: C Character I/O
• #include <stdio.h>
• int main()
• {
• char chr;
• printf("Enter a character: ");
• scanf("%c",&chr);
• printf("You entered %c.", chr);
• return 0;
• }
• Output

• Enter a character: g
• You entered g
• When a character is entered by the user in the above program, the character itself is not stored. Instead, an integer value (ASCII value) is stored.
• And when we display that value using %c text format, the entered character is displayed. If we use %d to display the character, it's ASCII value is printed.
Example 8: ASCII Value
• #include <stdio.h>
• int main()
• {
• char chr;
• printf("Enter a character: ");
• scanf("%c", &chr);

• // When %c is used, a character is displayed


• printf("You entered %c.\n",chr);

• // When %d is used, ASCII value is displayed


• printf("ASCII value is %d.", chr);
• return 0;
• }
I/O Multiple Values
• #include <stdio.h>
• int main()
• {
• int a;
• float b;

• printf("Enter integer and then a float: ");



• // Taking multiple inputs
• scanf("%d%f", &a, &b);

• printf("You entered %d and %f", a, b);


• return 0;
• }
• Output
C Arithmetic Operators
• An arithmetic operator performs mathematical operations such as
addition, subtraction, multiplication, division etc on numerical
values (constants and variables).
Operator Meaning of Operator

+ addition or unary plus

- subtraction or unary minus

* multiplication

/ division

remainder after division


%
(modulo division)
Example 1: Arithmetic Operators
• // Working of arithmetic operators
• #include <stdio.h>
• int main()
• {
• int a = 9,b = 4, c;

• c = a+b;
• printf("a+b = %d \n",c);
• c = a-b;
• printf("a-b = %d \n",c);
• c = a*b;
• printf("a*b = %d \n",c);
• c = a/b;
• printf("a/b = %d \n",c);
• c = a%b;
• printf("Remainder when a divided by b = %d \n",c);
• return 0;
• }
C Increment and Decrement Operators

• C programming has two operators increment ++ and decrement -- to change the value of
an operand (constant or variable) by 1.

• Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1.


These two operators are unary operators, meaning they only operate on a single operand.

• If you use the ++ operator as prefix like: ++var. The value of var is incremented by 1 then,
it returns the value.

• If you use the ++ operator as postfix like: var++. The original value of var is returned first
then, var is incremented by 1.
Example 2: Increment and Decrement Operators

• // Working of increment and decrement operators


• #include <stdio.h>
• int main()
• {
• int a = 10, b = 100;
• float c = 10.5, d = 100.5;

• printf("++a = %d \n", ++a);


• printf("--b = %d \n", --b);
• printf("++c = %f \n", ++c);
• printf("--d = %f \n", --d);

• return 0;
• }
C Assignment Operators

• An assignment operator is used for assigning a value to a variable. The


most common assignment operator is
• Operator Example Same as
•= a=b a=b
• += a += b a = a+b
• -= a -= b a = a-b
• *= a *= b a = a*b
• /= a /= b a = a/b
• %= a %= b a = a%b=
Example 3: Assignment Operators
• // Working of assignment operators

• #include <stdio.h>

• int main()

• {

• int a = 5, c;

• c = a; // c is 5

• printf("c = %d\n", c);

• c += a; // c is 10

• printf("c = %d\n", c);

• c -= a; // c is 5

• printf("c = %d\n", c);

• c *= a; // c is 25

• printf("c = %d\n", c);

• c /= a; // c is 5

• printf("c = %d\n", c);

• c %= a; // c = 0

• printf("c = %d\n", c);

• return 0;

• }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy