Programming Fundamentals Module II- C Programming Elements Day 3- Character Sets, Keywords, Constants S
Total Page:16
File Type:pdf, Size:1020Kb
CMS-A-CC-1-2-TH: Programming Fundamentals Module II- C Programming elements Day 3- Character Sets, Keywords, Constants S 1. What are character sets in C? As every language contains a set of characters used to construct words, statements, etc., C language also has a set of characters which include alphabets, digits, and special symbols. C language supports a total of 256 characters. Every C program contains statements. These statements are constructed using words and these words are constructed using characters from C character set. C language character set contains the following set of characters... 1. Alphabets 2. Digits 3. Special Symbols Alphabets C language supports all the alphabets from the English language. Lower and upper case letters together support 52 alphabets. lower case letters - a to z UPPER CASE LETTERS - A to Z Digits C language supports 10 digits which are used to construct numerical values in C language. Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Special Symbols C language supports a rich set of special symbols that include symbols to perform mathematical operations, to check conditions, white spaces, backspaces, and other special symbols. Special Symbols - ~ @ # $ % ^ & * ( ) _ - + = { } [ ] ; : ' " / ? . > , < \ | tab newline space NULL bell backspace verticaltab etc., Every character in C language has its equivalent ASCII (American Standard Code for Information Interchange) value. CMS-A-CC-1-2-TH: Programming Fundamentals Module II- C Programming elements Day 3- Character Sets, Keywords, Constants S Commonly used characters in C with thier ASCII values C program to print all the characters of C character Set #include<stdio.h> main() { int i; printf("ASCII ==> Character\n"); for(i = -128; i <= 127; i++) printf("%d ==> %c\n", i, i); } 2. What are ‘Keywords’ in C? Keywords are specific reserved words in C each of which has a specific feature associated with it. Almost all of the words which help us use the functionality of the C language are included in the list of keywords. So you can imagine that the list of keywords is not going to be a small one! CMS-A-CC-1-2-TH: Programming Fundamentals Module II- C Programming elements Day 3- Character Sets, Keywords, Constants S There are a total of 44 keywords in C (C89 – 32, C99 – 5, C11 – 7): auto extern short while break float signed _Alignas case for sizeof _Alignof char goto static _Atomic const if struct _Bool continue inline switch _Complex default int typedef _Generic do long union _Imaginary double register unsigned _Noreturn else restrict void _Static_assert enum return volatile _Thread_local Most of these keywords have already been discussed in the various sub-sections of the C language, like Data Types, Storage Classes, Control Statements, Functions etc. Some of the other keywords which allow us to use the basic functionality of C: const: const can be used to declare constant variables. Constant variables are variables which, when initialized, can’t change their value. Or in other words, the value assigned to them cannot be modified further down in the program. Syntax: const data_type var_name = var_value; Note: Constant variables must be initialized during their declaration. const keyword is also used with pointers. Please refer the const qualifier in C for understanding the same. extern: extern simply tells us that the variable is defined elsewhere and not within the same block where it is used. Basically, the value is assigned to it in a different block and this can be overwritten/changed in a different block as well. So an extern variable is nothing but a global variable initialized with a legal value where it is declared in order to be used elsewhere. It can be accessed within any function/block. Also, a normal global variable can be made extern as well by placing the ‘extern’ keyword before its declaration/definition in any function/block. This basically signifies that we are not initializing a new variable but instead we are using/accessing the global variable only. The main purpose of using extern variables is that they can be accessed between two different files which are part of a large program. Syntax: extern data_type var_name = var_value; static: static keyword is used to declare static variables, which are popularly used while writing programs in C language. Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve the value of their last use in their scope. So we can say that they are initialized only once and exist till the termination of the program. Thus, no new memory is CMS-A-CC-1-2-TH: Programming Fundamentals Module II- C Programming elements Day 3- Character Sets, Keywords, Constants S allocated because they are not re-declared. Their scope is local to the function to which they were defined. Global static variables can be accessed anywhere within that file as their scope is local to the file. By default, they are assigned the value 0 by the compiler. Syntax: static data_type var_name = var_value; void: void is a special data type. But what makes it so special? void, as it literally means, is an empty data type. It means it has nothing or it holds no value. For example, when it is used as the return data type for a function it simply represents that the function returns no value. Similarly, when its added to a function heading, it represents that the function takes no arguments. Note: void also has a significant use with pointers. Please refer to the void pointer in C for understanding the same. typedef: typedef is used to give a new name to an already existing or even a custom data type (like a structure). It comes in very handy at times, for example in a case when the name of the structure defined by you is very long or you just need a short-hand notation of a per-existing data type. 3. What are the “Constants” in C? Constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are enumeration constants as well. Constants are treated just like regular variables except that their values cannot be modified after their definition. Integer Literals An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal. An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order. Here are some examples of integer literals − 212 /* Legal */ 215u /* Legal */ 0xFeeL /* Legal */ 078 /* Illegal: 8 is not an octal digit */ 032UU /* Illegal: cannot repeat a suffix */ Following are other examples of various types of integer literals − 85 /* decimal */ 0213 /* octal */ CMS-A-CC-1-2-TH: Programming Fundamentals Module II- C Programming elements Day 3- Character Sets, Keywords, Constants S 0x4b /* hexadecimal */ 30 /* int */ 30u /* unsigned int */ 30l /* long */ 30ul /* unsigned long */ Floating-point Literals A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point literals either in decimal form or exponential form. While representing decimal form, you must include the decimal point, the exponent, or both; and while representing exponential form, you must include the integer part, the fractional part, or both. The signed exponent is introduced by e or E. Here are some examples of floating-point literals − 3.14159 /* Legal */ 314159E-5L /* Legal */ 510E /* Illegal: incomplete exponent */ 210f /* Illegal: no decimal or exponent */ .e55 /* Illegal: missing integer or fraction */ Character Constants Character literals are enclosed in single quotes, e.g., 'x' can be stored in a simple variable of char type. A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g., '\t'), or a universal character (e.g., '\u02C0'). There are certain characters in C that represent special meaning when preceded by a backslash for example, newline (\n) or tab (\t). String Literals String literals or constants are enclosed in double quotes "". A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters. You can break a long line into multiple lines using string literals and separating them using white spaces. Defining Constants There are two simple ways in C to define constants − ⚫ Using #define preprocessor. ⚫ Using const keyword. The #define Preprocessor Given below is the form to use #define preprocessor to define a constant − CMS-A-CC-1-2-TH: Programming Fundamentals Module II- C Programming elements Day 3- Character Sets, Keywords, Constants S #define identifier value The following example explains it in detail − #include <stdio.h> #define LENGTH 10 #define WIDTH 5#define NEWLINE '\n' int main() { int area; area = LENGTH * WIDTH; printf("value of area : %d", area); printf("%c", NEWLINE); return 0;} When the above code is compiled and executed, it produces the following result − value of area : 50 The const Keyword You can use const prefix to declare constants with a specific type as follows − const type variable = value; The following example explains it in detail − #include <stdio.h> int main() { const int LENGTH = 10; const int WIDTH = 5; const char NEWLINE = '\n'; int area; area = LENGTH * WIDTH; printf("value of area : %d", area); printf("%c", NEWLINE); return 0;} When the above code is compiled and executed, it produces the following result − value of area : 50 .