Apply for Zend Framework Certification Training

c



< Introduction to C Programming How to print in c in different ways >



16. Escape Sequences
Escape sequences are special character combinations beginning with \.
Common escape sequences
Escape Meaning
\n New line
\t Horizontal tab
\b Backspace
\r Carriage return
\\ Backslash
\' Single quote
\" Double quote
\0 Null character
Example
#include <stdio.h>
int main(){
    printf("Hello\nWorld");
    return 0;
}
Output:
Hello
World
Tab
printf("Name\tAge");
Output:
Name    Age
Double quote
printf("He said \"Hello\"");
Output:
He said "Hello"
17. Type Conversion
Type conversion means converting a value from one data type to another.
There are two major types:
Implicit conversion
Explicit conversion
17.1 Implicit Type Conversion
The compiler automatically performs the conversion.
Example:

#include <stdio.h>
int main(){
    int a = 10;
    float b = 2.5;
    float result = a + b;
    printf("%f", result);
    return 0;
}
Here a is automatically converted to float during the calculation.
Conceptually:
int → float
18. Explicit Type Conversion
The programmer explicitly converts one type to another using a cast.
Example:

#include <stdio.h>
int main(){
    int a = 10;
    int b = 3;
    float result = (float)a / b;
    printf("Result = %f", result);
    return 0;
}
Output:
Result = 3.333333
Without the cast:
float result = a / b;
the division is performed as integer division first, producing:
3
The cast:
(float)a
makes the division floating-point.
19. Coding Standards
Coding standards are rules and conventions that make programs:
Easy to read
Easy to understand
Easy to maintain
Less error-prone
Easier to debug
1. Use meaningful variable names
Poor:
int x;
int y;
Better:
int studentAge;
int totalMarks;
2. Use consistent indentation
Poor:
if(age>=18){
printf("Adult");
}
Better:
if (age >= 18){
    printf("Adult");
}
3. Use comments appropriately
// Calculate total marks
total = marks1 + marks2;
For multiple lines:
/*
   Calculate the average
   of three marks.
*/
average = total / 3.0;
4. Use constants for fixed values
Instead of:

area = 3.14159 * radius * radius;
use:
const float PI = 3.14159f;
area = PI * radius * radius;
5. Avoid unnecessary global variables
Prefer local variables when global scope is not required.
6. Keep functions focused
Instead of putting everything into main(), divide a large program into meaningful functions.
int calculateSum(int a, int b){
    return a + b;
}
20. Best Programming Practices
Practice 1: Initialize variables
Instead of:
int total;
when an initial value is appropriate:
int total = 0;
Practice 2: Check input
For example:

int result = scanf("%d", &age);
if (result != 1){
    printf("Invalid input");

 

< Introduction to C Programming How to print in c in different ways >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top