C Keywords and Identifiers 1
C Keywords and Identifiers 1
Identifiers
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;
Keywords in C Language
continu
for signed void
e
Do if static while
Default goto sizeof volatile
Along with these keywords, C supports other numerous keywords depending upon the
compiler.
All these keywords, their syntax and application will be discussed in their respective
topics. However, if you want a brief overview on these keywords without going further,
visit list of all keywords in C programming.
C Identifiers
Identifier refers to name given to entities such as variables, functions, structures etc.
Identifier must be unique. They are created to give unique name to a entity to identify it
during the execution of the program. For example:
int money;
double accountBalance;
Also remember, identifier names must be different from keywords. You cannot
use int as an identifier because int is a keyword.
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:
Here, playerScore is a variable of integer type. The variable is assigned value: 95.
In C programming, you have to declare a variable before you can use it.
C is a strongly typed language. What this means it that, the type of a variable cannot be
changed.
Visit this page to learn more about different types of data a variable can store.
Constants/Literals
A constant is a value or an identifier whose value cannot be altered in a program. For
example: 1, 2.5, "C programming is easy", etc.
Here, PI is a constant. Basically what it means is that, PI and 3.14 is same for this
program.
1. Integer constants
An integer constant is a numeric constant (associated with number) without any
fractional or exponential part. There are three types of integer constants in C
programming:
For example:
In C programming, octal constant starts with a 0 and hexadecimal constant starts with a
0x.
2. Floating-point constants
A floating point constant is a numeric constant that has either a fractional form or an
exponent form. For example:
-2.0
0.0000234
-0.22E-5
3. Character constants
A character constant is a constant which uses single quotation around characters. For
example: 'a', 'l', 'm', 'F'
4. Escape Sequences
Sometimes, it is necessary to use characters which 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 sequence is used.
For example: \n is used for newline. The backslash ( \ ) causes "escape" from the
normal way the characters are interpreted by the compiler.
Escape Sequences
Escape
Character
Sequences
\b Backspace
Escape Sequences
Escape
Character
Sequences
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\? Question mark
\0 Null character
5. String constants
String constants are the constants which are enclosed in a pair of double-quote marks.
For example:
Data types simply refers to the type and size of data associated
with variables and functions.
Data types in C
1. Fundamental Data Types
o Integer types
o Floating type
o Character type
2. Derived Data Types
o Arrays
o Pointers
o Structures
o Enumeration
In C programming, keyword int is used for declaring integer variable. For example:
int id;
Here, id is a variable of type integer.
The size of int is either 2 bytes(In older PC's) or 4 bytes. If you consider an integer
having size of 4 byte( equal to 32 bits), it can take 232 distinct states as: -231,-231+1, ...,-2,
-1, 0, 1, 2, ..., 231-2, 231-1. If you try to store larger number than 231-1, i.e,+2147483647
and smaller number than -231, i.e, -2147483648, program will not run correctly.
Similarly, int of 2 bytes, it can take 216 distinct states from -215 to 215-1.
float accountBalance;
double bookPrice;
Two commonly used functions for I/O (Input/Output) are printf() and scanf().
The scanf() function reads formatted input from standard input (keyboard) whereas
the printf() function sends formatted output to the standard output (screen).
Output
C Programming
How this program works?
All valid C program must contain the main() function. The code execution begins from
the start of main() function.
The printf() is a library function to send formatted output to the screen.
The printf()function is declared in "stdio.h" header file.
Here, stdio.h is a header file (standard input output header file) and #include is a
preprocessor directive to paste the code from the header file when necessary. When the
compiler encounters printf() function and doesn't find stdio.h header file, compiler
shows error.
The return 0; statement is the "Exit status" of the program. In simple terms, program
ends.
Number = 5
Inside the quotation of printf() function, there is a format string "%d" (for integer). If the
format string matches the argument (testInteger in this case), it is displayed on the
screen.
Output
Enter an integer: 4
Number = 4
The scanf() function reads formatted input from the keyboard. When user enters an
integer, it is stored in variable testInteger.
Note the '&' sign before testInteger; &testInteger gets the address of testInteger and
the value is stored in that address.
Example #3: C Floats Input/Output
#include <stdio.h>
int main()
{
float f;
printf("Enter a number: ");
// %f format string is used in case of floats
scanf("%f",&f);
printf("Value = %f", f);
return 0;
}
Output
Value = 23.450000
The format string "%f" is used to read and display formatted in case of floats.
Output
Enter a character: g
You entered g.