C – Environment Setup

C – Environment Setup ”; Previous Next To start learning programming in C, the first step is to setup an environment that allows you to enter and edit the program in C, and a compiler that builds an executable that can run on your operating system. You need two software tools available on your computer, (a) The C Compiler and (b) Text Editor. The C Compiler The source code written in the source file is the human readable source for your program. It needs to be “compiled”, into machine language so that your CPU can actually execute the program as per the instructions given. There are many C compilers available. Following is a select list of C compilers that are widely used − GNU Compiler Collection (GCC) − GCC is a popular open-source C compiler. It is available for a wide range of platforms including Windows, macOS, and Linux. GCC is known for its wide range of features and support for a variety of C standards. Clang: Clang is an open-source C compiler that is part of the LLVM project. It is available for a variety of platforms including Windows, macOS, and Linux. Clang is known for its speed and optimization capabilities. Microsoft Visual C++ − Microsoft Visual C++ is a proprietary C compiler that is developed by Microsoft. It is available for Windows only. Visual C++ is known for its integration with the Microsoft Visual Studio development environment. Turbo C − Turbo C is a discontinued C compiler that was developed by Borland. It was popular in the early 1990s, but it is no longer widely used. The examples in this tutorial are compiled on the GCC compiler. The most frequently used and free available compiler is the GNU C/C++ compiler. The following section explains how to install GNU C/C++ compiler on various operating systems. We keep mentioning C/C++ together because GNU gcc compiler works for both C and C++ programming languages. Installation on UNIX/Linux If you are using Linux or UNIX, then check whether GCC is installed on your system by entering the following command from the command line − $ gcc -v If you have GNU compiler installed on your Ubuntu Linux machine, then it should print a message as follows − $ gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu Configured with: ../src/configure -v . . . Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 11.3.0 (Ubuntu 11.3.0-1ubuntu1~22.04) If GCC is not installed, then you will have to install it yourself using the detailed instructions available at https://gcc.gnu.org/install/ Installation on Mac OS If you use Mac OS X, the easiest way to obtain GCC is to download the Xcode development environment from Apple”s web site and follow the simple installation instructions. Once you have Xcode setup, you will be able to use GNU compiler for C/C++. Xcode is currently available at developer.apple.com/technologies/tools/ Installation on Windows To install GCC on Windows, you need to install MinGW. To install MinGW, go to the MinGW downloads page, https://www.mingw-w64.org/downloads/, and follow the link to the MinGW download page. Download the latest version of the MinGW installation program, mingw-w64-install.exe from here. While installing Min GW, at a minimum, you must install gcc-core, gcc-g++, binutils, and the MinGW runtime, but you may wish to install more. Add the bin subdirectory of your MinGW installation to your PATH environment variable, so that you can specify these tools on the command line by their simple names. After the installation is complete, you will be able to run gcc, g++, ar, ranlib, dlltool, and several other GNU tools from the Windows command line. Text Editor You will need a Text Editor to type your program. Examples include Windows Notepad, OS Edit command, Brief, Epsilon, EMACS, and vim or vi. The name and version of the text editors can vary on different operating systems. For example, Notepad will be used on Windows, and vim or vi can be used on windows as well as on Linux or UNIX. The files you create with your editor are called the source files and they contain the program source codes. The source files for C programs are typically named with the extension “.c”. Before starting your programming, make sure you have one text editor in place and you have enough experience to write a computer program, save it in a file, compile it and finally execute it. Using an IDE Using a general-purpose text editor such as Notepad or vi for program development can be very tedious. You need to enter and save the program with “.c” extension (say “hello.c”), and then compile it with the following command − gcc -c hello.c -o hello.o gcc -o hello.exe hello.o The executable file is then run from the command prompt to obtain the output. However, if the source code contains errors, the compilation will not be successful. Hence we need to repeatedly switch between the editor program and command terminal. To avoid this tedious process, we should an IDE (Integrated Development Environment). There are many IDEs available for writing, editing, debugging and executing C programs. Examples are CodeBlocks, NetBeans, VSCode, etc. CodeBlocks is a popular open-source IDE for C and C++. It is available for installation on various operating system platforms like Windows, Linux, MacOS. For Windows, download codeblocks-20.03mingw-setup.exe from https://www.codeblocks.org/downloads/binaries/ URL. This will install CodeBlocks as well as MinGW compiler on your computer. During the installation process, choose MinGW as the compiler to use. Example After the installation is complete, launch it and enter the following code − #include <stdio.h> int main() { /* my first program in C */ printf(“Hello, World! n”); return 0; } Output On executing this code, you

C – Tokens

Tokens in C ”; Previous Next A token is referred to as the smallest unit in the source code of a computer language such as C. The term token is borrowed from the theory of linguistics. Just as a certain piece of text in a language (like English) comprises words (collection of alphabets), digits, and punctuation symbols. A compiler breaks a C program into tokens and then proceeds ahead to the next stages used in the compilation process. The first stage in the compilation process is a tokenizer. The tokenizer divides the source code into individual tokens, identifying the token type, and passing tokens one at a time to the next stage of the compiler. The parser is the next stage in the compilation. It is capable of understanding the language”s grammar. identifies syntax errors and translates an error-free program into the machine language. A C source code also comprises tokens of different types. The tokens in C are of the following types − Character set Keyword tokens Literal tokens Identifier tokens Operator tokens Special symbol tokens Let us discuss each of these token types. C Character set The C language identifies a character set that comprises English alphabets – upper and lowercase (A to Z, as well as a to z), digits 0 to 9, and certain other symbols with a special meaning attached to them. In C, certain combinations of characters also have a special meaning attached to them. For example, n is known as a newline character. Such combinations are called escape sequences. Here is the character set of C language − Uppercase: A to Z Lowercase: a to z Digits: 0 to 9 Special characters: ! ” # $ % & ” ( ) * + – . : , ; ` ~ = < > { } [ ] ^ _ / A sequence of any of these characters inside a pair of double quote symbols ” and ” are used to represent a string literal. Digits are used to represent numeric literal. Square brackets are used for defining an array. Curly brackets are used to mark code blocks. Back slash is an escape character. Other characters are defined as operators. C Keywords In C, a predefined sequence of alphabets is called a keyword. Compared to human languages, programming languages have fewer keywords. To start with, C had 32 keywords, later on, few more were added in subsequent revisions of C standards. All keywords are in lowercase. Each keyword has rules of usage (in programming it is called syntax) attached to it. The C compiler checks whether a keyword has been used according to the syntax, and translates the source code into the object code. C Literals In computer programming terminology, the term literal refers to a textual representation of a value to be assigned to a variable, directly hard-coded in the source code. A numeric literal contains digits, a decimal symbol, and/or the exponentiation character E or e. The string literal is made up of any sequence of characters put inside a pair of double quotation symbols. A character literal is a single character inside a single quote. Arrays can also be represented in literal form by putting a comma-separated sequence of literals between square brackets. In C, escape sequences are also a type of literal. Two or more characters, the first being a backslash character, put inside a single quote form an escape sequence. Each escape sequence has a predefined meaning attached to it. C Identifiers In contrast to the keywords, the identifiers are the user-defined elements in a program. You need to define various program elements by giving them an appropriate name. For example, variable, constant, label, user-defined type, function, etc. There are certain rules prescribed in C, to form an identifier. One of the important restrictions is that a reserved keyword cannot be used as an identifier. For example, for is a keyword in C, and hence it cannot be used as an identifier, i.e., name of a variable, function, etc. C Operators C is a computational language. Hence a C program consists of expressions that perform arithmetic and comparison operations. The special symbols from the character set of C are mostly defined as operators. For example, the well-known symbols, +, −, * and / are the arithmetic operators in C. Similarly, < and > are used as comparison operators. C Special symbols Apart from the symbols defined as operators, the other symbols include punctuation symbols like commas, semicolons, and colons. In C, you find them used differently in different contexts. Similarly, the parentheses ( and ) are used in arithmetic expressions as well as in function definitions. The curly brackets are employed to mark the scope of functions, code blocks in conditional and looping statements, etc. Print Page Previous Next Advertisements ”;

C – Comments

Comments in C ”; Previous Next Using comments in a C program increases the readability of the code. You must intersperse the code with comments at appropriate places. As far as the compiler is concerned, the comments are ignored. In C, the comments are one or more lines of text, that the compiler skips while building the machine code. The comments in C play an important part when the program needs to be modified, especially by somebody else other than those who have written it originally. Putting comments is often not given importance by the programmer, but using them effectively is important to improve the quality of the code. Why to Use Comments in C Programming? Any programming language, including C, is less verbose as compared to any human language like English. It has far less number of keywords, C being one of the smallest languages with only 32 keywords. Hence, the instructions in a C program can be difficult to understand, especially for someone with non-programming background. The C language syntax also varies and is also complicated. Usually, the programmer tries to add complexity to optimize the code. However, it makes the code hard to understand. Comments provide a useful explanation and convey the intention behind the use of a particular approach. For example, take the case of the ?: operator in C, which is a shortcut for the if-else statement. So, instead of the following code − if (a % 2 == 0){ printf(“%d is Evenn”, a); } else { printf(“%d is Oddn”, a); } One can use the following statement − (a % 2 == 0) ? printf(“%d is Evenn”, a) : printf(“%d is Oddn”, a); Obviously, the second method is more complicated than the first. If useful comments are added, it makes easier to understand the intention and logic of the statement used. Types of Comments in C In C, there are two types of comments − Single-line comments Multi-line comments Single-line Comment in C The C++-style single-line comments were incorporated in C compilers with C99 standards. If any text begins with the // symbol, the rest of the line is treated as a comment. The text followed by a double oblique or forward slash [//] in a code is treated as a single-line comment. All that text after // is ignored by the C compiler during compilation. Unlike the multi-line or block comment, it need not be closed. Syntax of Single-line C Comment //comment text The // symbol can appear anywhere. It indicates that all the text following it till the end of the line is a comment. The subsequent line in the editor is again a place to write a valid C statement. Example: Single-line Comment in C Take a look at the following program and observe how we have used single-line comments inside its main function − /* Online C Compiler and Editor */ #include <stdio.h> #include <math.h> /*forward declaration of function*/ float area_of_square(float); float area_of_square(float side){ float area = pow(side,2); return area; } // main function – entire line is a comment int main(){ // variable declaration (this comment is after the C statement) float side = 5.50; float area = area_of_square(side); // calling a function printf (“Side = %5.2f Area = %5.2f”, side, area); return 0; } Output When you run this code, it will produce the following output − Side = 5.50 Area = 30.25 Multi-line Comment in C In early versions of C (ANSI C), any length of text put in between the symbols /* and */ is treated as a comment. The text may be spread across multiple lines in the code file. You may call it a multi-line comment. A block of consecutive lines is treated as a comment. Syntax of Multi-line C Comment The general structure of a multi-line comment is as follows − /* The comment starts here Line 1 Line 2 ….. ….. Comment ends here*/ For example − /* Example of a multi-line comment program to print Hello World using printf() function */ Obviously, since the comments are ignored by the compiler, the syntax rules of C language don”t apply to the comment text. Comments can appear anywhere in a program, at the top, in between the code, or at the beginning of a function or a struct declaration, etc. Example: Multi-line Comment in C In this example, we have a multi-line comment that explains the role of a particular user-defined function used in the given code − /* program to calculate area of square */ /* headers */ #include <stdio.h> #include <math.h> /* forward declaration of function */ float area_of_square(float); /* main function */ int main(){ /* variable declaration */ float side = 5.50; /* calling function */ float area = area_of_square(side); printf(“Side = %5.2f Area = %5.2f”, side, area); return 0; } /* User-defined function to calculate the area of square. It takes side as the argument and returns the area */ float area_of_square(float side){ float area = pow(side, 2); return area; } Output When you execute the code, it will produce the following output − Side = 5.50 Area = 30.25 While inserting a comment, you must make sure that for every comment starting with /*, there must be a corresponding */ symbol. If you start a comment with /* and fail to close it, then the compiler will throw an error. Note: A blocked comment or multi-line comment must be put inside /* and */ symbols, whereas a single-line comment starts with the // symbol and is effective till the

C – Overview

C Language – Overview ”; Previous Next 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 originally first implemented on the DEC PDP-11 computer in 1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard. The UNIX operating system, the C compiler, and essentially all UNIX application programs have been written in C. C has now become a widely used professional language for various reasons − Easy to learn Structured language It produces efficient programs It can handle low−level activities It can be compiled on a variety of computer platforms Facts about C C was invented to write an operating system called UNIX. C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute (ANSI). The UNIX OS was totally written in C. Today C is the most widely used and popular System Programming Language. Most of the state-of-the-art software have been implemented using C. Today”s most popular Linux OS and RDBMS MySQL have been written in C. Why Use C Language? C was initially used for system development work, particularly the programs that make-up the operating system. C was adopted as a system development language because it produces code that runs nearly as fast as the code written in assembly language. Some examples of the use of C might be − Operating Systems Language Compilers Assemblers Text Editors Print Spoolers Network Drivers Modern Programs Databases Language Interpreters Utilities C covers all the basic concepts of programming. It”s a base or mother programming language to learn object−oriented programming like C++, Java, .Net, etc. Many modern programming languages such as C++, Java, and Python have borrowed syntax and concepts from C. It provides fine-grained control over hardware, making it highly efficient. As a result, C is commonly used to develop system−level programs, like designing Operating Systems, OS kernels, etc., and also used to develop applications like Text Editors, Compilers, Network Drivers, etc. C programs are portable; hence they can run on different platforms without significant modifications. C has played a pivotal role as a fundamental programming language over the course of programming history. However, its popularity for application development has somewhat diminished in comparison to more contemporary languages. This may be attributed to its low−level characteristics and the existence of higher−level languages that offer a greater abundance of pre−existing abstractions and capabilities. Nevertheless, the use of the programming language C remains indispensable in domains where factors such as optimal performance, meticulous management of system resources, and the imperative need for portability hold utmost significance. Advantages of C Language The following are the advantages of C language − Efficiency and speed − C is known for being high−performing and efficient. It can let you work with memory at a low level, as well as allow direct access to hardware, making it ideal for applications requiring speed and economical resource use. Portable − C programs can be compiled and executed on different platforms with minimal or no modifications. This portability is due to the fact that the language has been standardized and compilers are available for use on various operating systems globally. Close to Hardware − C allows direct manipulation of hardware through the use of pointers and low−level operations. This makes it suitable for system programming and developing applications that require fine-grained control over hardware resources. Standard Libraries − For common tasks such as input/output operations, string manipulation, and mathematical computations, C comes with a large standard library which helps developers write code more efficiently by leveraging pre−built functions. Structured Programming − C helps to organize code into modular and easy−to−understand structures. With functions, loops, and conditionals, developers can produce clear code that is easy to maintain. Procedural Language − C follows a procedural paradigm that is often simpler and more straightforward for some types of programming tasks. Versatility − C language is a versatile programming language and it can be used for various types of software such as system applications, compilers, firmware, application software, etc. Drawbacks of C Language The following are the disadvantages/drawbacks of C language − Manual Memory Management − C languages need manual memory management, where a developer has to take care of allocating and deallocating memory explicitly. No Object−Oriented Feature − Nowadays, most of the programming languages support the OOPs features. But C language does not support it. No Garbage Collection − C language does not support the concept of Garbage collection. A developer needs to allocate and deallocate memory manually and this can be error-prone and lead to memory leaks or inefficient memory usage. No Exception Handling − C language does not provide any library for handling exceptions. A developer needs to write code to handle all types of expectations. Applications of C Language The following are the applications of C language − System Programming − C language is used to develop system software which are close to hardware such as operating systems, firmware, language translators, etc. Embedded Systems − C language is used in embedded system programming for a wide range of devices such as microcontrollers, industrial controllers, etc. Compiler and Interpreters − C language is very common to develop language compilers and interpreters. Database Systems − Since C language is efficient and fast for low-level memory manipulation. It is used for developing DBMS and RDBMS engines. Networking Software − C language is used to develop networking software such as

C – History

History of C Language ”; Previous Next C programming is a general-purpose, procedure-oriented programming language. It is both machine-independent and structured. C is a high-level programming language developed by Dennis Ritchie in the early 1970s. It is now one of the most popular and influential programming languages worldwide. C is popular for its simplicity, efficiency, and versatility. It has powerful features including low-level memory access, a rich set of operators, and a modular framework. Apart from its importance with respect to the evolution of computer programming technologies, the design of C language has a profound influence on most of the other programming languages that are in use today. The languages that are influenced by C include Java, PHP, JavaScript, C#, Python and many more. These languages have designed their syntax, control structures and other basic features from C. C supports different hardware and operating systems due to its portability. Generally, it is considered as a basic language and influenced many other computer languages. It is most widely used in academia and industry. C”s relevance and extensive acceptance make it crucial for prospective programmers. The history of the C programming language is quite fascinating and pivotal in the development of computer science and software engineering. Year wise development of programming is as follows − Overview of C Language History A brief overview of C language history is given below − Origin of C Programming ”ALGOL” was the foundation or progenitor of programming languages. It was first introduced in 1960. ”ALGOL” was widely used in European countries. The ALGOL had introduced the concept of structured programming to the developer community. The year 1967 marked the introduction of a novel computer programming language known as ”BCPL”, an acronym for Basic Combined Programming Language. BCPL was designed by Martin Richards in the mid-1960s. Dennis Ritchie created C at Bell Laboratories in the early 1970s. It developed from an older language named B that Ken Thompson created. The main purpose of C”s creation was to construct the Unix operating system, which was crucial in the advancement of contemporary computers. BCPL, B, and C all fit firmly in the traditional procedural family typified by Fortran and Algol 60. BCPL, B and C differ syntactically in many details, but broadly they are similar. Development of C Programming In 1971, Dennis Ritchie started working on C, and he and other Bell Labs developers kept improving it. The language is appropriate for both system programming and application development because it was made to be straightforward, effective, and portable. Standardization of C Programming Dennis Ritchie commenced development on C in 1971 and, in collaboration with other developers at Bell Labs, proceeded to refine it. The language was developed with portability, simplicity, and efficiency in mind, rendering it applicable to both application and system programming. History of C Versions After Traditional C K&R C Dennis Ritchie along with Brian Kernighan published the first edition of their book “The C Programming Language”. Popularly known as K&R (the initials of its authors), the book served for many years as an informal specification of the language. The version of C that it describes is commonly referred to as “K&R C”. It is also referred to as C78. Many of the features of C language introduced in K&R C are still the part of the language ratified as late as in 2018. In early versions of C, only functions that return types other than int must be declared if used before the function definition; functions used without prior declaration were presumed to return type int. C compilers by AT&T and other vendors supported several features added to the K&R C language. Although C started gaining popularity, there was a lack of uniformity in implementation. Therefore, it was felt that the language specifications must be standardized. ANSI C In the 1980s, the American National Standards Institute (ANSI) began working on a formal standard for the C language. This led to the development of ANSI C, which was standardized in 1989. ANSI C introduced several new features and clarified ambiguities present in earlier versions of the language. C89/C90 The ANSI C standard was adopted internationally and became known as C89 (or C90, depending on the year of ratification). It served as the basis for compilers and development tools for many years. C99 In 1999, the ISO/IEC approved an updated version of the C standard known as C99. The C standard was further revised in the late 1990s. C99 introduced new features, including inline functions, several new data types such as a complex type to represent complex numbers, and variable-length arrays etc. It also added support for C++ style one-line comments beginning with //. C11 C11, published in 2011, is another major revision of the C standard. The C11 standard adds new features to C and the library and introduced features such as multi-threading support, anonymous structures and unions, and improved Unicode support. It includes type generic macros, anonymous structures, improved Unicode support, atomic operations, multi-threading, and bounds-checked functions. It has an improved compatibility with C++. C17 The C17 standard has been published in June 2018. C17 is the current standard for the C programming language. No new features have been introduced with this standard revision. It only performs certain technical corrections, and clarifications to defects in C11. C18 The most recent version of the C standard, C18, was published in 2018. It includes minor revisions and bug fixes compared to C11. C23 C23 is the informal name for the next major C language standard revision, expected to be published in 2024. 14 new keywords are expected to be introduced in this revision. C has remained popular over time because to its simplicity, efficiency, and versatility. It has been used to create

C – Features

Features of C Programming Language ”; Previous Next Dennis Ritchie and Ken Thompson developed the C programming language in 1972, primarily to re-implement the Unix kernel. Because of its features such as low-level memory access, portability and cross-platform nature etc., C is still extremely popular. Most of the features of C have found their way in many other programming languages. The development of C has proven to be a landmark step in the history of computing. Even though different programming languages and technologies dominate today in different application areas such as web development, mobile apps, device drivers and utilities, embedded systems, etc., the underlying technologies of all of them are inspired by the features of C language. The utility of any technology depends on its important features. The features also determine its area of application. In this chapter, we shall take an overview of some of the significant features of C language. C is a Procedural and Structured Language C is described as procedure-oriented and structured programming language. It is procedural because a C program is a series of instructions that explain the procedure of solving a given problem. It makes the development process easier. In C, the logic of a process can be expressed in a structured or modular form with the use of function calls. C is generally used as an introductory language to introduce programming to school students because of this feature. C is a General-Purpose Language The C language hasn”t been developed with a specific area of application as a target. From system programming to photo editing software, the C programming language is used in various applications. Some of the common applications of C programming include the development of Operating Systems, databases, device drivers, etc. C is a Fast Programming Language C is a compiler-based language which makes the compilation and execution of codes faster. The source code is translated into a hardware-specific machine code, which is easier for the CPU to execute, without any virtual machine, as some of the other languages like Java need. The fact that C is a statically typed language also makes it faster compared to dynamically typed languages. Being a compiler-based language, it is faster as compared to interpreter-based languages. C is Portable Another feature of the C language is its portability. C programs are machine-independent which means that you can compile and run the same code on various machines with none or some machine-specific changes. C programming provides the functionality of using a single code on multiple systems depending on the requirement. C is Extensible C is an extensible language. It means if a code is already written, you can add new features to it with a few alterations. Basically, it allows adding new features, functionalities, and operations to an existing C program. Standard Libraries in C Most of the C compilers are bundled with an extensive set of libraries with several built-in functions. It includes OS-specific utilities, string manipulation, mathematical functions, etc. Importantly, you can also create your user-defined functions and add them to the existing C libraries. The availability of such a vast scope of functions and operations allows a programmer to build a vast array of programs and applications using the C language. Pointers in C One of the unique features of C is its ability to manipulate the internal memory of the computer. With the use of pointers in C, you can directly interact with the memory. Pointers point to a specific location in the memory and interact directly with it. Using the C pointers, you can interact with external hardware devices, interrupts, etc. C is a Mid-Level Programming Language High-level languages have features such as the use of mnemonic keywords, user-defined identifiers, modularity etc. C programming language, on the other hand, provides a low-level access to the memory. This makes it a mid-level language. As a mid-level programming language, it provides the best of both worlds. For instance, C allows direct manipulation of hardware, which high-level programming languages do not offer. C Has a Rich Set of Built-in Operators C is perhaps the language with the most number of built-in operators which are used in writing complex or simplified C programs. In addition to the traditional arithmetic and comparison operators, its binary and pointer related operators are important when bit-level manipulations are required. Recursion in C C language provides the feature of recursion. Recursion means that you can create a function that can call itself multiple times until a given condition is true, just like the loops. Recursion in C programming provides the functionality of code reusability and backtracking. User-defined Data Types in C C has three basic data types in int, float and char. However, C programming has the provision to define a data type of any combination of these three types, which makes it very powerful. In C, you can define structures and union types. You also have the feature of declaring enumerated data types. Preprocessor Directives in C In C, we have preprocessor directives such as #include, #define, etc. They are not the language keywords. Preprocessor directives in C carry out some of the important roles such as importing functions from a library, defining and expanding the macros, etc. File Handling in C C language doesn”t directly manipulate files or streams. Handling file IO is not a part of the C language itself but instead is handled by libraries and their associated header files. File handling is generally implemented through high-level I/O which works through streams. C identifies stdin, stdout and stderr as standard input, output and error streams. These streams can be directed to a disk file to perform read/write operations. These are some of the important features of C language that make it

C – Data Types

C – Data Types ”; Previous Next Data types in C refer to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted. In this chapter, we will learn about data types in C. A related concept is that of “variables”, which refer to the addressable location in the memory of the processor. The data captured via different input devices is stored in the computer memory. A symbolic name can be assigned to the storage location called variable name. C is a statically typed language. The name of the variable along with the type of data it intends to store must be explicitly declared before actually using it. C is also a strongly typed language, which means that the automatic or implicit conversion of one data type to another is not allowed. The types in C can be classified as follows − Sr.No. Types & Description 1 Basic Types They are arithmetic types and are further classified into: (a) integer types and (b) floating-point types. 2 Enumerated types They are again arithmetic types and they are used to define variables that can only assign certain discrete integer values throughout the program. 3 The type void The type specifier void indicates that no value is available. 4 Derived types They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types. The array types and structure types are referred collectively as the aggregate types. The type of a function specifies the type of the function”s return value. We will see the basic types in the following section, where as other types will be covered in the upcoming chapters. Integer Data Types in C The following table provides the details of standard integer types with their storage sizes and value ranges − Type Storage size Value range char 1 byte -128 to 127 or 0 to 255 unsigned char 1 byte 0 to 255 signed char 1 byte -128 to 127 int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295 short 2 bytes -32,768 to 32,767 unsigned short 2 bytes 0 to 65,535 long 8 bytes -9223372036854775808 to 9223372036854775807 unsigned long 8 bytes 0 to 18446744073709551615 To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator. The expressions sizeof(type) yields the storage size of the object or type in bytes. Example of Integer Data Types Given below is an example to get the size of various type on a machine using different constant defined in limits.h header file − #include <stdio.h> #include <stdlib.h> #include <limits.h> #include <float.h> int main(int argc, char** argv) { printf(“CHAR_BIT : %dn”, CHAR_BIT); printf(“CHAR_MAX : %dn”, CHAR_MAX); printf(“CHAR_MIN : %dn”, CHAR_MIN); printf(“INT_MAX : %dn”, INT_MAX); printf(“INT_MIN : %dn”, INT_MIN); printf(“LONG_MAX : %ldn”, (long) LONG_MAX); printf(“LONG_MIN : %ldn”, (long) LONG_MIN); printf(“SCHAR_MAX : %dn”, SCHAR_MAX); printf(“SCHAR_MIN : %dn”, SCHAR_MIN); printf(“SHRT_MAX : %dn”, SHRT_MAX); printf(“SHRT_MIN : %dn”, SHRT_MIN); printf(“UCHAR_MAX : %dn”, UCHAR_MAX); printf(“UINT_MAX : %un”, (unsigned int) UINT_MAX); printf(“ULONG_MAX : %lun”, (unsigned long) ULONG_MAX); printf(“USHRT_MAX : %dn”, (unsigned short) USHRT_MAX); return 0; } Output When you compile and execute the above program, it produces the following result on Linux− CHAR_BIT : 8 CHAR_MAX : 127 CHAR_MIN : -128 INT_MAX : 2147483647 INT_MIN : -2147483648 LONG_MAX : 9223372036854775807 LONG_MIN : -9223372036854775808 SCHAR_MAX : 127 SCHAR_MIN : -128 SHRT_MAX : 32767 SHRT_MIN : -32768 UCHAR_MAX : 255 UINT_MAX : 4294967295 ULONG_MAX : 18446744073709551615 USHRT_MAX : 65535 Floating-Point Data Types in C The following table provides the details of standard floating-point types with storage sizes and value ranges and their precision − Type Storage size Value range Precision float 4 byte 1.2E-38 to 3.4E+38 6 decimal places double 8 byte 2.3E-308 to 1.7E+308 15 decimal places long double 10 byte 3.4E-4932 to 1.1E+4932 19 decimal places The header file “float.h” defines the macros that allow you to use these values and other details about the binary representation of real numbers in your programs. Example Floating-Point Data Types The following example prints the storage space taken by a float type and its range values − #include <stdio.h> #include <stdlib.h> #include <limits.h> #include <float.h> int main(int argc, char** argv) { printf(“Storage size for float : %zu n”, sizeof(float)); printf(“FLT_MAX : %gn”, (float) FLT_MAX); printf(“FLT_MIN : %gn”, (float) FLT_MIN); printf(“-FLT_MAX : %gn”, (float) -FLT_MAX); printf(“-FLT_MIN : %gn”, (float) -FLT_MIN); printf(“DBL_MAX : %gn”, (double) DBL_MAX); printf(“DBL_MIN : %gn”, (double) DBL_MIN); printf(“-DBL_MAX : %gn”, (double) -DBL_MAX); printf(“Precision value: %dn”, FLT_DIG ); return 0; } Output When you compile and execute the above program, it produces the following result on Linux − Storage size for float : 4 FLT_MAX : 3.40282e+38 FLT_MIN : 1.17549e-38 -FLT_MAX : -3.40282e+38 -FLT_MIN : -1.17549e-38 DBL_MAX : 1.79769e+308 DBL_MIN : 2.22507e-308 -DBL_MAX : -1.79769e+308 Precision value: 6 Note: “sizeof” returns “size_t”. The type of unsigned integer of “size_t” can vary depending on platform. And,

C Programming – Tutorial

C Tutorial Table of content C Tutorial Why to Learn C Programming? Facts about C Hello World using C Programming Applications of C Programming C Audiences C Prerequisites FAQs on C Programming PDF Version Quick Guide Resources Job Search Discussion C Tutorial C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. C is the most widely used computer language. It keeps fluctuating at number one scale of popularity along with Java programming language, which is also equally popular and most widely used among modern software programmers. Why to Learn C Programming? C programming language is a MUST for students and working professionals to become a great Software Engineer specially when they are working in Software Development Domain. Here are some of the important reasons why you should learn C Programming − It is a structured programming language and you can use the skills learned in C to master other programming languages. You can use C program to write efficient codes and develop robust projects. C is a low-level language and you can use it to interact more directly with the computer”s hardware and memory. Facts about C C is the most widely used and popular System Programming Language. Most of the state-of-the-art software have been implemented using C. Here are some facts about the C language: C was invented to write an operating system called UNIX. The UNIX OS was totally written in C. C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute (ANSI). Hello World using C Programming Just to give you a little excitement about C programming, I”m going to give you a small conventional C Programming Hello World program. You can run it here using the “Edit and Run” button. #include <stdio.h> int main() { /* my first program in C */ printf(“Hello, World! n”); return 0; } Applications of C Programming C was initially used for system development work, particularly the programs that make-up the operating system. C was adopted as a system development language because it produces code that runs nearly as fast as the code written in assembly language. Some examples of the use of C are – Operating Systems Language Compilers Assemblers Text Editors Print Spoolers Network Drivers Modern Programs Databases Language Interpreters Utilities Audience This tutorial is designed for software programmers with a need to understand the C programming language starting from scratch. This C tutorial will give you enough understanding on C programming language from where you can take yourself to higher level of expertise. Prerequisites Before proceeding with this tutorial, you should have a basic understanding of Computer Programming terminologies. A basic understanding of any of the programming languages will help you in understanding the C programming concepts and move fast on the learning track. FAQs on C Programming There are some very Frequently Asked Questions(FAQ) about C, this section tries to answer them briefly. Is C Programming still relevant today? C programming came into being in 1972. After more than 5 decades, C is still one of the most popular languages that ranks consistently in the top three. Since C can directly interact with the hardware, it is primarily used in low-level applications such as building operating systems, device drivers, embedded systems, networking etc. Therefore, C programming skills are very much in demand, in this age also. One’s career prospects will definitely brighter if he has a good proficiency in C programming. What are the applications of C Programming? C is a general-purpose programming language; therefore, it can be used to develop any type of applications. However, its ability to interact with the hardware makes it more suitable for developing system utilities, compilers and device drivers. C is predominantly used in building embedded systems and networking applications. C is significantly faster as compared to languages like Java or Python because it is directly compiled to the machine code. Hence, it is used in development of gaming applications. C is a versatile programming language that can be useful in development of a variety of software applications. Is C Programming difficult to learn for beginners? C is considered to be one of the easiest programming languages to learn for beginners. You can learn programming in C with the help of many online resources, such as the C tutorial provided by TutorialsPoint cprogramming. C does have a slightly steeper learning curve when you go towards advanced concepts. For attaining a high level of proficiency in C, you need to be able to master the features such as pointers, structures etc. Learning C allows you to build sound foundation with which you can easily learn other programming technologies. What are the advantages of learning C Programming? Here are some of the main advantages of learning C programming: C is a compiled language. It is translated directly into the machine language. That’s why the code execution is faster. Thus C offers better efficiency as compared to Java, Python. This fature is advantageous in applications like system utilities, embedded systems game development etc. C is a general-purpose language. Hence, it can be used to develop a variety of applications. C code easily portable. C compilers are available on all the operating system platforms. Hence, you can build an executable on relevant

C – Useful Resources

C Programming – Useful Resources ”; Previous Next The following resources contain additional information on C Programming. Please use them to get more in-depth knowledge on this. Useful Video Courses Learn Parallel Programming with c# and .NET Core 5 Best Seller 23 Lectures 2.5 hours V.D More Detail Complete C++ Programming Fundamentals with Example Projects 160 Lectures 16.5 hours Emenwa Global, Ejike IfeanyiChukwu More Detail Complete C++ Programming Course With Example Project 196 Lectures 21.5 hours Emenwa Global, Ejike IfeanyiChukwu More Detail C Programming Course from Scratch – Beginners in Hindi/Urdu 20 Lectures 7.5 hours Subrat Kumar Das More Detail C++ Programming for Beginners 15 Lectures 10.5 hours Uplatz More Detail Embedded C Programming Course 14 Lectures 10 hours Uplatz More Detail Print Page Previous Next Advertisements ”;