C Language में Program को लिखने का तरीका :
int main()
{
return 0 ;
}
C language में program लिखने के लिए सबसे पहले main() function लिखा जाता है | main () फंक्शन लिखना अनिवार्य होता है , क्योकि program execution main () से ही होता है |
और last में return 0 लिखा जाता है| return 0 main () function में 0 integer value value return करता है |
Data Type
c program में data value स्टोर करने के लिए पहले data type को define करना होता है | data type को define करने से machine को ये समझ में आ जाता है की user को अपने program को run कराने के लिए कितनी memory की जरुरत है , तथा किस प्रकार की value उस memory में store होगी |
different type की value store करने के लिए , different type के data type available है |
data type , 2 प्रकार के होते है :-
a) primary data type
b ) User define data type
a) primary data type
Primary data type c compiler में पहले से ही define रहते है |
1. char (1 byte )
2. int (2 byte )
3. float (4 byte )
4. double (8 byte )
5. void
b ) User define data type
ये data type user द्वारा define किये जाते है :-
1. Array
2. Structure
3. Union
4. Pointer
5 Enum etc
Scalar Data Type
Scalar data type में एक variable में एक समय में एक ही value store होती है | जैसे :-
1. arithmetic
a Integer
b Float
2. Pointer
3. Enum
Format specifier
different type के data type की value store और output value present कराने के लिए data type से related , format specifier का use किया जाता है | जैसे :-
1. int -> "%d "
2. float->%f
3 double->%lf
4 char ->%c
5 string ->%s
Token :
program में लिखी गई प्रत्येक individual unit token कहलाती है |
Token 6 प्रकार के होते है |
1 keyword
2 Identifier
3 constant
4 string
5 special symbol ({},())
6 Operator (+,-)
Identifier in C
C program में जिनकी किसी न किसी नाम के द्वारा एक निजी पहचान होती है , उन्हें C program में identifier कहते है |
जैसे की variable name , function name , array name |
Identifier लिखने के नियम :-
1. identifier लिखते समय आप character (a to z ,A to Z ) और digit (0 to 9 ) का use कर सकते हो |
2. identifier लिखते समय पहला letter character ही होगा |
3. underscore (_) के अलावा कोई दूसरा special symbol का use नहीं किया जाता है |
4. keyword का use नहीं कर सकते |
5 दो identifier के नाम कभी भी एक जैसे नहीं हनी चाइये |
C Compiler में 32 प्रकार के Keyword use किये जाते है |
Console Input /Output दो प्रकार क होते है |
1. Formatted Input /Output
2. Unformutted Input /Output
1. Formatted Input /Output
Use printf () and scanf ()
#include<stdio.h>
#include<conio.h>
int main()
{
char d[30];
printf("enter any character\n");
scanf("%s",d);
printf("%s",d);
gets(d);
puts(d);
}
2. Unformutted Input /Output
getchar () and putchar()
#include<stdio.h>
int main()
{
char d;
d=getchar();
putchar(d);
}
getche ()
putchar() , putch() की तरह putche () function का use नहीं किया जाता |
#include<stdio.h>
#include<conio.h>
int main()
{
char d;
puts("enter any character\n");
d=getche();
puts("\nthe output is " );
putch( d);
}
#include<stdio.h>
#include<conio.h>
int main()
{
char d[30];
puts("enter any character\n");
gets(d);
puts(d);
}
Program:
Keyword
C Compiler में 32 प्रकार के Keyword use किये जाते है |
- auto
- break
- case
- const
- char
- continue
- default
- do
- double
- else
- enum
- extern
- float
- for
- goto
- if
- int
- long
- register
- return
- short
- signed
- size of
- static
- struct
- switch
- typedef
- union
- unsigned
- volatile
- void
- while
Console Input /Output दो प्रकार क होते है |
1. Formatted Input /Output
2. Unformutted Input /Output
1. Formatted Input /Output
Formatted Function
|
||
Type
|
Input
|
Output
|
Char
|
Scanf()
|
Printf()
|
Int,float,string
|
Scanf()
|
Printf()
|
Use printf () and scanf ()
#include<stdio.h>
#include<conio.h>
int main()
{
char d[30];
printf("enter any character\n");
scanf("%s",d);
printf("%s",d);
gets(d);
puts(d);
}
2. Unformutted Input /Output
UnFormatted Function
|
||
Type
|
Input
|
Output
|
Char
|
getch()
getche()
getchar()
|
Putch()
Putchar()
|
string
|
gets()
|
Puts()
|
getchar () and putchar()
#include<stdio.h>
int main()
{
char d;
d=getchar();
putchar(d);
}
getche ()
putchar() , putch() की तरह putche () function का use नहीं किया जाता |
#include<stdio.h>
#include<conio.h>
int main()
{
char d;
puts("enter any character\n");
d=getche();
puts("\nthe output is " );
putch( d);
}
gets () and puts ()
#include<conio.h>
int main()
{
char d[30];
puts("enter any character\n");
gets(d);
puts(d);
}
Octal Constant:
#include<stdio.h>
int main()
{
int a=013;
printf("%d",a);
return 0;
}
Output:11
Hexadecimal Constant:
#include<stdio.h>
int main()
{
int a=0x13;
printf("%d",a);
return 0;
}
Output:19
Macros
#include<stdio.h>
#include<conio.h>
#define AB 10
int main()
{
printf("%d",AB);
#undef AB
printf("hii");
printf("%d",AB);
}
printf("%d",AB); line में error आएगी क्योकि AB को undefine कर दिआ गया है |
Type Conversion
Type Conversion दो प्रकार क होते है :-
जब different प्रकार के data type को operate किया जाता है, तब type conversion का use होता है |
(जैसे :- 2 +4. 5 ) यहाँ पे 2 integer type है , पर 4. 5 real type है |
1. Implicitly
2. Explicitly
1. Implicitly
Implicitly type conversion में compiler automatic ही data के type को convert करता है |
यदि दो different प्रकार के data type है तो compiler automatic ही answer को बढ़े data type में convert कर देता है , इसी को implicit conversion कहते है |
example:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=100,x;
char b='B';
x=a+b;
printf("%d",x);
float j=2.5;
j=x +j;
printf("\n%f",j);
}
Output:
166
168.50000
Explicitly
जब user different प्रकार के data type का use करता है तब implicitly conversion द्वारा compiler अपने आप ही result को बढ़े data type के अनुसार change कर देता है |
पर यदि user अपने अनुसार data convert करना चाहता है तब आपको explicit type conversion का use किया जाता है |
for example :
#include<stdio.h>
#include<stdlib.h>
int main()
{
float a=2.5;
int b=1,c;
c=(int)a+b;
printf("%d",c);
}
Output :
3
Ctype in C
Ctype.h , c compiler की library में मौजूद एक header file है | जिसके अंदर isalpha () ,isalnum () जैसे function शामील है , जिनका use pass किये गए character क बारे में जानकारी देने के लिए होता है | न
जैसे :-
1. #include<stdio.h>
#include<ctype.h>
int main()
{
int a= '5';
if isalnum(a)
printf("this is numeric value");
else
printf("not numeric value");
}
Output:
this is numeric value
2. #include<ctype.h>
int main()
{
int a= '6' ,b='\t',c='y';
if isalpha(a)
printf("\n this is numeric value");
else
printf("\n not numeric value");
if isalpha(b)
printf("\n this is numeric value");
else
printf("\n not numeric value");
if isalpha(c)
printf("\n this is numeric value");
else
printf("\n not numeric value");
}
2. #include<ctype.h>
int main()
{
int a= '6' ,b='\t',c='y';
if isalpha(a)
printf("\n this is numeric value");
else
printf("\n not numeric value");
if isalpha(b)
printf("\n this is numeric value");
else
printf("\n not numeric value");
if isalpha(c)
printf("\n this is numeric value");
else
printf("\n not numeric value");
}
Output:
not numeric value
not numeric value
this is numeric value
Print exact character
#include<stdio.h>
int main()
{
int b=4;
char a[20]="abcdefghi";
printf("%.5s",a);
printf("\n%.*s",b,a);
}
MCQ:
A) switch
B) goto
C) go back
D) return
2 Which of the following cannot be used as identifiers?
A) Spaces
B) Digits
C) Underscores
D) Letters
3. In the passage of text, individual words and punctuation marks are known as
3. In the passage of text, individual words and punctuation marks are known as
A) Constants
B) Keywords
C) Operators
D) Tokens
4 Choose the option that contains only valid hexadecimal integers.
A) 0x9F, 0xbcd, 0x1
B) 037, 0xx, 01000
C) 0x561u, 0x9h, 0xdd
D) H9F, HFF, HAA
5 Which one of the following are not valid variable names in C?
A) float_int, keyword, A1
B) ANSI, ascii, cpu
C) valid, variable, name
D) None of the above
6 Which of the following is a valid identifier?
A) 1return
B) return1
C) return
D) $return_1
7 Which of the following is a valid string constant?
A) “programming”
B) “programming
C) ‘programming
D) $ programming $
8 Which of the following language is predecessor to ‘C’ Programming Language?
A) A
B) B
B) B
C) ADA
D) C++
9 Which of the following a not a basic data type used in C language?
9 Which of the following a not a basic data type used in C language?
A) double
B) float
C) char
D) array
10 Which is an incorrect variable name?
10 Which is an incorrect variable name?
A) Id_No
B) ID_NO
C) IdNo
D) Id No
11 By default a real number is treated as a
A) float
B) double
C) long double
D) integer
12 ‘C’ Programming Language was developed and written by
A) Martin Richards
B) Dennis Ritchie
C) Ken Thompson
D) Herman Hellorith
13 Which of the following is false in ‘C’ Programming Language
A) Keywords can be used as variable names
B) Variable names can contain digits
C) Variable names do not contain blank spaces
D) Capital letters can be used in variable names.
14 By default a real number is treated as a
A) Float
B) double
C) long double
D) integer
15 What will be the output of the following program?
main()
{
int a; printf(“%d”,a);
}
main()
{
int a; printf(“%d”,a);
}
A) 0
B) 1
C) Error
D) Unpredictable Value
16 Suppose that x is initialized as: short int x; /* assume x is 16 bits in size */ What is the maximum number that can be printed using
16 Suppose that x is initialized as: short int x; /* assume x is 16 bits in size */ What is the maximum number that can be printed using
printf (“%d\n”, x),
A) 127
B) 128
C) 255
D) 32,767
55 Which of the following directive creates functions like macros?
55 Which of the following directive creates functions like macros?
A) #include
B) #define
C) #undef
D) #ifdef
56 Which format specification is used in printf statement to print hexadecimal format
A) %i
B) %c
C) %x
D) %u
57 size of operator returns the size in bytes of
A) identifier
B) type
C) identifier or type
D) array
59 Which of the following is a valid octal constant?
A) 32
B) 032
C) 049
D) 0x49
60 What will be the output of the following?
main()
{
int a = ‘A’;
printf(“%d”, a);
A) 65
B) A
C) a
D) the program will not compile as an integer variable is assigned a character constant.
61. Which one is incorrect statement for C Language?
A) C compiler supports octal integer constant.
B) C compiler supports hexadecimal integer constant.
C) C compiler supports binary integer constant.
D) C compiler supports decimal integer constant.
62. What will be the output of the following code?
int main()
{
int x,y,z;
x='1'-'0'; /* line-1 */
y='a'-'b'; /* line-2 */
z=x+y;
printf("%d",z); }
A) 0
B) Error because of incorrect line-1 only.
C) Error because of incorrect line-1 and line-2.
D) Error because of incorrect line-2 only.
63. Which of the following cannot be used as identifiers?
A) spaces
B) digits
C) underscores
D) letters
65. Identify the correct sequence of steps to run a program
A) Link, Load, Code, Compile & Execute
B) Code, Compile, Link, Execute & Load
C) Code, Compile, Link, Load & Execute
D) Compile, Code, Link, Load & Execute
67. What will be the output of following program
#include main()
{
int x,y = 10;
x = y * NULL;
printf(“\n %d \n", x);
}
A) error
B) 0
C) 10
D) Garbage value
3 Function of a compiler is to
A) put together the file and functions that are required by the program
B) translate the instructions into a form suitable for execution by the program
C) load the executable code into the memory and execute them
D) allow the user to type the program
4. find output
int x = 7538;
4. find output
int x = 7538;
printf("%d %d\n", x % 100, x / 10);
True/False
1. All of the following are expressions in c.
i. A=2+(b=5);
ii. A=b=c=5;
iii. A=11%3;
2 . Expression if a(=6).
3 . A variable is a string that varies during program execution.
4 . C language cannot be used for database manipulation.
5 . In the expression a=b=5 the order of assignment is not decided by associativity of operators.
6 . A printf statement can be used to display output on different lines.
7. # is used to insert comments in the program.
-> Calling a uninitialized variable will provide zero value.
8. #define is known as preprocessor compiler directive.
9. Sizes of short integer and long integer would vary from one platform to another.
10. If we have to execute the program with different set of inputs, we need to recompile the program each time.
11. Compiler translates the of source code into object code before the program can be executed.
12. The programming language happens to be the high level language with some assembly language features.
13. In C the graphics may be used to add graphical features to the program.
14. Every variable in c have three most fundamental attributes: Name, Value, Address.
The operating system converts the ‘C’ program into machine language.
15. Every function in ‘C’ must have a “return” statement.
16. C is an object oriented programming language.
17. Sizes of short integer and long integer would vary from one platform to another.
A preprocessor directive is a message from compiler to the linker.
18. cout and cin can be used for output and input respectively in ‘C’ language.
19. In computer programming, the translation of source code into object code is done by a compiler.
20. Scalar data types are not supported by ‘C’ language.
21. ‘C’ programs are converted into machine language with the help of an interpreter.
22. Every ‘C’ program must contain a main( ).
23. Putchar( ) is used only for single character input
24. 36 A printf( ) statement can generate only one line of output.
25. The #undef directive removes a name previously defined with #define directive
26.
27. In ‘C’, unsigned int can have maximum range of values between 0 to 65535.
27. In ‘C’, unsigned int can have maximum range of values between 0 to 65535.
28. An escape sequence begins with a backward slash followed by an alphabetical character
29. NULL is a keyword in C.
30. 1.0 is an example of double constant and float constant.
31. #define is used to define symbolic constant.
32. const is a keyword that is used to define symbolic constant.
38 int main()
{
int a;
scanf("%c",&a);
printf("%c",a);
return 0;
}
is their any error in above code.
39.There is no difference between '\0' and '0'.
40.Comments in the program make debugging of the program easier
41. The statement void p; is valid.
42. An algorithm is a graphical representation of the logic of a program.
43.Calling a uninitialized variable will provide zero value.
44. Linking is the process of putting together other program file and functions that are required by the program
45. Algorithm is the graphical representation of logic.
46. 20. The programming language happens to be the high level language with some assembly language features.
47. A preprocessor directive is a message from compiler to the linker.
48. cout and cin can be used for output and input respectively in ‘C’ language.
49. In computer programming, the translation of source code into object code is done by a compile
Question:
2. In the following declaration statement: char c=’A’; Variable c stores one byte of memory space while character constants ‘A’ stores one byte memory space. How one byte variables can stores two byte character constant? What is automatic type promotion in c?
3. What are preprocessor directions? Why do we need them? Explain various preprocessor directives?
4. How Compilation, Linking and Loading are related? Also explain the basic task of a Compiler, Linker and Loader?
8. What is the significance of header file in a C Program? What do the header files usually contain?
9. How Compilation, Linking and Loading are related? Also explain the basic task of a Compiler, Linker and Loader.
11. WAP for swaping two variable
12. What are the commonly used input functions in ‘C’? Write their syntax and explain the purpose of each.
13. What are logical, syntactic and execution errors? Give examples of each. Which is most difficult to find and why
14. Enumerate features of a good ‘C’ program. Describe the commonly used techniques as to how ‘C’ programs can be made highly readable and modifiable.
15. What is an algorithm?
16. ‘C’ compiler supports many pre-processor commands. Write their names only.
17 What is an execution error? Differentiate it from syntactic error. Give examples
18 It is said that ‘C’ is a middle level assembly language. Mention those features of ‘C’ which enable this description.
19 It is said that “C is a middle level language and is good for system level programming.” Describe three facilities available in ‘C’ which support this statement.
20. What do you understand by loading and linking of a program?
21. Explain the role of linker and loader in compilation.
23. Write a ‘C’ program to swap two variables without using third variable.
24. Write a ‘C’ program in which a scanf() function can read a complete paragraph of text.
25. How can you create your own header file in ‘C’ programming? Briefly explain.
27 #include<stdio.h>
#include<conio.h>
int main()
{
char d;
puts("enter any character\n");
gets(d);
puts(d);
}
29. Explain the working of shorthand assignment operators, pre and post increment operator and the ternary operator.
30. Differentiate declaration and definition of a variable.
31. 34 What is meant by formatted output? Mention the output of the following commands.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=10;
printf("hello");
exit(0);
printf("heelo2");
}
33. A double data type number uses 64 bits giving a precision of 14 digits.
34. Every C program must have at least one main ( ) function section.
35. Is the following statement valid in C? int my_num=100,000;
36. We need to recompile the program before executing it even if there is no change in the source code.
37 The default precision for printing of floating point values is 6. If the floating-point number is more precise than 6 digits, it will be rounded. 38 int main()
{
int a;
scanf("%c",&a);
printf("%c",a);
return 0;
}
is their any error in above code.
39.There is no difference between '\0' and '0'.
40.Comments in the program make debugging of the program easier
41. The statement void p; is valid.
42. An algorithm is a graphical representation of the logic of a program.
43.Calling a uninitialized variable will provide zero value.
44. Linking is the process of putting together other program file and functions that are required by the program
45. Algorithm is the graphical representation of logic.
46. 20. The programming language happens to be the high level language with some assembly language features.
47. A preprocessor directive is a message from compiler to the linker.
48. cout and cin can be used for output and input respectively in ‘C’ language.
49. In computer programming, the translation of source code into object code is done by a compile
Match the following
(A)
1.
|
Smallest individual unit in a program is known as
|
A
|
integer
|
2.
|
“2 “ is known as
|
B
|
4
|
3.
|
Control string required to print long int
|
C
|
ceil
|
4.
|
Default return type in any function is
|
D
|
String Constant
|
5.
|
Logical not operator is
|
E
|
%ld
|
6.
|
Size of long int n byte
|
F
|
C Tokens
|
7.
|
Terminates program
|
G
|
Unary
|
8.
|
Explicit conversion
|
H
|
main()
|
9.
|
Unary operator
|
I
|
three
|
10.
|
Number of operators required in ?: operator
|
J
|
/*
|
11.
|
To insert the comments in the program this symbol is used
|
K
|
exit()
|
12.
|
Size of float and double in bytes
|
L
|
Typecasting
|
13.
|
Round off a value 1.66 to 2.0
|
M
|
++
|
14.
|
Input/output function prototypes and macros are defined in
|
N
|
header
|
15.
|
This is a mandatory function in every ‘C’ program. Execution starts from this function
|
O
|
4,8
|
(B)
18.
|
Preprocessor commands are also known as
|
A
|
Converts a data type to another data type
|
19.
|
Formatted print is used to
|
B
|
Stdio.h
|
20
|
exit(0) in a ‘C’ program represents
|
C
|
‘a’ is a single character constant and “a” is a string character constant
|
21
|
Typecasting
|
D
|
directives
|
22
|
The difference in ‘a’ and “a” is
|
E
|
!
|
23
|
F
|
Take printout in special format
| |
24
|
The operator && is an example of
|
G
|
directives
|
25
|
Header files in ‘C’ contain
|
H
|
#
|
26
|
I
|
logical
| |
27
|
Preprocessor commands are always preceded by
|
J
|
main()
|
28
|
The bitwise AND operator is used for
|
K
|
\0
|
29
|
exit( ) function is used to terminate the
|
L
|
b=a
|
30
|
Null character is represented by
|
M
|
Library functions
|
18.
|
Preprocessor commands are also known as
|
N
|
Take printout in special format
|
19.
|
Formatted print is used to
|
O
|
Program
|
P
|
Termination of a program
| ||
Q
|
masking
| ||
(c)
31
|
Operator for giving remainder
|
g
|
Binary file
|
32
|
h
|
masking
| |
33
|
assignment of a with b
|
i
|
a^=b^=a^=b
|
34
|
Which is not possible in C?
|
j
|
program
|
35
|
Which is not portable?
|
k
|
%
|
36
|
Compiler converts a source File to
|
l
|
Static variable
|
37
|
Reserved word
|
m
|
?:
|
38
|
Swap a and b without temporary variable
|
n
|
Header File
|
39
|
o
|
Typecasting
| |
40
|
A sequence of bytes flowing into or out of program
|
p
|
keyword
|
41
|
An operator expressed in three part expression
|
q
|
Object file
|
42
|
An external source file that contains declarations and definitions.
|
r
|
a=b
|
43
|
Converting to a different data type
|
s
|
stream
|
44
|
Pictorial representation of logic
|
t
|
#define
|
45
|
Defining constants
|
u
|
Declaration
|
46
|
Space allocation to variables
|
v
|
nothing
|
w
|
Pictorial representation of logic
|
Question:
1. Define void data type
2. In the following declaration statement: char c=’A’; Variable c stores one byte of memory space while character constants ‘A’ stores one byte memory space. How one byte variables can stores two byte character constant? What is automatic type promotion in c?
3. What are preprocessor directions? Why do we need them? Explain various preprocessor directives?
4. How Compilation, Linking and Loading are related? Also explain the basic task of a Compiler, Linker and Loader?
5. Distinguish between compiler error and runtime error with the help of an example
6. What is a preprocessor and what are the advantages of preprocessor? What are the facilities provided by preprocessor?
7. What is meant by formatted output? Mention the output of the following commands. int n = 28; i) printf ( “% 5d,”n); ii) printf ( “%+5d”, n); iii) printf ( “%+5d”, n);
8. What is the significance of header file in a C Program? What do the header files usually contain?
9. How Compilation, Linking and Loading are related? Also explain the basic task of a Compiler, Linker and Loader.
10. List out the rules to declare a valid variable in ‘C’ program.
11. WAP for swaping two variable
12. What are the commonly used input functions in ‘C’? Write their syntax and explain the purpose of each.
13. What are logical, syntactic and execution errors? Give examples of each. Which is most difficult to find and why
14. Enumerate features of a good ‘C’ program. Describe the commonly used techniques as to how ‘C’ programs can be made highly readable and modifiable.
15. What is an algorithm?
16. ‘C’ compiler supports many pre-processor commands. Write their names only.
17 What is an execution error? Differentiate it from syntactic error. Give examples
18 It is said that ‘C’ is a middle level assembly language. Mention those features of ‘C’ which enable this description.
19 It is said that “C is a middle level language and is good for system level programming.” Describe three facilities available in ‘C’ which support this statement.
20. What do you understand by loading and linking of a program?
21. Explain the role of linker and loader in compilation.
23. Write a ‘C’ program to swap two variables without using third variable.
24. Write a ‘C’ program in which a scanf() function can read a complete paragraph of text.
25. How can you create your own header file in ‘C’ programming? Briefly explain.
26 #include<stdio.h>
int main()
{
char d;
d=getchar();
putchar(d);
}
If You enter input as "C Programming " then what output has to be shown
#include<conio.h>
int main()
{
char d;
puts("enter any character\n");
gets(d);
puts(d);
}
is this program इस true और not
28. What is void data type? Write any three use of void data type.
29. Explain the working of shorthand assignment operators, pre and post increment operator and the ternary operator.
30. Differentiate declaration and definition of a variable.
31. 34 What is meant by formatted output? Mention the output of the following commands.
int n = 28; i) printf ( “% 5d,”n); ii) printf ( “%+5d”, n); iii) printf ( “%+5d”, n);
32. output:
#include<stdlib.h>
int main()
{
int a=10;
printf("hello");
exit(0);
printf("heelo2");
}
19 What is meant by formatted output? Mention the output of the following commands.
int n = 28;
i) printf ( “% 5d,”n);
ii) printf ( “%+5d”, n);
iii) printf ( “%+5d”, n);
This comment has been removed by the author.
ReplyDeleteThanks mem for your worked for us
ReplyDeleteThanks mem
ReplyDelete