Anda di halaman 1dari 51

Introduction of c

C was developed in the early 1970s by Dennis Ritchie at Bell Laboratories in USA. C was originally first implemented on the DEC PDP-11 computer in 1972. C was originally designed for writing system software but today a variety of software programs are written in C. C can be used on many different types of computers but is mostly used with the UNIX operating system.It is also called as 'Procedure oriented programming language'

The 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 1970 The language was formalized in 1988 by the American National Standard Institute (ANSI). The UNIX OS was totally written in C By 1973. Today C is the most widely used and popular System Programming Language. Today's most popular Linux OS and RBDMS MySQL have been written in C.

Why to use C ?
C was initially used for system development work, in particular 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 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 Data Bases Language Interpreters Utilities It is a good idea to learn C because it has been around for a long time which means there is a lot of information available on it. Quite a few other programming languages such as C++ and Java are also based on C which means you will be able to learn them more easily in the future.

Simple C Program
#include<stdio.h> int main() { printf("Hello World\n"); return 0; }

#include<stdio.h>
This includes a file called stdio.h which lets us use certain commands. stdio is short for Standard Input/Output which means it has commands for input like reading from the keyboard and output like printing things on the screen.

int main()
int is what is called the return value which will be explained in a while. main is the name of the point where the program starts and the brackets are there for a reason that you will learn in the future but they have to be there.

{}
The 2 curly brackets are used to group all the commands together so it is known that the commands belong to main. These curly brackets are used very often in C to group things together.

printf("Hello World\n");
This is the printf command and it prints text on the screen. The data that is to be printed is put inside brackets. You will also notice that the words are inside inverted commas because they are what is called a string. Each letter is called a character and a series of characters that is grouped together is called a string. Strings must always be put between inverted commas. The \n is called an escape sequence and represents a newline character and is used because when you press ENTER it doesn't insert a new line character but instead takes you onto the next line in the text editor. You have to put a semi-colon after every command to show that it is the end of the command. Table of commonly used escape sequences: \aAudible signal \bBackspace \tTab\nNewline \vVertical tab \fNew page \Clear screen \rCarriage return

return 0;
The int in int main() is short for integer which is another word for number. We need to use the return command to return the value 0 to the operating system to tell it that there were no errors while the program was running. Notice that it is a command so it also has to have a semi-colon after it.

Print Integer
C Program to print Integer number in screen. Input is done using scanf function and number is printed on screen using printf.

Print integer program


#include<stdio.h> int main() { int a; printf("Enter an integer\n"); scanf("%d", &a); printf("Integer that you have entered is %d\n", a); return 0; }

Print String

This program print a string. String can be printed by using various functions such as printf, puts.

Print string
#include <stdio.h> main() { char array[20] = "Hello World"; printf("%s\n",array);

return 0;
}

input a string we use scanf function.


#include <stdio.h> main() { char array[100]; printf("Enter a string\n"); scanf("%s", array); printf("You entered the string %s\n",array); return 0; }

Input string containing spaces


#include <stdio.h> main() { char a[80]; gets(a); printf("%s\n", a);

return 0;
}

Data Types
Data types in any of the language means that what are the various type of data the variables can have in that particular language. Whenever a variable is declared it becomes necessary to define data type that what will be the type of data that variable can hold. Mainly there are two types of data types: 1. Primary Data Type 2. Secondary Data Type Primary Data Type : character, integer, float, double, void Secondary Data Type : arrays, pointer, structures, union, enum

data types program example


#include using namespace std; int main() {int a = 3000; // positive integer data type float b = 4.5345; // float data type char c = 'A'; // char data type long d = 31456; // long positive integer data type long e = -31456; // long -ve integer data type int f = -145; // -ve integer data type short g = 120; // short +ve integer data type short h = -120; // short -ve integer data type double i = 5.1234567890; // double float data type float j = -3.24; // float data type }

Pointers
Different from other normal variables which can store values, pointers are special variables that can hold the address of a variable. Since they store memory address of a variable, the pointers are very commonly said to point to variables. Lets try to understand the concept.

Pointer example
A normal variable var has a memory address of 1001 and holds a value 50. A pointer variable has its own address 2047 but stores 1001, which is the address of the variable var

What is Literal variable


The simple variables are used to store the literal values. Whenever a variable is declared in a program it is having same memory location in the ...

What is the variable


Variables in C are memory locations that are given names and can be assigned values. We use variables to store data in memory for later use.

Diff types of variables


There are 2 basic kinds of variables in C which are numeric and character.

array
array is a collection of multiple data item which are represented by using a single identifier. Since a single identifier representing all the data items will have generally a continuous allocation in arrays there is one base address to which all the contiguous memory location are attached. These memory location are having then difference in addresses and their addresses are continuous in manner.

Scalar factor
A pointer variable is incremented by using an increment operator then its address value is not increased by 1 but it will be increased by a complete address this type of incrementation is called pointer incrementation and the length the which pointer is incrementing is called scalar factor.

What is array
An array is a collection of multiple data items The arrays are generally in the form of continuous memory locations i.e. each memory location is having the address of its next memory location which is the part of that array. Hence arrays are generally shown either in the form of rows or in the form of columns structure arrays ch are represented by the same identifier.

Why is needed array for in c


In case of a simple variable there is a very big problem that a single value can be represented at a time by a single identifier. Hence if we want to represent multiple values through a single identifier than it is not possible through using single identifier. For this purpose c has provided yet another tool and it is called an array.

Array type
one-dimensional array
An array which is having either a single row or single column is termed as a one-dimensional array. One dimensional array have the subscripts in a linear manner.

two-dimensional array
One-dimensional array can store a list of linear variables but for storing the values which are in a tabular form, the multidimensional array are used. The multidimensional arrays can be though of having multiple rows and columns. In fact, when the wach index of one dimensional array is itself in the form of an array the upcoming structure is called a multidimensional array.

Element or content
The various values stored on the indexes are called the elements or the contents.

Array syntax
Data type array name[size]; The array of characters is called string. The array elements can be called out by writing the array name and its index no. in the brackets.

Use of array
The arrays in the C can be implemented by writing the data types of the elements along with the array name and its index numbers (no. of locations).

index
The various locations where the values can be stored are called indexes and in the technical terms they are referred as subscripts.

Data type in c
int main() {int a = 3000; // positive integer data type float b = 4.5345; // float data type char c = 'A'; // char data type long d = 31456; // long positive integer data type long e = -31456; // long -ve integer data type int f = -145; // -ve integer data type short g = 120; // short +ve integer data type short h = -120; // short -ve integer data type double i = 5.1234567890; // double float data type float j = -3.24; // float data type }

data type in c
Data types in any of the language means that what are the various type of data the variables can have in that particular language. Whenever a variable is declared it becomes necessary to define data type that what will be the type of data that variable can hold. Mainly there are two types of data types: 1. Primary Data Type 2. Secondary Data Type Primary Data Type : character, integer, float, double, void Secondary Data Type : arrays, pointer, structures, union, enum

Variable and constant in c


Variables in C are memory locations that are given names and can be assigned values. We use variables to store data in memory for later use. There are 2 basic kinds of variables in C which are numeric and character.

Numeric and character variable


Numeric: variables can either be integer values or they can be Real values. Integer values are whole numbers without a fraction part or decimal point in them. Real numbers can have a decimal Character: variables are letters of the alphabet as well as all characters on the ASCII chart and even the numbers 0 - 9. Characters must always be put between single quotes. A number put between single quotes is not the same thing as a number without them point in them.

What are constants in c


The difference between variables and constants is that variables can change their value at any time but constants can never change their value. Constants can be useful for items such as Pi or the charge on an electron. Using constants can stop you from changing the value of an item by mistake.

Declaring variable in c
To declare a variable we first put the type of variable and then give the variable a name= int,float ,char ,long short You can name a variable anything you like as long as it includes only letters, numbers or underscores and does not start with a number. It is a good idea to keep your variable names less than 32 characters long to save time on typing them out and for compiler compatibility reasons. Variables must always be declared at the top before any other commands are used. Now let's declare an integer variable called a and a character variable called b.

Last page attachment


int main() { int a; char b; return 0; }

We can declar more variables


int main() { int a,b,c; return 0; }

To declare a constant all you have to do it put the word const in front of a normal variable declaration and make assign a value to it. int main() { const float pi = 3.14; return 0;

Signed and unsigned variable in c


The difference between signed and unsigned variables is that signed variables can be either negative or positive but unsigned variables can only be positive. By using an unsigned variable you can increase the maximum positive range. When you declare a variable in the normal way it is automatically a signed variable. To declare an unsigned variable you just put the word unsigned before your variable declaration or signed for a signed variable although there is no reason to declare a variable as signed since they already are. Code:

Signed and unsigned program in c


int main() { unsigned int a; signed int b; return 0; }

To assign a value to a variable you use the equals sign.


int main() { int a; char b; a = 3; b = "H"; return 0; }

To perform a calculation you need to have a variable to put the answer into. You can also use both variables

int main() { int a,b; a = 5; b = a + 3; a = a - 3; return 0; }

You can read a variable from the keyboard with the scanf command and print a variable with the printf command.

#include<stdio.h> int main() { int a; scanf("%d",&a); a = a * 2; printf("The answer is %d",a); return 0; }

Format pacifier and purpose


%d integer data type %f float %u unsigned integer %s string %c char %ld long integer

Operators in c
"Operator is a symbol that is used to perform mathematical operations."C language is rich in built-in operators and provides following type of operators: Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Assignment Operators Misc Operators

Arithmetic operators and program


Arithmetic operators include the familiar addition (+), subtraction (-), multiplication (*) and division (/) operations. In addition there is the modulus operator (%) which gives the remainder left over from a division operation.

#include <stdio.h> void main() {int a = 150; int b = 20; int c; c = a + b; printf( "a + b = %d\n", c ); c = a - b; printf( "a - b = %d\n", c );

/* multiplication performed before call to printf() */ printf( "a * b = %d\n", a * b );


c = a / b; printf( "a / b = %d\n", c ); c = 100 % 3; printf( "a % b = %d\n", c ); }

Program output
a + b = 170 a - b = 130 a * b = 3000 a / b = 7.5 a % b = 30

Anda mungkin juga menyukai