dimanche 28 juin 2015

What is the difference between ' ' and " "? [duplicate]

This question already has an answer here:

I was creating a simple program to count the words and vowels of a string, and when I try to use double quotes inside of a 'for' or an 'if' statement it gives an erro. But when I change it for a single quote it works pretty good.

I thought they were the same thing, so what is the difference between them and why I cannot use double quotes inside of a statement or single quote inside a 'printf' function?

PS: I'm using code blocks as my IDE.

Here is my code, so you can see an example:

#include <stdio.h>
#include <conio.h>

int main(void){
    // Declaracao de Variaveis
    int cont_p = 0, cont_v = 0, i;
    char frase[1000];

    // Leitura de Dados
    printf("Digite a frase:\n");
    gets(frase);

    // Logica e Contagemm
    for (i=0; frase[i]!='\0';i++){
        if(frase[i] == ' '){
            cont_p = cont_p +1;
        }else if ((frase[i] == 'a') || (frase[i] == 'A')) {
            cont_v = cont_v + 1;
        }else if ((frase[i] == 'e') || (frase[i] == 'E')) {
            cont_v = cont_v + 1;
        }else if ((frase[i] == 'i') || (frase[i] == 'I')) {
            cont_v = cont_v + 1;
        }else if ((frase[i] == 'o') || (frase[i] == 'O')) {
            cont_v = cont_v + 1;
        }else if ((frase[i] == 'u') || (frase[i] == 'U')) {
            cont_v = cont_v + 1;
        }

    }
    cont_p++;

    // Exibindo o Resultado
    printf("\n\nNumero de PALAVRAS: %d\n", cont_p);
    printf("Numero de VOGAIS: %d\n\n\n", cont_v);
    printf("Programa Finalizado!\n");
    getch();
    return 0;
}

Embedding compile time information into binary

Suppose I have a variable date which is defined with extern in source code, i.e, extern date; then I want to assign a value to it at link time getting time from the computer on which it is compiled and assign to date variable. Is there a way to do that for example in gcc?

How to read the gpt partition table

To read the MBR partition table we use an offset of 0x1be , similarly what is the offset for reading the GPT partition table entries (ie, number of partitions and their sizes).

Iam writing a C program using "gdisk" to create the partitions,I need to write the filesystems onto these partitions. So for this i need to read the gpt header to get the number of partitions and their allocated sizes.

how this custom toupper() function works?

I've seen following program that uses custom toupper() function.

#include <stdio.h> 
void my_toUpper(char* str, int index)
{
    *(str + index) &= ~32;
}
int main()
{
    char arr[] = "geeksquiz";
    my_toUpper(arr, 0);
    my_toUpper(arr, 5);
    printf("%s", arr);
    return 0;
}

How this function works exactly? I can't understand logic behind it. It will be good If someone explains it easily.

Scaling an 8-bit Greyscale RAW in C

Currently, I'm trying to scale a 320x200, 8-bit RAW image to whatever size the user specifies as their preferred resolution. If their resolution is 320x200, It simply uses fread to read the data directly to fill it in, otherwise it'll double all pixels horizontally, producing a 640x200 image. However, this isn't what I want to do, I want to scale exactly to the value of PIXX/PIXY, even if it isn't a multiple. How would I do this?

Here's the important part of the code:

FILE    *f;

int x,y;
int x1,y1;

int c;

char    *p;

f = dopen("art",name,"rb");
if (f == 0) GTFO("Unable to open title");

p = vpage;

if ((PIXX == 320) && (PIXY == 200))
{
    fread(vpage,320,200,f);
}
else
{
    for (y1 = 0; y1 < 200; y1++)
    {
        for (x1 = 0; x1 < 320; x1++)
        {
            c = getc(f);

            *p = c;
            *(p + 1) = c;
            p += 2;
        }
    }
}

fclose(f);

If a function or libary exists that can take the image produced by fread, and perform linear scaling, outputting back to 8-bit raw would be excellent. The image gets stored in vpage.

EDIT: Here's my attempt to use the Bresenham algo, it creates garbage for some reason, but scales the garbage correctly, haha.

#include "imgscale.h"

void ScaleLine(unsigned char *Target, unsigned char *Source, int SrcWidth, int TgtWidth)
{
    int NumPixels = TgtWidth;
    int IntPart = SrcWidth / TgtWidth;
    int FractPart = SrcWidth % TgtWidth;
    int E = 0;
    while (NumPixels-- > 0)
    {
        *Target++ = *Source;
        Source += IntPart;

        E += FractPart;
        if (E >= TgtWidth)
        {
            E -= TgtWidth;
            Source++;
        } /* if */
    } /* while */
}

#define average(a, b)   (unsigned char)(( (int)(a) + (int)(b) ) >> 1)
void ScaleLineAvg(unsigned char *Target, unsigned char *Source, int SrcWidth, int TgtWidth)
{
    int NumPixels = TgtWidth;
    int Mid = TgtWidth / 2;
    int E = 0;
    char p;

    if (TgtWidth > SrcWidth)
    {
        NumPixels--;
    }

    while (NumPixels-- > 0)
    {
        p = *Source;

        if (E >= Mid)
        {
            p = average(p, *(Source+1));
        }

        *Target++ = p;
        E += SrcWidth;
        if (E >= TgtWidth)
        {
            E -= TgtWidth;
            Source++;
        } /* if */
    } /* while */

    if (TgtWidth > SrcWidth)
    {
        *Target = *Source;
    }
}

void ScaleRect(unsigned char *Target, unsigned char *Source, int SrcWidth, int SrcHeight, int TgtWidth, int TgtHeight)
{
    int NumPixels = TgtHeight;
    int IntPart = (SrcHeight / TgtHeight) * SrcWidth;
    int FractPart = SrcHeight % TgtHeight;
    int E = 0;
    char *PrevSource;
    while (NumPixels-- > 0)
    {
        if (Source == PrevSource)
        {
            memcpy(Target, Target-TgtWidth, TgtWidth*sizeof(*Target));
        }
        else
        {
            ScaleLine(Target, Source, SrcWidth, TgtWidth);
            PrevSource = Source;
        } /* if */

        Target += TgtWidth;
        Source += IntPart;
        E += FractPart;

        if (E >= TgtHeight)
        {
            E -= TgtHeight;
            Source += SrcWidth;
        } /* if */
    } /* while */
}

Strange Endianness Behaviour in C [duplicate]

This question already has an answer here:

Here's the C code:

/*Creates an integer of size 4 bytes (on my computer)
  and the value of each byte is equal to the ascii
  values of the characters 'A', 'B', 'C', 'D'*/

int num = ('A' << 24) | ('B' << 16) | ('C' << 8) | 'D';
char *pNum = &num;
printf("%c %c %c %c\n", pNum[0], pNum[1], pNum[2], pNum[3]);
printf("%c %c %c %c", *pNum, *pNum++, *pNum++, *pNum++);

The output is:

D C B A

A B C D

Why is there a difference in the output?

Why does (*p=*p) & (*q=*q); in C trigger undefined behavior

Why does (*p=*p) & (*q=*q); in C trigger undefined behavior if p and q are equal.

int f2(int * p, int * q)
{
  (*p=*p) & (*q=*q);
  *p = 1;
  *q = 2;
  return *p + *q;
}

Source (Nice article by the way): http://ift.tt/O82uIo