Introduction to C Programming: Data Types and Operators, Schemes and Mind Maps of C programming

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

2024/2025

Available from 08/22/2025

alameen-haris
alameen-haris 🇮🇳

2 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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:
1. BASIC (Beginner’s All Purpose Symbolic Instruction Code)
2. Pascal
3. C
4. C++,
5. Java etc
PROGRAMMING LANGUAGES
Since a digital computer is a programmable device, the user must instruct the computer
according to his requirements. The programmer must use some languages that can be understandable
by the computer. These languages are called programming languages.
Overview of C
C is a programming language. It is most popular computer language today because it is a structured
high level, machine independent language. Programmers need not worry about the hardware platform
where they will be implemented.
Dennis Ritchie invented C language. Ken Thompson created a language which was based upon a
language known as BCPL and it was called as B. B language was created in 1970, basically for unix
operating system Dennis Ritchie used ALGOL, BCPL and B as the basic reference language from
which he created C.
C has many qualities which any programmer may desire. It contains the capability of assembly
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Introduction to C Programming: Data Types and Operators and more Schemes and Mind Maps C programming in PDF only on Docsity!

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:

  1. BASIC (Beginner’s All Purpose Symbolic Instruction Code)
  2. Pascal
  3. C
  4. C++,
  5. Java etc PROGRAMMING LANGUAGES Since a digital computer is a programmable device, the user must instruct the computer according to his requirements. The programmer must use some languages that can be understandable by the computer. These languages are called programming languages. Overview of C C is a programming language. It is most popular computer language today because it is a structured high level, machine independent language. Programmers need not worry about the hardware platform where they will be implemented. Dennis Ritchie invented C language. Ken Thompson created a language which was based upon a language known as BCPL and it was called as B. B language was created in 1970, basically for unix operating system Dennis Ritchie used ALGOL, BCPL and B as the basic reference language from which he created C. C has many qualities which any programmer may desire. It contains the capability of assembly

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.

C TOKENS- KEYWORDS, IDENTIFIERS, CONSTANTS, STRINGS, OPERATORS

KEYWORDS AND IDENTIFIERS

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:

  1. You can use small caps a , b , c , d , e , f instead of uppercase letters while writing a hexadecimal constant.
  2. Every octal constant starts with 0 and hexadecimal constant starts with 0x in C programming.

"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

  1. They must always begin with a letter, although some systems permit underscore as the first character.
  2. The length of a variable must not be more than 8 characters.
  3. White space is not allowed and
  4. A variable should not be a Keyword
  5. It should not contain any special characters. Examples of Invalid Variable names are 123 (area) 6th

%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:

  1. Primary (or fundamental) data types
  2. User-defined data types
  3. Derived data types
  4. Empty data set All C compilers support four fundamental data types, namely integer (int), character (char), floating point (float), and double-precision floating point (double). Many of them also offer extended data types such as long int and long double. DATA TYPE RANGE OF VALUES char - 128 to 127 Int - 32768 to + float 3.4 e- 38 to 3.4 e+ double 1.7 e- 308 to 1.7 e+ Character type A single character can be defined as a character(char) type data. Characters are usually stored in 8 bits (one byte) of internal storage. The qualifier signed or unsigned may be explicitly applied to char. While unsigned chars have values between 0 and 255, signed chars have values from- 128 to 127. Integer Type : Integers are whole numbers with a machine dependent range of values. A good programming language has to support the programmer by giving a control on a range of numbers and storage space. C has 3 classes of integer storage namely short int, intand long int. All of these data types have signed and

Declaration of Variables Every variable used in the program should be declared to the compiler. The declaration does two things.

  1. Tells the compiler the variables name.
  2. Specifies what type of data the variable will hold. The general format of any declaration datatype v1, v2, v3, ............. vn; Where v1, v2, v3 are variable names. Variables are separated by commas. A declaration statement must end with a semicolon. Example : Int sum; Int number, salary; Double average, mean; User defined type declaration (User Defined Datatypes) In C language a user can define an identifier that represents an existing data type. The user defined datatype identifier can later be used to declare variables. typedef is a keyword used in C language to assign alternative name to existing type. The general syntax is typedef type identifier; here type represents existing data type and ‘identifier’ refers to the ‘new’ name given to the data type. Example: typedefint salary; typedeffloat marks; Here salary symbolizes int and average symbolizes float. They can be later used to declare variables as follows: salary s1,s2; marks name1[50]; Therefore s1and s2 are indirectly declared as integer datatype and name1 is indirectly float data type. The second type of user defined datatype is enumerated data type which is defined as follows. Enum identifier {value1, value2 …. Value n};

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

define symbolic_name value of constant

Valid examples of constant definitions are :

define marks 100

define total 50

define pi 3.

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:

  • 14%3= - 2
  • 14%-3= - 2 14%-3= RELATIONAL OPERATORS Relational operators are used in Boolean conditions or expressions. Boolean conditions or expressions return either true or false. The relational operator returns zero values or nonzero values. The zero value is taken as false while the nonzero value is taken as true. Operator Meaning == is equal to != is not equal to > is greater than < is less than >= is greater than or equal to <= is less than or equal to A simple relational expression contains only one relational operator and takes the following form Expression1 relational operator expression Eg: If(a>b) Else Printf(“a is largest”); Printf(“b is largest”); LOGICAL OPERATORS The logical operators are || for a logical or and && for a logical and and !for logical not. The and and or operate on two boolean values of true or false. In C, an expression that evaluates to 0 is falseand an expression that evaluates to non-zero is true. These operators return 1 for true or 0 for false. && logical AND || logical OR inclusive ! logical NOT Logical AND and OR The following table shows the results of applying these operators to different boolean values

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

  • = subtract assign right to left *= multiply assign right to left /= divide assign right to left %= remainder assign right to left >>= right shift assign right to left <<= left shift assign right to left &= AND assign right to left ^= exclusive OR assign right to left |= inclusive OR assign right to left , Comma operator left to right

TYPE CASTING

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.