Posts

Introduction to C programming

Image
C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was first implemented on the DEC PDP-11 computer in 1972. The main features of C language include low-level access to memory, simple set of keywords, and clean style, these features make C language suitable for system programming like operating system or compiler development.Many later languages have borrowed syntax/features directly or indirectly from C language. Like syntax of Java, PHP, JavaScript and many other languages is mainly based on C language. C++ is nearly a superset of C language having object oriented features. Ken Thompson  (left) with  Dennis Ritchie  (right, the inventor of the C programming language) In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard.When C was first written the standard was set by its authors Kernighan and Ri...

Primitive and Derived Data Type

 In the C programming language, data types are essential because they tell the compiler what kind of data a variable can hold . Choosing the correct data type improves the efficiency, clarity, and correctness of your program. Data types in C are broadly categorized into two: Primitive (Basic) Data Types Derived Data Types 🟢 1. Primitive (Basic) Data Types These are the fundamental building blocks of data in C. They are built into the language and represent the most basic kinds of data. ➤ Common Primitive Data Types: Data Type Description Example int Integer values (whole numbers) int age = 25; float Single-precision decimal values float price = 99.5; double Double-precision decimal values double pi = 3.14159; char Single characters char grade = 'A'; 📝 Note: Each type uses different memory sizes and has specific ranges . For example, int typically uses 4 bytes and ranges from -2,147,483,648 to 2,147,483,647 . 🔵 2. Derived Data Types Derived data...

Enumerated Data Type

Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain.The symbolically declared members are integer constants. The keyword  enum  is used to declare an enumeration type. The general construct used to declare an enumeration type is enum tag_name {member1, member2, member3,..,member} variable1, variable2,..,variable n In this declaration, either tag_name or variable may be omitted or both may be present. But at least one of them must exist in this declaration construct. The members are integer constants. By default the first member member1 assigned value 0 member2 assigned value 1 and so on. Eg: enum week{Mon, Tue, Wed,Thu,Fri,Sat}; enum week day; // Or enum week{Mon, Tue, Wed,Thu,Fri,Sat}day; #include <stdio.h> enum day {sunday, monday, tuesday, wednesday, thursday, friday, saturday}; int main() { enum day d = thursday; printf("The day number stored in d is %d", ...

Data Types and Qualifiers in C

Image
There are four data types in C language. They are, Types Data Types   Basic data types   int, char, float, double   Enumeration data type   enum   Derived data type   pointer, array, structure, union   Void data type   void Here we will discuss only basic data types. Each variable in C has an associated data type. Each data type requires different amounts of memory and has some specific operations which can be performed over it. Let us briefly describe them one by one:   Following are the examples of some very common primitive data types used in C:   char:  The most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers. int:  As the name suggests, an int variable is used to store an integer. float:  It is used to store decimal numbers (numbers with floating point value) with single precision. double:  It is used to store decimal numbers (numbers with floating p...

Keywords and Identifiers in C

Image
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, int is a keyword that indicates 'money' is a variable of type integer. 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.There are  32 keywords . Keywords In C Along with these keywords, C supports other numerous keywords depending upon the compiler. 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;  Here, money and accountBalance are identifiers.  Also remember, identifier names must be different from keywords. You cannot use 'int...

Escape Sequences

In C programming language, there are 256 numbers of characters in character set. The entire character set is divided into 2 parts i.e. the ASCII characters set and the extended ASCII characters set. But apart from that, some other characters are also there which are not the part of any characters set, known as ESCAPE characters. List of Escape Sequences \a Alarm or Beep \b Backspace \f Form Feed \n New Line \r Carriage Return \t Tab (Horizontal) \v Vertical Tab \\ Backslash \' Single Quote \" Double Quote \? Question Mark \ooo octal number \xhh hexadecimal number \0 Null

Variables and constants in C

Image
Variables In programming, a variable 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 integer type. The variable is assigned value: 95. The value of a variable can be changed, hence the name 'variable'. In C programming, you have to declare a variable before you can use it.   Rules for naming a variable in C   A variable name can have letters (both uppercase and lowercase letters), digits and underscore only. The first letter of a variable should be either a letter or an underscore. However, it is discouraged to start variable name with an underscore. It is because variable name that starts with an underscore can conflict with system name and may cause error. There is no rule on how long a variable can be. However, only the first 31 characters of a variab...