C is a programming langauge developed at At & T's Bell Laboratories of USA in 1972. It was designed and written by "Dennis Ritchie" .It is popular because of its reliable, simple and easy to use.
C Character set
Interview Question
To represent information character denotes alphabet, digit, special symbol used to represent information.
Alphabets: A, B, C, D….Z. a, b, c, d….z.
Character Set
The character set in C Language can be grouped into the following categories.
1. Letters
2. Digits
3. Special Characters
4. White Spaces
White Spaces are ignored by the compiler until they are a part of string constant. White Space may be used to separate words, but are strictly prohibited while using between characters of keywords or identifiers.
# include call the directory header file(header file is predefined function)
stdio.h standered header file.
conio.h formating header file.
main() All program code are wrote under the main function start the program
Void : by default function return some value. And void avoide to retrun value.
- Data types
- Bit-wise Operator
- C data type and operator
- hello program
- if statement.
- Switch statement
- do while loop
- FOR loop
- While loop
- Series
- Array
- Function
- Pointer
- Recursion
- Structure
- 1st, 2nd, 3rd largest element between 1d array
- To find greatest of 3 digits in one line
- Fibonacci Series
- Sorting
- Bubble sorting
- Flag
- String Handling
- C Program to Read file
- File Handling
- Print Date
- Prime Number or not
- Dynamic memory Allocation 2
- Global and Local variable
- Typecasting in C
- Graphics
That was an example of how flags work.
What is the break command?
The break command ends the loop in which it is placed just as if the while condition, or the condition in a for loop becomes false.
How to declare an array?
An array can be defined as follows:
int temp[5]={45,56,12,98,12};
This would mean the following:
temp[0]=45....temp[4]=12
This was a single dimension array with 5 elements of the integer type.If you wanted to depict float variables just use float temp instead of int temp.
Let us now see an example of using an array for two tasks.
main()
{
int temps[31];
int index,total;
float average,celsius;
total=0.0;
for(index=0;index<31;index++)
{ printf("enter temperature #%d:",index);
scanf("%d",&temps[index]); }
for(index=0;index<31;index++)
total+=temps[index];
average=total/31.0
printf("average is:%f\n\n", average);
puts9"fahrenheit\tcelsius\n");
for(index=0;index<31;index++)
{
celsius=(5.0/9.0)*(temps[index]-32);
printf("%d\t\t%6.2f\n",temps[index],celsius);
} }
Now I am going to show you how to pass an array. When you pass an array you are actually passing the address of the array.
example-10
#define count 31 main() { int temps[count]; int index; float celsius; for(index=0; index< count;index++) { celsius=(5.0/9.0)*(heat[index]-32); printf("%d\t\t%6.2f\n",heat[index],celsius); } }
We are now going to look at
1)comparing strings
2)determining string lengths.
3) combining strings
4)structures.
Comparing 2 strings:>> In c it is not possible to directly compare two strings so a statement like if (string1==string2) is not valid.
Most c libraries contain a function called the strcmp().This is used to compare two strings in the following manner.
if(strcmp(name1,name2)==0)
puts("The names are the same");
else
puts("The names are not the same.");
Determining string length.:>> This is done using the strlen() function.
a simple programming bit showing this function looks like this:
gets(name);
count=strlen(name);
printf("the string %s has %d characters",name,count);
Combining strings:>>We use the function strcpy() an example follows:
Example-11
strcpy(name,"Adam");
strcpy(name1,"and eve");
strcat(name,name1);
puts(name);
The assumption being that adam and eve are two values of the variables name1 and name2. The end result is the combination of the 2 names.
main()
Notice how the main function comes after the definition of the structure. In the example above the cd was a cd disk and I was writing the definition of a cd collection program.
________________________________________
Now in the fifth hour I will show you how to output your data onto a disk.After all what is the use of the program if you can't save output to a disk.
Inorder to do this we have to use a pointer. The pointer in this case is FILE. The syntax to declare a file is :FILE*file_ponter;
The link between your program, the file and the computer is established with the fopen() function using the syntax shown below:
pointer=fopen("FILENAME","mode");
For example to create a file by the name cd.dat we do the following:
FILE*cdfile;
cdfile=fopen("CD>DAT","w");
If you will be reading from the file above use "r" instead "w" in the
second sentence.
In order to rpint information use the following command:
FILE*cdfile;
cdfile=fopen("PRN","w");
A file is closed by using the fclose() command.Next we will look at an exam ple of reading from a file.
example-13
#include "stdio.h"
main()
{
FILE*fp;
int letter;
if((fp=fopen("MYFILE","r"))==NULL)
{
puts("Cannot oepn the file");
exit();
}
while((letter=fgetc(fp)) !=eof)
printf("%c",letter);
fclose(fp);
}
The eof statement means end of file and this is included in the stdio.h header file which was declared at the start of the example. The stdio.h header file is one of many that comes with your compiler. So check your compiler specifics for other header files which will help perform other functions.
Now that you went through this tutorial you should be in a position to write simple programs and save it to a disk so you can give it your friends or even your boss. In no way the depth of c can be done in 5 hours but the nut and bolts can be learned that fast.Wher e you go from there depends upon your ambitions and hard work.
No comments:
Post a Comment