C Keywords and Identifiers
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
• 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;
C Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
do if static while
default goto sizeof volatile
• A valid identifier can have letters (both uppercase and lowercase letters), digits
and underscores.
• However, you may run into problems in some compilers if the identifier is longer
than 31 characters
C Variables, Constants and Literals
• To indicate the storage area, each variable should be given a unique name (identifier).
• Here, playerScore is a variable of int type. Here, the variable is assigned an integer value 95.
• // some code
• ch = 'l';
Rules for naming a variable
• A variable name can only have letters (both uppercase and lowercase
• 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
• 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
• octal (base 8)
• 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,
• 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
• int id;
• Here, id is a variable of type integer.
• float salary;
• double price;
• In C, floating-point numbers can also be represented in exponential. For example,
• 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
• 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;
• 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.
• 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;
• 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;
• 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);
* multiplication
/ division
• C programming has two operators increment ++ and decrement -- to change the value of
an operand (constant or variable) by 1.
• 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
• return 0;
• }
C Assignment Operators
• #include <stdio.h>
• int main()
• {
• int a = 5, c;
• c = a; // c is 5
• c += a; // c is 10
• c -= a; // c is 5
• c *= a; // c is 25
• c /= a; // c is 5
• c %= a; // c = 0
• return 0;
• }