Basics of c programming


This article gives you A TO Z details about the basics of C programming , in question and answer format , which is very useful for beginners of C and also higher secondary and Engineering college students

The status of this resource is Pending and require some modification by the author.

C PROGRAMMING BASICS

1. What are the different data types available in "C"?
• C supports three classes of data types:
o Basic / Primary / Build-in data types
? int
? float
? char
? double
o Derived data types
? Array, pointer,
? Structure union and
? functions
o User-defined data types
? typedef
? enum

2. What is an Operator, Operand and Keywords?
Operator
• An operator is a symbol that specifies an operation to be performed on operands.
• Example: *, +, -, / are called arithmetic operators.
Operand
• The data items that operators act upon are called operands. Example: a+b; In this statement a and b are called operands.
Keywords
• Keywords are certain reserved words that have standard and pre-defined meaning in C'. These keywords can be used only for their intended purpose.

3. Define Constants in C. Mention the types.
• The constants refer to fixed values that the program may not alter during its execution.
• These fixed values are also called literals.
• The constants are treated just like regular variables except that their values cannot be modified after their definition.
• For example: 1, 2.5, "Programming is easy." etc.
• Types of constants are


4. i) What is Ternary operators or Conditional operators?
.

i) Ternary operators or Conditional operators
• Conditional operator itself checks the condition and executes the statement depending on the condition.
• It is also called as ternary operator.
• Syntax:
Condition ? exp1:exp2;

• The '?' operator acts as a ternary operator.
• Ternary operator works as follows,
? First, the conditions will be evaluated.
? if the condition is true exp1 is evaluated,
? if the condition is false exp2 is evaluated.
• Conditional operator works like if-else statement.
• Example:
a=10; b=15;
x = (a>b) ? a:b;
• Output:
x= b;
ii) Example for Ternary operator
/* Program for conditional Operator */
#include
main()
{
int a=5,b=3,x;
x=a>b?a:b;
printf("x is %d", x);
}
Output: x is 5

5.What are the Bitwise operators and logical operators available in C ?

Bitwise operators
Operator Meaning
& Bitwise AND
| Bitwise OR
~ One's complement
^ Bitwise XOR
<< Left shift
>> Right shift

Logical operators
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT

6. What is a Variable? Illustrate it with an example.
• The value of the variable can be changed during the execution of a program.
• C variable might be belonging to any of the data type like int, float, char etc.
• Each variable should be given a unique name called identifier.
• Variable name must begin with letter or underscore.
• Example:
Sum, average, etc

7. What is the difference between Logical AND and Bitwise AND?
• Logical AND (&&): Only used in conjunction with two expressions, to test more than one condition. If both the conditions are true the returns 1. If false then return 0.
• AND (&): Only used in Bitwise manipulation. It is a unary operator.

8. (i)What is the difference between "=" and "= =" operator?
• Where = is an assignment operator and == is a relational operator.
• Example: i=5 ?assigns value 5 to variable i, if(i==5)?checks variable i equal to 5 or not

(ii)What is the difference between 'a' and "a"?
'a' is a character constant and "a" is a string.

9. What is type casting?
• Type casting is the process of converting the value of an expression to a particular data type.
• Example: int x, y; c = (float) x/y; where a and y are defined as integers. Then the result of x/y is converted into float.

10. What are various types of C operators?
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment & decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators

11. What are the main features and applications of C language?
C is case sensitive language.
• Modularity: we can split the C program into no. of modules. It allows reusability of modules.
• Middle level language: as a middle level language C combines both the advantages of low level and high level languages. (arrays, pointers etc). Efficient Use of Pointers
• General purpose programming language: C can be used to implement any kind of applications such as math's oriented, graphics, business oriented applications.
• Portability: we can compile or execute C program in any operating system (unix,dos,windows).
• Powerful programming language: C is very efficient and powerful programming language, it is best used for data structures and designing system software.

12. What is the difference between while loop and do…while loop? (or) Differentiate Entry and Exit controlled constructs.

While do-while
Its an entry controlled loop Its an exit controlled loop
In the while loop the condition is first executed. If the condition is true then it executes the body of the loop. When the condition is false it comes of the loop In the do…while loop first the statement is executed and then the condition is checked. The do…while loop will execute at least one time even though the condition is false at the very first time
Syntax:
while(condition)
{
//body of the loop
} Syntax:
do
{
//body of the loop
} while(condition);

13. What is a Modulo Operator? What is the use of sizeof( ) operator?
Modulo Operator
• '%' is modulo operator. It gives the remainder of an integer division.
• Example: a=17, b=6. Then c=%b gives 5.

Sizeof( ) operator
• The sizeof ( ) operator gives the bytes occupied by a variable.
• No of bytes occupied varies from variable to variable depending upon its data types.
o Example:
int x,y;
printf("%d",sizeof(x));
o Output: 2

14. Define with example integer and floating type of data in C language.
• The int is used to define integer numbers. An integer occupies 2 bytes memory space and its value range limited to -32768 to +32767 (that is, -215 to +215-1)
o Example: int count=5

• The float is used to define floating point numbers. The float data type is used to store fractional numbers (real numbers) with 6 digits of precision. Floating point numbers are denoted by the keyword float.
o Example: float miles = 4.5

15. What is the difference between ++a and a++?

• ++a means do the increment before the operation (pre increment)
• a++ means do the increment after the operation (post increment)
• Example:
a=5;

x=a++; /* assign x=5 but 'a' value is incremented to 6*/
y=a; /*now y assigns y=6*/
x=++a; /*assigns x=7 and a value is incremented to 7*/
16. What are the types of I/O statements available in C?
scanf
printf
getch
putch
getchar
putchar


17. What is a program?
• A program is a set instruction written to carry out a particular task, so that computer can perform some specified task. Example program to find sum of 2 numbers
• Example program
#include
void main()
{
int a, b, sum;
printf("\nEnter two no: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum : %d", sum);
}
Output :
Enter two no: 5 6
Sum : 11

18. What is a global variable?
• The global variable is a variable that is declared outside of all the functions.
• The global variable is stored in memory, the default value is zero.
• Scope of this variable is available in all the functions.
• The variable is live as long as the program terminates.
• Example:
#include
int a,b; //a,b are global variables
main()
{...
}

19. Construct an infinite loop using while?
while (1)
{ }
Here 1 is a non zero value, so the condition is always true. So it is an infinite loop.


20. What are the Escape Sequences present in "C"
Escape Sequence Symbol Escape Sequence Name
\n New Line
\t Tab space
\r Carriage return
\f Form feed

21. Write the limitations of getchar( ) and scanf( ) functions for reading strings
• getchar( )-To read a single character from stdin, then getchar() is the appropriate.
• scanf( ) allows to read more than just a single character at a time. In scanf() when there is a blank was typed, the scanf() assumes that it is an end

22. What is the difference between scanf() and gets() function?
• In scanf() when there is a blank was typed, the scanf() assumes that it is an end.scanf() accepts any datatype using appropriate format specifier. gets() assumes the enter key as end.
• That is gets() gets a new line (\n) terminated string of characters from the keyboard and replaces the '\n' with '\0'. It accepts the input as only strings

23.Write a for loop statement to print numbers from 10 to 1.
#include
void main()
{
int i;
for( i=10; i>=1; i--)
printf("\n%d",i);
}
Output:
10 9 8 7 6 5 4 3 2 1

24.Write a C Program to find factorial of a given number using iteration.
#include
int main()
{
int i=1,f=1,num;
printf("Enter a number: ");
scanf("%d",&num);
while(i<=num)
{
f=f*i;
i++;
}
printf("Factorial of %d is: %d",num,f);
return 0;
}
Output:
Enter a number: 5 Factorial of 5 is 120

25.Define Compilation, linking process?
• Compilation refers to the processing of source code files (.c, .cc, or .cpp) and the creation of an 'object' file. This step doesn't create anything the user can actually run. Instead, the compiler merely produces the machine language instructions that correspond to the source code file that was compiled.

• Linking refers to the creation of a single executable file from multiple object files. In this step, it is common that the linker will complain about undefined functions (commonly, main itself). During compilation, if the compiler could not find the definition for a particular function, it would just assume that the function was defined in another file. If this isn't the case, there's no way the compiler would know it doesn't look at the contents of more than one file at a time. The linker, on the other hand, may look at multiple files and try to find references for the functions that weren't mentioned.

26. Define with example integer and floating type of data in C language.
The int is used to define integer numbers. An integer occupies 2 bytes memory space and its value range limited to -32768 to +32767 (that is, -215 to +215-1). . Floating point numbers are denoted by the int float.
int count
count=5
The float is used to define floating point numbers. The float data type is used to store fractional numbers (real numbers) with 6 digits of precision. Floating point numbers are denoted by the keyword float.
float miles
miles = 4.5

27. . What are the main features and applications of C language?
C programming language that was capable of both high level, machine independent
programming and would still allow the programmer to control the behavior of individual bits of information. C includes bitwise operators along with powerful pointer manipulation capabilities.

28. What is the difference between while loop and do…while loop?.
In the while loop the condition is first executed. If the condition is true then it executes the body of the loop. When the condition is false it comes of the loop. In the do…while loop first the statement is executed and then the condition is checked. The do…while loop will execute at least one time even though the condition is false at the very first time.

29.What is the importance of keywords in C?
Keywords are certain reserved words that have standard and pre-defined meaning in =C'.These keywords can be used only for their intended purpose.

30.List the various input and output statements in C?
Input : In any programming language input means to feed some data into program. This can be given in the form of file or from command line. C programming language provides a set of built-in functions to read given input and feed it to the program as per requirement.
Output : In any programming language output means to display some data on screen, printer or in any file. C programming language provides a set of built-in functions to output required data.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: