














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to the c programming language, covering fundamental concepts such as data types, variables, constants, operators, and type casting. It explains the character set, keywords, identifiers, and various types of constants including integer, octal, hexadecimal, floating-point, and character constants. The document also details different data types like char, int, float, and double, along with their ranges and declaration. Furthermore, it discusses user-defined data types, operators (arithmetic, relational, logical, assignment, increment/decrement, conditional, bitwise, and special), operator precedence, and type casting. This serves as a basic guide for beginners to understand the core elements of c programming, offering examples and explanations to facilitate learning. It is useful for understanding the basics of c programming language, including data types, operators, and variable declarations. It also covers user-defined data types and type casting.
Typology: Schemes and Mind Maps
1 / 22
This page cannot be seen from the preview
Don't miss anything!















Module 2 Program A program is a group of closely related and interdependent activities. A computer program is a series of organized instructions that directs a computer to perform tasks. A programming language is a set of words, symbols and codes that enables humans to communicate with computers. Hundreds of programming languages exist today. Each language has its own standard or rules for writing the commands and/or instructions. Examples of programming languages are:
language with the features of high level language which can be used for creating software packages, system software etc. It supports the programmer with a rich set of built-in functions and operators. C is highly portable.(ie C programs written on one computer can run on other computer without making any changes in the program.).Structured programming concept is well supported in C, this helps in dividing the programs into function modules or code blocks. Importance of C It is a robust language which consist of rich set of built in functions and operators that can be used to write any complex program. Programs written in c are efficient and fast. C is highly portable ie C programs written for one computer can be run on another with little or no modifications. C is a structured programming language. C has the ability to extend itself Sample program- 1 #include main () { /* Printing begins here / printf (“C is a very good programming language.”); / Printing ends here */ } The first line is a preprocessor command which adds the stdio header file into our program. Actually stdio stands for standard input out, this header file supports the input-output functions in a program. In a program, we need to provide input data and display processed data on standard output – Screen. The stdio.h header file supports these two activities. There are many header files which will be discussed in future. The second line main() tell the compiler that it is the starting point of the program, every program should essentially have the main function only once in the program. The opening and closing braces indicates the beginning and ending of the program. All the statements between these two braces form the function body. These statements are actually the C code which tells the computer to do something. Each statement is a instruction for the computer to perform specific task.
Every word in C language is a keyword or an identifier. All keywords have some fixed meaning and these meaning cannot be changed. Keywords in C language cannot be used as a variable name. They are specifically used by the compiler for its own purpose and they serve as building blocks of a c program. Keywords in C Language auto double int Struct continue for signed void break else long Switch do if static while case enum register Typedef default goto sizeof volatile char extern return Union const float short unsigned Identifiers refer to the name of user-defined variables, array and functions. Identifiers are user defined names and consist of a sequence of letters and digits with a letter as a first character. Constants A constant value is the one which does not change during the execution of a program. C supports several types of constants. Integer constants Integer constants are the numeric constants(constant associated with number) without any fractional part or exponential part. There are three types of integer constants in C language: decimal constant(base 10), octal constant(base 8) and hexadecimal constant(base 16). Decimal digits: 0 1 2 3 4 5 6 7 8 9 Octal digits: 0 1 2 3 4 5 6 7 Hexadecimal digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F. For example: Decimal constants: 0, - 9, 22 etc Octal constants: 021, 077, 033 etc Hexadecimal constants: 0x7f, 0x2a, 0x521 etc Notes:
"x" //string constant having single character. "Earth is round\n" //prints string with newline Enumeration constants Keyword enum is used to declare enumeration types. For example: enum color {yellow, green, black, white}; Here, the variable name is color and yellow, green, black and white are the enumeration constants having value 0, 1, 2 and 3 respectively by default Variables A Variable or an identifier is a data name that may be used to store a data value. A variable is a value that can change any time. It is a memory location used to store a data value. A variable name should be carefully chosen by the programmer so that its use is reflected in a useful way in the entire program. Variable names are case sensitive. Example of variable names are: Sun number Salary Emp_name average Any variable declared in a program should confirm to the following
%abc DATA TYPES Data types 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. C language contains numerous data types. Storage representations and machine instructions to handle constants differ from machine to machine. The varieties of data types available allow the programmer to select the type appropriate to the needs of the application as well as the machine. ANSIC supports four classes of data types:
Declaration of Variables Every variable used in the program should be declared to the compiler. The declaration does two things.
This identifier is a user defined enumerated datatype which can be used to declare variables that have one of the values enclosed within the braces. After the definition we can declare variables to be of this ‘new’ type as below. enum identifier V1, V2, V3, ……… Vn The enumerated variables V1, V2, …..Vn can have only one of the values value1, value2 ….. value n Defining Symbolic Constants A symbolic constant value can be defined as a preprocessor statement and used in the program as any other constant value. The general form of a symbolic constant is
Valid examples of constant definitions are :
These values may appear anywhere in the program, but must come before it is referenced in the program. It is a standard practice to place them at the beginning of the program. Declaring Variable as Constant The values of some variable may be required to remain constant through-out the program. We can do this by using the qualifier constat the time of initialization. constdatatypevar_name = value; Example: Constintclass_size = 40; The const data type qualifier tells the compiler that the value of the int variable class_size may not be modified in the program.
Eg:
Left Operation Right Result True && True True False && True False True && False False False && False False True || True True False || True True True || False True False || False False As you can see from the table, an and operation is true only when both its operands are true. An or operation is true if either one of its operands is true. The table also shows that these operators can do what is called “lazy evaluation” or “short circuit evaluation”. The operands are evaluated from left to right. For an and operation, if the left operand is false, the right operand is not even evaluated because the result is known to be false. For an or operation, if the left operand is true the right operand is not evaluated because the result will be true. LOGICAL NOT The logical not operator is a unary operator, it takes one operand after the !symbol. It inverts the logical value of the operand – a true value becomes false, and a false value becomes true. Remember that in C a non-zero value is true and zero is false. Eg: int a=-97; int b=!a; will set b to 0 because - 97 is non-zero which is true, and a logical not of true is false which is represented as 0.
addition or subtraction happens later. Prefix: the value is incremented/decremented first and then applied. Postfix: the value is applied and the value is incremented/decremented. Eg: m=5; y=++m; the value of y and m would be 6. m=5; y=m++; the value of y would be 5 and m would be 6. Similarly the decrement operator is same. CONDITIONAL OPERATORS(TERNARY OPERATOR ?) The conditional operator is unusual in that it takes three operands. The syntax of this operator is like this: Condition? Expression1 : Expression2; The Condition is first evaluate to true or false. If it is true Expression1 is evaluated and its value is returned. If Condition is false Expression2 is evaluated and its value is returned. Both Expression1 and Expression2 must be the same type (or convertible to the same type). Eg: A=10,b=15; X=(a>b)?a:b; After execution x=15; The above is same as If(a>b) X=a; Else X=b; BITWISE OPERATORS C's bitwise operators work on the bits stored in integral types. They work similar to the logical operators
except that instead of working on true and false values they work with ones and zeroes. There are several bitwise operators available: Symbol Meaning ~ Complent &bitwise And | bitwise or ^ Exclusive OR << Left shift >> Right shift Special Operators C supports some special operators of interest such as comma operator, size of operator, pointer operators (& and *) and member selection operators (. and →). The size of and the comma operators are discussed here. The Comma Operator The Comma operator can be used to link the related expressions together. Example For Comma Operator Comma Operator In for Loops for(i=0,j=1;i>10:i++,j++) Comma Operator In while Loops While(c<10,c--) Type cast Operator Syntax: ( type ) converts a to the specified type.Note that the use of a parenthesized type in a method declaration or definition is not an example of the use of the type cast operator. Example Float s= 12.5; int a; a = (int) s; now a value is 12. The sizeof operator
== is equal to left to right != is not equal to left to right & bitwise AND left to right ^ bitwise exclusive OR left to right | bitwise inclusive OR left to right && logical AND left to right || logical OR left to right = assign right to left += add assign right to left
Sometimes when expressions are evaluated the type of an operand is converted. These conversions may happen implicitly or explicitly. Implicit conversion is done automatically. For example when the operands to some operators have different types the smaller operand is converted to the larger operand's type. One thing to note here is that operators that take integers usually perform what is called “integer promotion” - converting smaller integral types such as char and short into int before carrying out the operation. Eg: #include void main() { char a = 100; char b = 28; int c; char d; c = a + b; d = a + b; printf( "c = %d, d = %dn", c, d); } o/p: c = 128, d = - 128 The addition operator performs implicit integer promotion. On this environment a char is a signed 8 bit value, which has a range of - 128 to 127. On line 10, though both a and b are chars they are converted into int before the addition is done, then the result is assigned to c. On line 11 the same thing happens, but the int result of 128 is converted back into a char, which causes the value to “wrap around” to - 128 since a positive 128 is not within the range of a char. Explicit type conversion using Casting operator With explicit type conversion we force a type conversion in a way that is different from automatic conversion.The way to do explicit type conversion is with the “casting” operator. The syntax for explicit type conversion is : (type-name) expression Type-name is a standard c data type.