Upload
others
View
10
Download
0
Embed Size (px)
Citation preview
C programming language
Syllabus
Programming concept:
Fundamental of C and
Operator in C and
Function array string pointer
C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. C is the most widely used computer language.
extension ".c";
A C program basically consists of the following parts −Preprocessor CommandsFunctionsVariablesStatements & ExpressionsComments
#include
int main() {/* my first program in C */printf(“hello \n");
return 0;}
Token in ca token is either a keyword, an identifier, a constant, a
string literal, or a symbol. For example, the following C statement consists of five tokens
SemicolonsComments
IdentifiersA C identifier is a name used to identify a variable, function, or any other user-defined item. An identifier starts with a letter A to Z, a to z, or an underscore '_' followed by zero or more letters, underscores, and digits (0 to 9)
C does not allow punctuation characters such as @, $, and % within identifiers
auto else long Switch
break enum register Typedef
case extern return Union
char float short Unsigned
const for signed Void
continue goto sizeof Volatile
default if Static While
do int Struct _Packed
Whitespace in CA line containing only whitespace, possibly with a comment, is known as a blank line, and a C compiler totally ignores it.
Data types in c
Basic Types
(a) integer types and (b) floating-point types.
Derived types
They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types.
Type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 or 4 bytes -32,768 to 32,767
unsigned int 2 or 4 bytes 0 to 65,535
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes
long 8 bytes
unsigned long 8 bytes
Type Storage size Value range
float 4 byte 1.2E-38 to 3.4E+38
double 8 byte 2.3E-308 to 1.7E+308
long double 10 byte 3.4E-4932 to 1.1E+4932
A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines the size and layout of the variable's memory;
The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because C is case-sensitive.
Variable Definition in C
Int x
Defining ConstantsThere are two simple ways in C to define constants −Using #define preprocessor.Using const keyword
#define int age=25;Const int age = 30;
We have four different storage classes in a C program −autoregisterstaticExtern
auto storage class is the default storage class for all local variables.
The register storage class is used to define local variables that should be stored in a register instead of RAM
The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program
The extern storage class is used to give a reference of a global variable that is visible to ALL the program files
Arithmetic OperatorsRelational OperatorsLogical OperatorsBitwise OperatorsAssignment OperatorsMisc Operators
Decision condition
If
If-else
Nested if
switch statement allows a variable to be tested for equality against a list of values.
Loop
A while loop in C programming repeatedly executes a target statement as long as a given condition is true.
while(condition) {
statement(s);}
/* while loop execution */while( a < 20 ) {
printf("value of a: %d\n", a);a++;
}
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
for ( init; condition; increment ) {statement(s);
}
A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time.
The syntax of a do...while loop in C programming language is −
do {statement(s);
} while( condition );
/* do loop execution */do {
printf("value of a: %d\n", a);a = a + 1;
}while( a < 10);
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type
Int a[5];
Initialize a array
Int a[5]={4,6,7,4,7}
pointer in C language is a variable which stores the address of another variable.
int n = 30;int* p = &n; // Variable p of type pointer is pointing to the address of the variable n oftype integer.
The string can be defined as the one-dimensional array of characters terminated by a null ('\0'). The character array or the string is used to manipulate text such as word or sentences
There are two ways to declare a string in c language.
By char array
By string literal
char ch[6]={‘v', ‘i', 'v', ‘e', ‘k', '\0'};
char ch[]=“vivek";
The gets() function enables the user to enter some characters followed by the enter key
The puts() function is used to print the string
The fscanf() function is used to read set of characters from file
The fputc() function is used to write a single character into file. It outputs a character to a stream.
The fgetc() function returns a single character from the file. It gets a character from the stream. It returns EOF at the end of file.
The maximum combined length of the command-line arguments including the spaces between adjacent arguments is
a. 128 charactersb. 256 charactersc. 67 charactersd. It may vary from one operating system to another
In the following code, the P2 is Integer Pointer or Integer?
typedef int *ptr;ptr p1, p2;
a. Integerb. Integer pointerc. Error in declarationd. None of the above
Input/output function prototypes and macros are defined in which header file?
a. conio.hb. stdlib.hc. stdio.hd. dos.h
Which standard library function will you use to find the last occurance of a character in a string in C?
a. strnchar()b. strchar()c. strrchar()d. strrchr()
What do the following declaration signify?
char **argv;
a. argv is a pointer to pointer.b. argv is a pointer to a char pointer.c. argv is a function pointer.d. argv is a member of function pointer
Which of the following is not a valid variable name declaration?a) int _a3;b) int a_3;c) int 3_a;d) int _3a
All keywords in C are in ____________a) LowerCase lettersb) UpperCase lettersc) CamelCase lettersd) None of the mentioned
Which of the following is not a valid C variable name?a) int number;b) float rate;c) int variable_count;d) int $main