We give a variable a meaningful name when we create it. Here are the rules that we must follow when naming it:
1. The name of the variable must not begin with a digit or special symbols.
2. A variable name can consist of digits, alphabets, and even special symbols such as an underscore ( _ ).
3. A variable name must not have any keywords, for instance, float, int, etc.
4. There must be no spaces or blanks in the variable name.
5. The C language treats lowercase and uppercase very differently, as it is case sensitive. Usually, we keep the name of the variable in the lower case.
Let us look at some of the examples,
int var1; // it is correct
int 1var; // it is incorrect – the name of the variable should not start using a number
int my_var1; // it is correct
int my$var // it is incorrect – no special characters should be in the name of the variable
char else; // there must be no keywords in the name of the variable
int my var; // it is incorrect – there must be no spaces in the name of the variable
int COUNT; // it is a new variable
int Count; // it is a new variable
int count; // it is a valid variable name
This section describes the elements of the C programming language, including the names, numbers, and characters used to construct a C program. The ANSI C syntax labels these components tokens.
This section explains how to define tokens and how the compiler evaluates them.
Tokens Comments Keywords Identifiers Constants String literals Punctuation and special characters
A token is source-program text that the compiler doesn't break down into component elements.
keyword
identifier
constant
string-literal
operator
punctuator
The keywords, identifiers, constants, string literals, and operators described in this section are examples of tokens. Punctuation characters such as brackets ([ ]), braces ({ }), parentheses ( ( ) ), and commas (,) are also tokens.
A "comment" is a sequence of characters beginning with a forward slash/asterisk combination (/*) that is treated as a single white-space character by the compiler and is otherwise ignored. A comment can include any combination of characters from the representable character set, including newline characters, but excluding the "end comment" delimiter (*/). Comments can occupy more than one line but can't be nested.
Comments can appear anywhere a white-space character is allowed. Since the compiler treats a comment as a single white-space character, you can't include comments within tokens. The compiler ignores the characters in the comment.
Example: Multiline comment
/* Comments can contain keywords such as
for and while without generating errors. */
Example : single line comment
// This is a valid comment
Alphabet
Uppercase: A B C ................................... X Y Z
Lowercase: a b c ...................................... x y z
Digits
0 1 2 3 4 5 6 7 8 9
Special symbols
| , | < | > | . | _ |
| ( | ) | ; | $ | : |
| % | [ | ] | # | ? |
| ' | & | { | } | " |
| ^ | ! | * | / | | |
| - | \ | ~ | + |
White space Characters Blank space, newline, horizontal tab, carriage return and form feed. |
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;
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.
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 |
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.
The identifier is only used to identify an entity uniquely in a program at the time of execution whereas, a variable is a name given to a memory location, that is used to hold a value. Variable is only a kind of identifier, other kinds of identifiers are function names, class names, structure names, etc.
int, while etc. as identifiers.You can choose any name as an identifier if you follow the above rule, however, give meaningful names to identifiers that make sense.
Input and output functions are available in the C language to perform the most common tasks. In every C program, three basic functions take place – accepting of data as input, the processing of data, and the generation of output. The acceptance of data refers to input and the presentation of data refers to the output. The C program accepts input from the keyboard and displays output on the screen.
The C program performs input and output operations using different input and output functions
The formatted functions basically present or accept the available data (input) in a specific format. The standard library in C contains various functions for the input-output operations. The scanf() and printf() out of these functions help a programmer format the functions in their desired format. The program can use these functions for reading any form of data, like a real number, an integer, a character, and many more.
We use the format specifiers when we are trying to print the values available with different data types with the use of printf() statement. As a matter of fact, we also use these format specifiers when taking the input from the users with the use of the scanf() function. It is because, here, we must let the program know what types of input it should be expecting from its user.
| Format Specifier | Format Specifier |
|---|---|
| %d, %i | int |
| %c | char |
| %lf | double |
| %f | float |
Variable is just a memory allocation where we are assign/store the different type of data
Text