C LANGUAGE

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Some basic syntax rule for C program

C is a case sensitive language so all C instructions must be written in lower case letter. All C statement must end with a semicolon. Whitespace is used in C to describe blanks and tabs. Whitespace is required between keywords and identifiers.

initialize

is the assignment of an initial value for a data object or variable.

Difference between Compile and Run

You must be thinking why it is a 2 step process, first we compile the code and then we run the code. So, compilation is the process where the compiler checks whether the program is correct syntax wise, and there are no errors in the syntax. When we run a compiled program, then it actually executes the statements inside the main() function.

truth table for bitwise &, | and ^

a b a & b a | b a ^ b 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0

following program

#include int main() { printf("Hello,World"); return 0; } if we take any one statement: printf("Hello,World"); Then the tokens in this statement are→ printf, (, "Hello,World", ) and ;. So C tokens are basically the building blocks of a C program.

Character type

Character types are used to store characters value. Size and range of Integer type on 16-bit machine Type Size(bytes) Range char or signed char 1 -128 to 127 unsigned char 1 0 to 255

Operators in C Language

operators in C language are known by two more names Ternary Operator ? : Operator It is actually the if condition that we use in C language decision making, but using --------- ----------, we turn the if condition statement into a short and simple operator. The syntax of a conditional operator is :expression 1 ? expression 2: expression 3 Explanation: The question mark "?" in the syntax represents the if part. The first expression (expression 1) generally returns either true or false, based on which it is decided whether (expression 2) will be executed or (expression 3) If (expression 1) returns true then the expression on the left side of " : " i.e (expression 2) is executed. If (expression 1) returns false then the expression on the right side of " : " i.e (expression 3) is executed. Special operator Operator Description Example sizeof Returns the size of an variable sizeof(x) return size of the variable x & Returns the address of an variable &x ; return address of the variable x * Pointer to a variable *x ; will be pointer to a variable x

Keywords

preserved words that have special meaning in C language. These meaning cannot be changed. ------------ cannot be used as variable names because that would try to change the existing meaning of the keyword, which is not allowed. There are total 32 ------------ in C language. auto double int struct break else long switch case enum register typedef const extern return union char float short unsigned continue for signed volatile default goto sizeof void do if static while

C language syntax specify

rules for sequence of characters to be written in C language. In simple language it states how to form statements in a C language program - How should the line of code start, how it should end, where to use double quotes, where to use curly brackets etc. The rule specify how the character sequence will be grouped together, to form tokens. A smallest individual unit in C program is known as C Token. Tokens are either keywords, identifiers, constants, variables or any symbol which has some meaning in C language. A C program can also be called as a collection of various tokens.

The bitwise shift operator

shifts the bit value. The left operand specifies the value to be shifted and the right operand specifies the number of positions that the bits in the value have to be shifted. Both operands have the same precedence.

The current latest version of C language is C11

which was introduced in 2011. It is supported by all the standard C language compilers.

Pre-processor

#include is the first word of any C program. It is also known as a pre-processor. The task of a pre-processor is to initialize the environment of the program, i.e to link the program with the header files required. So, when we say #include <stdio.h>, it is to inform the compiler to include the stdio.h header file to the program before executing it.

Bitwise operators

------- --------- perform manipulations of data at bit level. These operators also perform shifting of bits from right to left. Bitwise operators are not applied to float or double(These are datatypes, we will learn about them in the next tutorial). Operator Description & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR << left shift >> right shift

Header file

A Header file is a collection of built-in(readymade) functions, which we can directly use in our program. Header files contain definitions of the functions which can be incorporated into any C program by using pre-processor #include statement with the header file. Standard header files are provided with each compiler, and covers a range of areas like string handling, mathematical functions, data conversion, printing and reading of variables. With time, you will have a clear picture of what header files are, as of now consider as a readymade piece of function which comes packaged with the C language and you can use them without worrying about how they work, all you have to do is include the header file in your program. To use any of the standard functions, the appropriate header file must be included. This is done at the beginning of the C source file. For example, to use the printf() function in a program, which is used to display anything on the screen, the line #include <stdio.h> is required because the header file stdio.h contains the printf() function. All header files will have an extension .h

Return statement - return 0;

A return statement is just meant to define the end of any C program. All the C programs can be written and edited in normal text editors like Notepad or Notepad++ and must be saved with a file name with extension as .c If you do not add the extension .c then the compiler will not recognise it as a C language program file.

Rules for an Identifier

An Identifier can only have alphanumeric characters(a-z , A-Z , 0-9) and underscore(_). The first character of an identifier can only contain alphabet(a-z , A-Z) or underscore (_). Identifiers are also case sensitive in C. For example name and Name are two different identifiers in C. Keywords are not allowed to be used as Identifiers. No special characters, such as semicolon, period, whitespaces, slash or comma are permitted to be used in or as Identifier. When we declare a variable or any function in C language program, to use it we must provide a name to it, which identified it throughout the program, for example: int myvariable = "Studytonight"; Here myvariable is the name or identifier for the variable which stores the value "Studytonight" in it.

Comments

Comments are plain simple text in a C program that are not compiled by the compiler. We write comments for better understanding of the program. Though writing comments is not compulsory, but it is recommended to make your program more descriptive. It make the code more readable. There are two ways in which we can write comments. Using // This is used to write a single line comment. Using /* */: The statements enclosed within /* and */ , are used to write multi-line comments. Example of comments : // This is a comment /* This is a comment */ /* This is a long and valid comment */ // this is not a valid comment Some basic syntax rule for C program C is a case sensitive language so all C instructions must be written in lower case letter. All C statement must end with a semicolon. Whitespace is used in C to describe blanks and tabs. Whitespace is required between keywords and identifiers.

Floating point type

Floating types are used to store real numbers. Size and range of Integer type on 16-bit machine Type Size(bytes) Range Float 4 3.4E-38 to 3.4E+38 double 8 1.7E-308 to 1.7E+308 long double 10 3.4E-4932 to 1.1E+4932

Character set

In C language characters are grouped into the following catagories, Letters(all alphabets a to z & A to Z). Digits (all digits 0 to 9). Special characters, ( such as colon :, semicolon ;, period ., underscore _, ampersand & etc). White spaces.

Features of C language

It is a robust language with rich set of built-in functions and operators that can be used to write any complex program. The C compiler combines the capabilities of an assembly language with features of a high-level language. Programs Written in C are efficient and fast. This is due to its variety of data type and powerful operators. It is many time faster than BASIC. C is highly portable this means that programs once written can be run on another machines with little or no modification. Another important feature of C program, is its ability to extend itself. A C program is basically a collection of functions that are supported by C library. We can also create our own function and add it to C library. C language is the most widely used language in operating systems and embedded system development today.

Arithmetic operators

Operator Description + adds two operands - subtract second operands from first * multiply two operand / divide numerator by denominator % remainder of division ++ Increment operator - increases integer value by one -- Decrement operator - decreases integer value by one

Relational operators

Operator Description == Check if two operand are equal != Check if two operand are not equal. > Check if operand on the left is greater than operand on the right < Check operand on the left is smaller than right operand >= check left operand is greater than or equal to right operand <= Check if operand on left is smaller than or equal to right operand

Logical operators

Operator Description Example && Logical AND (a && b) is false || Logical OR (a || b) is true ! Logical NOT (!a) is false

Assignment Operators

Operator Description Example = assigns values from right side operands to left side operand a=b += adds right operand to the left operand and assign the result to left a+=b is same as a=a+b -= subtracts right operand from the left operand and assign the result to left operand a-=b is same as a=a-b *= mutiply left operand with the right operand and assign the result to left operand a*=b is same as a=a*b /= divides left operand with the right operand and assign the result to left operand a/=b is same as a=a/b %= calculate modulus using two operands and assign the result to left operand a%=b is same as a=a%b

Different parts of C program

Pre-processor Header file Function Variables Statements & expressions Comments

Comments

We can add comments in our program to describe what we are doing in the program. These comments are ignored by the compiler and are not executed. To add a single line comment, start it by adding two forward slashses // followed by the comment. To add multiline comment, enclode it between /* .... */, just like in the program above.

Operators in C Language

a symbol that tells the compiler to perform a certain mathematical or logical manipulation. ----------are used in programs to manipulate data and variables. ---------- can be classified into following types: Arithmetic operators Relational operators Logical operators Bitwise operators Assignment operators Conditional operators Special operators

C language identifiers

are the names given to variables, constants, functions and user-define data. These --------- are defined against a set of rules.

C language compiler

converts the readable C language program into machine instruction.

Semicolon ;

is used to mark the end of a statement and beginning of another statement. Absence of semicolon at the end of any statement, will mislead the compiler to think that this statement is not yet finished and it will add the next consecutive statement after it, which may lead to compilation(syntax) error. #include int main() { printf("Hello,World") return 0; } In the above program, we have omitted the semicolon from the printf("...") statement, hence the compiler will think that starting from printf uptill the semicolon after return 0 statement, is a single statement and this will lead to compilation error

main() function

main() function is a function that must be there in every C program. Everything inside this function in a C program will be executed. In the above example, int written before the main() function is the return type of main() function. we will discuss about it in detail later. The curly braces { } just after the main() function encloses the body of main() functio

Integer type

ntegers are used to store whole numbers. Size and range of Integer type on 16-bit machine: Type Size(bytes) Range int or signed int 2 -32,768 to 32767 unsigned int 2 0 to 65535 short int or signed short int 1 -128 to 127 unsigned short int 1 0 to 255 long int or signed long int 4 -2,147,483,648 to 2,147,483,647 unsigned long int 4 0 to 4,294,967,295

Data types in C Language

specify how we enter data into our programs and what type of data we enter. C language has some predefined set of data types to handle various kinds of data that we can use in our program. These datatypes have different storage capacities. C language supports 2 different type of data types: Primary data types: These are fundamental data types in C namely integer(int), floating point(float), character(char) and void. Derived data types: Derived data types are nothing but primary datatypes but a little twisted or grouped together like array, stucture, union and pointer. These are discussed in details later. Data type determines the type of data a variable will hold. If a variable x is declared as int. it means x can hold only integer values. Every variable which is used in the program must be declared as what data-type it is.

C LANGUAGE

structured programming language developed by Dennis Ritchie in 1973 at Bell Laboratories. It is one of the most popular computer languages today because of its structure, high-level abstraction, machine independent feature etc. C language was developed to write the UNIX operating system, hence it is strongly associated with UNIX, which is one of the most popular network operating system in use today and heart of internet data superhighway.

void type

void type means no value. This is usually used to specify the type of functions which returns nothing. We will get acquainted to this datatype as we start learning more advanced topics in C language, like functions, pointers etc.

Initially C language was mainly used for

writing system level programs, like designing operating systems, but there are other applications as well which can be very well designed and developed using C language, like Text Editors, Compilers, Network Drivers etc.


Ensembles d'études connexes

Module 2: Reading Assessment Quiz

View Set

World History - The Age of Napoleon (Ch. 3 Lesson 8)

View Set

Intro to Theatre Exam #2 Study Guide

View Set

Mr. Brewer's Java: Ch. 10 Review Questions

View Set

Management of Patients with Hematologic Neoplasms

View Set

Chapter 35 Assessment Of Immune Function

View Set